termrender 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ id-token: write
9
+
10
+ jobs:
11
+ publish:
12
+ runs-on: ubuntu-latest
13
+ environment: pypi
14
+ permissions:
15
+ id-token: write
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+ - uses: actions/setup-python@v5
19
+ with:
20
+ python-version: "3.12"
21
+ - run: pip install build
22
+ - run: python -m build
23
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,9 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ dist/
5
+ build/
6
+ .venv/
7
+ .eggs/
8
+ *.egg
9
+ .sisyphus/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Silas Rhyneer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,26 @@
1
+ Metadata-Version: 2.4
2
+ Name: termrender
3
+ Version: 0.1.0
4
+ Summary: Rich terminal rendering of directive-flavored markdown
5
+ Project-URL: Homepage, https://github.com/CaptainCrouton89/termrender
6
+ Project-URL: Repository, https://github.com/CaptainCrouton89/termrender
7
+ Project-URL: Issues, https://github.com/CaptainCrouton89/termrender/issues
8
+ Author: Silas Rhyneer
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: ansi,cli,markdown,rendering,terminal
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Terminals
22
+ Classifier: Topic :: Text Processing :: Markup :: Markdown
23
+ Requires-Python: >=3.10
24
+ Requires-Dist: mermaid-ascii>=1.0
25
+ Requires-Dist: mistune>=3.0
26
+ Requires-Dist: pygments>=2.0
@@ -0,0 +1,43 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "termrender"
7
+ version = "0.1.0"
8
+ description = "Rich terminal rendering of directive-flavored markdown"
9
+ requires-python = ">=3.10"
10
+ license = "MIT"
11
+ authors = [
12
+ { name = "Silas Rhyneer" },
13
+ ]
14
+ keywords = ["terminal", "markdown", "ansi", "cli", "rendering"]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "Environment :: Console",
18
+ "Intended Audience :: Developers",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.10",
22
+ "Programming Language :: Python :: 3.11",
23
+ "Programming Language :: Python :: 3.12",
24
+ "Programming Language :: Python :: 3.13",
25
+ "Topic :: Text Processing :: Markup :: Markdown",
26
+ "Topic :: Terminals",
27
+ ]
28
+ dependencies = [
29
+ "mistune>=3.0",
30
+ "mermaid-ascii>=1.0",
31
+ "pygments>=2.0",
32
+ ]
33
+
34
+ [project.urls]
35
+ Homepage = "https://github.com/CaptainCrouton89/termrender"
36
+ Repository = "https://github.com/CaptainCrouton89/termrender"
37
+ Issues = "https://github.com/CaptainCrouton89/termrender/issues"
38
+
39
+ [project.scripts]
40
+ termrender = "termrender.__main__:main"
41
+
42
+ [tool.hatch.build.targets.wheel]
43
+ packages = ["src/termrender"]
@@ -0,0 +1,43 @@
1
+ """termrender — render Markdown to ANSI terminal output."""
2
+
3
+ import os
4
+ import shutil
5
+ from termrender.parser import parse
6
+ from termrender.layout import layout
7
+ from termrender.emit import emit
8
+
9
+
10
+ class TerminalError(Exception):
11
+ """Raised when the terminal does not support required capabilities."""
12
+
13
+
14
+ def render(source: str, width: int | None = None, color: bool = True) -> str:
15
+ """Render directive-flavored markdown to ANSI terminal output.
16
+
17
+ Args:
18
+ source: Markdown string with optional directives
19
+ width: Terminal width in columns (auto-detected if None)
20
+ color: Enable ANSI color codes (respects NO_COLOR env var)
21
+
22
+ Returns:
23
+ Rendered string with ANSI escape sequences
24
+
25
+ Raises:
26
+ TerminalError: If terminal is unsupported (TERM=dumb)
27
+ """
28
+ # REQ-011: Check terminal capability
29
+ if os.environ.get("TERM") == "dumb":
30
+ raise TerminalError("Terminal type 'dumb' does not support Unicode rendering")
31
+
32
+ # Respect NO_COLOR convention (https://no-color.org/)
33
+ if os.environ.get("NO_COLOR") is not None:
34
+ color = False
35
+
36
+ # Auto-detect width
37
+ if width is None:
38
+ width = shutil.get_terminal_size().columns
39
+
40
+ # Pipeline: parse → layout → emit
41
+ doc = parse(source)
42
+ layout(doc, width)
43
+ return emit(doc, color)
@@ -0,0 +1,46 @@
1
+ """CLI entry point for termrender."""
2
+
3
+ import argparse
4
+ import sys
5
+
6
+ from termrender import render, TerminalError
7
+
8
+
9
+ def main():
10
+ parser = argparse.ArgumentParser(
11
+ prog="termrender",
12
+ description="Render Markdown to ANSI terminal output",
13
+ )
14
+ parser.add_argument(
15
+ "file",
16
+ nargs="?",
17
+ type=argparse.FileType("r"),
18
+ default=sys.stdin,
19
+ help="Markdown file to render (reads stdin if omitted)",
20
+ )
21
+ parser.add_argument(
22
+ "--width",
23
+ type=int,
24
+ default=None,
25
+ help="Terminal width in columns (auto-detected if omitted)",
26
+ )
27
+ parser.add_argument(
28
+ "--no-color",
29
+ action="store_true",
30
+ help="Disable ANSI color output",
31
+ )
32
+ args = parser.parse_args()
33
+
34
+ source = args.file.read()
35
+
36
+ try:
37
+ output = render(source, width=args.width, color=not args.no_color)
38
+ except TerminalError as e:
39
+ print(f"termrender: {e}", file=sys.stderr)
40
+ sys.exit(1)
41
+
42
+ sys.stdout.write(output)
43
+
44
+
45
+ if __name__ == "__main__":
46
+ main()
@@ -0,0 +1,48 @@
1
+ """Core block data model for termrender."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from enum import Enum
7
+ from typing import Any
8
+
9
+
10
+ class BlockType(Enum):
11
+ """Types of renderable blocks."""
12
+
13
+ DOCUMENT = "document"
14
+ PARAGRAPH = "paragraph"
15
+ HEADING = "heading"
16
+ PANEL = "panel"
17
+ COLUMNS = "columns"
18
+ COL = "col"
19
+ TREE = "tree"
20
+ CALLOUT = "callout"
21
+ QUOTE = "quote"
22
+ CODE = "code"
23
+ DIVIDER = "divider"
24
+ MERMAID = "mermaid"
25
+ LIST = "list"
26
+ LIST_ITEM = "list_item"
27
+
28
+
29
+ @dataclass
30
+ class InlineSpan:
31
+ """A span of inline text with optional formatting."""
32
+
33
+ text: str
34
+ bold: bool = False
35
+ italic: bool = False
36
+ code: bool = False
37
+
38
+
39
+ @dataclass
40
+ class Block:
41
+ """A renderable block element with optional children and inline text."""
42
+
43
+ type: BlockType
44
+ children: list[Block] = field(default_factory=list)
45
+ text: list[InlineSpan] = field(default_factory=list)
46
+ attrs: dict[str, Any] = field(default_factory=dict)
47
+ width: int | None = None
48
+ height: int | None = None
@@ -0,0 +1,52 @@
1
+ """AST walker that dispatches laid-out blocks to renderers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from termrender.blocks import Block, BlockType
6
+ from termrender.renderers import panel, columns, tree, code, text, divider, quote, mermaid
7
+
8
+
9
+ def emit_block(block: Block, color: bool) -> list[str]:
10
+ """Render a single block and its children, returning output lines."""
11
+ match block.type:
12
+ case BlockType.DOCUMENT | BlockType.COL:
13
+ lines: list[str] = []
14
+ for child in block.children:
15
+ lines.extend(emit_block(child, color))
16
+ return lines
17
+
18
+ case BlockType.PANEL:
19
+ return panel.render(block, color, render_child=emit_block)
20
+
21
+ case BlockType.CALLOUT:
22
+ return panel.render_callout(block, color, render_child=emit_block)
23
+
24
+ case BlockType.COLUMNS:
25
+ return columns.render(block, color, render_child=emit_block)
26
+
27
+ case BlockType.QUOTE:
28
+ return quote.render(block, color, render_child=emit_block)
29
+
30
+ case BlockType.CODE:
31
+ return code.render(block, color, render_child=emit_block)
32
+
33
+ case BlockType.PARAGRAPH | BlockType.HEADING | BlockType.LIST | BlockType.LIST_ITEM:
34
+ return text.render(block, color)
35
+
36
+ case BlockType.TREE:
37
+ return tree.render(block, color)
38
+
39
+ case BlockType.MERMAID:
40
+ return mermaid.render(block, color)
41
+
42
+ case BlockType.DIVIDER:
43
+ return divider.render(block, color)
44
+
45
+ case _:
46
+ return []
47
+
48
+
49
+ def emit(doc: Block, color: bool) -> str:
50
+ """Walk the block tree and return the fully rendered string."""
51
+ lines = emit_block(doc, color)
52
+ return "\n".join(lines)
@@ -0,0 +1,134 @@
1
+ """Two-pass layout engine: resolve widths top-down, heights bottom-up."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import subprocess
6
+
7
+ from termrender.blocks import Block, BlockType
8
+ from termrender.style import wrap_text
9
+
10
+
11
+ def _plain_text(spans: list) -> str:
12
+ return "".join(s.text for s in spans)
13
+
14
+
15
+ def resolve_width(block: Block, available: int) -> None:
16
+ block.width = available
17
+
18
+ bt = block.type
19
+ if bt in (BlockType.PANEL, BlockType.CALLOUT, BlockType.CODE, BlockType.QUOTE):
20
+ inner = max(available - 4, 1)
21
+ for child in block.children:
22
+ resolve_width(child, inner)
23
+
24
+ elif bt == BlockType.COLUMNS:
25
+ n = len(block.children)
26
+ if n == 0:
27
+ return
28
+ gaps = n - 1
29
+ inner = available
30
+
31
+ # Separate children into explicit-width and auto-width
32
+ explicit: dict[int, int] = {}
33
+ for i, col in enumerate(block.children):
34
+ w = col.attrs.get("width")
35
+ if w is not None:
36
+ w_str = str(w)
37
+ try:
38
+ if w_str.endswith("%"):
39
+ explicit[i] = max(int(inner * float(w_str[:-1]) / 100), 1)
40
+ else:
41
+ explicit[i] = max(int(w_str), 1)
42
+ except ValueError:
43
+ pass
44
+
45
+ used = sum(explicit.values()) + gaps
46
+ remaining = max(inner - used, 0)
47
+ auto_count = n - len(explicit)
48
+
49
+ for i, col in enumerate(block.children):
50
+ if i in explicit:
51
+ col_w = explicit[i]
52
+ elif auto_count > 0:
53
+ col_w = max(remaining // auto_count, 1)
54
+ else:
55
+ col_w = 1
56
+ resolve_width(col, col_w)
57
+
58
+ else:
59
+ for child in block.children:
60
+ resolve_width(child, available)
61
+
62
+
63
+ def resolve_height(block: Block) -> None:
64
+ # Recurse children first (bottom-up)
65
+ for child in block.children:
66
+ resolve_height(child)
67
+
68
+ bt = block.type
69
+ width = block.width or 1
70
+
71
+ if bt == BlockType.PARAGRAPH:
72
+ text = _plain_text(block.text)
73
+ lines = wrap_text(text, width)
74
+ block.height = len(lines)
75
+
76
+ elif bt == BlockType.HEADING:
77
+ block.height = 1
78
+
79
+ elif bt == BlockType.DIVIDER:
80
+ block.height = 1
81
+
82
+ elif bt in (BlockType.PANEL, BlockType.CALLOUT):
83
+ block.height = sum(c.height or 0 for c in block.children) + 2
84
+
85
+ elif bt == BlockType.CODE:
86
+ source = block.attrs.get("source") or _plain_text(block.text)
87
+ code_lines = source.split("\n") if source else [""]
88
+ block.height = len(code_lines) + 2
89
+
90
+ elif bt == BlockType.COLUMNS:
91
+ block.height = max((c.height or 0 for c in block.children), default=0)
92
+
93
+ elif bt in (BlockType.COL, BlockType.DOCUMENT, BlockType.LIST):
94
+ block.height = sum(c.height or 0 for c in block.children)
95
+
96
+ elif bt == BlockType.LIST_ITEM:
97
+ text_height = 0
98
+ if block.text:
99
+ text_lines = wrap_text(_plain_text(block.text), max(width - 2, 1))
100
+ text_height = len(text_lines)
101
+ block.height = text_height + sum(c.height or 0 for c in block.children)
102
+
103
+ elif bt == BlockType.TREE:
104
+ source = block.attrs.get("source", "")
105
+ block.height = len(source.split("\n")) if source else 1
106
+
107
+ elif bt == BlockType.MERMAID:
108
+ source = block.attrs.get("source", "") or _plain_text(block.text)
109
+ rendered = source # fallback
110
+ try:
111
+ result = subprocess.run(
112
+ ["mermaid-ascii", "-f", "-"],
113
+ input=source,
114
+ capture_output=True,
115
+ text=True,
116
+ check=True,
117
+ timeout=30,
118
+ )
119
+ rendered = result.stdout
120
+ except (subprocess.CalledProcessError, FileNotFoundError, subprocess.TimeoutExpired):
121
+ pass
122
+ block.attrs["_rendered"] = rendered
123
+ block.height = len(rendered.split("\n")) if rendered else 1
124
+
125
+ elif bt == BlockType.QUOTE:
126
+ block.height = sum(c.height or 0 for c in block.children) + (1 if block.attrs.get("by") else 0)
127
+
128
+ else:
129
+ block.height = sum(c.height or 0 for c in block.children)
130
+
131
+
132
+ def layout(doc: Block, width: int) -> None:
133
+ resolve_width(doc, width)
134
+ resolve_height(doc)