Skip to content

Conversation

@dereuromark
Copy link
Contributor

@dereuromark dereuromark commented Dec 2, 2025

Summary

Adds a new AnsiRenderer that outputs ANSI-formatted text for terminal display. This is inspired by tjot, a terminal renderer for Djot.

Features

  • Colored headings - Different colors per level, h1/h2 get underlines
  • Text styling - Bold, italic, underline, strikethrough via ANSI codes
  • Colored inline code - Yellow highlighting
  • Unicode tables - Box-drawing characters (┌─┬─┐ etc.)
  • Blockquote bars - Colored │ prefix
  • List bullets - • for unordered, numbers for ordered
  • Task checkboxes - ☑ (checked) / ☐ (unchecked)
  • Super/subscript - Unicode characters (² ₂)
  • Symbols to emoji - ❤️ → ❤
  • Configurable - Colors, Unicode, terminal width

Use Cases

  • CLI documentation viewers
  • Terminal-based Djot editors/previewers
  • Developer tooling (show formatted help)
  • README previews in terminal

Example Usage

use Djot\DjotConverter;
use Djot\Renderer\AnsiRenderer;

$converter = new DjotConverter();
$renderer = new AnsiRenderer();

$document = $converter->parse($djotContent);
echo $renderer->render($document);

Example Output

Run php examples/ansi-demo.php to see:

p1 p2
Text preview (without colors)
Welcome to Djot
═══════════════

Djot is a lightweight markup language with emphasis, strong, and inline code.

Features
────────

• Highlighted text for important notes
• Inserted and deleted text
• Superscript: E=mc²
• Subscript: H₂O
• Symbols: I ❤ Djot!

Tables
──────

┌──────────────┬──────────┬──────┐
│ Feature      │ Markdown │ Djot │
├──────────────┼──────────┼──────┤
│ Highlighting │ No       │ Yes  │
│ Attributes   │ No       │ Yes  │
└──────────────┴──────────┴──────┘

Configuration

$renderer = new AnsiRenderer(
    terminalWidth: 120,  // For line wrapping
    useColors: true,     // ANSI color codes
    useUnicode: true,    // Unicode bullets/boxes
);

// Or fluent:
$renderer
    ->setTerminalWidth(80)
    ->setUseColors(false)  // Plain text fallback
    ->setUseUnicode(false); // ASCII fallback

@codecov
Copy link

codecov bot commented Dec 2, 2025

Codecov Report

❌ Patch coverage is 86.21701% with 47 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.84%. Comparing base (ec22f0f) to head (142ff6c).

Files with missing lines Patch % Lines
src/Renderer/AnsiRenderer.php 86.21% 47 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master      #13      +/-   ##
============================================
- Coverage     93.35%   92.84%   -0.51%     
- Complexity     1631     1789     +158     
============================================
  Files            59       60       +1     
  Lines          4258     4599     +341     
============================================
+ Hits           3975     4270     +295     
- Misses          283      329      +46     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Adds a new renderer that outputs ANSI-formatted text for terminal display.

Features:
- Colored headings (different colors per level, underlined h1/h2)
- Bold, italic, underline, strikethrough styling
- Colored inline code
- Unicode box-drawing for tables
- Blockquote bars with color
- Bullet points (• or * fallback)
- Task list checkboxes (☑/☐)
- Super/subscript using Unicode characters
- Symbol to emoji mapping (:heart: → ❤)
- Configurable: colors, Unicode, terminal width

Use cases:
- CLI documentation viewers
- Terminal-based Djot editors
- Developer tooling
- README previews in terminal
@dereuromark dereuromark force-pushed the feature/ansi-renderer branch from 294e8a1 to 77cb6ef Compare December 2, 2025 18:31
@dereuromark dereuromark added the enhancement New feature or request label Dec 2, 2025
@dereuromark dereuromark marked this pull request as ready for review December 2, 2025 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants