* Converting filter to custom writer @ 2022-06-24 14:09 BPJ [not found] ` <CADAJKhAyQY_deNGV4SN61jodEM4sLUBSa4zpdxRdXg8+LLMzTA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> 0 siblings, 1 reply; 3+ messages in thread From: BPJ @ 2022-06-24 14:09 UTC (permalink / raw) To: pandoc-discuss [-- Attachment #1: Type: text/plain, Size: 2044 bytes --] I have a not-so-old "pseudo-writer" implemented as a filter which mostly injects tons of raw markup into the document tree so that the output of the plain writer looks like my target format, returning a table of filters at the end like this ``````lua return { { Meta = get_config }, { SmallCaps = SmallCaps, Str = Str }, note_filter, main_filter } `````` where `get_config` just extracts some info from the metadata and populates a table declared at the top of the script which the functions in the other filters refer to. Am I right that I in principle could convert this into a new style custom writer by removing the Meta filter, replacing `return {<filters>}` above with `local filters = {<filters>}` and adding the following at the bottom of the script? ``````lua function Writer (doc, opts) get_config(doc.meta) for _,filter in ipairs(filters) do doc = doc:walk(filter) end doc.blocks = blocks return pandoc.write(doc, 'plain', opts) `````` (This got me thinking: what if `walk` accepted one more argument which will be passed as an extra argument to the functions in the filters, which could be used to pass config and/or maintain state? It somehow feels more intuitive than defining a table outside the metadata handler which all handlers close around.) (BTW the main reason that the `note_filter` is separate is that it needs to be topdown because it counts and collects the notes, while `main_filter` needs to be bottomup because it sometimes relies on the content of elements already having been processed. I was very happy when I realized that this would work!) -- 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/CADAJKhAyQY_deNGV4SN61jodEM4sLUBSa4zpdxRdXg8%2BLLMzTA%40mail.gmail.com. [-- Attachment #2: Type: text/html, Size: 3159 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
[parent not found: <CADAJKhAyQY_deNGV4SN61jodEM4sLUBSa4zpdxRdXg8+LLMzTA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>]
* Re: Converting filter to custom writer [not found] ` <CADAJKhAyQY_deNGV4SN61jodEM4sLUBSa4zpdxRdXg8+LLMzTA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> @ 2022-06-29 16:23 ` BPJ 2022-06-29 17:22 ` Albert Krewinkel 0 siblings, 1 reply; 3+ messages in thread From: BPJ @ 2022-06-29 16:23 UTC (permalink / raw) To: pandoc-discuss [-- Attachment #1: Type: text/plain, Size: 2283 bytes --] I saw no answer to this one. Hopefully it just got lost. Apologies to anyone who already got it. Den fre 24 juni 2022 16:09BPJ <> skrev: > I have a not-so-old "pseudo-writer" implemented as a filter which mostly > injects tons of raw markup into the document tree so that the output of > the plain writer looks like my target format, returning a table of filters > at the end like this > > ``````lua > return { > { > Meta = get_config > }, > { > SmallCaps = SmallCaps, > Str = Str > }, > note_filter, > main_filter > } > `````` > > where `get_config` just extracts some info from the metadata and populates > a table declared at the top of the script which the functions in the other > filters refer to. > > Am I right that I in principle could convert this into a new style custom > writer by removing the Meta filter, replacing `return {<filters>}` above > with `local filters = {<filters>}` and adding the following at the bottom > of the script? > > ``````lua > function Writer (doc, opts) > get_config(doc.meta) > for _,filter in ipairs(filters) do > doc = doc:walk(filter) > end > doc.blocks = blocks > return pandoc.write(doc, 'plain', opts) > `````` > > (This got me thinking: what if `walk` accepted one more argument which > will be passed as an extra argument to the functions in the filters, which > could be used to pass config and/or maintain state? It somehow feels more > intuitive than defining a table outside the metadata handler which all > handlers close around.) > > (BTW the main reason that the `note_filter` is separate is that it needs > to be topdown because it counts and collects the notes, while `main_filter` > needs to be bottomup because it sometimes relies on the content of elements > already having been processed. I was very happy when I realized that this > would work!) > > -- 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/CADAJKhBPpu1%3DFnxP0byMDWGEBF-7XAV7mVc%2BTzDSPX2t2GXN%3Dg%40mail.gmail.com. [-- Attachment #2: Type: text/html, Size: 3573 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Converting filter to custom writer 2022-06-29 16:23 ` BPJ @ 2022-06-29 17:22 ` Albert Krewinkel 0 siblings, 0 replies; 3+ messages in thread From: Albert Krewinkel @ 2022-06-29 17:22 UTC (permalink / raw) To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw BPJ <melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes: > I saw no answer to this one. Thanks for the reminder :) > Den fre 24 juni 2022 16:09BPJ <> skrev: > > I have a not-so-old "pseudo-writer" implemented as a filter which > mostly injects tons of raw markup into the document tree so that the > output of the plain writer looks like my target format, returning a > table of filters at the end like this > > [...] > > Am I right that I in principle could convert this into a new style > custom writer by removing the Meta filter, replacing `return > {<filters>}` above with `local filters = {<filters>}` and adding the > following at the bottom of the script? > > ``````lua > function Writer (doc, opts) > get_config(doc.meta) > for _,filter in ipairs(filters) do > doc = doc:walk(filter) > end > doc.blocks = blocks > return pandoc.write(doc, 'plain', opts) > `````` Yes, I believe that would work. I'm actually starting to use custom writers in a similar way. The only downside is that you can't use the default extension mechanism (like `plain-multiline_tables`) on the command line to tune the writer, but we're working on that. > (This got me thinking: what if `walk` accepted one more argument which > will be passed as an extra argument to the functions in the filters, > which could be used to pass config and/or maintain state? It somehow > feels more intuitive than defining a table outside the metadata handler > which all handlers close around.) Good idea. Although I have to admit that I'm not really enthusiastic about it due to many small, slightly annoying changes that implementing this feature would require in the Haskell code. > (BTW the main reason that the `note_filter` is separate is that it > needs to be topdown because it counts and collects the notes, while > `main_filter` needs to be bottomup because it sometimes relies on the > content of elements already having been processed. I was very happy > when I realized that this would work!) 😊 -- Albert Krewinkel GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124 -- 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/87edz7bc3h.fsf%40zeitkraut.de. ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-06-29 17:22 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-06-24 14:09 Converting filter to custom writer BPJ [not found] ` <CADAJKhAyQY_deNGV4SN61jodEM4sLUBSa4zpdxRdXg8+LLMzTA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org> 2022-06-29 16:23 ` BPJ 2022-06-29 17:22 ` Albert Krewinkel
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).