Convert text with ANSI color codes to HTML or to LaTeX.

Overview
Build Status - main branch

ansi2html

Author: Ralph Bean <[email protected]>
Contributor: Robin Schneider <[email protected]>

Convert text with ANSI color codes to HTML or to LaTeX.

Inspired by and developed off of the work of pixelbeat and blackjack.

Read the docs for more informations.

Example - Python API

>> html = conv.convert(ansi)">
>>> from ansi2html import Ansi2HTMLConverter
>>> conv = Ansi2HTMLConverter()
>>> ansi = "".join(sys.stdin.readlines())
>>> html = conv.convert(ansi)

Example - Shell Usage

$ ls --color=always | ansi2html > directories.html
$ sudo tail /var/log/messages | ccze -A | ansi2html > logs.html
$ task rc._forcecolor:yes limit:0 burndown | ansi2html > burndown.html

See the list of full options with:

$ ansi2html --help

Get this project:

$ pip3 install ansi2html

Source: https://github.com/pycontribs/ansi2html/

pypi: https://pypi.org/project/ansi2html/

License

ansi2html is licensed LGPLv3+.

Comments
  • Fixes to respect bright colors in palette

    Fixes to respect bright colors in palette

    Fixes #133

    It seems there are two errors in the existing logic in style.py:

    1. When a palette defines the upper 8 colors (the "bright" colors), the existing code overwrites them anyway using the results of intensify.
    2. When a palette does not define the upper 8 colors, the existing code does not define replacements for them in the 256-color palette rule families.

    In other words, the code was overwriting some of the explicitly defined colors, but also failing to provide automatically brightened colors when it could have :-)

    Note that with this fix in place, the existing test suite doesn't "pass" because the output colors will be different. For some users, this may be an unexpected change in behavior. In that case, maybe the old behavior should be preserved as a default while the new behavior is toggled optionally.

    The code could be optimized to not make so many redundant calls to intensify, but I haven't bothered to make that change here.

    bug 
    opened by echuber2 41
  • gpl license prevents direct usage in non-gpl python tools

    gpl license prevents direct usage in non-gpl python tools

    hi,

    pytest-dev/pytest-html#96 has just brought this to my attention, as things are pytest-html would have to drop usage and/or implement an sub-process communication to keep using ansi2html while also keeping its own license terms

    i would like to solve this by just version-bumping the ansi2html dependency but i can understand if you want to keep the gpl

    opened by RonnyPfannschmidt 40
  • missing header.mak when easy_installed on Python 2.6

    missing header.mak when easy_installed on Python 2.6

    It seems header.mak is not being included when ansi2html is installed via easy_install on Python 2.6, which results in:

      File "/Library/Python/2.6/site-packages/tw2.core-2.0b4-py2.6.egg/tw2/core/dottedtemplatelookup.py", line 103, in __load
        self.template_cache[template_name] = Template(open(filename).read(),
    IOError: [Errno 2] No such file or directory: u'/Users/ana/.python-eggs/ansi2html-0.6.0-py2.6.egg-tmp/ansi2html/templates/header.mak'
    

    When I look in the directory it's trying to search:

    $ tree /Users/ana/.python-eggs/ansi2html-0.6.0-py2.6.egg-tmp/ansi2html/
    /Users/ana/.python-eggs/ansi2html-0.6.0-py2.6.egg-tmp/ansi2html/
    └── templates
        └── full.mak
    

    Installing via pip or from source seems to fix this, and the setup.py and MANIFEST.in files look fine to me, so this is probably more of an easy_install issue, but maybe releasing an egg for 2.6 instead of source would be a simple workaround? Or maybe explicitly listing the template files instead of a wildcard?

    Note this was reported to me here: http://discuss.dexy.it/index.php?p=/discussion/13/ioerror-errno-2-no-such-file-or-directory-...-header.mak

    And I have just come across this issue myself on a new machine (OSX 10.6.7) running Python 2.6.1

    opened by ananelson 13
  • Add type hinting

    Add type hinting

    Adds some basic type hinting.

    mypy ansi2html/style.py is happy and mypy ansi2html/converter.py spits out a few errors I don't know how to fix.

    ansi2html/converter.py:38: error: Cannot find implementation or library stub for module named "ordereddict"
    ansi2html/converter.py:38: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
    ansi2html/converter.py:38: error: Name "OrderedDict" already defined (possibly by an import)
    ansi2html/converter.py:559: error: Incompatible return value type (got "Union[bool, str, Set[str]]", expected "str")
    ansi2html/converter.py:567: error: Unsupported right operand type for in ("Union[bool, str, Set[str]]")
    ansi2html/converter.py:726: error: "TextIO" has no attribute "detach"
    Found 5 errors in 1 file (checked 1 source file)
    

    Fixes: #140

    bug 
    opened by ziegenberg 10
  • HTML and URI in source text

    HTML and URI in source text

    This is not an issue, but a feature:

    Another cool feature would be to make it do any of two things:

    --linkify : where it sees ://, make hrefs out of them.

    --has-html: where it sees html tags, render rather than escape them.

    Not a big deal, but might be cool for some ;)

    Nima

    opened by nima 9
  • fix including of CHANGELOG.rst

    fix including of CHANGELOG.rst

    Currently building the documentation does not include the changelog. It fails with the following error:

    Running Sphinx v4.2.0
    building [mo]: all of 0 po files
    building [html]: all source files
    updating environment: [new config] 3 added, 0 changed, 0 removed
    reading sources... [ 33%] api
    reading sources... [ 66%] changelog
    reading sources... [100%] index
    
    /home/daniel/Development/github/ansi2html/docs/source/changelog.rst:4: WARNING: Problems with "include" directive path:
    InputError: [Errno 2] No such file or directory: 'changelog.rst'.
    

    As can be seen here the changelog in the documentation is empty. The documentation built by this CI run does include the recent changelog.

    bug docs 
    opened by ziegenberg 8
  • --partial strips newlines from output

    --partial strips newlines from output

    (virtenv) [email protected] ~ws/repos/pkg/testpack % ls -la | ansi2html-3.2 -p
    total 44 drwxr-xr-x  4 mocksoul users  4096 Aug  9 17:28 . drwxr-xr-x 11 mocksoul users  4096 Aug  9 16:50 .. drwxr-xr-x  6 mocksoul users  4096 Aug  8 19:47 build -rw-r--r--  1 mocksoul users  2219 Aug  1 00:34 cat -rw-r--r--  1 mocksoul users   872 Aug  9 17:13 PKGBUILD drwxr-xr-x  2 mocksoul users  4096 Jul 26 17:39 __pycache__ -rw-r--r--  1 mocksoul users 18084 Aug  9 18:01 res.html %```
    
    ansi2html eat my lines! =)
    
    This is expected output:
    
    ```bash
    
    (virtenv) [email protected] ~ws/repos/pkg/testpack % ls -la --color=always | ansi2html-2.7 -p
    total 44
    drwxr-xr-x  4 mocksoul users  4096 Aug  9 17:28 <span class="ansi1 ansi34">.</span>
    drwxr-xr-x 11 mocksoul users  4096 Aug  9 16:50 <span class="ansi1 ansi34">..</span>
    drwxr-xr-x  6 mocksoul users  4096 Aug  8 19:47 <span class="ansi1 ansi34">build</span>
    -rw-r--r--  1 mocksoul users  2219 Aug  1 00:34 cat
    -rw-r--r--  1 mocksoul users   872 Aug  9 17:13 PKGBUILD
    drwxr-xr-x  2 mocksoul users  4096 Jul 26 17:39 <span class="ansi1 ansi34">__pycache__</span>
    -rw-r--r--  1 mocksoul users 18084 Aug  9 18:01 res.html```
    
    opened by mocksoul 8
  • Color mix

    Color mix

    Hi! I use a buildbot running ansi2html with lettuce's default output to serve some test on a website and I noticed a swap between body_foreground & ansi30 css' colors (#000 and #AAA respectively here).

    Also, since I upgraded of 0.6.x (if I remember correctly) to latest release, I had to patch the code to add an encode('utf-8') to the final print statement to make the program run without "ansi" decode error.

    I am running python2.6 and the content of my environment is:

    USER=fab31
    LOGNAME=fab31
    HOME=/home/fab31
    PATH=/home/fab31/.pythonbrew/bin:/home/fab31/pyenv/bbot/bin:/home/fab31/utils:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/home/fab31/.local/bin:/sbin:/usr/sbin:/usr/local/bin:/opt/java/jre/bin
    MAIL=/var/mail/fab31
    SHELL=/bin/zsh
    SSH_CLIENT=79.84.236.37 53268 22
    SSH_CONNECTION=79.84.236.37 53268 88.190.18.171 22
    SSH_TTY=/dev/pts/4
    TERM=xterm
    LANG=fr_FR.UTF-8
    SHLVL=1
    PWD=/home/fab31/pyenv/bbot/master
    OLDPWD=/home/fab31/pyenv/bbot/slave
    G_BROKEN_FILENAMES=1
    LC_NUMERIC=C
    CLICOLOR=1
    SVN_EDITOR=~/utils/svn_editor.sh
    GDK_USE_XFT=1
    PAGER=less
    X2=x2x -west -to :0 -west 
    VISUAL=vim
    EDITOR=vim
    PYTHONSTARTUP=/home/fab31/.pystartup
    HG_REPOS=/home/fab31/prog/hg-stable
    PS1=(bbot)%n@%m:%~%# 
    LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.bz2=01;31:*.rpm=01;31:*.deb=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.mpg=01;37:*.avi=01;37:*.mov=01;37:*.tbz=01;31:*.zip=01;31:*.mp3=00;34:*.png=01;35:*.89t=00;33
    VIRTUALENVWRAPPER_VIRTUALENV_ARGS=--no-site-packages
    WORKON_HOME=/home/fab31/pyenv
    VIRTUALENVWRAPPER_LOG_DIR=/home/fab31/pyenv
    PROJECT_HOME=/home/fab31/pyenv
    PIP_VIRTUALENV_BASE=/home/fab31/pyenv
    PIP_RESPECT_VIRTUALENV=true
    VIRTUAL_ENV=/home/fab31/pyenv/bbot
    _=/usr/bin/env
    
    
    opened by fdev31 8
  • Fix --input-encoding=<encoding> regression added in PR #143

    Fix --input-encoding= regression added in PR #143

    While adding type-hinting the option to specify an input encoding got ignored. This commit fixes this regression.

    This commit also fixes the tests which call ansi2html as a command. As the pytest documentation states, during test execution stdin is set to a “null” object which will fail on attempts to read from it because it is rarely desired to wait for interactive input when running automated tests. So we also patch now sys.stdin using an io.TextIOWrapper and wrapping any actual input in a io.BytesIO.

    bug 
    opened by ziegenberg 7
  • Add API documentation generation as part of the build worflow

    Add API documentation generation as part of the build worflow

    Hi,

    This PR adds API documentation with pydoctor.

    This avoids having to skim through the code to find the right arguments to the convert function for instance.

    enhancement 
    opened by tristanlatr 7
  • Does not support 24-bit

    Does not support 24-bit "truecolor" escape sequences.

    A good reference for these escape sequences can be found here: https://gist.github.com/XVilka/8346728

    I took a quick glance at the Ansi2HTMLConverter, and it looks like the regex you're using to parse the escape sequences is robust enough to do the business, but I haven't gone any further than that.

    --Jays

    opened by jaysonlarose 7
  • Copyright headers still refer to GPL

    Copyright headers still refer to GPL

    In #72 you all got consensus to relicense this project under the LGPL v3, but the headers in the source code weren't updated. Could you update them and release a new package please, to resolve any ambiguity?

    bug 
    opened by leifwalsh 2
  • [>=1.7.0a0] typing_extensions missing from requirements

    [>=1.7.0a0] typing_extensions missing from requirements

    Hi! I would like to use ansi2html on a machine running Python 3.7. However, I get the following crash:

    Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import ansi2html
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Program Files\Python37\lib\site-packages\ansi2html\__init__.py", line 1, in <module>
        from ansi2html.converter import Ansi2HTMLConverter
      File "C:\Program Files\Python37\lib\site-packages\ansi2html\converter.py", line 45, in <module>
        from typing_extensions import TypedDict
    ModuleNotFoundError: No module named 'typing_extensions'
    

    I guess the package is missing the dependency, for now I installed it manually. I don't have any experience regarding package setups, so I won't try to submit a fix, but at least wanted to mention it :)

    bug 
    opened by nikluep-3ds 1
  • Support white background (currently always #AAAAAA gray)

    Support white background (currently always #AAAAAA gray)

    Hi. Browsing through style.py seems to indicate there's currently no way to override #AAAAAA with plain old #FFFFFF. Am I missing something, or is this a feature request?

    feature 
    opened by lkraav 4
  • ❤️‍🩹 Looking for maintainers!

    ❤️‍🩹 Looking for maintainers!

    This project could really benefit from some love. I am looking for others that are willing to help maintaining it as my time is very limited these days.

    Please state your interest here, I am more than happy to add new contributors to the project.

    opened by ssbarnea 17
  • Crash for `--latex` and italic `^[[3m`

    Crash for `--latex` and italic `^[[3m`

    Using this example file: italic.txt

    $ cat italic.txt | ansi2html --latex --inline
    

    Crashes with this traceback:

    Traceback (most recent call last):
      File "/usr/bin/ansi2html", line 33, in <module>
        sys.exit(load_entry_point('ansi2html==1.6.0', 'console_scripts', 'ansi2html')())
      File "/usr/lib/python3.9/site-packages/ansi2html/converter.py", line 696, in main
        output = conv.convert(
      File "/usr/lib/python3.9/site-packages/ansi2html/converter.py", line 507, in convert
        attrs = self.prepare(ansi, ensure_trailing_newline=ensure_trailing_newline)
      File "/usr/lib/python3.9/site-packages/ansi2html/converter.py", line 485, in prepare
        body, styles = self.apply_regex(ansi)
      File "/usr/lib/python3.9/site-packages/ansi2html/converter.py", line 300, in apply_regex
        parts = self._collapse_cursor(parts)
      File "/usr/lib/python3.9/site-packages/ansi2html/converter.py", line 461, in _collapse_cursor
        for part in parts:
      File "/usr/lib/python3.9/site-packages/ansi2html/converter.py", line 434, in _apply_regex
        yield "\\textcolor[HTML]{%s}{" % style[0]
    IndexError: list index out of range
    
    bug 
    opened by sedrubal 0
Releases(1.8.0)
  • 1.8.0(Jul 7, 2022)

    Minor Changes

    • style.py: Drop unused CSS class .bold (#161) @hartwork
    • Bring back putting a rendered man page into release archives (fixes #124) (#173) @hartwork
    • Migrate from mock to unittest.mock of Python >=3.3 (fixes #169) (#171) @hartwork

    Bugfixes

    • Fix for ANSI color codes that include blank values (#178) @jaysonlarose
    • Added py.typed file (#176) @abrahammurciano
    • style.py: Get CSS class .inv_foreground in sync with .body_foreground (#160) @hartwork
    • Fix --input-encoding= regression added in PR #143 + related tests (alternative to PR #162) (#172) @hartwork
    • Add ability to also recognize colons in ANSI escapes (#167) @jaysonlarose
    • Fixes to respect bright colors in palette (#126) @echuber2
    • CHANGELOG.rst: Fix misleading 1.6.0 entry and missing 1.7.0 entries (#159) @hartwork

    Kudos goes to: @abrahammurciano, @echuber2, @hartwork, @jaysonlarose, @pre-commit-ci and @pre-commit-ci[bot]

    Source code(tar.gz)
    Source code(zip)
  • 1.7.0(Jan 31, 2022)

    Minor Changes

    • Replace recommendation of using system packages with pip3 (#129) @ssbarnea
    • Add truecolor support (#155) @miltolstoy
    • Generate documentation with Sphinx and use Read the Docs (#141) @tristanlatr
    • Adds support for OSC hyperlink sequences. (#131) @hakonhagland

    Bugfixes

    • Add test coverage (#148) @ziegenberg
    • Replace pkg_resources with importlib.metadata (#144) @ziegenberg
    • fix including of CHANGELOG.rst (#151) @ziegenberg
    • Update linting dependencies (#147) @ziegenberg
    • Upgrade the build process (#145) @ziegenberg
    • Add type hinting (#143) @ziegenberg
    • Update CI badges in readme (#142) @ssbarnea
    • Bump setuptools-scm version (#138) @ssbarnea

    Kudos goes to: @hakonhagland, @miltolstoy, @pre-commit-ci, @pre-commit-ci[bot], @ssbarnea, @tristanlatr and @ziegenberg

    Source code(tar.gz)
    Source code(zip)
  • 1.6.0(Oct 15, 2020)

    Changes

    • Switching from nosetest to unittest (#103) @paolostivanin
    • Add dracula colorscheme (#106) @ahmubashshir
    • Refactor packaging by replacing old setup.py based packaging with modern pep517 based one (#112) @ssbarnea
    • Replace travis with github actions (#121) @ssbarnea

    Bugfixes

    • Correct author metadata (#122) @ssbarnea

    Deprecations

    • Officialy retire support for python <= 3.5 (#112) @ssbarnea

    Kudos goes to: @ssbarnea, @paolostivanin, @ahmubashshir

    Source code(tar.gz)
    Source code(zip)
Owner
PyContribs
PyContribs main purpose is to assure that different Python-related projects remain maintained.
PyContribs
GitHub Actions Version Updater Updates All GitHub Action Versions in a Repository and Creates a Pull Request with the Changes.

GitHub Actions Version Updater GitHub Actions Version Updater is GitHub Action that is used to update other GitHub Actions in a Repository and create

Maksudul Haque 42 Dec 22, 2022
Install packages with pip as if you were in the past!

A PyPI time machine Do you wish you could just install packages with pip as if you were at some fixed date in the past? If so, the PyPI time machine i

Thomas Robitaille 51 Jan 09, 2023
A module to prevent invites and joins to Matrix rooms by checking the involved server(s)' domain.

Synapse Domain Rule Checker A module to prevent invites and joins to Matrix rooms by checking the involved server(s)' domain. Installation From the vi

matrix.org 4 Oct 24, 2022
General tricks that may help you find bad, or noisy, labels in your dataset

doubtlab A lab for bad labels. Warning still in progress. This repository contains general tricks that may help you find bad, or noisy, labels in your

vincent d warmerdam 449 Dec 26, 2022
Is a polybar module that will show you your progress in Hack The Box

HTB-Status for Polybar Is a polybar module that will show you your progress in Hack The Box indicating your current rank, global rank, points and resp

bitc0de 8 Jan 14, 2022
Aevsploit İçin Destekde Bulun Papara: 1427113016

Aevsploit İçin Destekde Bulun Papara: 1427113016 Toolu Geliştirmek İçin Fikirlerinizi Bekliyorum Telegram

9 Jun 07, 2022
PORTSCANNING-IN-PYTHON - A python threaded portscanner to scan websites and ipaddresses

PORTSCANNING-IN-PYTHON This is a python threaded portscanner to scan websites an

1 Feb 16, 2022
Automator anble you to create automations on your system

WELCOME TO AUTOMATOR BETA This programm is able to create automations on your system. This programm is only an experimantal release; infact it works v

Davide 1 Jan 12, 2022
1st Online Python Editor With Live Syntax Checking and Execution

PythonBuddy 🖊️ 🐍 Online Python 3 Programming with Live Pylint Syntax Checking! Usage Fetch from repo: git clone https://github.com/ethanchewy/Python

Ethan Chiu 255 Dec 23, 2022
fast_bss_eval is a fast implementation of the bss_eval metrics for the evaluation of blind source separation.

fast_bss_eval Do you have a zillion BSS audio files to process and it is taking days ? Is your simulation never ending ? Fear no more! fast_bss_eval i

Robin Scheibler 99 Dec 13, 2022
Lightweight library for accessing data and configuration

accsr This lightweight library contains utilities for managing, loading, uploading, opening and generally wrangling data and configurations. It was ba

appliedAI Initiative 7 Mar 09, 2022
京东热爱狂欢趴&京东扫码获取cookie

京东热爱狂欢趴 一键完成任务脚本来袭 活动地址: https://wbbny.m.jd.com/babelDiy/Zeus/2s7hhSTbhMgxpGoa9JDnbDzJTaBB/index.html#/home 2021-06-02更新: 1、删除京东星推官 2、更新脚本,修复火爆问题 2021

xoyi 48 Dec 28, 2022
jmespath.rs Python binding

rjmespath-py jmespath.rs Python binding.

messense 3 Dec 14, 2022
🏆 A ranked list of awesome Python open-source libraries and tools. Updated weekly.

Best-of Python 🏆 A ranked list of awesome Python open-source libraries & tools. Updated weekly. This curated list contains 230 awesome open-source pr

Machine Learning Tooling 2.7k Jan 03, 2023
Python library and cli util for https://www.zerochan.net/

Zerochan Library for Zerochan.net with pics parsing and downloader included! Features CLI utility for pics downloading from zerochan.net Library for c

kiriharu 10 Oct 11, 2022
This bot uploads telegram files to MixDrop.co,File.io.

What is about this bot ? This bot uploads telegram files to MixDrop.co, File.io. Usage: Send any file, and the bot will upload it to MixDrop.co, File.

Abhijith NT 3 Feb 26, 2022
This is a simple quizz which can ask user for login/register session, then consult to the Quiz interface.

SIMPLE-QUIZ- This is a simple quizz which can ask user for login/register session, then consult to the Quiz interface. By CHAKFI Ahmed MASTER SYSTEMES

CHAKFI Ahmed 1 Jan 10, 2022
take home quiz

guess the correlation data inspection a pretty normal distribution train/val/test split splitting amount .dataset: 150000 instances ├─8

HR Wu 1 Nov 04, 2021
Mnemosyne: efficient learning with powerful digital flash-cards.

Mnemosyne: Optimized Flashcards and Research Project Mnemosyne is: a free, open-source, spaced-repetition flashcard program that helps you learn as ef

359 Dec 24, 2022
A chain of stores wants a 3-month demand forecast for its 10 different stores and 50 different products.

Demand Forecasting Objective A chain store wants a machine learning project for a 3-month demand forecast for 10 different stores and 50 different pro

2 Jan 06, 2022