Rework pydantic model examples display#51
Conversation
| has_default = False | ||
| else: | ||
| has_default = True | ||
| examples = [repr(example) for example in examples or []] |
There was a problem hiding this comment.
Move example formatting closer in sphinx task utils, closer to rendering
| } | ||
|
|
||
| .ewokssphinx-task code[class="highlight"] { | ||
| white-space: pre-wrap; |
There was a problem hiding this comment.
Needed to keep carriage return in pretty printed code
| lexer = PythonLexer() | ||
| formatter = HtmlFormatter(nowrap=True) | ||
| return ( | ||
| f'<code class="highlight">{highlight(code, lexer, formatter).rstrip()}</code>' |
There was a problem hiding this comment.
nodes.literal_block uses:
<pre>which is meant for block display- has border, background and
1rempadding
Rather than changing this through CSS and inlining <pre>, I used <code> (meant for inline).
| ) | ||
|
|
||
|
|
||
| def _examples_node(formatted_examples: list[Any]) -> nodes.container: |
There was a problem hiding this comment.
Input examples are not formatted yet.
| def _examples_node(formatted_examples: list[Any]) -> nodes.container: | |
| def _examples_node(examples: list[Any]) -> nodes.container: |
| ] | ||
|
|
||
| if sum(len(str(example)) for example in formatted_examples) < 50 and not any( | ||
| "\n" in example for example in formatted_examples |
There was a problem hiding this comment.
What case are you trying to handle when checking for \n ?
There was a problem hiding this comment.
Pretty print span the example on multiple lines, e.g. a long dict of list
|
As for the HTML limitation, you should be able to get the output format from the directive/app options and change the formatter accordingly. Could be another PR. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Thanks for the hint! Last commit bf8055d adds fallbacks (literal and literal_block) for other formatters |
d60a652 to
bf8055d
Compare
bf8055d to
fb3e8d2
Compare
This PR proposes to rework pydantic model examples display by using pretty print and syntax highlighting.
It also inlines "short" examples.
It only works for html output, I hope this is affordable.
TBH, it's quite a struggle to do what you want through
docutils😅 .closes #9
What it looks with this PR:
What it is without it: