Skip to content

Commit

Permalink
Misc Ascii and browser fixes.
Browse files Browse the repository at this point in the history
This move some of the coloration outside of the templating engine, this
should let us add an option to make colors optionals, and should make
testing easier, as now we can have expected colorless outputs and test
the codebase against it.

This also add some fixes to the urwid frontend – which still crashes in
a number of places, so I'm wondering if we shoudl rewrite it using
textual. One of the other current limitation with urwind is the lack of
ability to reflow paragraph is they contain multiple entries.
  • Loading branch information
Carreau committed Nov 23, 2023
1 parent 620b585 commit 73d0dfa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 31 deletions.
16 changes: 13 additions & 3 deletions papyri/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def __init__(self, attr, text, cb):
self.text = text
self.cb = cb

def selectable(self):
return True


class TextWithLink(urwid.Text):
# _selectable = True
Expand Down Expand Up @@ -292,7 +295,7 @@ def render_Directive(self, d):
c = converter()

return ("math", c.convert(cont))
return ("directive", f"{d.domain}:{d.role}:`{cont}`")
return Text(("directive", f"{d.domain}:{d.role}:`{cont}`"))

def render_Example(self, ex):
acc = []
Expand All @@ -307,15 +310,15 @@ def render_Example(self, ex):

def render_Link(self, link):
if link.reference.kind == "local":
return ("local", link.value)
return Text(("local", link.value))
return Link("link", link.value, lambda: self.cb(link.reference))

def render_BlockQuote(self, quote):
return urwid.Padding(
urwid.Pile([self.render(c) for c in quote.children]), left=4, right=4
)

def render_Admonition(self, adm):
def render_MAdmonition(self, adm):
kind = adm.kind
title = (f"{kind} : {adm.title}",)
if kind == "versionchanged":
Expand All @@ -332,6 +335,13 @@ def render_Admonition(self, adm):
),
)

def render_MText(self, text):
return urwid.Text(text.value)

def render_MParagraph(self, paragraph):
stuff = [self.render(c) for c in paragraph.children]
return urwid.Pile(stuff)

def render_BlockMath(self, math):
from flatlatex import converter

Expand Down
9 changes: 4 additions & 5 deletions papyri/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@

import jedi

try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import tomllib
import tomli_w
from IPython.core.oinspect import find_file
from IPython.utils.path import compress_user
Expand Down Expand Up @@ -1993,7 +1990,9 @@ def collect_api_docs(self, root: str, limit_to: List[str]):
"error with %s %s", qa, list(ecollector._errors.keys())
)
else:
self.log.info("only expected error with %s", qa)
self.log.info(
"only expected error with %s, %s", qa, ecollector._errors.keys()
)
continue

try:
Expand Down
15 changes: 14 additions & 1 deletion papyri/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def minify(s: str) -> str:


def unreachable(*obj):
return str(obj[1])
return str(obj)
assert False, f"Unreachable: {obj=}"


Expand Down Expand Up @@ -1200,6 +1200,18 @@ def _ascii_env():
env.globals["len"] = len
env.globals["unreachable"] = unreachable
env.globals["sidebar"] = False

env.globals["bold"] = lambda x: f"\x1b[1;m{x}\x1b[0;m"
env.globals["underline"] = lambda x: f"\x1b[4;m{x}\x1b[0;m"
env.globals["black"] = lambda x: f"\x1b[30;m{x}\x1b[0;m"
env.globals["red"] = lambda x: f"\x1b[31;m{x}\x1b[0;m"
env.globals["green"] = lambda x: f"\x1b[32;m{x}\x1b[0;m"
env.globals["yellow"] = lambda x: f"\x1b[33;m{x}\x1b[0;m"
env.globals["blue"] = lambda x: f"\x1b[34;m{x}\x1b[0;m"
env.globals["magenta"] = lambda x: f"\x1b[35;m{x}\x1b[0;m"
env.globals["cyan"] = lambda x: f"\x1b[36;m{x}\x1b[0;m"
env.globals["white"] = lambda x: f"\x1b[37;m{x}\x1b[0;m"

try:
c = converter()

