Skip to content

Commit

Permalink
Better chomping.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Alama committed Jul 5, 2013
1 parent d6df3ff commit 5f9005a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
62 changes: 60 additions & 2 deletions render-tptp.xsl
Expand Up @@ -43,13 +43,71 @@
<xsl:template match="comment">
<xsl:for-each select="text()">
<xsl:variable name="s" select="."/>
<xsl:variable name="c">
<xsl:call-template name="chomp">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="emit-percents">
<xsl:with-param name="s" select="substring-after ($s, &quot;
&quot;)"/>
<xsl:with-param name="s" select="$c"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<!-- if the string starts with a newline, delete it -->
<xsl:template name="chomp-front">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="starts-ith ($s, &quot;\n&quot;)">
<xsl:value-of select="substring-after ($s, &quot;
&quot;)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="chomp-tail">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains ($s, &quot;\n&quot;)">
<xsl:variable name="before" select="substring-before ($s, &quot;
&quot;)"/>
<xsl:choose>
<xsl:when test="contains ($before, &quot;\n&quot;)">
<xsl:variable name="chomped">
<xsl:call-template name="chomp-tail">
<xsl:with-param name="s" select="substring-after ($before, &quot;
&quot;)"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat ($before, &quot;
&quot;, $chomped)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$before"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="chomp">
<xsl:param name="s"/>
<xsl:variable name="front">
<xsl:call-template name="chomp-front">
<xsl:with-param name="s" select="$s"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="chomp-tail">
<xsl:with-param name="s" select="$front"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="emit-percents">
<xsl:param name="s"/>
<xsl:choose>
Expand Down
31 changes: 30 additions & 1 deletion render-tptp.xsltxt
Expand Up @@ -34,10 +34,39 @@ tpl [tstp] {
tpl [comment] {
for-each [text()] {
$s = `.`;
emit-percents (#s = `substring-after ($s, "\n")`);
$c = chomp (#s = $s);
emit-percents (#s = $c);
}
}

// if the string starts with a newline, delete it
tpl chomp-front (#s) {
if [starts-ith ($s, "\n")] {
`substring-after ($s, "\n")`;
} else {
$s;
}
}

tpl chomp-tail (#s) {
if [contains ($s, "\n")] {
$before = `substring-before ($s, "\n")`;
if [contains ($before, "\n")] {
$chomped = chomp-tail (#s = `substring-after ($before, "\n")`);
`concat ($before, "\n", $chomped)`;
} else {
$before;
}
} else {
$s;
}
}

tpl chomp (#s) {
$front = chomp-front (#s = $s);
chomp-tail (#s = $front);
}

tpl emit-percents (#s) {
choose {
when [$s = ""] {
Expand Down

0 comments on commit 5f9005a

Please sign in to comment.