Comments (3)
I think I'm doing that only for HTMLSpan
and not for HTMLBlock
, in accordance with CommonMark. Part of the reason, and CommonMark is aware of this, is the performance cost when recursively rendering within HTML elements. The interesting thing is that CommonMark has some pretty particular definitions for HTMLBlock
. Using these definitions, I think not rendering inner elements for HTMLBlock
would be following the rule of least surprises. You can take a look at Section 4.6 in the spec in case you're interested.
Sorry for the late response! I'm closing the issue, but feel free to comment further.
from mistletoe.
I played with it a little, and perhaps this is what's going on. Using this code:
class Aside(SpanToken):
pattern = re.compile(r"<aside>(.+?)</aside>")
class CustomRenderer(HTMLRenderer):
def __init__(self):
super().__init__(Aside)
def render_aside(self, token):
return '<aside>{}</aside>'.format(self.render_inner(token))
I can render:
bar <aside>*foo*</aside>
to:
<p>bar <aside><em>foo</em></aside></p>
But this markdown snippet will not render the inner emphasis:
<aside>*foo*</aside>
Because the entire block is first captured by the HTMLBlock
token. To get around this, you need to define both a block-level aside tag and a span-level one, and pass both of them into the super
constructor. Something like:
from mistletoe.span_token import tokenize_inner
class AsideBlock(BlockToken):
def __init__(self, lines):
# edit: strings are immutable! typo in the previous version of this reply
lines[0] = lines[0].replace('<aside>', '')
lines[-1] = lines[-1].replace('</aside>', '')
super().__init__(''.join(lines), tokenize_inner)
@classmethod
def start(cls, line):
return line.lstrip().startswith('<aside>')
@classmethod
def read(cls, lines):
line_buffer = []
for line in lines:
line_buffer.append(line)
if line.rstrip().endswith('</aside>') or line == '\n':
break
return line_buffer
By the way, rendering the inner elements of HTMLSpan
should also be the standard behavior, and I'll probably add it soon. Let me know if there're any further questions!
from mistletoe.
Cool, this works. (Note: I did have to modify the AsideBlock
init function a bit since strings are immutable so lines[0].replace
returns a string but doesn't modify the given one). Thanks!
In terms of making it the default behavior to render inner elements, do you think you'd do that for both HTMLSpan
and HTMLBlock
or just one of them? I went back and forth on whether I wanted to do this for all elements or not - I think it probably makes sense, though I don't think it's strictly commonmark compliant.
from mistletoe.
Related Issues (20)
- **`what`:** gives odd output HOT 3
- Support of Smart punctuation / Simple typographic replacements HOT 3
- 0.8.2: pytest is failing in `test/test_contrib/test_github_wiki.py::TestGithubWiki::test_parse` unit and `DeprecationWarning` warnings HOT 5
- Record line numbers on tokens HOT 6
- Unused imports HOT 1
- question: how to render AST back to a markdown string HOT 1
- LaTeX renderer does not handle inline code with vertical bars correctly HOT 2
- pikchr integration? HOT 1
- Question: Inconsistencies in the block tokens HOT 2
- Enable tables which interrupt a paragraph (like GFM does) HOT 5
- IndexError on malformed markdown tables HOT 1
- Emphasis around link ignored when followed by a special punctuation character HOT 1
- Don't convert the quotes HOT 3
- Contrib sub-package is missing in the mistletoe 1.0.0 PyPI version HOT 3
- Question how to modify ast and write back out as markdown HOT 2
- Do not escape double or single quotes by default HOT 1
- Use strict PascalCase for class names HOT 3
- Renderer for Natural Docs HOT 6
- How to add a custom token inside a block token? HOT 1
- How to add a custom token inside a block token? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mistletoe.