10000 [ADD] extensions: icon_role odoo ui and font awesome · odoo/documentation@20b0760 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20b0760

Browse files
committed
[ADD] extensions: icon_role odoo ui and font awesome
1 parent b643a56 commit 20b0760

File tree

10 files changed

+952
-0
lines changed

10 files changed

+952
-0
lines changed

content/contributing/documentation/rst_cheat_sheet.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,26 @@ Use the `command` markup to highlight a command.
211211
Run the command :command:`make clean html` to delete existing built files and build the
212212
documentation to HTML.
213213

214+
.. _contributing/icons:
215+
216+
Icons
217+
-----
218+
219+
Use the `icon` markup to add a class name of an icon. There are two main icon sets used: *Odoo UI*
220+
and *Font Awesome*. It is recommended to accompany an icon with a label (`guilabel`), however, it is
221+
not mandatory.
222+
223+
.. list-table::
224+
:class: o-showcase-table
225+
226+
* - The graph view is represented by the :icon:`fa-bar-chart` :guilabel:`(bar chart)` icon.
227+
Return to the Kanban view by clicking the :icon:`oi-view-kanban` icon.
228+
229+
* - .. code-block:: text
230+
231+
The graph view is represented by the :icon:`fa-bar-chart` :guilabel:`(bar chart)` icon.
232+
Return to the Kanban view by clicking the :icon:`oi-view-kanban` icon.
233+
214234
.. _contributing/lists:
215235

216236
Lists

extensions/odoo_theme/__init__.py

Lines changed: 56 additions & 0 deletions
+
if text in icon_classes:
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import re
12
from docutils import nodes
3+
from docutils.parsers.rst import roles
24
from sphinx import addnodes
35
from sphinx.environment.adapters import toctree
46

@@ -15,6 +17,7 @@ def setup(app):
1517
app.add_js_file('js/menu.js')
1618
app.add_js_file('js/page_toc.js')
1719
app.add_js_file('js/switchers.js')
20+
roles.register_local_role('icon', icon_role)
1821

1922
return {
2023
'parallel_read_safe': True,
@@ -107,3 +110,56 @@ def _set_docname_as_class(_reference_node, _node_docname):
107110
if resolved_toc: # `resolve` returns None if the depth of the TOC to resolve is too high
108111
_update_toctree_nodes(resolved_toc)
109112
return resolved_toc
113+
114+
115+
def extract_icon_classes(filename):
116+
"""
117+
Return a list of icon classes from a CSS file.
118+
"""
119+
icon_classes = []
120+
try:
121+
with open(filename, 'r') as f:
122+
for line in f:
123+
match = re.search(r'\.([a-zA-Z0-9_-]+):before', line)
124+
if match:
125+
icon_class = match.group(1)
126+
icon_classes.append(icon_class)
127+
except FileNotFoundError:
128+
print(f"Error: File '{filename}' not found.")
129+
except Exception as e:
130+
print(f"Error: {e}")
131+
return icon_classes
132+
133+
def handle_error(inliner, rawtext, error_message, lineno):
134+
"""
135+
Handle an error by creating a system message node.
136+
"""
137+
error_node = nodes.system_message(
138+
lineno, backrefs=[], source=rawtext, type='error', level=1)
139+
error_node['message'] = inliner.reporter.error(error_message, line=lineno)
140+
return [error_node], []
141+
142+
def icon_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
143+
"""
144+
Custom role for adding icons to the documentation.
145+
"""
146+
# Paths to icon sets used by the :icon: role
147+
odoo_ui_icon_path = 'extensions/odoo_theme/static/scss/_odoo_ui_icon.scss'
148+
font_awesome_icon_path = 'extensions/odoo_theme/static/scss/_font_awesome.scss'
149+
# Check which icon set the icon belongs to
150+
if 'oi-' in text:
151+
icon_file = odoo_ui_icon_path
152+
elif 'fa-' in text:
153+
icon_file = font_awesome_icon_path
154+
else:
155+
return handle_error(
156+
inliner, rawtext, f"'{text}' is not an 'oi-' or 'fa-' icon set", lineno)
157+
# Extract icon classes from the icon set
158+
icon_classes = extract_icon_classes(icon_file)
159+
# Check if the icon class is in the icon set and create the element
160
161+
inline_node = nodes.inline(classes=[text])
162+
return [inline_node], []
163+
else:
164+
return handle_error(
165+
inliner, rawtext, f"Icon class '{text}' not found in {icon_file}", lineno)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)
0