Skip to main content

Compare text, and produce human-readable differences or deltas which look like track changes in Microsoft Word.

Project description

Redlines

Repository banner image

Redlines produces a Markdown text showing the differences between two strings/text. The changes are represented with strike-throughs and underlines, which looks similar to Microsoft Word's track changes. This method of showing changes is more familiar to lawyers and is more compact for long series of characters.

Redlines uses SequenceMatcher to find differences between words used.

Example

Given an original string:

The quick brown fox jumps over the lazy dog.

And the string to be tested with:

The quick brown fox walks past the lazy dog.

The library gives a result of:

The quick brown fox <del>jumps over </del><ins>walks past </ins>the lazy dog.

Which is rendered like this:

The quick brown fox jumps over walks past the lazy dog.

Install

pip install redlines

Usage

The library contains one class: Redlines, which is used to compare text.

from redlines import Redlines

test = Redlines(
    "The quick brown fox jumps over the lazy dog.",
    "The quick brown fox walks past the lazy dog.",
)
assert (
        test.output_markdown
        == "The quick brown fox <del>jumps over </del><ins>walks past </ins>the lazy dog."
)

Alternatively, you can create Redline with the text to be tested, and compare several times to see the results.

from redlines import Redlines

test = Redlines("The quick brown fox jumps over the lazy dog.")
assert (
        test.compare("The quick brown fox walks past the lazy dog.")
        == "The quick brown fox <del>jumps over </del><ins>walks past </ins>the lazy dog."
)

assert (
        test.compare("The quick brown fox jumps over the dog.")
        == "The quick brown fox jumps over the <del>lazy </del>dog."
)

Redlines also features a simple command line tool redlines to visualise the differences in text in the terminal.

 Usage: redlines text [OPTIONS] SOURCE TEST                                                                                                                                                                                                   
                                                                                                                                                                                                                                              
 Compares the strings SOURCE and TEST and produce a redline in the terminal. 

Custom styling in markdown

By default, markdown output is styled in "red_green", like the following:

"The quick brown fox jumps over walks past the lazy dog."

Set the markdown_style option in the constructor or compare function to change the styling. The available styles are "red"" and "none".

You can also use css classes to provide custom styling by setting markdown_style as "custom_css". Insertions and deletions are now styled using the "redline-inserted" and "redline-deleted" CSS classes. You can also set your own CSS classes by specifying the name of the CSS class in the options "ins_class" and "del_class" respectively in the constructor or compare function.

Uses

Roadmap / Contributing

Please feel free to post issues and comments. I work on this in my free time, so please excuse lack of activity.

Nice things to do

  • Style the way changes are presented
  • Other than Markdown, have other output formats (HTML? PDF?)
  • Associate changes with an author
  • Show different changes by different authors or times.

If this was useful to you, please feel free to contact me!

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redlines-0.4.0.tar.gz (6.3 kB view hashes)

Uploaded Source

Built Distribution

redlines-0.4.0-py3-none-any.whl (7.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page