From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/29672 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?S=C3=A9bastien_Boisg=C3=A9rault?= Newsgroups: gmane.text.pandoc Subject: Re: Pandoc Document Model in Python Date: Sat, 4 Dec 2021 07:35:38 -0800 (PST) Message-ID: <0afdf96b-bc19-4117-a78b-bdbee13e076an@googlegroups.com> References: <3b5d75fe4e2a45e38ab45a820d110faf@unibe.ch> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_2822_1091149874.1638632138871" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19997"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBCIOBSUCXMMRBTMVV2GQMGQELZIDXTY-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Sat Dec 04 16:35:44 2021 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-oi1-f183.google.com ([209.85.167.183]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mtX4i-0004zG-7W for gtp-pandoc-discuss@m.gmane-mx.org; Sat, 04 Dec 2021 16:35:44 +0100 Original-Received: by mail-oi1-f183.google.com with SMTP id p186-20020aca42c3000000b002bc99d2b74asf4351005oia.18 for ; Sat, 04 Dec 2021 07:35:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:date:from:to:message-id:in-reply-to:references:subject :mime-version:x-original-sender:reply-to:precedence:mailing-list :list-id:list-post:list-help:list-archive:list-subscribe :list-unsubscribe; bh=d6EbitX1bIHgsBGzeumYQ1j+rScBgLdmxZT3RVkw9eQ=; b=akXaa2+mY21Vrf8inTdescJx4tt7LOOv2nDvaaEzy0q3nHbb5m1v9+Lw5Nkj1h4C5U pzSRTSqEd82kTkwI5sM3M2fUu8Evs/q+mp1z0DplbTtPWagLLnurWY2P4jvJfj061cp7 J5INkuf/qrkf+2AtEvTe5z7bBMUqZ/56o3MoUphgm87iu2rJqeYAPuJspoRfb7Qt7Gz1 hOgItbQQQgSWqb7EQ58RN1dAZJ8q1V0vI+xHVGO1KqHfzs/YSByz6fARsizupsjNqMba Vm55UcHbSWQoRB0HAAmOIsTwFaOSqJM4qsi++bB4C0cejpfDhDzDpp4hN3rqk+xxykR0 P+dw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:message-id:in-reply-to:references:subject:mime-version :x-original-sender:reply-to:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-subscribe:list-unsubscribe; bh=d6EbitX1bIHgsBGzeumYQ1j+rScBgLdmxZT3RVkw9eQ=; b=A1KuU7bRLWy+4hMcMNb+w8uUWshm9Sb8Kji99FlCCLK/EDgljMbF/+HzsNnUoJlL64 oc5kerZKcoBkLhS+Sq8qNUQlCtoaviJGXg8NfVJr8870GU+qt0oJ3udlngGE1yRjNIrx cav/s7n+0RpOyh3jzgae/QgaT0eKapTUvftdZ3YhyjTkQBKiw3g9ujYtJ6L5FpHr9r1H LYctJehfo+DGn8gkfXBZLa7aUAn8y/0fA5VfqTJqC1kXS8butCSDWM/3Po24UN0ONl+1 YRfmPXFLJLkgzySd4NRfeUwTWCVQYHdmjcWH8V8roH9m90YiMhO0lbH6NyvBXQEUbFHe 2wFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:date:from:to:message-id:in-reply-to :references:subject:mime-version:x-original-sender:reply-to :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=d6EbitX1bIHgsBGzeumYQ1j+rScBgLdmxZT3RVkw9eQ=; b=glyyZv63FOlgQaUI6y6fxwSBU9SeBs1TEEFVT1PkP9aHFuQvxaThfoxeu6b6/y559X YSTq2/9RPEI3W8YMgdWgjPBBMfiKOohKewaza7wSYJbiGaVMGNm7vmEE3fD6nuEEXoVD FOxpnHBjxbMlfIr6q0XemvMlv0giwKEsfZujzd1K8x2IeaRk0F2OntyF6xi0wJeKwVbA 23hguX/2kr4SckhTdF9xtLMPZfbANQK7e3hFcgtznjQUu48/FcLkbx5hjchdRIRb9IyX CKaE7Dqxrv93raJk5T3yBhBLTN1Fi1oyY/qgg4LueW6sNRO8XIf/NBna5vmKY5onRqMW e2dg== Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOAM533kUrSd8eSoCCVZ6su/f5Rb3P56CRivSJCFvWi04v/m5jH28zMh 0Buv+FrbXWAIlTqYRv6+bR0= X-Google-Smtp-Source: ABdhPJxZbHJwxOV1wURnJbbgE0Eh25gXoixFBLBna0E6m3+WdEo2eItqrYVcSiSDR6Nb6glCaARTVw== X-Received: by 2002:a05:6808:3c2:: with SMTP id o2mr16049873oie.112.1638632142717; Sat, 04 Dec 2021 07:35:42 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:aca:41d5:: with SMTP id o204ls4709751oia.9.gmail; Sat, 04 Dec 2021 07:35:39 -0800 (PST) X-Received: by 2002:aca:1b08:: with SMTP id b8mr14896618oib.148.1638632139452; Sat, 04 Dec 2021 07:35:39 -0800 (PST) In-Reply-To: X-Original-Sender: Sebastien.Boisgerault-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Precedence: list Mailing-list: list pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org; contact pandoc-discuss+owners-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org List-ID: X-Google-Group-Id: 1007024079513 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Xref: news.gmane.io gmane.text.pandoc:29672 Archived-At: ------=_Part_2822_1091149874.1638632138871 Content-Type: multipart/alternative; boundary="----=_Part_2823_1090370034.1638632138871" ------=_Part_2823_1090370034.1638632138871 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable You can also use read and write files instead of Python strings. For=20 example, if you have a `notebookify` function that takes a doc (pandoc AST)= =20 and returns a Jupyter notebook (as a Python JSON-like dictionnary), your=20 wrapper script could be something like: # file: notebookify.py # here: imports, notebookify definition, etc. # ... def main(): filename =3D sys.argv[1] doc =3D pandoc.read(file=3Dfilename) notebook =3D notebookify(doc) ipynb =3D Path(filename).with_suffix(".ipynb") with open(ipynb, "w", encoding=3D"utf-8") as output: json.dump(notebook, output, ensure_ascii=3DFalse, indent=3D2) if __name__ =3D=3D "__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=C3=A9cembre 2021 =C3=A0 15:58:31 UTC+1, denis...-NSENcxR/0n0@public.gmane.org a = =C3=A9crit : > Thanks S=C3=A9bastian. > > So, if I understand correctly, you'll use that in in a script that reads= =20 > your markdowns file and outputs another. Correct? (I just mean, usually= =20 > your doc will be in your filesystem, not in a python variable.) > > Best > Denis > > ________________________________________ > Von: pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org im Auftrag= =20 > von S=C3=A9bastien Boisg=C3=A9rault > 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=20 > library, docs (Pandoc instances) represent this AST, so a typical AST=20 > (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] =3D elt[0].upper() # elt: Str(Text) > > If you need to make a markdown to markdown transformation instead, you=20 > read the input markdown, transform it then write it back: > > >>> markdown =3D "Hello world!" > >>> doc =3D pandoc.read(markdown) > >>> uppercase(doc) > >>> markdown =3D 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=C3=A9bastien > > Le samedi 4 d=C3=A9cembre 2021 =C3=A0 15:06:10 UTC+1, denis...-NSENcxR/0n0@public.gmane.org a= =C3=A9crit : > Hi Sebastian > Looks interesting. But I don't undertake how you'd use that as an=20 > alternative to a filter. Can you give an example? > Denis > > > > ________________________________________ > Von: pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org im Auftrag= =20 > von S=C3=A9bastien Boisg=C3=A9rault > 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=20 > 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=20 > transformation of documents with Python (roughly speaking, an alternative= =20 > to pandoc filters); it is not to convert from one format to another (it c= an=20 > do it, but so can the standard pandoc command-line tool). > > Feedback welcome ! > > Cheers, > > S=C3=A9bastien > > > > -- > You received this message because you are subscribed to the Google Groups= =20 > "pandoc-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an= =20 > email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > To view this discussion on the web visit=20 > 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=3Demail&utm_source=3Dfooter > >. > > -- > You received this message because you are subscribed to the Google Groups= =20 > "pandoc-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an= =20 > email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org pandoc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>. > To view this discussion on the web visit=20 > 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=3Demail&utm_source=3Dfooter > >. > --=20 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 e= mail 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. ------=_Part_2823_1090370034.1638632138871 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
You can also use read and write files instead of Python strings. For e= xample, if you have a `notebookify` function that takes a doc (pandoc AST) = and returns a Jupyter notebook (as a Python JSON-like dictionnary), your wr= apper script could be something like:

# file: note= bookify.py

# here: imports, notebookify definition= , etc.
# ...

def main():
 &n= bsp;  filename =3D sys.argv[1]
    doc =3D pandoc.re= ad(file=3Dfilename)

    notebook = =3D notebookify(doc)

    ipynb = =3D Path(filename).with_suffix(".ipynb")
    with open(ip= ynb, "w", encoding=3D"utf-8") as output:
     &= nbsp;  json.dump(notebook, output, ensure_ascii=3DFalse, indent=3D2)

if __name__ =3D=3D "__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=C3=A9cembre 2021 =C3=A0 15:58:31 UTC+1, denis...@unib= e.ch a =C3=A9crit=C2=A0:
Thanks S=C3=A9bastian.

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, usual= ly your doc will be in your filesystem, not in a python variable.)