Expand Down Expand Up @@ -1245,6 +1257,7 @@ async def ascii_render(name, store=None):
key = next(iter(gstore.glob((None, None, "module", name))))

env, template = _ascii_env()

builtins.print(await _ascii_render(key, gstore, env=env, template=template))


Expand Down
54 changes: 32 additions & 22 deletions papyri/templates/ascii.tpl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,53 @@
{% endif %}
{% endmacro %}

{% macro render_list(objs) -%}
|{% for item in objs -%}
|{{- render_II(item) -}}
|{% endfor -%}
{%- endmacro %}

{% macro render_II(obj) -%}
|{% if obj.__class__.__name__ == 'Paragraph' %}
| |{{block_paragraph(obj)}}
|{% else %}
|{%- if obj.__class__.__name__ == 'Paragraph' -%}
| |{{- block_paragraph(obj) -}}
|{%- else -%}
|{%- set type = obj.__class__.__name__ -%}
| |{%- if type in ('Directive', 'Link', 'Math', 'Verbatim') -%}
| | |{{unreachable()}}
| |{% elif type == 'MMystDirective' %}
| |{% elif type == 'MMystDirective' -%}
| | |{{'\n'}}{{myst_directive(obj)}}
| |{% elif type == 'Verbatim' %}
| |{% elif type == 'Verbatim' -%}
| | |{{obj}}
| |{% elif type == 'Paragraph' %}
| |{% elif type == 'Paragraph' -%}
| | |{{block_paragraph(obj)}}
| |{% elif type == 'BlockVerbatim' %}
| |{% elif type == 'BlockVerbatim' -%}
| | |{{obj.value}}
| |{% elif type == 'DefList' %}
| |{% elif type == 'DefList' -%}
| | | {% for item in obj.children %}
| | | {{render_II(item.dt)}}
| | | {{render_II(item.dd)}}
| | | {%endfor %}
| | | {%endfor %}
| |{% elif type == 'DefListItem' %}
| | {{-block(obj)-}}
| |{% elif type == 'Example' %}
| | | |{{block(obj)}}
| |{% elif type == 'MLink' %}
| | |{{render_list(obj.children)}}
| |{% elif type == 'MText' %}
| | |{{obj.value}}
| |{% elif type == 'MParagraph' -%}
| | |{{- render_list(obj.children) -}}
| |{% elif type == 'Parameters' %}
| | {% for item in obj.children %}
| | {%- for item in obj.children %}
| | {{ render_II(item) }}
| | {% endfor %}
| | {%- endfor %}
| |{% elif type == 'Section' %}
| | {% for item in obj %}
| | {{ render_II(item) }}
| | {% endfor %}
| |{% else %}
| | |Some B {{type}} Not implemented yet
| | {#{{unreachable(obj)}}#}
| | {%- for item in obj %}
| | {{- render_II(item) -}}
| | {%- endfor %}
| |{%- else %}
| | |Some B {{type}} Not implemented yet:
| | |{{unreachable(obj)}}
| |{%- endif -%}
|{%- endif -%}
{%- endmacro %}
Expand All @@ -106,12 +118,12 @@
|{% endif %}
|{% for prg in prgs %}
| | |{%- for obj in prg.children -%}
| | | {{- render_inner(obj.__class__.__name__, obj) -}}
| | | {{- render_inner(obj.__class__.__name__, obj) -}}
| | |{%- endfor -%}
|{%- endfor -%}
{%- endmacro %}


{%- macro esc(val, stuff) -%}
[{{val}};m{{stuff}}
{%- endmacro %}
Expand Down Expand Up @@ -148,10 +160,8 @@
|{{bold(section)}}
|
|{% endif %}
|{% if section in ['Summary', 'Extended Summary', 'Notes'] %}
|
|{%- if section in ['Summary', 'Extended Summary', 'Notes'] -%}
| |{{render_II(doc.content[section]) | wordwrap}}
|
|{% elif section in ['Signature'] %}
|{{unreachable()}}
|{% elif section in ['Examples'] %}
Expand Down

0 comments on commit 73d0dfa

Please sign in to comment.