jgm/pandocfilters

Include markdown example?

Closed this issue · 6 comments

pd3 commented

Hi,
there are examples of pandoc filters which allow to include code blocks or raw tex. I'd like to include markdown - is it possible at all?

jgm commented

Yes, you can have a raw markdown block, just like raw latex.
It will only render in the target document if your output
format is markdown.

+++ pd3 [Nov 07 15 00:26 ]:

Hi,
there are examples of pandoc filters which allow to include code blocks
or raw tex. I'd like to include markdown - is it possible at all?


Reply to this email directly or [1]view it on GitHub.

References

  1. #27
pd3 commented

Sorry if my question was confusing. What I meant was: can I split my markdown document into logical parts and then include them from one main document. Each included part would be normally processed as a markdown text. I know there are solutions like cat-ing the parts together and then feeding the concatenated parts into pandoc, but that is not what I am after. I want to be able to write some kind of include statement in the markdown and have the included part markdown-processed.

jgm commented

This can be done, but not with filters, since filters
operate after the document is parsed.

What you want is some kind of preprocessor, which you could
write in any language you like (you might use a preprocessor
like m4).

+++ pd3 [Nov 08 15 23:58 ]:

Sorry if my question was confusing. What I meant was: can I split my
markdown document into logical parts and then include them from one
main document. Each included part would be normally processed as a
markdown text. I know there are solutions like cat-ing the parts
together and then feeding the concatenated parts into pandoc, but that
is not what I am after. I want to be able to write some kind of include
statement in the markdown and have the included part
markdown-processed.


Reply to this email directly or [1]view it on GitHub.

References

  1. #27 (comment)

A bit late, but this could be done with the convert_markdown(text, format) function of panflute. If the include statement is $include=path, a quick sketch could be:

from panflute import *

def include_filter(elem, doc):
    if type(elem) == Para and len(elem.items) == 1:
        e = elem.items[0]
        if type(e) == Str and e.text.startswith('$include='):
            include, fn = e.text.split('=')
            with open(fn) as f:
                items = convert_markdown(f.read(), format='json')
            return(Div(*items), attributes={'source': fn)

if __name__ == "__main__":
    toJSONFilter(include_filter)

This has the advantage of allowing recursive includes (although that probably is a bad idea). This is not robust to spaces in the text though, so perhaps we need to apply stringify to the contents of the paragraph.

Off topic:

On an unrelated note, this code would be far easier with a .parent element (so we don't have apply the filter to a Para instead of to a Str). However, with a table the .parent attribute is not very useful as it doesn't tell the filter if the element is part of the header, the caption, or the table (so perhaps we need elements like TableHeader and TableData, with the extra layer of complications?)

ickc commented

@pd3, @sergiocorreia has an complete example on includes using panflute too.

Should this issue be closed?

pd3 commented

Yes, thank you