Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
*.py[cod]
*.sw[op]

# C extensions
*.so

# Packages
*.egg
*.egg-info
dist
build
_build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
lib
lib64
__pycache__
.mypy_cache

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox
nosetests.xml
htmlcov
.cache
venv
32 changes: 18 additions & 14 deletions bin/rst2ansi
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
#!/usr/bin/env python

import sys
from rst2ansi import rst2ansi
import argparse
import io
import sys

from rst2ansi import rst2ansi

parser = argparse.ArgumentParser(description='Prints a reStructuredText input in an ansi-decorated format suitable for console output.')
parser.add_argument('file', type=str, nargs='?', help='A path to the file to open')
parser = argparse.ArgumentParser(
description="Prints a reStructuredText input in an ansi-decorated format suitable for console output."
)
parser.add_argument("file", type=str, nargs="?", help="A path to the file to open")

args = parser.parse_args()


def process_file(f):
out = rst2ansi(f.read())
if out:
try:
print(out)
except UnicodeEncodeError:
print(out.encode('ascii', errors='backslashreplace').decode('ascii'))
out = rst2ansi(f.read())
if out:
try:
print(out)
except UnicodeEncodeError:
print(out.encode("ascii", errors="backslashreplace").decode("ascii"))


if args.file:
with io.open(args.file, 'rb') as f:
process_file(f)
with io.open(args.file, "rb") as f:
process_file(f)
else:
process_file(sys.stdin)

process_file(sys.stdin)
40 changes: 21 additions & 19 deletions rst2ansi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
The MIT License (MIT)

Expand All @@ -23,30 +22,33 @@
THE SOFTWARE.
"""

from __future__ import unicode_literals

from docutils import nodes, core
from docutils.parsers.rst import roles

from .visitor import Writer
from docutils import core, nodes

from .ansi import COLORS, STYLES
from .visitor import Writer

def rst2ansi(input_string, output_encoding='utf-8'):

overrides = {}
overrides['input_encoding'] = 'unicode'
def rst2ansi(input_string, output_encoding="utf-8"):
overrides = {}
overrides["input_encoding"] = "unicode"

def style_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
return [nodes.TextElement(rawtext, text, classes=[name])], []
def style_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
return [nodes.TextElement(rawtext, text, classes=[name])], []

for color in COLORS:
roles.register_local_role('ansi-fg-' + color, style_role)
roles.register_local_role('ansi-bg-' + color, style_role)
for style in STYLES:
roles.register_local_role('ansi-' + style, style_role)
for color in COLORS:
roles.register_local_role(f"ansi-fg-{color}", style_role)
roles.register_local_role(f"ansi-bg-{color}", style_role)
for style in STYLES:
roles.register_local_role(f"ansi-{style}", style_role)

if hasattr(input_string, 'decode'):
input_string = input_string.decode('utf-8')
if hasattr(input_string, "decode"):
input_string = input_string.decode("utf-8")

out = core.publish_string(input_string, settings_overrides=overrides, writer=Writer(unicode=output_encoding.startswith('utf')))
return out.decode(output_encoding)
out = core.publish_string(
input_string,
settings_overrides=overrides,
writer=Writer(unicode=output_encoding.startswith("utf")),
)
return out.decode(output_encoding)
Loading