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".

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

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.

Redlines does not seem to work under certain markdown environments, eg. Streamlit or GitHub

Styling may not appear in markdown environments which disallow HTML. There is no consistent support for strikethroughs and colors in the markdown standard, and styling is largely accomplished through raw HTML. If you are using GitHub or Streamlit, you may not get the formatting you expect or see any change at all.

Try these options:

  • Enable use of HTML. In Streamlit, you need to set the unsafe_allow_html argument in st.write or st.markdown to True.
  • Set markdown_style as "ghfm": the GitHub Flavoured Markdown provides support for strikethrough (but no colours or underline)
  • Set markdown_style as "none": some environments only support a subset of HTML tags in markdown (for eg. GitHub). This might include the ins and del HTML tags used by this style. Styling usually follows that of the browser.

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.

For questions and discussions, you can join my matrix room on matrix.esq.social. Matrix is a federated communication platform similar to Discord.

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.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

redlines-0.4.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file redlines-0.4.2.tar.gz.

File metadata

  • Download URL: redlines-0.4.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for redlines-0.4.2.tar.gz
Algorithm Hash digest
SHA256 e6bc0411fc52d8095447029ba18cd194d36d251ba1c2587480ba14669e6bebaa
MD5 9cedbb72c34cf4b9eeb12e06df6d9877
BLAKE2b-256 a12bb99cc9898b89a55a7e41f10892eb6cd08c6a6b044f5382ba98d68dc4be06

See more details on using hashes here.

File details

Details for the file redlines-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: redlines-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for redlines-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f0fc90025ea138232c20d7444b4490fc48c17e822097755f0fc281bd07f9ee63
MD5 12e93ca14811e035bced444f8927d06c
BLAKE2b-256 efff6b9a9a40e1f1839ce1687527e5e7559d7eb4b28747d5263ac61fa9fa2d13

See more details on using hashes here.

Supported by

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