pdoc3 / pdoc

:snake: :arrow_right: :scroll: Auto-generate API documentation for Python projects
https://pdoc3.github.io/pdoc/
GNU Affero General Public License v3.0
1.12k stars 145 forks source link

More/custom hierarchy in html left navigation bar #316

Closed ogallagher closed 3 years ago

ogallagher commented 3 years ago

The current default layout for the generated html pages includes an index/navigation bar along the left side, organized like so:

I would much prefer that within each class, the members be better organized (and also collapsible), much like how they’re organized in the main section of the page:

How do I add a custom template with a different file path to be used instead of pdoc/templates/html.mako? In the documentation for Module.html(self, minify=True, **kwargs) --> str I’m not seeing an indication that this is possible.

kernc commented 3 years ago

Rendering to HTML always takes html.mako template, but you can influence the directory where html.mako is sought (programatically) by amending pdoc.tpl_lookup: https://github.com/pdoc3/pdoc/blob/72e41dbf91646a39bc900ca6f875e5f75660e6a4/pdoc/__init__.py#L54-L66 or (CLI) via --template-dir: https://github.com/pdoc3/pdoc/blob/72e41dbf91646a39bc900ca6f875e5f75660e6a4/pdoc/cli.py#L105-L112

Curious to see what you end up with! :eyes:

ogallagher commented 3 years ago

@kernc Thank you! I’ll attempt amending pdoc.tpl_lookup.directories and will see how it goes.

ogallagher commented 3 years ago

I’ve managed to, at least in terms of utility, achieve what I was looking for. After inserting my custom template directory as the first element in pdoc.tpl_lookup.directories, I then copied the default mako template files into the new location and made some modifications. I only extracted key snippets in part due to the required secrecy of the project involved, but others interested in customizing the generated html output from pdoc could use this as an example:

css.mako ```mako <%def name="myproject_css()" filter="minify_css"> ## ogallagher 2021-02-25 myproject custom css rules .myproject-collapsee { display: none; } .myproject-collapsee.show { display: block; } .myproject-collapser { display: inline; padding-left: 8px; padding-right: 8px; } .myproject-collapser:hover { font-weight: bold; cursor: pointer; } ```
html.mako ```mako ## ogallagher custom html char aliases for legibility <% alias_to_html_char = { 'triangle-solid-right': '►', 'triangle-solid-right-small': '▸', 'triangle-solid-down': '▼', 'triangle-solid-down-small': '▾', 'arrow-down-to-bar': '⤓', 'arrow-up-to-bar': '⤒' } %> ## ... ## ogallagher customized so
    can be collapsible <%def name="show_column_list(items, name=None)"> <% two_column = len(items) >= 6 and all(len(i.name) < 20 for i in items) %>
      % for item in items:
    • ${link(item, item.name)}
    • % endfor
    ## ... ## ogallagher section collapse controller <%def name="myproject_collapser(collapsee_id)">
    ${alias_to_html_char['triangle-solid-right-small']}
    ## ... <%def name="myproject_index_collapse_js()"> ## ... <%def name="module_index(module)"> ## custom module index column (collapsible headers) ## ... ## ... ## ... <%namespace name="css" file="css.mako" /> ## ...
    % if module_list:
    ${show_module_list(modules)}
    % else:
    ${show_module(module)}
    ${module_index(module)} % endif
    ## ... ${myproject_index_collapse_js()} ```

Here's what my result looks like:

pdoc_navbar_custom

Notably I forgot to sort members alphabetically within each class member, but that should be trivial compared to what’s done already.

DOSull commented 2 years ago

It would be great if this behaviour could be added as an option to pdoc. My JS/CSS/HTML isn't good enough to pick up the posted files and just use them in my project, unfortunately!