Best
Denis

________________________________________
Von: pandoc-...@googlegroups= .com <pandoc-...@googlegr= oups.com> im Auftrag von S=C3=A9bastien Boisg=C3=A9rault <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-p= lace) transform would be:

import pandoc
from pandoc.types import *

def uppercase(doc):
for elt in pandoc.iter(doc):
if isinstance(elt, Str):
elt[0] =3D 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 =3D "Hello world!"
>>> doc =3D pandoc.read(markdown)
>>> uppercase(doc)
>>> markdown =3D 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=C3=A9bastien

Le samedi 4 d=C3=A9cembre 2021 =C3=A0 15:06:10 UTC+1, denis...-NSENcxR/0n0@public.gmane.org a =C3=A9crit :
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-...@googlegroups= .com <pandoc-...@googlegr= oups.com> im Auftrag von S=C3=A9bastien Boisg=C3=A9rault <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 e= xposes the Pandoc document model to Python :

https://github.com/boisgera/pandoc#-overview

The main goal is to enable all kinds of analysis, generation and transf= ormation of documents with Python (roughly speaking, an alternative to pand= oc 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=C3=A9bastien



--
You received this message because you are subscribed to the Google Grou= ps "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send = an email to pandoc-discus...@goo= glegroups.com<mailto:pand= oc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/f224cd2c-7d= 68-40b4-a855-7d4d0d7aa442n%40googlegroups.com<https://groups.google.com/d/msgid/pandoc-di= scuss/f224cd2c-7d68-40b4-a855-7d4d0d7aa442n%40googlegroups.com?utm_medium= =3Demail&utm_source=3Dfooter>.

--
You received this message because you are subscribed to the Google Grou= ps "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send = an email to pandoc-discus...@goo= glegroups.com<mailto:pand= oc-discus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/de1fd005-0d= 0d-49a2-86cc-5a72c764835dn%40googlegroups.com<https://groups.google.com/d/msgid/pandoc-di= scuss/de1fd005-0d0d-49a2-86cc-5a72c764835dn%40googlegroups.com?utm_medium= =3Demail&utm_source=3Dfooter>.

--
You received this message because you are subscribed to the Google Groups &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pand= oc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@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.
------=_Part_2823_1090370034.1638632138871-- ------=_Part_2822_1091149874.1638632138871--