Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ viewmd examples/sample.md --head examples/sample.css --head examples/sample.js
viewmd examples/nbsample.ipynb
```

`fillmd` instantiates a template from the command line: it executes the template's `{python}` blocks, fills tokens from frontmatter `formdata:` plus an optional YAML values file (scalars stay strings), and writes the filled Markdown to stdout or `--out`. `--lenient` defers unresolved tokens with warnings instead of raising, for staged fills.
`fillmd` instantiates a template from the command line: it executes the template's `{python}` blocks, fills tokens from frontmatter `formdata:` plus an optional YAML values file (scalars stay strings), and writes the filled Markdown to stdout or `--out`. `--lenient` defers unresolved tokens with warnings instead of raising, for staged fills. Executing code needs `execnb`, which the `fill` extra installs (`pip install 'mdhtml[fill]'`); everything else works without it.

A dialog or notebook `.ipynb` works as the template too. Its code cells are the executable blocks (`eval: false` cells are skipped), each cell's rendered outputs weave in as prose in place of the source, messages participate per aidialog's `export_filter` rule (every exported message when any exist, otherwise every non-pinned one), and a leading frontmatter message is consumed for `formdata:` rather than emitted.

Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dynamic = ["version"]
description = "A bounded-time, Pandoc-leaning Markdown parser with GFM, Extra/kramdown, math, fenced divs, and MDHTML output."
license = {text = "Apache-2.0"}
requires-python = ">=3.11"
dependencies = ["fast5ever>=0.1.1", "fastcore>=2.2.7", "aidialog>=0.0.22", "pyyaml>=6.0.3", "execnb>=0.3.2"]
dependencies = ["fast5ever>=0.1.1", "fastcore>=2.2.7", "aidialog>=0.0.22", "pyyaml>=6.0.3"]
readme = "README.md"
authors = [{name = "Jeremy Howard", email = "j@fast.ai"}]
classifiers = [
Expand All @@ -28,7 +28,8 @@ Repository = "https://github.com/AnswerDotAI/mdhtml"
Issues = "https://github.com/AnswerDotAI/mdhtml/issues"

[project.optional-dependencies]
dev = ["fastship>=0.0.14", "maturin~=1.0", "pytest", "fastpylight>=0.1.6", "math-core~=0.7.0"]
fill = ["execnb>=0.3.2"]
dev = ["fastship>=0.0.14", "maturin~=1.0", "pytest", "execnb>=0.3.2", "fastpylight>=0.1.6", "math-core~=0.7.0"]

[project.entry-points.fastaudit_safe_native]
mdhtml = "mdhtml"
Expand Down
14 changes: 10 additions & 4 deletions python/mdhtml/fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
text; `instantiate` adds data gathering (frontmatter `formdata:` via `fastcore.xtras.frontmatter`
with `strvals=True`: structure kept, every scalar a `str` except `true`/`True`/`false`/`False`,
which are `bool`) and the one execution point for
`{python}` blocks (an `execnb` shell: IPython last-expression semantics, `_repr_markdown_`
`{python}` blocks (an `execnb` shell, from the `fill` extra: IPython last-expression semantics, `_repr_markdown_`
preferred over `str()`, stdout discarded). Dialog templates (`instantiate_nb`) run code opt-in:
only cells marked `#| eval: true` participate (all but `eval: false` cells when the dialog's own
frontmatter says `eval: true`), and a cell that doesn't participate contributes nothing to the
Expand All @@ -29,7 +29,6 @@
from fastcore.script import call_parse
from fastcore.xtras import frontmatter, strloader
from fastcore.nbio import nb_frontmatter, cell_frontmatter
from execnb.shell import CaptureShell
from aidialog.dialog import dlg2md
from aidialog.ipynb import read_ipynb
from fast5ever import parse_fragment
Expand Down Expand Up @@ -271,11 +270,18 @@ def frontmatter_data(src):



def _capture_shell():
"A `CaptureShell`, imported lazily: a bare install carries no execnb or IPython (the `fill` extra provides them)."
try: from execnb.shell import CaptureShell
except ImportError as e: raise ImportError("executing code needs execnb: pip install 'mdhtml[fill]'") from e
return CaptureShell()


