Files
klammertext/sks/font/font.py
Andy Kopra 37b6ba1c4f Verbatim-safe typography, ^-punctuation quoting, full-range ^UUUU^, polyglot html
Typographic transforms (---, quote pairs, ~) no longer touch verbatim
text: @c/@code/@source_listing content and ^'...'^ spans show exactly the
characters written. "^" before any punctuation character quotes it in
every target (the apostrophe excepted: ^' opens a literal span), with the
new :resolve option on @@@target declaring per-target renderings. The
^UUUU^ code-point form accepts 4-6 hex digits, the full Unicode range.
The html output and transform spellings are polyglot (XML-valid), in
preparation for an EPUB target. New suites: transform_test, character_test
(engine), typography_test (SKS).

(from dev 07ce5ea86a0a)
2026-08-23 20:48:26 +02:00

56 lines
1.6 KiB
Python

import re
import klammer_base
class Subsuper(klammer_base.Klammer_base):
def __init__(self, K, position):
super().__init__(K)
self.position = position
"""
print("Subsuper:", self.args)
parts = self.args.strip().split()
if len(parts) != 2:
raise Exception(f"Two arguments required, but argument is: {self.args.strip()}")
self.base, self.script = parts
"""
def html(self):
return f"{self.base}<{self.position}>{self.script}</sub>"
def tex(self):
if self.position == "sub":
return f"${self.base}_{self.script}$"
else:
return f"${self.base}^{self.script}$"
def html_fontify(text, font_symbol, font_size):
cls = {"r" : "",
"i" :"ritalic",
"b" : "bold",
"s" : "sansserif",
"t" : "monospace",
"c" : "code"}[font_symbol]
result = text
if cls:
if font_symbol == "c":
result = re.sub(" ", "&#160;", result)
cls = f'class="{cls}"'
sty = ""
if font_size != 1:
sty = f'style="font-size: {float(font_size) * 100}%"'
attr = (cls + " " + sty).strip()
if attr:
result = f'<span {attr}>{result}</span>'
return result
def tex_fontify(text, font_symbol, font_size):
command = {"r" : "\\rmfamily",
"i" :"\\it",
"b" : "\\bfseries",
"s" : "\\sffamily",
"t" : "\\ttfamily",
"c" : "\\ttfamily"}[font_symbol]
result = f"{{{command}\\scalefont{{{font_size}}}{{{text}}}}}"
return result