Skip to content

Commit 3e06b80

Browse files
committed
📚 DOCS: Live preview renders theme
The live preview now renders only the body HTML, and inherits the outer theme
1 parent e49b70f commit 3e06b80

File tree

5 files changed

+89
-30
lines changed

5 files changed

+89
-30
lines changed

docs/_static/local.css

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ h3::before {
3737
}
3838

3939
/** No icon for admonitions with no-icon class */
40-
.admonition > .admonition-title, div.admonition.no-icon > .admonition-title::before {
40+
div.admonition.no-icon > .admonition-title::before {
4141
content: "";
4242
}
43-
.admonition > .admonition-title, div.admonition.no-icon > .admonition-title {
43+
div.admonition.no-icon > .admonition-title {
4444
padding-left: .6rem;
4545
}
4646

4747
/* Live preview page */
4848
iframe.pyscript, textarea.pyscript {
4949
width: 100%;
50-
height: 400px;
50+
height: 100%;
51+
min-height: 300px;
52+
}
53+
div.pyscript {
54+
min-height: 300px;
5155
}
5256
iframe.pyscript {
5357
padding: 4px;

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"sphinx_design",
3636
"sphinx_copybutton",
3737
"sphinxext.rediraffe",
38-
"sphinxcontrib.mermaid",
38+
# disabled due to https://github.com/mgaitan/sphinxcontrib-mermaid/issues/109
39+
# "sphinxcontrib.mermaid",
3940
"sphinxext.opengraph",
4041
"sphinx_pyscript",
4142
"sphinx_tippy",

docs/live-preview.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ You can edit the text/configuration below and see the live output.[^note]
2222
::::::::{grid} 1 1 1 2
2323

2424
:::::::{grid-item}
25-
:child-align: end
25+
:child-align: start
2626

2727
```{raw} html
28-
<div><u><span id="myst-version"></span></u></div>
28+
<div><u><span id="myst-version">myst-parser v</span></u></div>
2929
```
3030

3131
:::::{tab-set}
32+
:class: sd-h-100
33+
3234
::::{tab-item} Input text
35+
:class-container: sd-h-100
36+
:class-content: sd-h-100
37+
3338
````{raw} html
3439
<textarea class="pyscript" id="input_myst">
3540
# Heading 1
@@ -40,49 +45,66 @@ Hallo world!
4045
An admonition note!
4146
```
4247
48+
## Math
49+
50+
```python
51+
from package import module
52+
module.call("string")
53+
```
54+
55+
## Definition list
56+
4357
term
4458
: definition
4559
46-
$$\pi = 3.14159$$
60+
## Math
4761
48-
```{list-table}
49-
:header-rows: 1
50-
:align: center
62+
$$\pi = 3.14159$$
5163
52-
* - Header 1
53-
- Header 2
54-
* - Item 1
55-
- Item 2
56-
```
64+
## Figures
5765
5866
```{figure} https://via.placeholder.com/150
5967
:width: 100px
6068
:align: center
6169
6270
Figure caption
6371
```
72+
73+
## Tables
74+
75+
```{list-table}
76+
:header-rows: 1
77+
:align: center
78+
79+
* - Header 1
80+
- Header 2
81+
* - Item 1 a
82+
- Item 2 a
83+
* - Item 1 b
84+
- Item 2 b
85+
```
6486
</textarea>
6587
````
6688

6789
::::
6890
::::{tab-item} Configuration (YAML)
91+
:class-container: sd-h-100
92+
:class-content: sd-h-100
93+
6994
<textarea class="pyscript" id="input_config">
7095
# see: https://docutils.sourceforge.io/docs/user/config.html
7196
myst_enable_extensions:
7297
- colon_fence
7398
- deflist
7499
- dollarmath
75-
myst_highlight_code_blocks: false
76-
embed_stylesheet: true
77-
stylesheet_path:
78-
- minimal.css
100+
myst_highlight_code_blocks: true
79101
</textarea>
80102
::::
81103
:::::
82104

83105
:::::::
84106
:::::::{grid-item}
85-
:child-align: end
107+
:child-align: start
86108

87109
```{raw} html
88110
<div class="display-flex">
@@ -97,7 +119,7 @@ stylesheet_path:
97119

98120
::::{tab-set}
99121
:::{tab-item} HTML Render
100-
<iframe class="pyscript" id="output_html" readonly="true"></iframe>
122+
<div class="pyscript" id="output_html"></div>
101123
:::
102124
:::{tab-item} Raw Output
103125
<textarea class="pyscript" id="output_raw" readonly="true"></textarea>

docs/live_preview.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,38 @@
33

44
import yaml
55
from docutils.core import publish_string
6+
from docutils.frontend import filter_settings_spec
7+
from docutils.writers.html5_polyglot import HTMLTranslator, Writer
68
from js import document
79

810
from myst_parser import __version__
911
from myst_parser.parsers.docutils_ import Parser
1012

1113

14+
class SimpleTranslator(HTMLTranslator):
15+
def visit_literal_block(self, node):
16+
node["classes"].append("highlight")
17+
return super().visit_literal_block(node)
18+
19+
def stylesheet_call(self, *args, **kwargs):
20+
return ""
21+
22+
23+
class SimpleWriter(Writer):
24+
settings_spec = filter_settings_spec(
25+
Writer.settings_spec,
26+
"template",
27+
)
28+
29+
def apply_template(self):
30+
subs = self.interpolation_dict()
31+
return "%(body)s\n" % subs
32+
33+
def __init__(self):
34+
self.parts = {}
35+
self.translator_class = SimpleTranslator
36+
37+
1238
def convert(input_config: str, input_myst: str, writer_name: str) -> dict:
1339
warning_stream = StringIO()
1440
try:
@@ -21,14 +47,22 @@ def convert(input_config: str, input_myst: str, writer_name: str) -> dict:
2147
{
2248
"output_encoding": "unicode",
2349
"warning_stream": warning_stream,
50+
# to mimic the sphinx parser
51+
"doctitle_xform": False,
52+
"sectsubtitle_xform": False,
53+
"initial_header_level": 1,
2454
}
2555
)
2656
try:
2757
output = publish_string(
2858
input_myst,
2959
parser=Parser(),
30-
writer_name=writer_name,
3160
settings_overrides=settings,
61+
**(
62+
{"writer": SimpleWriter()}
63+
if "html" in writer_name
64+
else {"writer_name": writer_name}
65+
),
3266
)
3367
except Exception as exc:
3468
output = f"ERROR: conversion:\n{exc}\n{traceback.format_exc()}"
@@ -38,7 +72,7 @@ def convert(input_config: str, input_myst: str, writer_name: str) -> dict:
3872
version_label = document.querySelector("span#myst-version")
3973
config_textarea = document.querySelector("textarea#input_config")
4074
input_textarea = document.querySelector("textarea#input_myst")
41-
output_iframe = document.querySelector("iframe#output_html")
75+
output_iframe = document.querySelector("div#output_html")
4276
output_raw = document.querySelector("textarea#output_raw")
4377
warnings_textarea = document.querySelector("textarea#output_warnings")
4478
oformat_select = document.querySelector("select#output_format")
@@ -48,11 +82,9 @@ def do_convert(event=None):
4882
result = convert(config_textarea.value, input_textarea.value, oformat_select.value)
4983
output_raw.value = result["output"]
5084
if "html" in oformat_select.value:
51-
output_iframe.contentDocument.body.innerHTML = result["output"]
85+
output_iframe.innerHTML = result["output"]
5286
else:
53-
output_iframe.contentDocument.body.innerHTML = (
54-
"Change output format to HTML to see output"
55-
)
87+
output_iframe.innerHTML = "Change output format to HTML to see output"
5688
warnings_textarea.value = result["warnings"]
5789

5890

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ linkify = ["linkify-it-py~=1.0"]
5454
# Note: This is only required for internal use
5555
rtd = [
5656
"ipython",
57-
# currently required to get sphinx v5
58-
"sphinx-book-theme==0.4.0rc1",
59-
"sphinx-design",
57+
"sphinx-book-theme==1.0.0rc2",
58+
"pydata-sphinx-theme==v0.13.0rc4",
59+
"sphinx-design2",
6060
"sphinx-copybutton",
6161
"sphinxext-rediraffe~=0.2.7",
6262
"sphinxcontrib.mermaid~=0.7.1",

0 commit comments

Comments
 (0)