File size: 12,815 Bytes
2f6b10b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
"""
sphinx.ext.extlinks
~~~~~~~~~~~~~~~~~~~
Extension to save typing and prevent hard-coding of base URLs in the reST
files.
This adds a new config value called ``extlinks`` that is created like this::
extlinks = {'exmpl': ('https://example.invalid/%s.html', caption), ...}
Now you can use e.g. :exmpl:`foo` in your documents. This will create a
link to ``https://example.invalid/foo.html``. The link caption depends on
the *caption* value given:
- If it is ``None``, the caption will be the full URL.
- If it is a string, it must contain ``%s`` exactly once. In this case the
caption will be *caption* with the role content substituted for ``%s``.
You can also give an explicit caption, e.g. :exmpl:`Foo <foo>`.
Both, the url string and the caption string must escape ``%`` as ``%%``.
:copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
import re
from typing import Any, Dict, List, Tuple
import sphinx
from docutils import nodes, utils
from docutils.nodes import Node, system_message
from docutils.parsers.rst import Directive
from docutils.parsers.rst import directives
from sphinx_needs.api import add_dynamic_function
from docutils.parsers.rst.states import Inliner
from sphinx.application import Sphinx
from sphinx.util import caption_ref_re
from sphinx.roles import Abbreviation
from sphinx.util.docutils import SphinxDirective
from sphinx_design.icons import FontawesomeRole, fontawesome
class IpaElement(nodes.Element):
tagname = 'div'
default_ipa_class = 'ipa'
def __init__(self, rawsource='', *children, ipa_class='', **attributes):
if isinstance(ipa_class, str):
ipa_class = [ipa_class]
if self.default_ipa_class not in ipa_class:
ipa_class.insert(0, self.default_ipa_class)
self.ipa_classes = ipa_class
self.ipa_classes =[x if x.startswith('ipa') else 'ipa-'+x for x in self.ipa_classes if x]
super().__init__(rawsource, *children, **attributes)
def for_html(self):
return {'class': ' '.join(self.ipa_classes)}
class Divider(IpaElement):
default_ipa_class = 'divider'
def __init__(self):
circle = CircleIcon()
t = IpaText('',circle)
super().__init__('', t)
class IpaToolTip(IpaElement):
default_ipa_class = 'tooltip'
class IpaPhone(IpaElement):
default_ipa_class = 'phone'
def __init__(self, phone, extra_data, *children, **attributes):
ipa_children = [IpaPhoneCheck(phone), IpaPhoneText(phone)]
#t = IpaText('', nodes.Text(phone))
#ipa_children.append(t)
if extra_data:
tool_tip_childern = []
for k,v in extra_data.items():
key = IpaToolTipKey(k)
if isinstance(v, dict):
value = IpaToolTipValue('')
line = IpaToolTipLine('', key, value)
tool_tip_childern.append(line)
for k2, v2 in v.items():
key = IpaToolTipKey(k2, second_level=True, ipa_class=['ipa-second-level'])
value = IpaToolTipValue(v2,highlight=phone)
line = IpaToolTipLine('', key, value)
tool_tip_childern.append(line)
else:
value = IpaToolTipValue(v)
line = IpaToolTipLine('', key, value)
tool_tip_childern.append(line)
t = IpaToolTip('', *tool_tip_childern)
ipa_children.append(t)
super().__init__('', *children, *ipa_children, **attributes)
class IpaText(IpaElement):
tagname = 'span'
default_ipa_class = 'text'
def __init__(self, text, *children, **attributes):
n = []
if text:
n.append(nodes.Text(text))
super().__init__('', *n, *children, **attributes)
class IpaPhoneCheck(IpaElement):
tagname = 'input'
default_ipa_class = 'phone-checkbox'
def __init__(self, phone, *children, **attributes):
super().__init__('', *children, **attributes)
self.phone = phone
def for_html(self):
data = super().for_html()
data.update({'type': 'checkbox', 'ids':[f'{self.phone}_checkbox']})
return data
class IpaPhoneText(IpaElement):
tagname = 'label'
default_ipa_class = 'phone-text'
def __init__(self, phone, *children, **attributes):
self.phone = phone
t = IpaText(phone)
attributes.update({'for': f'{phone}_checkbox'})
super().__init__('',t, *children, **attributes)
def for_html(self):
data = super().for_html()
data.update({'for':f'{self.phone}_checkbox'})
return data
class IpaToolTipLine(IpaElement):
default_ipa_class = 'tooltip-line'
class IpaToolTipKey(IpaText):
default_ipa_class = 'tooltip-key'
def __init__(self, key, *children, second_level=False, **attributes):
span_children = []
if second_level:
span_children.append(nodes.Text(' * '))
#span_children.append(CircleIcon())
#span_children.append(nodes.Text(' '))
key = nodes.Text(f'{key}:')
span_children.append(key)
super().__init__('', *children, *span_children, **attributes)
class IpaToolTipValue(IpaElement):
default_ipa_class = 'tooltip-value'
def __init__(self, value, *children, highlight=None, **attributes):
span_children = []
try:
if ',' in value:
value = value.replace(',', '')
value = int(value)
value = f"{value:,}"
value = IpaText(value, ipa_class='highlight')
span_children.append(value)
except ValueError:
if highlight is not None:
highlight_pattern = re.compile(rf'{highlight}[0-5˩˨˧˦˥ˀ]*')
phones = value[1:-1].split(' ')
span_children.append(IpaText('[', ipa_class='bracket'))
for i, p in enumerate(phones):
if highlight_pattern.match(p):
span_children.append(IpaText(p, ipa_class=['highlight', 'transcription']))
else:
span_children.append(IpaText(p, ipa_class='transcription'))
if i != len(phones) - 1:
span_children.append(nodes.Text(' '))
span_children.append(IpaText(']', ipa_class='bracket'))
super().__init__('', *children, *span_children, **attributes)
def checkbox_html_visit(self, node):
"""
Visitor method for Need-node of builder 'html'.
Does only wrap the Need-content into an extra <div> with class=need
"""
self.body.append(self.starttag(node, node.tagname, **node.for_html()))
def checkbox_label_html_visit(self, node):
"""
Visitor method for Need-node of builder 'html'.
Does only wrap the Need-content into an extra <div> with class=need
"""
self.body.append(self.starttag(node, node.tagname, **node.for_html()))
def html_visit(self, node):
"""
Visitor method for Need-node of builder 'html'.
Does only wrap the Need-content into an extra <div> with class=need
"""
extra = {}
if hasattr(node, "for_html"):
extra = node.for_html()
self.body.append(self.starttag(node, node.tagname, "",**extra))
def html_depart(self, node):
self.body.append(f"</{node.tagname}>")
class CircleIcon(nodes.Element):
tagname = 'i'
def for_html(self):
return {'class': 'fas fa-circle ipa-dot'}
class IpaCell(Directive):
has_content = True
def run(self):
if not self.content:
return [IpaElement(ipa_class='empty')]
node = nodes.Element() # anonymous container for parsing
self.state.nested_parse(self.content, self.content_offset, node)
output_nodes = []
manner_type = ''
for i, row_list in enumerate(node[0]):
phone_type = row_list[0].children[0].astext()
phone_type_list = row_list[1]
phone_type_children = []
if phone_type in {'voiceless', 'voiced'}:
manner_type = 'obstruent'
elif phone_type in {'unrounded', 'rounded'}:
manner_type = 'vowel'
else:
manner_type = 'sonorant'
for phone_list in phone_type_list:
phone = phone_list[0].children[0].astext()
extra_data = {}
if len(phone_list) > 1:
for extra_list in phone_list[1:]:
for extra in extra_list:
base = extra.children[0].astext()
if ':' in base:
key, value = base.split(': ')
extra_data[key] = value
else:
extra_data[base] = {}
for e in extra.children[1:]:
for c in e.children:
example = c.children[0].astext()
key, value = example.split(': ')
extra_data[base][key] = value
phone_type_children.append(IpaPhone(phone, extra_data))
output_nodes.append(IpaElement('',*phone_type_children, ipa_class=phone_type))
if phone_type in {'voiceless', 'unrounded'}:
output_nodes.append(Divider())
if i == len(node[0]) - 1:
if phone_type == 'voiceless':
output_nodes.append(IpaElement('', ipa_class='voiced'))
else:
output_nodes.append(IpaElement('', ipa_class='rounded'))
elif phone_type in {'voiced', 'rounded'} and i == 0:
output_nodes.insert(0,Divider())
if phone_type == 'voiced':
output_nodes.insert(0,IpaElement('', ipa_class='voiceless'))
else:
output_nodes.insert(0,IpaElement('', ipa_class='unrounded'))
root_node = IpaElement('', *output_nodes, ipa_class=['cell', manner_type])
return [root_node]
icon_short_cuts = {
'right-arrow': 'long-arrow-alt-right'
}
class IpaFontAwesome(FontawesomeRole):
def __init__(self):
super(IpaFontAwesome, self).__init__('fas')
def run(self) -> Tuple[List[nodes.Node], List[nodes.system_message]]:
"""Run the role."""
icon, classes = self.text.split(";", 1) if ";" in self.text else [self.text, ""]
icon = icon.strip()
icon = icon_short_cuts.get(icon, icon)
node = fontawesome(
icon=icon, classes=[self.style, f"fa-{icon}", 'ipa-icon'] + classes.split()
)
self.set_source_info(node)
return [node], []
def xref(
typ: str,
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
options: dict = None,
content: List[str] = None,
) -> Tuple[List[Node], List[system_message]]:
title = target = text
# look if explicit title and target are given with `foo <bar>` syntax
brace = text.find("<")
if brace != -1:
m = caption_ref_re.match(text)
if m:
target = m.group(2)
title = m.group(1)
else:
# fallback: everything after '<' is the target
target = text[brace + 1 :]
title = text[:brace]
link = xref.links[target]
if brace != -1:
pnode = nodes.reference(target, title, refuri=link[1])
else:
pnode = nodes.reference(target, link[0], refuri=link[1])
return [pnode], []
def get_refs(app):
xref.links = app.config.xref_links
def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value("xref_links", {}, "env")
app.add_node(IpaPhoneCheck, html=(checkbox_html_visit, html_depart))
app.add_node(IpaPhoneText, html=(checkbox_label_html_visit, html_depart))
app.add_node(CircleIcon, html=(html_visit, html_depart))
app.add_node(Divider, html=(html_visit, html_depart))
app.add_node(IpaToolTip, html=(html_visit, html_depart))
app.add_node(IpaToolTipKey, html=(html_visit, html_depart))
app.add_node(IpaToolTipValue, html=(html_visit, html_depart))
app.add_node(IpaToolTipLine, html=(html_visit, html_depart))
app.add_node(IpaPhone, html=(html_visit, html_depart))
app.add_node(IpaElement, html=(html_visit, html_depart))
app.add_directive("ipa_cell", IpaCell)
app.add_role("ipa_icon", IpaFontAwesome())
app.add_role("xref", xref)
app.connect("builder-inited", get_refs)
return {"version": sphinx.__display_version__, "parallel_read_safe": True}
|