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

"level" template transition next step #2334

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 6 additions & 4 deletions xsl/pretext-assembly.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.

<xsl:template match="node()|@*" mode="augment">
<xsl:param name="parent-struct" select="''"/>
<xsl:param name="level" select="0"/>
<xsl:param name="level" select="-1"/>
<xsl:param name="ordered-list-level" select="0"/>

<xsl:copy>
Expand Down Expand Up @@ -1881,8 +1881,10 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
<!-- modal template will return a count of preceding peers at that level. -->
<!-- The @struct attribute is the structure number of the *parent* -->
<!-- (container), which seems odd here, but fits the general scheme better. -->
<!-- The @level attribute is helpful, and trvislly to compute here. -->
<xsl:template match="part|chapter|appendix|section|subsection|subsubsection|exercises|solutions|reading-questions|references|glossary|worksheet" mode="augment">
<!-- The @level attribute is helpful, and trivial to compute here. -->
<!-- Use &STRUCTURAL; here for easier maintenance, but some templates will -->
<!-- override with priority (e.g. frontmatter|backmatter) -->
<xsl:template match="&STRUCTURAL;" mode="augment" priority="0">
<xsl:param name="parent-struct"/>
<xsl:param name="level"/>
<xsl:param name="ordered-list-level" />
Expand Down Expand Up @@ -1945,7 +1947,7 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
<!-- backmatter. So we need to increment the level in this case, only. -->
<!-- NB: this might consolidate with above, but seems better solo. -->
<!-- NB: with some study and work, this situation might be improved? -->
<xsl:template match="frontmatter|backmatter" mode="augment">
<xsl:template match="frontmatter|backmatter" mode="augment" priority="1">
<xsl:param name="parent-struct"/>
<xsl:param name="level"/>

Expand Down
51 changes: 36 additions & 15 deletions xsl/pretext-common.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -525,37 +525,58 @@ Book (with parts), "section" at level 3
<!-- use of levels computed during the "assembly" phase. So we use careful -->
<!-- matches and we use careful choices for application. At every -->
<!-- application we compute the "old" level to test for consistency. -->
<!-- 2024-12-19: next step - use "safe" version of level for now and watch -->
<!-- for issues. The safe version tests "new-level" for all uses of "level" -->

<!-- ####################################################################### -->
<xsl:template match="part|chapter|appendix|section|subsection|subsubsection|exercises|solutions|reading-questions|references|glossary|worksheet" mode="new-level">
<!-- temporary "level" entry point -->
<xsl:template match="&STRUCTURAL;" mode="level">
<xsl:variable name="old-level">
<xsl:apply-templates select="." mode="level"/>
<xsl:apply-templates select="." mode="level-expensive"/>
</xsl:variable>
<xsl:if test="not($old-level = @level)">
<xsl:message>PTX:BUG: development bug, new level does not match old level for "<xsl:value-of select="local-name(.)"/>"</xsl:message>
<xsl:variable name="new-level">
<xsl:apply-templates select="." mode="new-level"/>
</xsl:variable>
<xsl:if test="not($old-level = $new-level)">
<xsl:message>PTX:BUG: development bug, new level (<xsl:value-of select="$new-level"/>) does not match old level (<xsl:value-of select="$old-level"/>) for "<xsl:value-of select="local-name(.)"/>"</xsl:message>
<xsl:apply-templates select="." mode="location-report" />
</xsl:if>
<!-- actual value here, above is debugging -->
<!-- trust the old value -->
<xsl:value-of select="$old-level"/>
</xsl:template>

<!-- ####################################################################### -->
<!-- These "new-level" eventually become "level" -->
<xsl:template match="&STRUCTURAL;" mode="new-level">
<xsl:value-of select="@level"/>
</xsl:template>

