From: "Sébastien Boisgérault" <sebastien.boisgerault-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: pandoc-discuss <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: Re: Pandoc Document Model in Python
Date: Sat, 4 Dec 2021 07:35:38 -0800 (PST) [thread overview]
Message-ID: <0afdf96b-bc19-4117-a78b-bdbee13e076an@googlegroups.com> (raw)
In-Reply-To: <fafa9cffd5e4437c865e71875b2f58a2-NSENcxR/0n0@public.gmane.org>
[-- Attachment #1.1: Type: text/plain, Size: 5535 bytes --]
You can also use read and write files instead of Python strings. For
example, if you have a `notebookify` function that takes a doc (pandoc AST)
and returns a Jupyter notebook (as a Python JSON-like dictionnary), your
wrapper script could be something like:
# file: notebookify.py
# here: imports, notebookify definition, etc.
# ...
def main():
filename = sys.argv[1]
doc = pandoc.read(file=filename)
notebook = notebookify(doc)
ipynb = Path(filename).with_suffix(".ipynb")
with open(ipynb, "w", encoding="utf-8") as output:
json.dump(notebook, output, ensure_ascii=False, indent=2)
if __name__ == "__main__":
main()
you would invoke it with
$ python notebookify.py my_document.md
and it would produce a my_document.ipynb Jupyter notebook.
This example is developped with more details here:
https://boisgera.github.io/pandoc/examples/#jupyter-notebooks
Cheers,
SB
Le samedi 4 décembre 2021 à 15:58:31 UTC+1, denis...-NSENcxR/0n0@public.gmane.org a écrit :
> Thanks Sébastian.
>
> So, if I understand correctly, you'll use that in in a script that reads
> your markdowns file and outputs another. Correct? (I just mean, usually
> your doc will be in your filesystem, not in a python variable.)
>
> Best
> Denis
>
> ________________________________________
> Von: pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> im Auftrag
> von Sébastien Boisgérault <sebastien....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> Gesendet: Samstag, 4. Dezember 2021 15:43:42
> An: pandoc-discuss
> Betreff: Re: Pandoc Document Model in Python
>
> (Ooops probably answered Denis only and not the group. Here we go again!)
>
> AFAICT filters are document AST to AST transformations. In this Python
> library, docs (Pandoc instances) represent this AST, so a typical AST
> (in-place) transform would be:
>
> import pandoc
> from pandoc.types import *
>
> def uppercase(doc):
> for elt in pandoc.iter(doc):
> if isinstance(elt, Str):
> elt[0] = elt[0].upper() # elt: Str(Text)
>
> If you need to make a markdown to markdown transformation instead, you
> read the input markdown, transform it then write it back:
>
> >>> markdown = "Hello world!"
> >>> doc = pandoc.read(markdown)
> >>> uppercase(doc)
> >>> markdown = pandoc.write(doc)
> >>> print(markdown)
> HELLO WORLD!
>
> There are more examples here:
>
> https://boisgera.github.io/pandoc/examples/
>
> and a documentation of common patterns here:
>
> https://boisgera.github.io/pandoc/cookbook/
>
> Cheers,
>
> Sébastien
>
> Le samedi 4 décembre 2021 à 15:06:10 UTC+1, denis...-NSENcxR/0n0@public.gmane.org a écrit :
> Hi Sebastian
> Looks interesting. But I don't undertake how you'd use that as an
> alternative to a filter. Can you give an example?
> Denis
>
>
>
> ________________________________________
> Von: pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> im Auftrag
> von Sébastien Boisgérault <sebastien....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> Gesendet: Samstag, 4. Dezember 2021 14:20:03
> An: pandoc-discuss
> Betreff: Pandoc Document Model in Python
>
> Hi everyone,
>
> I have just released the 2.0 version of a pandoc python library which
> exposes the Pandoc document model to Python :
>
> https://github.com/boisgera/pandoc#-overview
>
> The main goal is to enable all kinds of analysis, generation and
> transformation of documents with Python (roughly speaking, an alternative
> to pandoc filters); it is not to convert from one format to another (it can
> do it, but so can the standard pandoc command-line tool).
>
> Feedback welcome !
>
> Cheers,
>
> Sébastien
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "pandoc-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<mailto:
> pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pandoc-discuss/f224cd2c-7d68-40b4-a855-7d4d0d7aa442n%40googlegroups.com
> <
> https://groups.google.com/d/msgid/pandoc-discuss/f224cd2c-7d68-40b4-a855-7d4d0d7aa442n%40googlegroups.com?utm_medium=email&utm_source=footer
> >.
>
> --
> You received this message because you are subscribed to the Google Groups
> "pandoc-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<mailto:
> pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pandoc-discuss/de1fd005-0d0d-49a2-86cc-5a72c764835dn%40googlegroups.com
> <
> https://groups.google.com/d/msgid/pandoc-discuss/de1fd005-0d0d-49a2-86cc-5a72c764835dn%40googlegroups.com?utm_medium=email&utm_source=footer
> >.
>
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/0afdf96b-bc19-4117-a78b-bdbee13e076an%40googlegroups.com.
[-- Attachment #1.2: Type: text/html, Size: 9727 bytes --]
next prev parent reply other threads:[~2021-12-04 15:35 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <AQHX6RGokE17J35tB0eLtRDrIrd1JqwiXcS8///5/wCAABOrKA==>
[not found] ` <AQHX6RGokE17J35tB0eLtRDrIrd1JqwiXcS8>
2021-12-04 13:20 ` Sébastien Boisgérault
[not found] ` <f224cd2c-7d68-40b4-a855-7d4d0d7aa442n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2021-12-04 14:06 ` AW: " denis.maier-NSENcxR/0n0
[not found] ` <3b5d75fe4e2a45e38ab45a820d110faf-NSENcxR/0n0@public.gmane.org>
2021-12-04 14:43 ` Sébastien Boisgérault
[not found] ` <de1fd005-0d0d-49a2-86cc-5a72c764835dn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2021-12-04 14:58 ` AW: " denis.maier-NSENcxR/0n0
[not found] ` <fafa9cffd5e4437c865e71875b2f58a2-NSENcxR/0n0@public.gmane.org>
2021-12-04 15:35 ` Sébastien Boisgérault [this message]
2021-12-04 15:30 ` Joseph Reagle
[not found] ` <fe2b314b-863d-f8c0-8dfc-1104422fbf52-T1oY19WcHSwdnm+yROfE0A@public.gmane.org>
2021-12-04 16:17 ` Sébastien Boisgérault
[not found] ` <1e952a20-a77f-4987-9e7f-bac963ba4385n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2021-12-04 16:48 ` Sébastien Boisgérault
2021-12-04 17:30 ` John MacFarlane
2021-12-22 18:05 ` Joseph Reagle
[not found] ` <c0c49e25-898d-c72c-3303-69005985ea01-T1oY19WcHSwdnm+yROfE0A@public.gmane.org>
2021-12-23 10:56 ` Sébastien Boisgérault
[not found] ` <e45c083b-fff3-46ac-8af5-b416c60f6a97n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2021-12-23 14:25 ` Joseph Reagle
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0afdf96b-bc19-4117-a78b-bdbee13e076an@googlegroups.com \
--to=sebastien.boisgerault-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
--cc=pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).