Markdown and a layout-quine
Categories: Development, speedata Publisher
The speedata Publisher has now (version 4.17.11) basic support for markdown, an easy to use markup language.
As an example of markdown formatting, this snippet creates a level 1 heading and a simple bullet list:
# A title
* one
* anotherone
* three
Using markdown with the speedata Publisher is very easy. There is a new layout function called sd:markdown()
<Layout xmlns="urn:speedata.de:2009/publisher/en"
xmlns:sd="urn:speedata:2009/publisher/functions/en">
<Record element="data">
<PlaceObject>
<Textblock>
<Paragraph>
<Value select="sd:markdown(.)" />
</Paragraph>
</Textblock>
</PlaceObject>
</Record>
</Layout>
The result is, as expected, a heading and a bullet list.
Markdown comes with several flavors and extensions, for example a table extension that lets you write
| foo | bar |
| --- | --- |
| baz | bim |
to create a table. To activate these extensions, use the <Options>
command:
<Options markdown-extensions="highlight,table" />
The complete list of options is available in the documentation.
A markdown-layout quine
What is a quine? The wikipedia article defines: “A quine is a computer program that takes no input and produces a copy of its own source code as its only output.”
The idea is to create a markdown file with the contents of the layout.xml
file, surround it with the markdown instructions for code formatting and render this to the PDF.
Run the following code with sp --dummy
to get the PDF:
<Layout xmlns="urn:speedata.de:2009/publisher/en"
xmlns:sd="urn:speedata:2009/publisher/functions/en">
<Options markdown-extensions="highlight,hlstyle_tango" />
<Record element="data">
<SetVariable
variable="raw"
select="unparsed-text('layout.xml')" />
<SetVariable
variable="fenced"
select="concat('```xml
', $raw ,'
```'))"/>
<PlaceObject>
<Textblock>
<Paragraph>
<Value select="sd:markdown($fenced)" />
</Paragraph>
</Textblock>
</PlaceObject>
</Record>
</Layout>
The markdown code that is generated looks like this:
```xml
<Layout xmlns="urn:speedata.de:2009/publisher/en"
...
</Layout>
```
the contents between the backticks is taken from the non-interpreted layout.xml
file, which is read with the xpath function unparsed-text()
.
This is the result:
Admittedly, this quine is not perfect, since it reads the source code from storage. It would be nicer to have a “to_xml()” function for the layout XML that marshals the layout instructions back to XML.