Files
klammertext/sks/table/table_cell.py
Andy Kopra 2ba7ceee7a Initial commit: Klammertext source distribution
Curated source subset assembled by klammertext-dev's doc/make_dist.sh: the Klammermachine (mac), the Standard Klammer Set (sks), the commands (com), editor plugins and install guides (doc), a test subset (tst), and lib/bin placeholders. Builds with 'make -C com'.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-18 19:32:38 +02:00

61 lines
2.1 KiB
Python

import border
import font
import html_util
from html_util import E
class Cell:
def __init__(self, text, font, hpos,
top, right, bottom, left,
left_all, right_all,
rowspan, colspan):
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
#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))
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
if self.font != "r":
result = font.tex_fontify(result, self.font, 1.0)
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))