From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/13315 Path: news.gmane.org!not-for-mail From: Thomas Hodgson Newsgroups: gmane.text.pandoc Subject: Filter for linguex.sty examples with LaTeX Date: Fri, 17 Jul 2015 10:24:13 -0700 (PDT) Message-ID: <014a67f7-d1e9-4544-8cdb-bdbe56cb0f00@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_201_1620325340.1437153853080" X-Trace: ger.gmane.org 1437153858 21680 80.91.229.3 (17 Jul 2015 17:24:18 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 17 Jul 2015 17:24:18 +0000 (UTC) To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-X-From: pandoc-discuss+bncBCEYTH5K2IARBPPUUSWQKGQEKJE4S2Y-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Fri Jul 17 19:24:16 2015 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane.org Original-Received: from mail-qg0-f61.google.com ([209.85.192.61]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZG9Ms-0002g1-P5 for gtp-pandoc-discuss@m.gmane.org; Fri, 17 Jul 2015 19:24:14 +0200 Original-Received: by qgdf45 with SMTP id f45sf16217887qgd.1 for ; Fri, 17 Jul 2015 10:24:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=date:from:to:message-id:subject:mime-version:content-type :x-original-sender:reply-to:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe; bh=yqsdIkgObxLa2ghaW1MYrFJ5slDIfPlWFgjLDGoL6Sk=; b=q2jL4kaXmct+xRZbDi2Ao22JgTe9KCZiE9+UuOJKF8UZHBFvE7F3Lp+GANyueWB6T/ svYkp8mlwLaAW0UOmVuSd0KWGs6FlojGobz/sHImvhjxrz5EDA6w8s8Z0UpejSLpjk/5 M4+062aXACee7tXCAZVwJp/EjeZo9nYc0zZ52/YAQsSRjNxcg+6lErwQK3HK7wsKxA2P xVqWxq33El8tcf6CZUvpT3190wkRFWOA8htKI1dTp8C0NTjNYrXy53d1Wzl3BvwUjBOA 4rdItbqJH+Zkgismji6DSkRA1pHieTjC15mNp20gD5JPsBcchKBllASWbqoTzneIgQ0m qMuQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:message-id:subject:mime-version:content-type :x-original-sender:reply-to:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe; bh=yqsdIkgObxLa2ghaW1MYrFJ5slDIfPlWFgjLDGoL6Sk=; b=lnmYilfS4eK96fa9SOSoW4rOhJo3QpTTaSngM2sLAuAkSkmpCiCg75/c9RthszvmQg INyLAJAtYH6/mGA5AzR1zKEyR4Ckwi3snAvrfoM3xtYQ9AGzsPpYcdb/ThUj4q0uNlcq uQsMh9R3lrsj65KCiAWGkB5XNWXPlUYvdcX9DxkuUC98Gi07yxgnhD6Qfb6dLZsnZ927 gUibbAvThu6/T/CsdoqdpJJ2pScYQSIknsqn6l1r/sJ2Vyb1GGeZ1T7Hd9H1KDeMH4uo RDPnR2xRe3ZIHqD//KE71FAtIT4q6TtZUjnzUicJ/gu00pX7WdBwxJtxbKzeHFCLKex5 wugA== X-Received: by 10.182.230.135 with SMTP id sy7mr194614obc.35.1437153854060; Fri, 17 Jul 2015 10:24:14 -0700 (PDT) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 10.182.133.3 with SMTP id oy3ls869060obb.11.gmail; Fri, 17 Jul 2015 10:24:13 -0700 (PDT) X-Received: by 10.182.95.9 with SMTP id dg9mr38816obb.6.1437153853536; Fri, 17 Jul 2015 10:24:13 -0700 (PDT) X-Original-Sender: thomas.hodgson-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-Spam-Checked-In-Group: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Google-Group-Id: 1007024079513 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Xref: news.gmane.org gmane.text.pandoc:13315 Archived-At: ------=_Part_201_1620325340.1437153853080 Content-Type: multipart/alternative; boundary="----=_Part_202_1410161786.1437153853080" ------=_Part_202_1410161786.1437153853080 Content-Type: text/plain; charset=UTF-8 I've been trying to write a filter to convert numbered examples into linguex.sty examples. What I've come up with is below, but there are two things that I can't figure out. 1. I need to ignore ordinary `OrderedList`; I thought it would be easy because they are identified as Examples in the AST. But the things that I tried didn't work. My current hack treats length 1 lists as examples but that's obviously unsatisfactory. 2. Only references that come after the example are converted into LaTeX cross references; references before are left as strings. If there was a way to walk the tree twice as part of one filter then this could be avoided. Is there a way to do that? ``` #!/usr/bin/env python3 import re from pandocfilters import toJSONFilter, stringify, RawBlock, RawInline, Str numbers_to_labels = {} def make_example(obj): content = stringify(obj) label = re.sub('\W', '', content.lower()) numbers_to_labels[len(numbers_to_labels) + 1] = label return RawBlock('latex', '\\ex.\label{{{}}} '.format(label) + content) def examples(key, value, format, meta): # This is a silly hack because there could be length 1 enumerations if key == 'OrderedList' and format == 'latex' and len(value[1]) == 1: return make_example(value[1][0]) if key == 'Str' and re.match('\(\d\)', value): try: n = int(value.lstrip('(').rstrip(')')) return RawInline('latex', '(\\ref{{{}}})'.format(numbers_to_labels[n])) except KeyError: return Str(value) if __name__ == "__main__": toJSONFilter(examples) ``` I should also make it impossible for two examples to have the same label, at the moment 'Hello world' and 'Hello world?' would. -- 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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/014a67f7-d1e9-4544-8cdb-bdbe56cb0f00%40googlegroups.com. For more options, visit https://groups.google.com/d/optout. ------=_Part_202_1410161786.1437153853080 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I've been trying to write a filter to convert numbered= examples into linguex.sty examples. What I've come up with is below, b= ut there are two things that I can't figure out.