<xsl:template match="*" mode="new-level">
<xsl:message>PTX:BUG: an element ("<xsl:value-of select="local-name(.)"/>") does not know its *new* level</xsl:message>
<xsl:apply-templates select="." mode="location-report" />
<!-- Safety catch all -->
<xsl:template match="*" mode="new-level" priority="-1">
<xsl:choose>
<xsl:when test="@level">
<xsl:message>PTX:BUG: an element ("<xsl:value-of select="local-name(.)"/>") thinks it doesn't do *new* level but does </xsl:message>
<xsl:value-of select="@level"/>
</xsl:when>
<xsl:otherwise>
<xsl:message>PTX:BUG: an element ("<xsl:value-of select="local-name(.)"/>") does not know its *new* level</xsl:message>
<xsl:apply-templates select="." mode="location-report" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- ####################################################################### -->

<!-- Specific top-level divisions -->
<!-- These "level-expensive" go away once there is confidence in "new-level" -->
<!-- Specific top-level divisions -->
<!-- article/frontmatter, article/backmatter are faux divisions, but -->
<!-- will function as a terminating condition in recursive count below -->
<xsl:template match="book|article|slideshow|letter|memo|article/frontmatter|article/backmatter" mode="level">
<xsl:template match="book|article|slideshow|letter|memo|article/frontmatter|article/backmatter" mode="level-expensive">
<xsl:value-of select="0"/>
</xsl:template>

<!-- A book/part will divide the mainmatter, so a "chapter" is at -->
<!-- level 2, so we also put the faux divisions at level 1 in the -->
<!-- case of parts, to again terminate recursive count -->
<xsl:template match="book/part|book/frontmatter|book/backmatter" mode="level">
<xsl:template match="book/part|book/frontmatter|book/backmatter" mode="level-expensive">
<xsl:choose>
<xsl:when test="$b-has-parts">
<xsl:value-of select="1"/>
Expand All @@ -577,14 +598,14 @@ Book (with parts), "section" at level 3
<!-- chapters of books, sections of articles, or in the case of -->
<!-- solutions or references, children of an appendix. -->

<xsl:template match="colophon|biography|dedication|acknowledgement|preface|chapter|section|subsection|subsubsection|slide|appendix|index|colophon|exercises|reading-questions|references|solutions|glossary|worksheet" mode="level">
<xsl:template match="colophon|biography|dedication|acknowledgement|preface|chapter|section|subsection|subsubsection|slide|appendix|index|colophon|exercises|reading-questions|references|solutions|glossary|worksheet" mode="level-expensive">
<xsl:variable name="level-above">
<xsl:apply-templates select="parent::*" mode="level"/>
<xsl:apply-templates select="parent::*" mode="level-expensive"/>
</xsl:variable>
<xsl:value-of select="$level-above + 1"/>
</xsl:template>

<xsl:template match="*" mode="level">
<xsl:template match="*" mode="level-expensive">
<xsl:message>PTX:BUG: an element ("<xsl:value-of select="local-name(.)"/>") does not know its level</xsl:message>
<xsl:apply-templates select="." mode="location-report" />
</xsl:template>
Expand Down
18 changes: 18 additions & 0 deletions xsl/pretext-numbers.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ You should have received a copy of the GNU General Public License
along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************-->

<!DOCTYPE xsl:stylesheet [
<!ENTITY % entities SYSTEM "entities.ent">
%entities;
]>

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
Expand Down Expand Up @@ -120,4 +125,17 @@ along with PreTeXt. If not, see <http://www.gnu.org/licenses/>.
<xsl:number from="backmatter" level="any" count="appendix|solutions" format="A"/>
</xsl:template>

<!-- "augment" may ask about any structural elements. -->
<!-- this is included as a low-priority catch-all for -->
<!-- anything not captured above. -->
<xsl:template match="&STRUCTURAL;" mode="division-serial-number" priority="-1">
<xsl:value-of select="''"/>
</xsl:template>

<!-- final catch all to flag anything unexpected -->
<xsl:template match="*" mode="division-serial-number" priority="-1">
<xsl:message>PTX:BUG the "division-serial-number" was applied to an element <xsl:value-of select="name(.)"/>. That is unexpected, please report it.</xsl:message>
<xsl:value-of select="''"/>
</xsl:template>

</xsl:stylesheet>