Create custom XML output from pandoc

Viewed 53

I am using pandoc to create an revealjs presentation. I also want to take the same source (markdown via Quarto) and create a custom XML metadata file to accompany the presentation. The file would take some of the metadata, heading, etc. and put them into a common format that could be used for managing the presentation later.

Reading through the docs, it looks like I need to create a custom writer and maybe also a template(?).

It looks like for custom writers, there are two styles:

  • The old one you just specify how you want to handle each type of element, more or less transforming or wrapping that (e.g., if header level 1 for html you wrap with <h1></h1> tags and you could potentially add other attributes as you wish. It appears for the old style the function Doc() is what is used to put it all together?

  • New Style where you just need to have a function Writer (doc, opts) that returns a string. The writer receives the doc and opts which are the AST you can parse and then build however you want (??) e.g., the simplest would be like:

    function Writer (doc, opts)
      return "Hello world"
    end
    

It appears that the custom writer builds the $body$ that would then be inserted into a template, for example here is the opml template.

It's note quite clear to me how and why to split up between writer and template. It would seem I could make an XML template with $variable$s and then just build it all there for my use case. Or potentially I could just write a new style custom writer that puts it all together? Is the idea o the template that most people just want to do customization around metadata stuff. I would prefer to just put everything in one place unless there is some later downside.

Overall, is this the correct approach? I want to do something similar to OPML. I see in the pandoc source that it's a small haskell file should I be doing something via haskell instead versus creating a writer or template using lua?

Should I be creating a template, but then importing it somehow into the customer writer I am making with Lua.

I keep darting around the docs and trying stuff, but it doesn't seem right or is coming out very spaghetti-like. The docs are not very deep, there are minimal examples/references (esp end to end) on this area vs normal end user topics.

The export really wouldn't be just the normal flow represented differently, which seems to be the typical pandoc use case (e.g., markdown to html) but I want to plug in various metadata and elements into an XML template.

1 Answers

If you just want to produce simple XML with fields filled in from your metadata, then a custom template is probably the way to go.

For example, create mymetadata.xml:

<mymetadata>
  <title>$title$</title>
$for(author)$
  <author>$author$</author>
$endfor$
</mymetadata>

Run using --template mymetadata.xml --standalone -t html yourslideshow.md. (This will use HTML for any formatting in the title or author metadata; if you prefer, you could use -t docbook instead of -t html, but you'll want to use some XMLish format, so that characters like & get escaped properly.)

If your needs are more complex, you may need a custom writer, but see how far you can get with this simple approach.

Related