Skip to content

Commit

Permalink
a number of other fixes to ascii rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Nov 24, 2023
1 parent 73d0dfa commit c5f8387
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
2 changes: 2 additions & 0 deletions papyri/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,8 @@ def _ascii_env():
env.globals["len"] = len
env.globals["unreachable"] = unreachable
env.globals["sidebar"] = False
env.globals["str"] = str
env.filters["pad"] = lambda ss:'│'+'\n│'.join([x.strip() for x in ss.split('\n')])

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"
Expand Down
87 changes: 47 additions & 40 deletions papyri/templates/ascii.tpl.j2
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,9 @@
{%- endif -%}
{%- endmacro %}
{#--#}
{% macro render_inner(type, obj) -%}
|{%- if type == 'Directive' -%}
| |:{{blue(obj.domain)}}:{{blue(obj.role)}}:`{{blue(obj.value)}}`
|{%- elif type == 'Link' %}
| |{% if obj.kind=='local' %}
| | {{-green(obj.value)-}}
| |{% elif obj.kind=='exists' %}
| | {{-blue(obj.value)-}}
| |{% elif obj.kind=='missing' %}
| | {{-unreachable(obj)}}
| |{% else %}
| |{% endif %}
|{% elif type == 'Math' %}
| ${{obj.value}}$
|{% elif type == 'Verbatim' %}
| {{-yellow(obj.text)-}}
|{% else %}
| |Some A {{type}}
|{%- endif -%}
{%- endmacro %}

{% macro block_paragraph(pgr) -%}
|{%- for c in pgr.children -%}
| |{{-render_inner(c.__class__.__name__, c)-}}
| |{{-render_II(c) | trim-}}
|{%- endfor -%}
{% endmacro %}

Expand All @@ -72,39 +51,67 @@
| |{{- block_paragraph(obj) -}}
|{%- else -%}
|{%- set type = obj.__class__.__name__ -%}
| |{%- if type in ('Directive', 'Link', 'Math', 'Verbatim') -%}
| | |{{unreachable()}}
| |{% elif type == 'MMystDirective' -%}
| |{%- if type in ('Math', 'Verbatim') -%}
| | |{{unreachable(obj)}}
| |{%- elif type == 'MAdmonitionTitle' -%}
| | | ..
| | | {%- for item in obj.children %}
| | | | {{render_II(item)|trim}}
| | | {%- endfor %}{{'::\n\n'}}
| |{%- elif type == 'MAdmonition' -%}{{'\n'}}
| | | {% for item in obj.children %}
| | | |{{render_II(item)|wordwrap|pad}}
| | | {%endfor %}
| | | |{{'\n'}}
| |{%- elif type == 'Directive' -%}
| | |{{blue(':'+str(obj.domain)+':'+str(obj.role)+':`'+obj.value+'`')}}
| |{%- 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 %}
| |{% elif type == 'DefListItem' %}
| |{%- elif type == 'DefListItem' %}
| | {{-block(obj)-}}
| |{% elif type == 'Example' %}
| |{%- elif type == 'Example' %}
| | | |{{block(obj)}}
| |{% elif type == 'MLink' %}
| |{%- elif type == 'MLink' %}
| | |{{render_list(obj.children)}}
| |{% elif type == 'MText' %}
| |{%- elif type == 'MInlineCode' -%}
| | |{{bold(obj.value)}}
| |{%- elif type == 'MText' -%}
| | |{{obj.value}}
| |{% elif type == 'MParagraph' -%}
| |{%- elif type == 'MParagraph' -%}
| | |{{- render_list(obj.children) -}}
| |{% elif type == 'Parameters' %}
| | {%- for item in obj.children %}
| | {{ render_II(item) }}
| | {%- endfor %}
| |{% elif type == 'Section' %}
| |{%- elif type == 'Section' %}
| | {%- for item in obj %}
| | {{- render_II(item) -}}
| | {%- endfor %}
| |{%- elif type == 'Directive' -%}
| | |:{{blue(obj.domain)}}:{{blue(obj.role)}}:`{{blue(obj.value)}}`
| |{%- elif type == 'Link' %}
| | |{% if obj.kind=='local' %}
| | | {{-green(obj.value)-}}
| | |{% elif obj.exists %}
| | | {{-blue(obj.value)-}}
| | |{% else %}
| | | {{-unreachable(obj)}}
| | |{% endif %}
| |{% elif type == 'Math' %}
| | ${{obj.value}}$
| |{% elif type == 'Verbatim' %}
| | {{-yellow(obj.text)-}}
| |{%- else %}
| | |Some B {{type}} Not implemented yet:
| | |{{unreachable(obj)}}
Expand All @@ -118,7 +125,7 @@
|{% endif %}
|{% for prg in prgs %}
| | |{%- for obj in prg.children -%}
| | | {{- render_inner(obj.__class__.__name__, obj) -}}
| | | {{- render_II(obj) -}}
| | |{%- endfor -%}
|{%- endfor -%}
{%- endmacro %}
Expand Down Expand Up @@ -161,7 +168,7 @@
|
|{% endif %}
|{%- if section in ['Summary', 'Extended Summary', 'Notes'] -%}
| |{{render_II(doc.content[section]) | wordwrap}}
| |{{render_II(doc.content[section]) | wordwrap| trim}}
|{% elif section in ['Signature'] %}
|{{unreachable()}}
|{% elif section in ['Examples'] %}
Expand Down Expand Up @@ -193,7 +200,7 @@
| | | | {{green(p[1])}}
| | | |{% endif %}
| | | | {% for item in p[2] %}
| | | | {{render_II(item)| wordwrap | indent(width=8)}}
| | | | {{render_II(item)| wordwrap |trim| indent(width=8)}}
| | | | {% endfor -%}
| | |{%- endfor -%}
| |{%- endfor -%}
Expand All @@ -207,8 +214,8 @@
{% if doc.see_also %}
|{{bold("See Also")}}
|{% for sa in doc.see_also %}
| {{bold(render_inner(sa.name.__class__.__name__, sa.name))}}
| {{render_paragraph(sa.descriptions) | wordwrap | indent(width=8)}}
| {{bold(render_II(sa.name))}}
| {{render_paragraph(sa.descriptions) | wordwrap |trim| indent(width=8)}}
|{% endfor %}
{% endif %}

0 comments on commit c5f8387

Please sign in to comment.