public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Filter for linguex.sty examples with LaTeX
@ 2015-07-17 17:24 Thomas Hodgson
       [not found] ` <014a67f7-d1e9-4544-8cdb-bdbe56cb0f00-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Hodgson @ 2015-07-17 17:24 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


[-- Attachment #1.1: Type: text/plain, Size: 2300 bytes --]

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.

[-- Attachment #1.2: Type: text/html, Size: 3315 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter for linguex.sty examples with LaTeX
       [not found] ` <014a67f7-d1e9-4544-8cdb-bdbe56cb0f00-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2015-07-17 19:51   ` John MacFarlane
       [not found]     ` <20150717195133.GE9326-nFAEphtLEs/fysO+viCLMa55KtNWUUjk@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: John MacFarlane @ 2015-07-17 19:51 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

+++ Thomas Hodgson [Jul 17 15 10:24 ]:
>   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.

Doesn't testing for value[0][1] == 'Example' work?

>   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?

Have you looked at the walk function?


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter for linguex.sty examples with LaTeX
       [not found]     ` <20150717195133.GE9326-nFAEphtLEs/fysO+viCLMa55KtNWUUjk@public.gmane.org>
@ 2015-07-17 20:55       ` Thomas Hodgson
       [not found]         ` <77de4784-2537-49b0-ae97-dc7427aac121-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Hodgson @ 2015-07-17 20:55 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


[-- Attachment #1.1: Type: text/plain, Size: 1915 bytes --]

I tried `value[0][1] == 'Example'`, but it doesn't work. None of them are 
matched.

I've just looked at walk. Is the idea that I could have one function as an 
argument to toJSONfilter that first walks the whole tree with an examples 
action, and then walks whole modified tree again with a references action? 
That would be great and I'll try to work out how to do it.

On Friday, 17 July 2015 14:51:49 UTC-5, John MacFarlane wrote:
>
> +++ Thomas Hodgson [Jul 17 15 10:24 ]: 
> >   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. 
>
> Doesn't testing for value[0][1] == 'Example' work? 
>
> >   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? 
>
> Have you looked at the walk function? 
>
>

-- 
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/77de4784-2537-49b0-ae97-dc7427aac121%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 2617 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter for linguex.sty examples with LaTeX
       [not found]         ` <77de4784-2537-49b0-ae97-dc7427aac121-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2015-07-26 18:26           ` Thomas Hodgson
       [not found]             ` <bcf21c64-f380-4a90-91bc-8c581d4546cb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas Hodgson @ 2015-07-26 18:26 UTC (permalink / raw)
  To: pandoc-discuss; +Cc: thomas.hodgson-Re5JQEeQqe8AvxtiuMwx3w


[-- Attachment #1.1: Type: text/plain, Size: 2494 bytes --]

I feel like I should be able to work this out, but I haven't been able to. 
Is there are an example available somewhere of using walk in the way 
required i.e. to walk not just a subtree but the whole tree, and returning 
an object ready to be walked again? The only one I could find (deemph.py) 
only walks a tree that matches a condition.

I haven't made any progress with testing for examples either; what you say 
looks like it should work but it doesn't for me.

On Friday, 17 July 2015 15:55:57 UTC-5, Thomas Hodgson wrote:
>
> I tried `value[0][1] == 'Example'`, but it doesn't work. None of them are 
> matched.
>
> I've just looked at walk. Is the idea that I could have one function as an 
> argument to toJSONfilter that first walks the whole tree with an examples 
> action, and then walks whole modified tree again with a references action? 
> That would be great and I'll try to work out how to do it.
>
> On Friday, 17 July 2015 14:51:49 UTC-5, John MacFarlane wrote:
>>
>> +++ Thomas Hodgson [Jul 17 15 10:24 ]: 
>> >   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. 
>>
>> Doesn't testing for value[0][1] == 'Example' work? 
>>
>> >   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? 
>>
>> Have you looked at the walk function? 
>>
>>

-- 
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/bcf21c64-f380-4a90-91bc-8c581d4546cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 3329 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter for linguex.sty examples with LaTeX
       [not found]             ` <bcf21c64-f380-4a90-91bc-8c581d4546cb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2015-09-23 10:42               ` msprevak
       [not found]                 ` <8f89b303-3abc-4b6f-9daa-5630f90362dd-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: msprevak @ 2015-09-23 10:42 UTC (permalink / raw)
  To: pandoc-discuss; +Cc: thomas.hodgson-Re5JQEeQqe8AvxtiuMwx3w


[-- Attachment #1.1: Type: text/plain, Size: 927 bytes --]

`value[0][1]['t'] == 'Example'` should work.

Note that you can use `print(xxxx, file=sys.stderr)` inside a Python3 
filter to print out what xxxx is. This is very handy when debugging filters!

On Friday, 17 July 2015 15:55:57 UTC-5, Thomas Hodgson wrote:
>>
>> I tried `value[0][1] == 'Example'`, but it doesn't work. None of them are 
>> matched.
>>
>

-- 
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/8f89b303-3abc-4b6f-9daa-5630f90362dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 1676 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter for linguex.sty examples with LaTeX
       [not found]                 ` <8f89b303-3abc-4b6f-9daa-5630f90362dd-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2015-09-23 10:47                   ` msprevak
  0 siblings, 0 replies; 6+ messages in thread
From: msprevak @ 2015-09-23 10:47 UTC (permalink / raw)
  To: pandoc-discuss; +Cc: thomas.hodgson-Re5JQEeQqe8AvxtiuMwx3w


[-- Attachment #1.1: Type: text/plain, Size: 1275 bytes --]

On your original issue (2), the entire ast is passed as a json string to a 
filter via stdin, and returned to pandoc via stdout. You can do what you 
like with this in the meantime -- walk it multiple times, search/replace, 
etc. The world is your oyster. 

`toJSONFilter` is, at least in my understanding, a convenience function 
that wraps a single decode/walk+do something/encode cycle. This covers many 
use cases for filters, but not all.

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?


-- 
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/85e641ed-919b-4f39-b744-3bb48d85f3a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 1924 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-09-23 10:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-17 17:24 Filter for linguex.sty examples with LaTeX Thomas Hodgson
     [not found] ` <014a67f7-d1e9-4544-8cdb-bdbe56cb0f00-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2015-07-17 19:51   ` John MacFarlane
     [not found]     ` <20150717195133.GE9326-nFAEphtLEs/fysO+viCLMa55KtNWUUjk@public.gmane.org>
2015-07-17 20:55       ` Thomas Hodgson
     [not found]         ` <77de4784-2537-49b0-ae97-dc7427aac121-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2015-07-26 18:26           ` Thomas Hodgson
     [not found]             ` <bcf21c64-f380-4a90-91bc-8c581d4546cb-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2015-09-23 10:42               ` msprevak
     [not found]                 ` <8f89b303-3abc-4b6f-9daa-5630f90362dd-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2015-09-23 10:47                   ` msprevak

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).