From 3a15eb52334be6ddee374e044e70d3a0a8e6426a Mon Sep 17 00:00:00 2001 From: Vivien Kraus Date: Thu, 6 Oct 2022 21:52:56 +0200 Subject: Put IDs to figures containing the listing, not the listing itself. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If both the figure and the program listing had an ID, this would make cross-referencing a nightmare. Since it seems like it’s usually better to put an ID on figures, I will consider the listings directly within a figure with a given ID to have this ID. --- mped.xml | 128 ++++++++++++++++++++++++++++++++++----------------- tangle-bootstrap.xsl | 8 ++-- 2 files changed, 89 insertions(+), 47 deletions(-) diff --git a/mped.xml b/mped.xml index 04dd33d..8739fcf 100644 --- a/mped.xml +++ b/mped.xml @@ -128,13 +128,16 @@ attribute. - To tangle a document, an XSLT stylesheet is defined. It reads a - Docbook document, and outputs a shell script that writes the + To tangle a document, an XSLT stylesheet is defined. It reads + a Docbook document, and outputs a shell script that writes the correct pieces of code to the correct file names. The key - template to do the task is: + template to do the task is . - - + Copy a specific programlisting to disk + + mkdir -p $(dirname " @@ -149,8 +152,9 @@ _MPED_EOF - ]]> - + ]]> + + This template starts by creating the directory where the file should go, then fills the file with the source code. XML has a @@ -160,50 +164,73 @@ correctly, and has too many empty lines. To counter this effect, we use a code source formatter, a program that reads source code and indents it correctly. For - XSLT, we can use xmllint from + XSLT, in , we can use + xmllint from libxml2. The important thing about the formatter is that it should take its input from standard input and write the formatted code to standard output. - - + Use xmllint as a formatter for XML languages + + xmllint --format - - ]]> - + ]]> + + + + Unfortunately, xmllint does not accept an XML processing + instruction after the first line, so you will still need to + put no whitespace between the start tag of programlisting and + the text for XML listings. + If no formatter applies, then we can resort to - cat. + cat, see . - - + Use cat as the default formatter for any language + + cat - ]]> - + ]]> + + - We also need to specify how the source code is copied. + We also need to specify how the source code is copied. It is + very simple: copies + the text verbatim. - - + Copy the source code as text + + - ]]> - + ]]> + + Tangling should never touch anything else. So, text should not - be copied to output. + be copied to output. This is why we disable text matching by + default with . - - + Ignore text by default when tangling + + - ]]> - + ]]> + +
Paste other listings in place @@ -214,44 +241,58 @@ “mped:copy”. It has a “linkend” attribute that resolves to a program listing anywhere in the document. When copying source code, matching this element will insert the linked listing - directly here. + directly here. This is done in . - - + More precisely, it looks if there is a single program listing + that is directly under a figure with the given ID. This way, + we can refer to listings as the figure they appear in, which + makes cross-referencing easier. + +
+ + Insert literally a listing in another when tangling + + + + select="count(//docbook:figure[@xml:id = $ref]/docbook:programlisting)" /> - There are no listing with ID ' + There are no listing directly within a figure with ID ' '. - There are multiple listings with ID ' + There are multiple listings directly within a figure with ID ' '. - - ]]> - + ]]> + +
Putting it all together - The collection of all these templates gives the following: + The collection of all these templates gives a full stylesheet, + in . - +
+ The full stylesheet for tangling + ]]> - + +
diff --git a/tangle-bootstrap.xsl b/tangle-bootstrap.xsl index baa9669..a646bca 100644 --- a/tangle-bootstrap.xsl +++ b/tangle-bootstrap.xsl @@ -29,10 +29,10 @@ _MPED_EOF - + - There are no listing with ID ' + There are no listing directly within a figure with ID ' '. @@ -40,12 +40,12 @@ _MPED_EOF - There are multiple listings with ID ' + There are multiple listings directly within a figure with ID ' '. - + -- cgit v1.2.3