1. = I need to ignore ordinary `OrderedList`; I thought it would be easy because= they are identified as Examples in the AST. But the things that I tried di= dn't work. My current hack treats length 1 lists as examples but that&#= 39;s obviously unsatisfactory.

2. Only references = that come after the example are converted into LaTeX cross references; refe= rences before are left as strings. If there was a way to walk the tree twic= e as part of one filter then this could be avoided. Is there a way to do th= at?

```
#!/usr/bin/env python3
import re
from pandocfilters import toJSONFilter, stringify= , RawBlock, RawInline, Str

numbers_to_labels =3D {= }


def make_example(obj):
= =C2=A0 =C2=A0 content =3D stringify(obj)
=C2=A0 =C2=A0 label =3D = re.sub('\W', '', content.lower())
=C2=A0 =C2=A0 n= umbers_to_labels[len(numbers_to_labels) + 1] =3D label
=C2=A0 =C2= =A0 return RawBlock('latex', '\\ex.\label{{{}}} '.format(la= bel) + content)


def examples(key, v= alue, format, meta):
=C2=A0 =C2=A0 # This is a silly hack because= there could be length 1 enumerations
=C2=A0 =C2=A0 if key =3D=3D= 'OrderedList' and format =3D=3D 'latex' and len(value[1]) = =3D=3D 1:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return make_example(value[1= ][0])
=C2=A0 =C2=A0 if key =3D=3D 'Str' and re.match('= ;\(\d\)', value):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 try:
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 n =3D int(value.lstrip('('= ;).rstrip(')'))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= return RawInline('latex', '(\\ref{{{}}})'.format(numbers_t= o_labels[n]))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 except KeyError:
<= div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return Str(value)
<= br>

if __name__ =3D=3D "__main__":
=
=C2=A0 =C2=A0 toJSONFilter(examples)
```
I should also make it impossible for two examples to have the s= ame label, at the moment 'Hello world' and 'Hello world?' w= ould.

--
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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d/= msgid/pandoc-discuss/014a67f7-d1e9-4544-8cdb-bdbe56cb0f00%40googlegroups.co= m.
For more options, visit http= s://groups.google.com/d/optout.
------=_Part_202_1410161786.1437153853080-- ------=_Part_201_1620325340.1437153853080--