Thomas,
The fenced_code_block extension uses a preprocessor to parse fenced code
blocks, which stashes the blocks for later retrieval. They are then
reinserted into the rendered HTML by a postprocessor. As you are only
running the blockprocessors, none of the pieces of the extension
actually ever are run. The only way to use the included fenced code
block extension is to render to HTML. Then run the output through an
HTML parser and iterate over that. Of course, it is then imposable to
tell whether the code block started as a indented block or fenced block.
We do have plans to improve things as outlined in
https://github.com/waylan/Python-Markdown/issues/53 but I can't say when
that will happen. Its not particularly high on my priority list as the
current solution is "good enough" for my needs.
I suppose you could iterate over the stashed blocks. But you would need
to at least run the preprocessor first. The main concern here is that
the stash is just the `HtmlStash` instance for raw HTML. There is no
differentiation between the code blocks and other raw HTML. You might
need to disable raw HTML parsing (another preprocessor). Although, you
might be able to just run `FencedCodeExtension.run` directly. Just make
sure you do whitespace normalization first.
I hope that helps. Let me know if you get stuck on anything and I'll see
if I can help.
Waylan Limberg
On 12/30/2016 02:18 AM, Thomas Baruchel wrote:
> Hi, I would like to iterate over code-blocks of a Markdown document without
> rendering it to anything. I can do it easely with Python-Markdown, but I can't
> manage to add the fenced block extension. My code is:
>
>
> import markdown
> from markdown.extensions.fenced_code import FencedCodeExtension
> M = markdown.Markdown()
> FencedCodeExtension().extendMarkdown(M, markdown.__dict__)
>
> f = open("demo.md", "r")
> tree = M.parser.parseDocument(f.readlines())
> f.close()
> for k in tree.iter():
> if k.tag == "code":
> print(k)
> print(k.text)
>
> It still detects indented code-blocks but not the fenced blocks.
> What am I doing wrong?
>
> Thank you by advance,
>
> regards,
>
|