from IPython.display import HTML,Markdown,display
from mistletoe import markdownMy Heading
Highlight
def Highlight(
match
):
Base token class.
Token has two subclasses:
block_token.BlockToken, for all block level tokens. A block level token is text which occupies the entire horizontal width of the “page” and is offset for the surrounding sibling block with line breaks.span_token.SpanToken, for all span-level (or inline-level) tokens. A span-level token appears inside the flow of the text lines without any surrounding line break.
Custom __repr__ methods in subclasses: The default __repr__ implementation outputs the number of child tokens (from the attribute children) if applicable, and the content attribute if applicable. If any additional attributes should be included in the __repr__ output, this can be specified by setting the class attribute repr_attributes to a tuple containing the attribute names to be output.
Superscript
def Superscript(
match
):
Base token class.
Token has two subclasses:
block_token.BlockToken, for all block level tokens. A block level token is text which occupies the entire horizontal width of the “page” and is offset for the surrounding sibling block with line breaks.span_token.SpanToken, for all span-level (or inline-level) tokens. A span-level token appears inside the flow of the text lines without any surrounding line break.
Custom __repr__ methods in subclasses: The default __repr__ implementation outputs the number of child tokens (from the attribute children) if applicable, and the content attribute if applicable. If any additional attributes should be included in the __repr__ output, this can be specified by setting the class attribute repr_attributes to a tuple containing the attribute names to be output.
Subscript
def Subscript(
match
):
Base token class.
Token has two subclasses:
block_token.BlockToken, for all block level tokens. A block level token is text which occupies the entire horizontal width of the “page” and is offset for the surrounding sibling block with line breaks.span_token.SpanToken, for all span-level (or inline-level) tokens. A span-level token appears inside the flow of the text lines without any surrounding line break.
Custom __repr__ methods in subclasses: The default __repr__ implementation outputs the number of child tokens (from the attribute children) if applicable, and the content attribute if applicable. If any additional attributes should be included in the __repr__ output, this can be specified by setting the class attribute repr_attributes to a tuple containing the attribute names to be output.
Emoji
def Emoji(
match
):
Base token class.
Token has two subclasses:
block_token.BlockToken, for all block level tokens. A block level token is text which occupies the entire horizontal width of the “page” and is offset for the surrounding sibling block with line breaks.span_token.SpanToken, for all span-level (or inline-level) tokens. A span-level token appears inside the flow of the text lines without any surrounding line break.
Custom __repr__ methods in subclasses: The default __repr__ implementation outputs the number of child tokens (from the attribute children) if applicable, and the content attribute if applicable. If any additional attributes should be included in the __repr__ output, this can be specified by setting the class attribute repr_attributes to a tuple containing the attribute names to be output.
FootnoteRef
def FootnoteRef(
match
):
Base token class.
Token has two subclasses:
block_token.BlockToken, for all block level tokens. A block level token is text which occupies the entire horizontal width of the “page” and is offset for the surrounding sibling block with line breaks.span_token.SpanToken, for all span-level (or inline-level) tokens. A span-level token appears inside the flow of the text lines without any surrounding line break.
Custom __repr__ methods in subclasses: The default __repr__ implementation outputs the number of child tokens (from the attribute children) if applicable, and the content attribute if applicable. If any additional attributes should be included in the __repr__ output, this can be specified by setting the class attribute repr_attributes to a tuple containing the attribute names to be output.
FootnoteEntry
def FootnoteEntry(
match
):
Base class for block-level tokens. Recursively parse inner tokens.
Naming conventions:
* lines denotes a list of (possibly unparsed) input lines, and is
commonly used as the argument name for constructors.
* BlockToken.children is a list with all the inner tokens (thus if
a token has children attribute, it is not a leaf node; if a token
calls span_token.tokenize_inner, it is the boundary between
span-level tokens and block-level tokens);
* BlockToken.start takes a line from the document as argument, and
returns a boolean representing whether that line marks the start
of the current token. Every subclass of BlockToken must define a
start function (see block_tokenizer.tokenize).
* BlockToken.read takes the rest of the lines in the document as an
iterator (including the start line), and consumes all the lines
that should be read into this token.
Default to stop at an empty line.
Note that BlockToken.read does not have to return a list of lines.
Because the return value of this function will be directly
passed into the token constructor, we can return any relevant
parsing information, sometimes even ready-made tokens,
into the constructor. See block_tokenizer.tokenize.
If BlockToken.read returns None, the read result is ignored,
but the token class is responsible for resetting the iterator
to a previous state. See block_tokenizer.FileWrapper.get_pos,
block_tokenizer.FileWrapper.set_pos.
Attributes: children (list): inner tokens. line_number (int): starting line (1-based).
AutoLink
def AutoLink(
match
):
Base token class.
Token has two subclasses:
block_token.BlockToken, for all block level tokens. A block level token is text which occupies the entire horizontal width of the “page” and is offset for the surrounding sibling block with line breaks.span_token.SpanToken, for all span-level (or inline-level) tokens. A span-level token appears inside the flow of the text lines without any surrounding line break.
Custom __repr__ methods in subclasses: The default __repr__ implementation outputs the number of child tokens (from the attribute children) if applicable, and the content attribute if applicable. If any additional attributes should be included in the __repr__ output, this can be specified by setting the class attribute repr_attributes to a tuple containing the attribute names to be output.
Strikethrough
def Strikethrough(
match
):
Base token class.
Token has two subclasses:
block_token.BlockToken, for all block level tokens. A block level token is text which occupies the entire horizontal width of the “page” and is offset for the surrounding sibling block with line breaks.span_token.SpanToken, for all span-level (or inline-level) tokens. A span-level token appears inside the flow of the text lines without any surrounding line break.
Custom __repr__ methods in subclasses: The default __repr__ implementation outputs the number of child tokens (from the attribute children) if applicable, and the content attribute if applicable. If any additional attributes should be included in the __repr__ output, this can be specified by setting the class attribute repr_attributes to a tuple containing the attribute names to be output.
ExtendedHtmlRenderer
def ExtendedHtmlRenderer(
args:VAR_POSITIONAL, kwargs:VAR_KEYWORD
):
HTML renderer class.
See mistletoe.base_renderer module for more info.
def render_md(md): return HTML(markdown(md, ExtendedHtmlRenderer))render_md("Here's a sentence with a footnote[^1] and another[^2].\n\n[^1]: First note.\n[^2]: Second note.")render_md("~~strikethrough~~ and **bold ~~strikethrough~~**")strikethrough and bold strikethrough
render_md("Check out https://fast.ai and http://example.com for more info!")Check out https://fast.ai and http://example.com for more info!
render_md("Here's a sentence with a link to <http://www.example.org>.")Here's a sentence with a link to http://www.example.org.
render_md("""Here's some code:
```
https://fast.ai
~~strikethrough~~
:smile:
H~2~O
```
And outside the block: https://fast.ai ~~strikethrough~~ :smile: H~2~O""")Here's some code:
https://fast.ai
~~strikethrough~~
:smile:
H~2~O
And outside the block: https://fast.ai strikethrough 😊 H2O
render_md("H~2~O and E=mc^2^")H2O and E=mc2
test_md2 = """
- [x] Completed task
- [ ] Incomplete task
- Regular item"""
render_md(test_md2)- Completed task
- Incomplete task
- Regular item
render_md("==highlighted== and :smile: :rocket: :heart:")highlighted and 😊 🚀 ❤️
parse_attrs
def parse_attrs(
text
):
parse_attrs('{#my-id .class1 .class2 width="50%" height="200 px"}')' id="my-id" class="class1 class2" width="50%" height="200 px"'
ExtendedHtmlRenderer.render_heading
def render_heading(
token
):
<style>
.important { background-color: yellow; font-weight: bold; }
</style>render_md("# My Heading {#intro .important}")Tests
from fastcore.test import test_eqwith ExtendedHtmlRenderer() as renderer:
test_eq(renderer.render(Document(':sm ile:')), '<p>:sm ile:</p>\n')
test_eq(renderer.render(Document(':unknown:')), '<p>:unknown:</p>\n')
test_eq(renderer.render(Document(':smile:')), '<p>😊</p>\n')
test_eq(renderer.render(Document('H~ 2~O')), '<p>H~ 2~O</p>\n')
test_eq(renderer.render(Document('H~2~O')), '<p>H<sub>2</sub>O</p>\n')
test_eq(renderer.render(Document('E=mc^ 2^')), '<p>E=mc^ 2^</p>\n')
test_eq(renderer.render(Document('E=mc^2^')), '<p>E=mc<sup>2</sup></p>\n')
test_eq(renderer.render(Document('~~no space~~')), '<p><del>no space</del></p>\n')
test_eq(renderer.render(Document('**~~nested~~**')), '<p><strong><del>nested</del></strong></p>\n')
test_eq(renderer.render(Document('==~~double~~==')), '<p><mark><del>double</del></mark></p>\n')
html = renderer.render(Document('```\nhttps://fast.ai\n```'))
assert 'href' not in html and 'https://fast.ai' in html
html = renderer.render(Document('https://fast.ai'))
assert '<a href="https://fast.ai">https://fast.ai</a>' in html
html = renderer.render(Document('[link](https://fast.ai)'))
assert html.count('href') == 1
test_eq(renderer.render(Document('[^1]')), '<p><sup><a href="#fn-1" id="fnref-1">[1]</a></sup></p>\n')
html = renderer.render(Document('- [x] done\n- [ ] todo\n- regular'))
assert 'checked' in html and html.count('checkbox') == 2with ExtendedHtmlRenderer() as renderer:
test_eq(renderer.render(Document('# Heading {#intro}')), '<h1 id="intro">Heading</h1>\n')
test_eq(renderer.render(Document('## Test {.important}')), '<h2 class="important">Test</h2>\n')
test_eq(renderer.render(Document('### Multi {.c1 .c2}')), '<h3 class="c1 c2">Multi</h3>\n')
html = renderer.render(Document('# Full {#id1 .cls data-level="1"}'))
assert 'id="id1"' in html and 'class="cls"' in html and 'data-level="1"' in html
test_eq(renderer.render(Document('# Plain')), '<h1>Plain</h1>\n')
test_eq(renderer.render(Document('## Empty {}')), '<h2>Empty {}</h2>\n')
html = renderer.render(Document('# Order {.first #myid .second key="val"}'))
assert 'id="myid"' in html and 'class="first second"' in html and 'key="val"' in html
test_eq(renderer.render(Document('# Spaces {data-value="hello world"}')), '<h1 data-value="hello world">Spaces</h1>\n')