From 95c3c983e87d5ee1fb02848f1315cb65a7c6fa0c Mon Sep 17 00:00:00 2001 From: Vivien Kraus Date: Thu, 6 Oct 2022 21:02:35 +0200 Subject: Use a formatter instead of trying to strip space. --- mped.xml | 160 +++++++++++---------------------------------------- tangle-bootstrap.xsl | 76 +++--------------------- 2 files changed, 42 insertions(+), 194 deletions(-) diff --git a/mped.xml b/mped.xml index ecfe859..04dd33d 100644 --- a/mped.xml +++ b/mped.xml @@ -140,7 +140,8 @@ ") - cat >> + + > << "_MPED_EOF" @@ -152,139 +153,45 @@ This template starts by creating the directory where the file - should go, then fills the file with the source code. For this to - work, we need to do two things about the text of the program - listing: remove the first empty lines and the last empty - lines of the content (but preserve indentation). + should go, then fills the file with the source code. XML has a + precise behavior when it comes to whitespace preservation, but + it’s not always the prettiest when we write it with whitespace + output in mind. So, the code output is frequently not indented + 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 + 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. - - Let us start with removing leading or trailing empty - lines. Removing leading empty lines seems easier. - - - - - - - - - - - - - - - - - - - - - - - - - - - ]]> - - - There are three different cases. If the text starts with a - newline, discard the indentation that we carried and the - newline. If the text starts with whitespace, carry it and look - at the next character. Otherwise, the whitespace that we carried - is indentation, so print it, and print the text. - - - To avoid exposing the carried indentation, it is better to mark - this template as internal and wrap it in a new template. - - + - - - - - + + xmllint --format - ]]> - To remove trailing empty lines, the solution is easier since - there is no indentation to keep around: just discard all the - trailing whitespace. + If no formatter applies, then we can resort to + cat. - + - - - - - - - - - - - - - - - - - - - - + + cat ]]> - Using these templates, we can process the program listing code - in the “copy-source-code” mode: if there is only one text node, - then remove the leading emtpy lines and trailing - whitespace. Otherwise, remove the leading emtpy lines from the - first text node and the trailing whitespace from the last text - node. By “first” (respectively, “last”) text node, I mean the - text node that has no preceding (respectively, following) - siblings. Maybe there are no such text nodes. + We also need to specify how the source code is copied. - - - - - - - - - - - - - - - - - - - + + ]]> @@ -340,10 +247,12 @@ The collection of all these templates gives the following: - - + + ]]> - - - + + diff --git a/tangle-bootstrap.xsl b/tangle-bootstrap.xsl index ffe19f5..baa9669 100644 --- a/tangle-bootstrap.xsl +++ b/tangle-bootstrap.xsl @@ -7,7 +7,8 @@ ") - cat >> + + > << "_MPED_EOF" @@ -16,75 +17,14 @@ _MPED_EOF - - - - - - - - - - - - - - - - - - - - - + + xmllint --format - - - - - - - + + cat - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + -- cgit v1.2.3