Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bunch of removal and cleanup.
Browse files Browse the repository at this point in the history
Some cleanup and removal of useless code while looking how to have a
cleaner fix for #311
Carreau committed Nov 9, 2023
1 parent ca879e0 commit c27ba9b
Showing 2 changed files with 28 additions and 74 deletions.
84 changes: 27 additions & 57 deletions papyri/render.py
Original file line number Diff line number Diff line change
@@ -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,
@@ -1037,7 +1048,7 @@ def __init__(self, store, prefix: str, extension: str):
if p in self.version:
# todo, likely parse version here if possible.
maxv = max(v, self.version[p])
print("multiple version for package", p, "Trying most recent", maxv)
# print("multiple version for package", p, "Trying most recent", maxv)
self.version[p] = v

def exists_resolve(self, info) -> Tuple[bool, Optional[str]]:
@@ -1139,54 +1150,26 @@ 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
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 : <Insert Type here>
<Multiline Description Here>
graph : <Insert Type here>
<Multiline Description Here>
logo : <Insert Type here>
<Multiline Description Here>
"""

assert isinstance(meta, dict)
# 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 +1181,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,38 +1223,31 @@ 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=[],
)


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):
18 changes: 1 addition & 17 deletions papyri/templates/ascii.tpl.j2
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit c27ba9b

Please sign in to comment.