Files
klammertext/sks/table/table_cell.py
Andy Kopra 8a2699a253 Typed arguments, calculated tables, spans, closed-world fonts, top-level fnt/ and env/
Sync with klammertext-dev through b90b0e09:

- Argument types end to end: :python_cast values are applied (Python
  @eval receives real bools/numbers/lists), argument values are
  validated against their argtype patterns with the argtype's
  description as the error message, argtypes can declare :default
  (overridable per declaration), and parameterized type families are
  supported: rest(N) casts a rest argument to an N-dimensional list
  (bar-count = dimension).
- Unified indexed_range syntax (selector with parenthesized subsets,
  composable mnemonic names) for table lines and spans.
- Table klammer: caption fonts fixed in both targets, :column_width /
  :leading / :colsep wired, :colspan and :rowspan render (HTML
  attributes; \multicolumn / \multirow), calculated cell values (:calc)
  with prefix operators, display-precision semantics, :calc_format and
  :decimal period|comma.
- Fonts: closed-world resolution on the Klammertext font store
  (infrastructure in mac/font_store; no Google Fonts links or fetch).
  Default fonts live in the top-level fnt/; additional fonts install
  into KLAMMERTEXT_FONTS directories via kdesc --font (list, samples,
  preview, install — classification by font metadata).  CSS font family
  names are quoted (digit-initial families were silently lost).
- Environment files moved from mac/env/ to the top-level env/; shell
  profiles source env/runtime.env.  Dead per-host variants removed.
- Container: fnt/ ships in the image; curl removed (no network use).
2026-07-22 18:17:43 +02:00

89 lines
3.4 KiB
Python

import re
import border
import font
import html_util
from html_util import E
# A purely numeric cell value (either decimal-mark style, optional sign).
number_rgx = re.compile(r"[-+]?[\d.,]+$")
class Cell:
def __init__(self, text, font, hpos,
top, right, bottom, left,
left_all, right_all,
rowspan, colspan, first_column=False):
self.text = text
self.font = font
self.hpos = hpos
self.border = border.Border(top, right, bottom, left, left_all, right_all)
self.rowspan = rowspan
self.colspan = colspan
self.first_column = first_column
#print("Cell:", text, hpos)
def __str__(self):
b = str(self.border)
b = f"[{b}]" if b else ""
rs = f"[r{self.rowspan}]" if self.rowspan else ""
cs = f"[r{self.colspan}]" if self.colspan else ""
#return f'<cell"{self.text}"{self.font}{self.hpos}{b}>'
return f'<cell"{self.text}"{b}{rs}{cs}>'
def __repr__(self):
return self.__str__()
def html(self):
result = self.text
result = E("td").body(font.html_fontify(result, self.font, 1.0))
if self.rowspan > 1:
result.attr("rowspan", self.rowspan)
if self.colspan > 1:
result.attr("colspan", self.colspan)
for pred, cls_name in zip(self.border.has(), "Bt Br Bb Bl".split()):
if pred:
result.cls(cls_name)
result.cls(f"H{self.hpos}")
return result.str()
def tex(self, debug=False): # , left_line, right_line):
result = self.text
# A number must not line-break (LaTeX breaks after a minus sign
# read as a hyphen in narrow fit-width columns).
if number_rgx.match(result.strip()):
result = f"\\mbox{{{result.strip()}}}"
if self.font != "r":
result = font.tex_fontify(result, self.font, 1.0)
if self.rowspan > 1:
result = f"\\multirow{{{self.rowspan}}}{{*}}{{{result}}}"
if self.colspan > 1:
# \multicolumn carries the merged cell's own column spec. A
# left bar may only be given when the span starts at the
# table's first column: elsewhere the bar to the left belongs
# to the preceding column's preamble entry, and adding one
# here draws a doubled line.
pos = self.hpos
if self.border.left and self.first_column:
pos = "|" + pos
if self.border.right:
pos = pos + "|"
return f"\\multicolumn{{{self.colspan}}}{{{pos}}}{{{result}}}"
remove_left = self.border.left_all and not self.border.left
remove_right = self.border.right_all and not self.border.right
if debug:
left_marker = "xL" if remove_left else "L" if self.border.left_all else ""
right_marker = "Rx" if remove_right else "R" if self.border.right_all else ""
result = f"{left_marker} {result} {right_marker}"
if remove_left or remove_right:
pos = self.hpos
if self.border.left_all and self.border.left:
pos = "|" + pos
if self.border.right_all and self.border.right:
pos = pos + "|"
result = f"\\multicolumn{{1}}{{{pos}}}{{{result}}}"
return result
if __name__ == "__main__":
print(Cell("dog", "r", "c", True, False, True, False))