From: Norman Walsh Date: Fri, 27 Dec 2002 15:51:54 +0000 (+0000) Subject: Deal with leading/trailing newlines even when tangling so that extra whitespace isn... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5ef5eb858ce6505021bb89b7becbc4256bd982ff;p=docbook-dsssl Deal with leading/trailing newlines even when tangling so that extra whitespace isn't output --- diff --git a/litprog/tangle.xweb b/litprog/tangle.xweb index d905a77d5..a0b78667e 100644 --- a/litprog/tangle.xweb +++ b/litprog/tangle.xweb @@ -136,15 +136,207 @@ as the primary starting point.
Processing Fragments In order to tangle an &xweb; document, we need -only copy the contents of the fragments to the result tree. -Processing src:fragment elements is easy, simply copy -their children: +only copy the contents of the fragments to the result tree. + +Processing src:fragment elements is +conceptually easy, simply copy their children. However, if we simply used: + +<xsl:apply-templates mode="copy"/> + +we'd copy the newlines at the beginning and end of a fragment that the +author might have added for editing convenience. In environments where +whitespace is significant (e.g., Python), this would introduce errors. +We must avoid copying the first and last newlines. + - + + + + + +
Convenience Variables + +For convenience, we store subexpressions containing the first, +last, and all the middle nodes in variables. + + + + + + + +
+ +
Handle First Node + +Handling the leading newline is conceptually a simple matter of +looking at the first character on the line and skipping it if it is +a newline. A slight complexity is introduced by the fact that if the +fragment contains only a single text node, the first node is also the +last node and we have to possibly trim off a trialing newline as well. +We separate that out as a special case. + + + + + + + + + + +
Handle A Fragment that Contains a Single Node + +If the $first-node is a text node and the +fragment contains only a single child, then it is also the last node. + +In order to deal with a single text node child, we must address +four cases: the node has both leading and trailing newlines, the node +has only leading newlines, only trailing newlines, or no newlines at +all. + + + + + + + + + + + + + +
More Convenience Variables + +For convenience, we calculate whether or not the node in question +has leading and/or trailing newlines and store those results in variables. + + + + + + +
+ +
Handle a Single Node With Leading and Trailing Newlines + +If the node has both leading and trailing newlines, trim a character +off each end. + + + + + + +
+ +
Handle a Single Node With Only Leading Newlines + +If the node has only leading newlines, trim off the first character. + + + + + + + +
+ +
Handle a Single Node with Only Trailing Newlines + +If the node has only trailing newlines, trim off the last character. + + + + + + + +
+ +
Handle a Single Node with No Newlines + +Otherwise, the node has no newlines and it is simply printed. + + + + + + + +
+
+ +
Handle a First Node with a Leading Newline + +If the first node is a text node and begins with a newline, +trim off the first character. + + + + + + +
+ +
Handle a First Node without a Leading Newline + +Otherwise, the first node is not a text node or does not begin +with a newline, so use the copy mode to copy it to +the result tree. + + + + + + +
+
+ +
Handle Last Node + +Handling the last node is roughly analagous to handling the first +node, except that we know this code is only evaluated if the last node +is not also the first node. + +If the last node is a text node and ends with a newline, strip +it off. Otherwise, just copy the content of the last node +using the copy mode. + + + + + + + + + + + + + +
+ +
Handle the Middle Nodes + +The middle nodes are easy, just copy them +using the copy mode. + + + + + +
Copying Elements