File: rst2ansi

package info (click to toggle)
python-rst2ansi 0.1.5-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 256 kB
  • sloc: python: 779; sh: 16; makefile: 16
file content (26 lines) | stat: -rwxr-xr-x 635 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/python3

import sys
from rst2ansi import rst2ansi
import argparse
import io

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'))

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