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'{level_number} {title}' """ title = f'{NUMBER_VAR} {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}\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() # The \hypertarget name. K_document_file is the chapter's basename, # set by @document (document_latex.cpp): with it the name is stable # across the page cache, where a counter alone collided when some # chapters were served from cache and others reprocessed. basename = getattr(self, "K_document_file", "") or "" if self.id: id = self.id else: id = f"{basename}-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) # The announcement @document's section-link resolver reads (fields # separated by KTSL; see sks/document/section_link.h). Title last: # it may hold anything. result = f"%__sectionlink__{id}__KTSL__{basename}__KTSL__{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} @"