diff --git a/papyri/render.py b/papyri/render.py index 015e7cf0..21e1ef87 100644 --- a/papyri/render.py +++ b/papyri/render.py @@ -45,6 +45,22 @@ CSS_DATA = HtmlFormatter(style="pastie").get_style_defs(".highlight") +def group_backrefs(backrefs: List[RefInfo]) -> Dict[str, List[RefInfo]]: + """ + Take a list of backreferences and group them by the module they are comming from + """ + + group = defaultdict(lambda: []) + for ref in backrefs: + assert isinstance(ref, RefInfo) + if "." in ref.path: + mod, _ = ref.path.split(".", maxsplit=1) + else: + mod = ref.path + group[mod].append(ref) + return group + + def minify(s: str) -> str: return minify_html.minify( s, minify_js=True, remove_processing_instructions=True, keep_closing_tags=True @@ -563,15 +579,7 @@ def render_one( # Here if we have too many references we group them on where they come from. assert not hasattr(doc, "logo") if len(backrefs) > 30: - b2 = defaultdict(lambda: []) - for ref in backrefs: - assert isinstance(ref, RefInfo) - if "." in ref.path: - mod, _ = ref.path.split(".", maxsplit=1) - else: - mod = ref.path - b2[mod].append(ref) - backrefs = (None, b2) + backrefs = (None, group_backrefs(backrefs)) else: backrefs = (backrefs, None) @@ -738,11 +746,14 @@ async def _write_api_file( template = self.env.get_template("html.tpl.j2") gfiles = list(self.store.glob((None, None, "module", None))) random.shuffle(gfiles) - + if config.ascii: + env, template = _ascii_env() + else: + env, template = None, None for _, key in progress(gfiles, description="Rendering API..."): module, version = key.module, key.version if config.ascii: - await _ascii_render(key, store=self.store) + await _ascii_render(key, store=self.store, env=env, template=template) if config.html: doc_blob, qa, siblings, parts_links, backward, forward = await loc( key, @@ -1035,9 +1046,10 @@ def __init__(self, store, prefix: str, extension: str): for (package, version, _, _) in self.store.glob((None, "*", "meta", None)) }: if p in self.version: + pass # todo, likely parse version here if possible. - maxv = max(v, self.version[p]) - print("multiple version for package", p, "Trying most recent", maxv) + # maxv = max(v, self.version[p]) + # print("multiple version for package", p, "Trying most recent", maxv) self.version[p] = v def exists_resolve(self, info) -> Tuple[bool, Optional[str]]: @@ -1139,12 +1151,7 @@ def old_render_one( qa: str, *, current_type, - backrefs, - parts: Dict[str, List[Tuple[str, str]]], - parts_links=(), - graph="{}", meta, - toctrees, ): """ Return the rendering of one document @@ -1152,22 +1159,11 @@ def old_render_one( Parameters ---------- template - a Jinja@ template object used to render. + a Jinja template object used to render. doc : DocBlob a Doc object with the informations for current obj qa : str fully qualified name for current object - backrefs : list of str - backreferences of document pointing to this. - parts : Dict[str, list[(str, str)] - used for navigation and for parts of the breakcrumbs to have navigation to siblings. - This is not directly related to current object. - parts_links : - - graph : - - logo : - """ @@ -1175,18 +1171,6 @@ def old_render_one( # TODO : move this to ingest likely. # Here if we have too many references we group them on where they come from. assert not hasattr(doc, "logo") - if len(backrefs) > 30: - b2 = defaultdict(lambda: []) - for ref in backrefs: - assert isinstance(ref, RefInfo) - if "." in ref.path: - mod, _ = ref.path.split(".", maxsplit=1) - else: - mod = ref.path - b2[mod].append(ref) - backrefs = (None, b2) - else: - backrefs = (backrefs, None) try: resolver = Resolver(store, prefix="/p/", extension="") @@ -1198,16 +1182,10 @@ def old_render_one( return template.render( current_type=current_type, doc=doc, - logo=meta.get("logo", None), name=qa.split(":")[-1].split(".")[-1], version=meta["version"], module=qa.split(".")[0], - backrefs=backrefs, - parts=list(parts.items()), - parts_links=parts_links, - graph=graph, meta=meta, - toctrees=toctrees, ) except Exception as e: raise ValueError("qa=", qa) from e @@ -1246,30 +1224,21 @@ def math(s): return env, template -async def _ascii_render(key: Key, store: GraphStore, known_refs=None, template=None): +async def _ascii_render(key: Key, store: GraphStore, *, env, template): assert store is not None assert isinstance(store, GraphStore) ref = key.path - env, template = _ascii_env() - doc_blob = encoder.decode(store.get(key)) meta = encoder.decode(store.get_meta(key)) - # exercise the reprs - assert str(doc_blob) - return old_render_one( store, - parts={}, current_type="API", meta=meta, template=template, doc=doc_blob, qa=ref, - backrefs=[], - graph="{}", - toctrees=[], ) @@ -1277,7 +1246,8 @@ async def ascii_render(name, store=None): gstore = GraphStore(ingest_dir, {}) key = next(iter(gstore.glob((None, None, "module", "papyri.examples")))) - builtins.print(await _ascii_render(key, gstore)) + env, template = _ascii_env() + builtins.print(await _ascii_render(key, gstore, env=env, template=template)) async def loc(document: Key, *, store: GraphStore, tree, known_refs, ref_map): diff --git a/papyri/templates/ascii.tpl.j2 b/papyri/templates/ascii.tpl.j2 index 9bb80d17..5e5dee8a 100644 --- a/papyri/templates/ascii.tpl.j2 +++ b/papyri/templates/ascii.tpl.j2 @@ -201,20 +201,4 @@ | {{render_paragraph(sa.descriptions) | wordwrap | indent(width=8)}} |{% endfor %} {% endif %} -{% if backrefs[0] or backrefs[1] %} -{{"\n"}} -{{underline(bold("Back References"))}} - {% if backrefs[0] is not none %} - {% for r in backrefs[0] %} - {{r}} - {% endfor %} - {% endif%} - {% if backrefs[1] is not none %} - {% for k,v in backrefs[1].items() -%} - {{"\n\n "}}{{underline(k+':')}}{{"\n "}} - {%- for r in v %} - {{-blue(r)}}{{", "-}} - {%- endfor %} - {% endfor %} - {% endif%} -{% endif %} +