fix menu item icons

This commit is contained in:
Antoine Vandevenne (anv) 2021-02-22 16:27:16 +01:00
parent f4037b0a57
commit 141d2b4c01
3 changed files with 28 additions and 33 deletions

View File

@ -1,3 +1,5 @@
:show_content:
============ ============
Applications Applications
============ ============

View File

@ -32,14 +32,10 @@ class Monkey(object):
@Monkey(toctree.TocTree) @Monkey(toctree.TocTree)
def resolve(old_resolve, tree, docname, *args, **kwargs): def resolve(old_resolve, tree, docname, *args, **kwargs):
def _clear_empty_pages_reference(_node) -> None: def _update_toctree_nodes(_node) -> None:
""" Disable references to 'empty' toctree pages. """ Make necessary changes to Docutils' nodes of the toc.
Inspect node's children to determine whether the page is a toc and, if so, clear its Internal structure of toc nodes:
reference URL. (<a href="#"/>)
If the page has the `show_content` metadata, don't clear the reference.
Internal structure of nodes:
<ul> <ul>
<li> <li>
<p><a/></p> <p><a/></p>
@ -51,16 +47,12 @@ def resolve(old_resolve, tree, docname, *args, **kwargs):
<ul/> <ul/>
""" """
if isinstance(_node, nodes.reference): # The node is a reference (<a/>) if isinstance(_node, nodes.reference): # The node is a reference (<a/>)
_subnode_docname = _get_docname(_node) _node_docname = _get_docname(_node)
if _subnode_docname and any( _clear_reference_if_empty_page(_node, _node_docname)
isinstance(_subnode, nodes.bullet_list) _set_docname_as_class(_node, _node_docname)
for _subnode in _node.parent.parent.children
): # The node references a toc
if 'show_content' not in tree.env.metadata[_subnode_docname]:
_node['refuri'] = '#' # The page must not be accessible
elif isinstance(_node, (addnodes.compact_paragraph, nodes.bullet_list, nodes.list_item)): elif isinstance(_node, (addnodes.compact_paragraph, nodes.bullet_list, nodes.list_item)):
for _subnode in _node.children: for _subnode in _node.children:
_clear_empty_pages_reference(_subnode) _update_toctree_nodes(_subnode)
def _get_docname(_node): def _get_docname(_node):
""" """
@ -81,7 +73,25 @@ def resolve(old_resolve, tree, docname, *args, **kwargs):
_docname = '/'.join(_res) _docname = '/'.join(_res)
return _docname return _docname
def _clear_reference_if_empty_page(_reference_node, _node_docname):
""" Clear reference of 'empty' toctree pages.
Inspect parent node's siblings to determine whether the node references a toc and, if so,
clear its reference URL. (<a href="#"/>)
If the page has the `show_content` metadata, don't clear the reference.
"""
if _node_docname and any(
isinstance(_subnode, nodes.bullet_list)
for _subnode in _reference_node.parent.parent.children
): # The node references a toc
if 'show_content' not in tree.env.metadata[_node_docname]:
_reference_node['refuri'] = '#' # The page must not be accessible
def _set_docname_as_class(_reference_node, _node_docname):
_node_docname = _node_docname or docname # refuri==None <-> href="#"
_reference_node.parent.parent['classes'].append(f'o_menu_{_node_docname.replace("/", "_")}')
resolved_toc = old_resolve(tree, docname, *args, **kwargs) resolved_toc = old_resolve(tree, docname, *args, **kwargs)
if resolved_toc: # `resolve` returns None if the depth of the TOC to resolve is too high if resolved_toc: # `resolve` returns None if the depth of the TOC to resolve is too high
_clear_empty_pages_reference(resolved_toc) _update_toctree_nodes(resolved_toc)
return resolved_toc return resolved_toc

View File

@ -9,9 +9,6 @@
// Allow to respectively highlight and expand the TOC entries and their related TOC // Allow to respectively highlight and expand the TOC entries and their related TOC
// entry list whose page is displayed. // entry list whose page is displayed.
_flagActiveTocEntriesAndLists(); _flagActiveTocEntriesAndLists();
// Add a class with the name of the file to each corresponding menu item
_flagMenuItemsWithFileName();
}); });
/** /**
@ -42,18 +39,4 @@
} }
}; };
/**
* Add the name of the file as class of the corresponding menu item.
*/
const _flagMenuItemsWithFileName = () => {
this.navigationMenu.querySelectorAll('li').forEach(menuItem => {
let href = menuItem.querySelector('a').href;
if (href === '#') { // Selected nodes don't have their file name in the href
href = window.location.href; // Get it from the current window location
}
const fileName = href.substring(href.lastIndexOf('/') + 1, href.lastIndexOf('.html'));
menuItem.classList.add(`o_menu_${fileName}`);
});
};
})(); })();