-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathicons.py
More file actions
147 lines (111 loc) · 4.01 KB
/
icons.py
File metadata and controls
147 lines (111 loc) · 4.01 KB
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
from functools import lru_cache
import json
from pathlib import Path
from docutils import nodes
from .utils import string_to_func_inputs
OPTICON_VERSION = "0.0.0-dd899ea"
OPTICON_CSS = """\
.octicon {
display: inline-block;
vertical-align: text-top;
fill: currentColor;
}"""
@lru_cache(1)
def get_opticon_data():
path = Path(__file__).parent.joinpath("data", "opticons.json")
return json.loads(path.read_text())
def list_opticons():
return list(get_opticon_data().keys())
def get_opticon(
name: str,
classes: str = None,
width: int = None,
height: int = None,
aria_label: str = None,
size: int = 16,
):
assert size in [16, 24], "size must be 16 or 24"
try:
data = get_opticon_data()[name]
except KeyError:
raise KeyError(f"Unrecognised opticon: {name}")
content = data["heights"][str(size)]["path"]
options = {
"version": "1.1",
"width": data["heights"][str(size)]["width"],
"height": int(size),
"class": f"octicon octicon-{name}",
}
if width is not None or height is not None:
if width is None:
width = round((int(height) * options["width"]) / options["height"], 2)
if height is None:
height = round((int(width) * options["height"]) / options["width"], 2)
options["width"] = width
options["height"] = height
options["viewBox"] = f'0 0 {options["width"]} {options["height"]}'
if classes is not None:
options["class"] += " " + classes.strip()
if aria_label is not None:
options["aria-label"] = aria_label
options["role"] = "img"
else:
options["aria-hidden"] = "true"
opt_string = " ".join(f'{k}="{v}"' for k, v in options.items())
return f"<svg {opt_string}>{content}</svg>"
def opticon_role(
role, rawtext: str, text: str, lineno, inliner, options={}, content=[]
):
try:
args, kwargs = string_to_func_inputs(text)
svg = get_opticon(*args, **kwargs)
except Exception as err:
msg = inliner.reporter.error(f"Opticon input is invalid: {err}", line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
node = nodes.raw("", nodes.Text(svg), format="html")
return [node], []
class fontawesome(nodes.Element, nodes.General):
pass
def create_fa_node(name, classes: str = None, style="fa"):
assert style.startswith("fa"), "style must be a valid prefix, e.g. fa, fas, etc"
return fontawesome(
icon_name=name,
classes=[style, f"fa-{name}"] + (classes.split() if classes else []),
)
def fontawesome_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
try:
args, kwargs = string_to_func_inputs(text)
node = create_fa_node(*args, **kwargs)
except Exception as err:
msg = inliner.reporter.error(
f"FontAwesome input is invalid: {err}", line=lineno
)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
return [node], []
def visit_fontawesome_html(self, node):
self.body.append(self.starttag(node, "span", ""))
def depart_fontawesome_html(self, node):
self.body.append("</span>")
def visit_fontawesome_latex(self, node):
if self.config.panels_add_fontawesome_latex:
self.body.append(f"\\faicon{{{node['icon_name']}}}")
raise nodes.SkipNode
def add_fontawesome_pkg(app, config):
if app.config.panels_add_fontawesome_latex:
app.add_latex_package("fontawesome")
def setup_icons(app):
app.add_role("opticon", opticon_role)
app.add_role("fa", fontawesome_role)
app.add_config_value("panels_add_fontawesome_latex", False, "env")
app.connect("config-inited", add_fontawesome_pkg)
app.add_node(
fontawesome,
html=(visit_fontawesome_html, depart_fontawesome_html),
singlehtml=(visit_fontawesome_html, depart_fontawesome_html),
latex=(visit_fontawesome_latex, None),
text=(None, None),
man=(None, None),
texinfo=(None, None),
)