redlines

Redlines

Redlines compares two strings/text and shows their differences. The changes are represented with strike-throughs and highlights, similar to Microsoft Word's track changes.

The output can be in JSON, Markdown, HTML, or rich format. JSON is the default for CLI and automation use.

Quick Start

from redlines import Redlines

# Create a Redlines object
test = Redlines(
    "The quick brown fox jumps over the lazy dog.",
    "The quick brown fox walks past the lazy dog.",
)

# Get markdown output with <del>/<ins> tags
print(test.compare(markdown_style="none"))
# Output: The quick brown fox <del>jumps over </del><ins>walks past </ins>the lazy dog.

# Get JSON output (structured data)
print(test.output_json())

# Get rich output (terminal display)
print(test.output_rich)

Installation

pip install redlines

# Optional: PDF file comparison
pip install redlines[pdf]

# Optional: Advanced sentence detection (Python 3.11+)
pip install redlines[nupunkt]

# Optional: Levenshtein distance statistics
pip install redlines[levenshtein]

For AI Agents & Automation

🤖 Using with AI coding agents?

See the Agent Integration Guide for:

  • JSON schema documentation
  • CLI automation patterns
  • Error handling cookbook
  • Performance guidelines
  • Runnable examples

Quick CLI usage:

# Outputs JSON by default
redlines "old text" "new text"

# Pretty-print JSON
redlines file1.txt file2.txt --pretty

# Other formats
redlines text "source" "test"       # Rich terminal display
redlines markdown file1.txt file2.txt  # Markdown output

API Documentation

  • Redlines - Main comparison class
  • Document - File input support
  • PDF - PDF file support (optional)
  • Processor - Custom tokenization
  • CLI - Command-line interface

The structured comparison (M2)

compare is the structured half of the library: it reads both documents into block trees, aligns them and returns a Comparison carrying both trees, the alignment and a flat change tree.

from redlines import compare

result = compare(source_markdown, test_markdown)
for change in result.changes:
    print(change.kind, change.test_address or change.source_address)
  • Comparison - compare, Comparison, ComparisonConfig
  • Changes - ChangeTree, Change, InlineOp
  • Alignment - AlignmentConfig and the passes it configures
  1"""
  2# Redlines
  3
  4`Redlines` compares two strings/text and shows their differences. The changes are represented with
  5strike-throughs and highlights, similar to Microsoft Word's track changes.
  6
  7The output can be in JSON, Markdown, HTML, or `rich` format. JSON is the default for CLI and automation use.
  8
  9## Quick Start
 10
 11```python
 12from redlines import Redlines
 13
 14# Create a Redlines object
 15test = Redlines(
 16    "The quick brown fox jumps over the lazy dog.",
 17    "The quick brown fox walks past the lazy dog.",
 18)
 19
 20# Get markdown output with <del>/<ins> tags
 21print(test.compare(markdown_style="none"))
 22# Output: The quick brown fox <del>jumps over </del><ins>walks past </ins>the lazy dog.
 23
 24# Get JSON output (structured data)
 25print(test.output_json())
 26
 27# Get rich output (terminal display)
 28print(test.output_rich)
 29```
 30
 31## Installation
 32
 33```bash
 34pip install redlines
 35
 36# Optional: PDF file comparison
 37pip install redlines[pdf]
 38
 39# Optional: Advanced sentence detection (Python 3.11+)
 40pip install redlines[nupunkt]
 41
 42# Optional: Levenshtein distance statistics
 43pip install redlines[levenshtein]
 44```
 45
 46## For AI Agents & Automation
 47
 48**🤖 Using with AI coding agents?**
 49
 50See the [Agent Integration Guide](https://github.com/houfu/redlines/blob/main/AGENT_GUIDE.md) for:
 51- JSON schema documentation
 52- CLI automation patterns
 53- Error handling cookbook
 54- Performance guidelines
 55- [Runnable examples](https://github.com/houfu/redlines/tree/main/examples)
 56
 57Quick CLI usage:
 58```bash
 59# Outputs JSON by default
 60redlines "old text" "new text"
 61
 62# Pretty-print JSON
 63redlines file1.txt file2.txt --pretty
 64
 65# Other formats
 66redlines text "source" "test"       # Rich terminal display
 67redlines markdown file1.txt file2.txt  # Markdown output
 68```
 69
 70## API Documentation
 71
 72* [Redlines](redlines/redlines.html) - Main comparison class
 73* [Document](redlines/document.html) - File input support
 74* [PDF](redlines/pdf.html) - PDF file support (optional)
 75* [Processor](redlines/processor.html) - Custom tokenization
 76* [CLI](redlines/cli.html) - Command-line interface
 77
 78## The structured comparison (M2)
 79
 80`compare` is the structured half of the library: it reads both documents into
 81block trees, aligns them and returns a `Comparison` carrying both trees, the
 82alignment and a flat change tree.
 83
 84```python
 85from redlines import compare
 86
 87result = compare(source_markdown, test_markdown)
 88for change in result.changes:
 89    print(change.kind, change.test_address or change.source_address)
 90```
 91
 92* [Comparison](redlines/comparison.html) - `compare`, `Comparison`, `ComparisonConfig`
 93* [Changes](redlines/changes.html) - `ChangeTree`, `Change`, `InlineOp`
 94* [Alignment](redlines/alignment.html) - `AlignmentConfig` and the passes it configures
 95
 96"""
 97
 98from .alignment import AlignmentConfig
 99from .changes import Change, ChangeTree, InlineOp
100from .comparison import *
101from .document import *
102from .enums import *
103from .pdf import *
104from .processor import *
105from .redlines import *