Skip to content

Commit

Permalink
Bunch of removal and cleanup. (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau authored Nov 10, 2023
2 parents ca879e0 + 867579f commit adb4c03
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 76 deletions.
88 changes: 29 additions & 59 deletions papyri/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]]:
Expand Down Expand Up @@ -1139,54 +1151,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="")
Expand All @@ -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
Expand Down Expand Up @@ -1246,38 +1224,30 @@ 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):
Expand Down
18 changes: 1 addition & 17 deletions papyri/templates/ascii.tpl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 adb4c03

Please sign in to comment.