GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
1
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
nigel.stanger
/
XML
Browse code
- Updated some documentation.
- Updated an error message.
master
1 parent
7fd5e91
commit
14cfd708cf485eba43209387578b76c66eb9dd4b
nstanger
authored
on 23 Feb 2012
Patch
Showing
2 changed files
modules/code-formatting.xml
modules/number-formatting.xml
Ignore Space
Show notes
View
modules/code-formatting.xml
<?xml version="1.0" encoding="utf-8"?> <!-- Inline code and code blocks (preformatted text, in HTML terms). TODO (2011-10-07): * Switch to listings for actual code. * Add an "inline-verbatim|verb" template for inline verbatim (which is what the "code" template does now). * Add "block-verbatim" as a match for "verbatim". --> <stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Inline code. --> <template name="code" match="code"> <!-- Inline code. LaTeX uses listings to display this; delimiters for \lstinline are chosen automatically, depending on the contents. --> <common formats="/latex/xelatex/"> <xsl:text>\lstinline</xsl:text> <xsl:if test="@language"> <xsl:text>[language=</xsl:text> <xsl:value-of select="@language" /> <xsl:text>]</xsl:text> </xsl:if> <!-- We could just call choose-delimiter twice, but this avoids any possibility of weird corner cases where the second call returns a different delimiter from the first call. It's also slightly more efficient to only call the function once. --> <xsl:variable name="code-delimiter" select="infosci:choose-delimiter( node() )" /> <xsl:value-of select="$code-delimiter" /> <xsl:apply-templates /> <xsl:value-of select="$code-delimiter" /> </common> <common formats="/html/xhtml/"> <code><xsl:apply-templates /></code> </common> </template> <!-- Choose a valid delimiter for a LaTeX inline verbatim macro (e.g., \verb or \lstinline). "Valid" means that the delimiter can't occur in the source text. The function thus scans through a list of possible delimiters and looking for those that don't occur in the source text. $source-text: The source text to be wrapped inside a \verb macro. Returns: The function actually finds /all/ valid delimiters for the source text because of the effective parallel execution of xsl:for-each on sequences, but it only returns the first one found. If no valid delimiter can be found, the function raises an error and terminates the transformation. --> <function name="infosci:choose-delimiter" as="xs:string"> <common formats="/latex/xelatex/"> <xsl:param name="source-text" /> <xsl:variable name="valid-delimiters"> <!-- The LaTeX documentation says that you can use any character as a delimiter except * (because there is a \verb* form). To keep things simple, we've restricted the possible delimiters to punctuation that can be easily expressed in XML. --> <xsl:for-each select=" '`', '~', '!', '@', '#', '$', '%', '^', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\', '|', ';', ':', ',', '.', '/', '?'"> <xsl:if test="not( contains( $source-text, . ) )"> <xsl:value-of select = "." /> </xsl:if> </xsl:for-each> </xsl:variable> <!-- $valid-delimiters will be empty if none were found. --> <xsl:if test="string-length( $valid-delimiters ) = 0"> <xsl:message terminate="yes"> <xsl:text>ERROR: unable to determine a valid delimiter to use in \verb or \lstinline for the source text: </xsl:text> <xsl:value-of select="$source-text" /> </xsl:message> </xsl:if> <xsl:sequence select="substring( $valid-delimiters, 1, 1 )" /> </common> </function> <!-- Displayed code block. The LaTeX template uses the listings package. @allow-breaks: Whether or not to allow page breaks in the code block. 'yes' [default] 'no @language: Any language supported by the listings package in LaTeX. Has no effect in (X)HTML. --> <template name="code-block" match="code-block"> <common formats="/latex/xelatex/"> <xsl:call-template name="newline-internal" /> <!-- If the code-block specifies "allow-breaks='no'", wrap the code block inside a minipage to avoid page breaks within the code. --> <xsl:if test="@allow-breaks = 'no'"> <xsl:call-template name="newline-internal" /> <xsl:text>\begin{minipage}{\textwidth}</xsl:text> </xsl:if> <xsl:call-template name="newline-internal" /> <xsl:text>\begin{lstlisting}</xsl:text> <xsl:if test="@language"> <xsl:text>[language=</xsl:text> <xsl:value-of select="@language" /> <xsl:text>]</xsl:text> </xsl:if> <xsl:apply-templates /> <xsl:text>\end{lstlisting}</xsl:text> <xsl:call-template name="newline-internal" /> <xsl:if test="@allow-breaks = 'no'"> <xsl:text>\end{minipage}</xsl:text> <xsl:call-template name="newline-internal" /> </xsl:if> <xsl:call-template name="newline-internal" /> </common> <common formats="/html/xhtml/"> <pre class="code"><xsl:apply-templates /></pre> </common> </template> <!-- Inline verbatim text (as opposed to inline code). In LaTeX this equates to \verb; delimiters for this are chosen automatically, depending on the contents. --> <template name="inline-verbatim" match="inline-verbatim|verb"> <common formats="/latex/xelatex/"> <!-- We could just call choose-delimiter twice, but this avoids any possibility of weird corner cases where the second call returns a different delimiter from the first call. It's also slightly more efficient to only call the function once. --> <xsl:variable name="verb-delimiter" select="infosci:choose-delimiter( node() )" /> <xsl:value-of select="$verb-delimiter" /> <xsl:apply-templates /> <xsl:value-of select="$verb-delimiter" /> </common> <common formats="/html/xhtml/"> <code><xsl:apply-templates /></code> </common> </template> <!-- This is for stuff that should unequivocally be treated verbatim (e.g., problem code). --> <template name="verbatim" match="verbatim|block-verbatim"> <common formats="/latex/xelatex/"> <xsl:call-template name="newline-internal" /> <xsl:text>\begin{verbatim}</xsl:text> <xsl:call-template name="newline-internal" /> <xsl:apply-templates /> <xsl:call-template name="newline-internal" /> <xsl:text>\end{verbatim}</xsl:text> <xsl:call-template name="newline-internal" /> </common> <common formats="/html/xhtml/"> <pre><xsl:apply-templates /></pre> </common> </template> </stylesheet>
<?xml version="1.0" encoding="utf-8"?> <!-- Inline code and code blocks (preformatted text, in HTML terms). TODO (2011-10-07): * Switch to listings for actual code. * Add an "inline-verbatim|verb" template for inline verbatim (which is what the "code" template does now). * Add "block-verbatim" as a match for "verbatim". --> <stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Inline code. --> <template name="code" match="code"> <!-- Inline code. LaTeX uses listings to display this; delimiters for \lstinline are chosen automatically, depending on the contents. --> <common formats="/latex/xelatex/"> <xsl:text>\lstinline</xsl:text> <xsl:if test="@language"> <xsl:text>[language=</xsl:text> <xsl:value-of select="@language" /> <xsl:text>]</xsl:text> </xsl:if> <!-- We could just call choose-delimiter twice, but this avoids any possibility of weird corner cases where the second call returns a different delimiter from the first call. It's also slightly more efficient to only call the function once. --> <xsl:variable name="code-delimiter" select="infosci:choose-delimiter( node() )" /> <xsl:value-of select="$code-delimiter" /> <xsl:apply-templates /> <xsl:value-of select="$code-delimiter" /> </common> <common formats="/html/xhtml/"> <code><xsl:apply-templates /></code> </common> </template> <!-- Choose a valid delimiter for a LaTeX inline verbatim macro (e.g., \verb or \lstinline). "Valid" means that the delimiter can't occur in the source text. The function thus scans through a list of possible delimiters and looking for those that don't occur in the source text. It actually finds /all/ valid delimiters for the source text because of the effective parallel execution of for-each on sequences, but it only returns the first one found. If no valid delimiter can be found, the function raises an error and terminates the transformation. $source-text: The source text to be wrapped inside a \verb macro. --> <function name="infosci:choose-delimiter" as="xs:string"> <common formats="/latex/xelatex/"> <xsl:param name="source-text" /> <xsl:variable name="valid-delimiters"> <!-- The LaTeX documentation says that you can use any character as a delimiter except * (because there is a \verb* form). To keep things simple, we've restricted the possible delimiters to punctuation that can be easily expressed in XML. --> <xsl:for-each select=" '`', '~', '!', '@', '#', '$', '%', '^', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\', '|', ';', ':', ',', '.', '/', '?'"> <xsl:if test="not( contains( $source-text, . ) )"> <xsl:value-of select = "." /> </xsl:if> </xsl:for-each> </xsl:variable> <!-- $valid-delimiters will be empty if none were found. --> <xsl:if test="string-length( $valid-delimiters ) = 0"> <xsl:message terminate="yes"> <xsl:text>ERROR: unable to determine a valid delimiter to use in \verb or \lstinline for the source text: </xsl:text> <xsl:value-of select="$source-text" /> </xsl:message> </xsl:if> <xsl:sequence select="substring( $valid-delimiters, 1, 1 )" /> </common> </function> <!-- Displayed code block. The LaTeX template uses the listings package. @allow-breaks: Whether or not to allow page breaks in the code block. 'yes' [default] 'no @language: Any language supported by the listings package in LaTeX. Has no effect in (X)HTML. --> <template name="code-block" match="code-block"> <common formats="/latex/xelatex/"> <xsl:call-template name="newline-internal" /> <!-- If the code-block specifies "allow-breaks='no'", wrap the code block inside a minipage to avoid page breaks within the code. --> <xsl:if test="@allow-breaks = 'no'"> <xsl:call-template name="newline-internal" /> <xsl:text>\begin{minipage}{\textwidth}</xsl:text> </xsl:if> <xsl:call-template name="newline-internal" /> <xsl:text>\begin{lstlisting}</xsl:text> <xsl:if test="@language"> <xsl:text>[language=</xsl:text> <xsl:value-of select="@language" /> <xsl:text>]</xsl:text> </xsl:if> <xsl:apply-templates /> <xsl:text>\end{lstlisting}</xsl:text> <xsl:call-template name="newline-internal" /> <xsl:if test="@allow-breaks = 'no'"> <xsl:text>\end{minipage}</xsl:text> <xsl:call-template name="newline-internal" /> </xsl:if> <xsl:call-template name="newline-internal" /> </common> <common formats="/html/xhtml/"> <pre class="code"><xsl:apply-templates /></pre> </common> </template> <!-- Inline verbatim text (as opposed to inline code). In LaTeX this equates to \verb; delimiters for this are chosen automatically, depending on the contents. --> <template name="inline-verbatim" match="inline-verbatim|verb"> <common formats="/latex/xelatex/"> <!-- We could just call choose-delimiter twice, but this avoids any possibility of weird corner cases where the second call returns a different delimiter from the first call. It's also slightly more efficient to only call the function once. --> <xsl:variable name="verb-delimiter" select="infosci:choose-delimiter( node() )" /> <xsl:value-of select="$verb-delimiter" /> <xsl:apply-templates /> <xsl:value-of select="$verb-delimiter" /> </common> <common formats="/html/xhtml/"> <code><xsl:apply-templates /></code> </common> </template> <!-- This is for stuff that should unequivocally be treated verbatim (e.g., problem code). --> <template name="verbatim" match="verbatim|block-verbatim"> <common formats="/latex/xelatex/"> <xsl:call-template name="newline-internal" /> <xsl:text>\begin{verbatim}</xsl:text> <xsl:call-template name="newline-internal" /> <xsl:apply-templates /> <xsl:call-template name="newline-internal" /> <xsl:text>\end{verbatim}</xsl:text> <xsl:call-template name="newline-internal" /> </common> <common formats="/html/xhtml/"> <pre><xsl:apply-templates /></pre> </common> </template> </stylesheet>
Ignore Space
Show notes
View
modules/number-formatting.xml
<?xml version="1.0" encoding="utf-8"?> <!-- Format numbers according to ISO 80000-1 specifications. In particular, sequences of digits longer than four are separated into groups of up to three digits, separated by a thin space. --> <stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- General template for formatting numbers. --> <template name="number" match="number"> <!-- LaTeX just uses the \num macro from the siunitx package. --> <common formats="/latex/xelatex/"> <xsl:text>\num{</xsl:text> <xsl:apply-templates /> <xsl:text>}</xsl:text> </common> <common formats="/html/xhtml/"> <xsl:value-of select="infosci:format-number( node() )" /> </common> </template> <!-- Format a number in according to ISO 80000-1, as follows: * Sign is preserved. * A decimal number with no digits before the decimal point has a zero (0) inserted before the decimal point. * Sequences of digits longer than four are grouped into thousands separated by a thin space. Note: HTML only, as LaTeX can simply use the \num macro in the siunitx package. $unformatted-value: The value to be formatted. Returns: The formatted number as a string. --> <function name="infosci:format-number" as="xs:string"> <common formats="/html/xhtml/"> <xsl:param name="unformatted-value" /> <xsl:variable name="sign" select=" if ( matches( $unformatted-value, '^[-+]' ) ) then replace( $unformatted-value, '^([-+]).*', '$1' ) else ''" /> <xsl:variable name="left" select=" if ( matches( $unformatted-value, '^[-+]?\d+' ) ) then replace( $unformatted-value, '^[-+]?(\d+).*', '$1' ) else '0'" /> <xsl:variable name="right" select=" if ( matches ( $unformatted-value, '^[-+]?\d*\.\d+' ) ) then replace ( $unformatted-value, '^[-+]?\d*\.(\d+)', '$1' ) else ''" /> <xsl:sequence select=" concat( $sign, if ( string-length ( $left ) > 4 ) then infosci:separate-thousands( $left, 'l' ) else $left, if ( $right != '' ) then '.' else '', if ( string-length ( $right ) > 4 ) then infosci:separate-thousands( $right, 'r' ) else $right )" /> </common> </function> <!-- Splits a sequence of digits into thousand groups, separated by thin spaces (U+2009 THIN SPACE; this appears to work in both UTF-8 and ISO-8859-1). $unseparated-value: The value to be formatted. $mode: The "direction" in which to separate the digit sequence. 'l' or 'L': the input sequence is left of the decimal point. [default] 'r' or 'R': if the input sequence is right of the decimal point. Returns: The formatted number as a string. --> <function name="infosci:separate-thousands" as="xs:string"> <common formats="/html/xhtml/"> <xsl:param name="unseparated-value" /> <xsl:param name="mode" /> <!-- Sanity check. --> <xsl:choose> <xsl:when test="lower-case( $mode ) = 'l'" /> <xsl:when test="lower-case( $mode ) = 'r'" /> <xsl:otherwise> <xsl:message terminate="yes">ERROR: The mode parameter of function infosci:separate-thousands must be one of the values 'L', 'l', 'R' or 'r'.</xsl:message> </xsl:otherwise> </xsl:choose> <!-- Recursively subdivide the sequence. Terminate when sequence length <= 3. Note that we have to use the numeric entity for thin space rather than the character entity   because of the weird way XSLT handles character entities. Using the character entity, the HTML visibly displays as " ". --> <xsl:sequence select=" if ( string-length( $unseparated-value ) > 3 ) then concat( if ( lower-case( $mode ) = 'r' ) then substring( $unseparated-value, 1, 3 ) else infosci:separate-thousands( substring( $unseparated-value, 1, string-length( $unseparated-value ) - 3 ), $mode ), ' ', if ( lower-case( $mode ) = 'r' ) then infosci:separate-thousands( substring( $unseparated-value, 4 ), $mode ) else substring( $unseparated-value, string-length( $unseparated-value ) - 2 ) ) else $unseparated-value" /> </common> </function> </stylesheet>
<?xml version="1.0" encoding="utf-8"?> <!-- Format numbers according to ISO 80000-1 specifications. In particular, sequences of digits longer than four are separated into groups of up to three digits, separated by a thin space. --> <stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- General template for formatting numbers. --> <template name="number" match="number"> <!-- LaTeX just uses the siunitx package. --> <common formats="/latex/xelatex/"> <xsl:text>\num{</xsl:text> <xsl:apply-templates /> <xsl:text>}</xsl:text> </common> <common formats="/html/xhtml/"> <xsl:value-of select="infosci:format-number( node() )" /> </common> </template> <!-- Format a number in according to ISO 80000-1, as follows: * Sign is preserved. * A decimal number with no digits before the decimal point has a zero (0) inserted before the decimal point. * Sequences of digits longer than four are grouped into thousands separated by a thin space. Note: HTML only, as LaTeX can simply use the \num macro in the siunitx package. $unformatted-value: The value to be formatted. Returns: The formatted number as a string. --> <function name="infosci:format-number" as="xs:string"> <common formats="/html/xhtml/"> <xsl:param name="unformatted-value" /> <xsl:variable name="sign" select=" if ( matches( $unformatted-value, '^[-+]' ) ) then replace( $unformatted-value, '^([-+]).*', '$1' ) else ''" /> <xsl:variable name="left" select=" if ( matches( $unformatted-value, '^[-+]?\d+' ) ) then replace( $unformatted-value, '^[-+]?(\d+).*', '$1' ) else '0'" /> <xsl:variable name="right" select=" if ( matches ( $unformatted-value, '^[-+]?\d*\.\d+' ) ) then replace ( $unformatted-value, '^[-+]?\d*\.(\d+)', '$1' ) else ''" /> <xsl:sequence select=" concat( $sign, if ( string-length ( $left ) > 4 ) then infosci:separate-thousands( $left, 'l' ) else $left, if ( $right != '' ) then '.' else '', if ( string-length ( $right ) > 4 ) then infosci:separate-thousands( $right, 'r' ) else $right )" /> </common> </function> <!-- Splits a sequence of digits into thousand groups, separated by thin spaces (U+2009 THIN SPACE; this appears to work in both UTF-8 and ISO-8859-1). $unseparated-value: The value to be formatted. $mode: The "direction" in which to separate the digit sequence. 'l' or 'L': the input sequence is left of the decimal point. [default] 'r' or 'R': if the input sequence is right of the decimal point. Returns: The formatted number as a string. --> <function name="infosci:separate-thousands" as="xs:string"> <common formats="/html/xhtml/"> <xsl:param name="unseparated-value" /> <xsl:param name="mode" /> <!-- Sanity check. --> <xsl:choose> <xsl:when test="lower-case( $mode ) = 'l'" /> <xsl:when test="lower-case( $mode ) = 'r'" /> <xsl:otherwise> <xsl:message terminate="yes">The mode parameter of function infosci:separate-thousands must be one of the values 'L', 'l', 'R' or 'r'.</xsl:message> </xsl:otherwise> </xsl:choose> <!-- Recursively subdivide the sequence. Terminate when sequence length <= 3. Note that we have to use the numeric entity for thin space rather than the character entity   because of the weird way XSLT handles character entities. Using the character entity, the HTML visibly displays as " ". --> <xsl:sequence select=" if ( string-length( $unseparated-value ) > 3 ) then concat( if ( lower-case( $mode ) = 'r' ) then substring( $unseparated-value, 1, 3 ) else infosci:separate-thousands( substring( $unseparated-value, 1, string-length( $unseparated-value ) - 3 ), $mode ), ' ', if ( lower-case( $mode ) = 'r' ) then infosci:separate-thousands( substring( $unseparated-value, 4 ), $mode ) else substring( $unseparated-value, string-length( $unseparated-value ) - 2 ) ) else $unseparated-value" /> </common> </function> </stylesheet>
Show line notes below