Files
klammertext/sks/section/section.py
T
ackandClaude Fable 5.1 63956c00e5 txt section numbering, @typeface :middle/:lift and html scale, NewGardinerOmni, html :leading (from dev 9fa469dd74ed)
Section headings under txt are numbered inside a @document only; @typeface
gains :middle and :lift and its :scale now means the same in html and
pdf; NewGardinerOmni ships (OFL 1.1, Mark-Jan Nederhof) and @egyptian /
@chinese replace @cjk; @table :leading and :colsep reach html; kdesc
--typeface help names the flag; @document.txt is a Python class.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-18 20:02:44 +02:00

86 lines
3.3 KiB
Python

import kutil
import klammer_base
import phases
NUMBER_VAR = "___NUM___"
class Section(klammer_base.Klammer_base):
label_number = 0
count = 7
indices = [0] * count
def __init__(self, K, level, numbered=True):
super().__init__(K)
# Add front and back matter elements as special cases:
if level == "preface":
self.preface = True
self.level = 0
self.title = None
else:
self.preface = False
self.level = level
self.numbered = numbered
self.part = 1
def html(self):
tags = "kt-part kt-chapter h1 h2 h3 h4 h5 h6 h7".split()
display_names = "Part Chapter".split()
id = f' id="{self.id}"' if self.id else ""
# number_class = " numbered" if self.numbered else ""
number_class = "" # The variable is sufficient?
title = self.title
tag = tags[self.level]
if self.numbered:
"""
Section.indices[self.level] += 1
for i in range(self.level + 1, Section.count):
Section.indices[i] = 0
level_number = ".".join([str(e) for e in Section.indices])
title = f'<span class="sectionnumber">{level_number}</span> {title}'
"""
title = f'<span class="sectionnumber">{NUMBER_VAR}</span> {self.title if self.title else ""}'
if self.level < 1: # < 2 for "Chapter"
#title = f"{tag} {Section.indices[self.level]} {title}"
title = f"{display_names[self.level]} {title if title else ''}"
result = f'<{tag}{id}{number_class}>{title}</{tag}>\n\n'
return result
def tex(self):
# names = "part chapter section subsection subsubsection paragraph subparagraph subsubparagraph subsubsubparagraph".split()
names = "part sA sB sC sD sE".split()
if self.id:
id = self.id
else:
id = f"-s{Section.label_number}"
Section.label_number += 1
result = ''
if getattr(self, "n", -1) > -1: # absent for @part and @h1..@h7
result += f'\\setcounter{{chapter}}{{{int(self.n)-1}}}\n'
suffix = "" if self.numbered else "*"
result += f'\\{names[self.level]}{suffix}{{{self.title}}}'
result += f'\n\\hypertarget{{{id}}}{{}}'
# result = kutil.protect_klammertext_special_characters(result)
result = f"%__sectionlink__{id}__{self.title}__" + "\n" + result
return result
def txt(self):
# The number is a marker phases.txt_insert_section_numbers resolves
# -- to a number inside a @document, to nothing outside one -- so
# the space after it is the marker's, not the title's. A preface
# and an unnumbered heading carry no marker at all; a part carries
# a level-0 marker and its name, as the html "Part" prefix.
if self.preface:
return f"@b {self.name} @"
if not self.numbered:
return f"@b {self.title} @"
# :n comes from section_args, which @part and the @h headings do
# not declare, so it is absent there rather than -1.
marker = phases.txt_section_marker(self.level, getattr(self, "n", -1))
if self.level == 0:
return f"@b {marker} {self.name} {self.title} @"
return f"@b {marker} {self.title} @"