def _weave(norm, data, tmpls):
"Execute `{python}` blocks once each, in document order, in one shared `execnb` shell, and splice each block's rendered output: what a notebook's output area shows (`CaptureShell.run_text`). Blocks may read and mutate `__data__` (the live values dict); rebinding it is ignored."
spans = [b for b in _blocks(norm, templates=tmpls) if b["type"] == "code_block" and b.get("info") == "{python}"]
if not spans: return norm
shell = CaptureShell()
shell = _capture_shell()
shell.user_ns["__data__"] = data
starts = _line_starts(norm)
out, cur = [], 0
Expand Down Expand Up @@ -323,7 +329,7 @@ def instantiate_nb(
d = read_ipynb(fname)
fd = nb_frontmatter(d, strvals=True).get("formdata")
merged = {**(fd if isinstance(fd, dict) else {}), **(data or {})}
shell = CaptureShell()
shell = _capture_shell()
shell.user_ns["__data__"] = merged
ran = d.execute(default_eval=False, shell=shell)
if shell.exc:
Expand Down
54 changes: 27 additions & 27 deletions tests/source/cmark-gfm/spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ bar
<h1>Foo</h1>
````````````````````````````````

```````````````````````````````` example
```````````````````````````````` example disabled
*→*→*→
.
<hr />
Expand Down Expand Up @@ -548,7 +548,7 @@ of three or more matching `-`, `_`, or `*` characters, each followed
optionally by any number of spaces or tabs, forms a
[thematic break](@).

```````````````````````````````` example
```````````````````````````````` example disabled
***
---
___
Expand Down Expand Up @@ -590,7 +590,7 @@ __</p>

One to three spaces indent are allowed:

```````````````````````````````` example
```````````````````````````````` example disabled
***
***
***
Expand Down Expand Up @@ -622,7 +622,7 @@ Foo

More than three characters may be used:

```````````````````````````````` example
```````````````````````````````` example disabled
_____________________________________
.
<hr />
Expand All @@ -631,21 +631,21 @@ _____________________________________

Spaces are allowed between the characters:

```````````````````````````````` example
```````````````````````````````` example disabled
- - -
.
<hr />
````````````````````````````````


```````````````````````````````` example
```````````````````````````````` example disabled
** * ** * ** * **
.
<hr />
````````````````````````````````


```````````````````````````````` example
```````````````````````````````` example disabled
- - - -
.
<hr />
Expand All @@ -654,7 +654,7 @@ Spaces are allowed between the characters:

Spaces are allowed at the end:

```````````````````````````````` example
```````````````````````````````` example disabled
- - - -
.
<hr />
Expand Down Expand Up @@ -688,7 +688,7 @@ So, this is not a thematic break:

Thematic breaks do not need blank lines before or after:

```````````````````````````````` example
```````````````````````````````` example disabled
- foo
***
- bar
Expand All @@ -705,7 +705,7 @@ Thematic breaks do not need blank lines before or after:

Thematic breaks can interrupt a paragraph:

```````````````````````````````` example
```````````````````````````````` example disabled
Foo
***
bar
Expand Down Expand Up @@ -738,7 +738,7 @@ bar
When both a thematic break and a list item are possible
interpretations of a line, the thematic break takes precedence:

```````````````````````````````` example
```````````````````````````````` example disabled
* Foo
* * *
* Bar
Expand All @@ -755,7 +755,7 @@ interpretations of a line, the thematic break takes precedence:

If you want a thematic break in a list item, use a different bullet:

```````````````````````````````` example
```````````````````````````````` example disabled
- Foo
- * * *
.
Expand Down Expand Up @@ -955,7 +955,7 @@ of the closing sequence:
ATX headings need not be separated from surrounding content by blank
lines, and they can interrupt paragraphs:

```````````````````````````````` example
```````````````````````````````` example disabled
****
## foo
****
Expand Down Expand Up @@ -1021,7 +1021,7 @@ Simple examples:

Dialect deviation: no setext headings - a `---` underline is a thematic break and an `===` underline is paragraph text.

```````````````````````````````` example
```````````````````````````````` example disabled
Foo *bar*
=========

Expand Down Expand Up @@ -1065,7 +1065,7 @@ baz</em>→

The underlining can be any length:

```````````````````````````````` example
```````````````````````````````` example disabled
Foo
-------------------------

Expand All @@ -1082,7 +1082,7 @@ Foo
The heading content can be indented up to three spaces, and need
not line up with the underlining:

```````````````````````````````` example
```````````````````````````````` example disabled
Foo
---

Expand Down Expand Up @@ -1122,7 +1122,7 @@ Foo
The setext heading underline can be indented up to three spaces, and
may have trailing spaces:

```````````````````````````````` example
```````````````````````````````` example disabled
Foo
----
.
Expand All @@ -1144,7 +1144,7 @@ Foo

The setext heading underline cannot contain internal spaces:

```````````````````````````````` example
```````````````````````````````` example disabled
Foo
= =

Expand All @@ -1160,7 +1160,7 @@ Foo

Trailing spaces in the content line do not cause a line break:

```````````````````````````````` example
```````````````````````````````` example disabled
Foo
-----
.
Expand All @@ -1171,7 +1171,7 @@ Foo

Nor does a backslash at the end:

```````````````````````````````` example
```````````````````````````````` example disabled
Foo\
----
.
Expand All @@ -1183,7 +1183,7 @@ Foo\
Since indicators of block structure take precedence over
indicators of inline structure, the following are setext headings:

```````````````````````````````` example
```````````````````````````````` example disabled
`Foo
----
`
Expand Down Expand Up @@ -1297,7 +1297,7 @@ in these examples gets interpreted as a thematic break:
````````````````````````````````


```````````````````````````````` example
```````````````````````````````` example disabled
- foo
-----
.
Expand All @@ -1318,7 +1318,7 @@ in these examples gets interpreted as a thematic break:
````````````````````````````````


```````````````````````````````` example
```````````````````````````````` example disabled
> foo
-----
.
Expand All @@ -1332,7 +1332,7 @@ in these examples gets interpreted as a thematic break:
If you want a heading with `> foo` as its literal text, you can
use backslash escapes:

```````````````````````````````` example
```````````````````````````````` example disabled
\> foo
------
.
Expand Down Expand Up @@ -1399,7 +1399,7 @@ bar</p>
or use a thematic break that cannot count as a [setext heading
underline], such as

```````````````````````````````` example
```````````````````````````````` example disabled
Foo
bar
* * *
Expand Down Expand Up @@ -1571,7 +1571,7 @@ blocks:

Dialect deviation: no setext headings - a `---` underline is a thematic break and an `===` underline is paragraph text.

```````````````````````````````` example
```````````````````````````````` example disabled
# Heading
foo
Heading
Expand Down Expand Up @@ -3881,7 +3881,7 @@ foo
In general, blank lines are not needed before or after block
quotes:

```````````````````````````````` example
```````````````````````````````` example disabled
> aaa
***
> bbb
Expand Down
6 changes: 3 additions & 3 deletions tests/source/php-markdown-extra.mdtest/Abbr.text
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ Let's transfert documents through TCP/IP, using TCP packets.
*[IP]: Internet Protocol
*[TCP]: Transmission Control Protocol

---
---

Bienvenue sur [CMS](http://www.bidulecms.com "Bidule CMS").

*[CMS]: Content Management System

---
---

ATCCE

*[ATCCE]: Abbreviation "Testing" Correct 'Character' < Escapes >
*[ATCCE]: Abbreviation "Testing" Correct 'Character' < Escapes >
6 changes: 3 additions & 3 deletions tests/source/php-markdown-extra.mdtest/Definition Lists.text
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Definition 1 paragraph 1 line 2 (lazy)
Definition 1 paragraph 2 line 1 ...
Definition 1 paragraph 2 line 2 (lazy)

* * *
---

A mix:

Expand Down Expand Up @@ -105,11 +105,11 @@ Term 4
Definition 9 paragraph 2 line 1
: Definition 10 (no paragraph)

* * *
---

Special cases:

Term

: code block
as first element of a definition
as first element of a definition
4 changes: 2 additions & 2 deletions tests/source/php-markdown-extra.mdtest/Footnotes.text
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ footnote test[^reference].
from another footnote. But [^reference] should be litteral
since the footnote with that name has already been used.

- - -
---

Testing unusual footnote name[^1$^!"'].

[^1$^!"']: Haha!

- - -
---

Footnotes mixed with images[^image-mixed]
![1800 Travel][img6]
Expand Down
Loading