Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix single line program #2309

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/sample-article/sample-article.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10688,6 +10688,10 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
</program>
</listing>

<p>Although a program should have a <tag>code</tag> element surrounding its code, we attempt to provide one when it is missing. This next sample tests that and intentionally has no leading or trailing newline inside the program.</p>

<program languge="python">print("Hello world")</program>

<p>If you are discussing algorithms in the abstract (or even concretely), you can set them off like a theorem, with a number, a title and a target for cross-references. Sometimes you claim an algorithm produces something in particular, or has certain properties, such as a theoretical run time, so a proof may be included. See the discussion just preceding about (limited) options for pseudo-code.</p>

<algorithm xml:id="algorithm-sieve-eratosthenes">
Expand Down
7 changes: 4 additions & 3 deletions xsl/pretext-html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -9202,9 +9202,10 @@ along with MathBook XML. If not, see <http://www.gnu.org/licenses/>.
<xsl:with-param name="substr" select="'&#xA;'" />
</xsl:call-template>
</xsl:if>
<xsl:call-template name="substring-before-last">
<xsl:with-param name="input" select="code" />
<xsl:with-param name="substr" select="'&#xA;'" />
<!-- code section MUST end with newline, which author may or may not have included -->
<!-- so remove up to one newline and trailing space from end of code then add a newline -->
<xsl:call-template name="strip-trailing-whitespace-line">
<xsl:with-param name="text" select="code" />
</xsl:call-template>
<xsl:text>&#xA;</xsl:text>
<xsl:if test="postamble[not(@visible = 'no')]">
Expand Down
24 changes: 24 additions & 0 deletions xsl/pretext-text-utilities.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,30 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
</xsl:choose>
</xsl:template>

<!-- Working from end, remove whitespace back to and including last newline -->
<xsl:template name="strip-trailing-whitespace-line">
<xsl:param name="text" />
<xsl:variable name="last-char" select="substring($text, string-length($text), 1)" />
<xsl:choose>
<!-- if empty, quit -->
<xsl:when test="not($last-char)" />
<!-- if last character is newline, return everything else -->
<xsl:when test="$last-char = '&#xa;'">
<xsl:value-of select="substring($text, 1, string-length($text)-1)" />
</xsl:when>
<!-- if last character is whitespace, drop it -->
<xsl:when test="contains($whitespaces, $last-char)">
<xsl:call-template name="strip-trailing-whitespace">
<xsl:with-param name="text" select="substring($text, 1, string-length($text)-1)" />
</xsl:call-template>
</xsl:when>
<!-- else finished stripping, output as-is -->
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- spurious newlines introduce whitespace on either side -->
<!-- we split at newlines, strip consecutive whitesapce on either side, -->
<!-- and replace newlines by spaces (could restore a single newline) -->
Expand Down