Support subsections in HTML output
authorJeremy Harris <jgh@wizmail.org>
Sat, 9 Jul 2022 16:30:59 +0000 (17:30 +0100)
committerJeremy Harris <jgh@wizmail.org>
Sat, 9 Jul 2022 16:30:59 +0000 (17:30 +0100)
script/gen
templates/doc/chapter.xsl

index dc2926adeb7d9407f00a07367cfbdf53b6570641..3a4587164d7c7d0a8df6e2dea08b9c74faf8faa3 100755 (executable)
@@ -287,6 +287,7 @@ sub xref_fixup {
         my $section_counter = 0;
         foreach my $section ( $chapter->findnodes('section') ) {
             ++$section_counter;
+           $section->setAttribute( 'sectprefix', $section_counter );
 
             my $section_id = $section->getAttribute('id');
             unless ($section_id) {    # synthesise missing id
@@ -301,6 +302,31 @@ sub xref_fixup {
                 section_id    => $section_counter,
                 section_title => $section_title
             };
+
+           # 2022/07/07 jgh: added loop for sections under sections, which are resulting from the .subsection macro
+           # Add a "level" attribute to these nodes
+           ## Iterate over each subsection
+           my $subsec_counter = 0;
+           foreach my $subsection ( $section->findnodes('section') ) {
+             ++$subsec_counter;
+
+             $subsection->setAttribute( 'level', "2" );
+             $subsection->setAttribute( 'sectprefix', sprintf("%d.%d", $section_counter, $subsec_counter) );
+
+             my $subsec_id = $subsection->getAttribute('id');
+             unless ($subsec_id) {    # synthesise missing id
+                 $subsec_id = sprintf( 'section_noid_%04d_%04d_%04d', $chapter_counter, $section_counter, $subsec_counter );
+                 $subsection->setAttribute( 'id', $subsec_id );
+             }
+             my $subsec_title = $subsection->findvalue('title');
+
+             $index{$subsec_id} = {
+                 chapter_id    => $chapter_counter,
+                 chapter_title => $chapter_title,
+                 section_id    => $subsec_counter,
+                 section_title => $subsec_title
+             };
+           }
         }
     }
     ## Build indexes as new chapters
index 17d772ad7a16cfa287fc448675ec22bb972f7290..03fa3485f9e97d4c44086ccbdfc6fc622eca71f4 100644 (file)
       </xsl:template>
 
    <!-- Section -->
-      <xsl:template match="/chapter/section">
+      <xsl:template match="*/section">
+
+        <!-- Add 2 to the "level" attr for the html header level; default h3 -->
+        <xsl:variable name="hlevel">
+          <xsl:choose>
+             <xsl:when test="@level!=''"><xsl:value-of select="@level + 2"/></xsl:when>
+             <xsl:otherwise>3</xsl:otherwise>
+          </xsl:choose>
+        </xsl:variable>
+
+        <!-- Take the "sectprefix" attr, default position() -->
+        <xsl:variable name="sectprefix">
+          <xsl:choose>
+             <xsl:when test="@sectprefix!=''"> <xsl:value-of select="@sectprefix"/> </xsl:when>
+             <xsl:otherwise>                   <xsl:value-of select="position()"/>  </xsl:otherwise>
+          </xsl:choose>
+        </xsl:variable>
+
          <!-- Section Wrapper -->
          <div class="section{@class}">
 
                   </h3>
                </xsl:when>
                <xsl:otherwise>
-                  <h3 id="{@id}" class="{@class}">
-                     <xsl:value-of select="concat(position(),'. ',title)"/>
-                  </h3>
+                 <xsl:element name="h{$hlevel}">
+                   <xsl:attribute name="id">    <xsl:value-of select="@id"/>    </xsl:attribute>
+                   <xsl:attribute name="class"> <xsl:value-of select="@class"/> </xsl:attribute>
+                     <xsl:value-of select="concat($sectprefix,'. ',title)"/>
+                 </xsl:element>
                </xsl:otherwise>
             </xsl:choose>