"""pubsalesacc/utils/text.py — Text and HTML accumulator classes.""" import os class Textobj: """Accumulates lines of text and can print or save to a .txt file.""" def __init__(self, text: str = "") -> None: self.text = text def __str__(self) -> str: return self.text def add(self, addtext: object, linebreak: int = 2) -> None: """Append text with leading newlines (default 2).""" s = str(addtext) if len(self.text) > 1: s = "\n" * linebreak + s self.text += s def print(self) -> None: print(self.text) def save(self, filename: str) -> None: """Save to file, appending .txt if no extension provided.""" if not os.path.splitext(filename)[1]: filename += ".txt" with open(filename, "w", encoding="utf-8") as f: f.write(self.text) print(f"\nOutput saved to: {filename}") class Htmlobj: """Accumulates HTML content and can print or save to a .html file.""" def __init__(self, text: str = "") -> None: self.text = text def __str__(self) -> str: return self.text def add(self, addtext: object, formatting: str = "") -> None: """Append HTML-escaped text with
separators.""" s = str(addtext) if len(self.text) > 1: if not s.startswith(""): s = "

" + s.replace("\n", "
") if formatting == "b": s = f"{s}" elif formatting == "i": s = f"{s}" self.text += s def print(self) -> None: print(self.text) def save(self, filename: str) -> None: filename = os.path.splitext(filename)[0] + ".html" with open(filename, "w", encoding="utf-8") as f: f.write(self.text) print(f"\nOutput saved to: {filename}")