1""" 2 python-creole commandline interface 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 5 :copyleft: 2013-2020 by the python-creole team, see AUTHORS for more details. 6 :license: GNU GPL v3 or above, see LICENSE for more details. 7""" 8 9 10import argparse 11import codecs 12 13from creole import VERSION_STRING, creole2html, html2creole, html2rest, html2textile 14 15 16class CreoleCLI: 17 def __init__(self, convert_func): 18 self.convert_func = convert_func 19 self.parser = argparse.ArgumentParser( 20 description=( 21 "python-creole is an open-source (GPL) markup converter" 22 " in pure Python for:" 23 " creole2html, html2creole, html2ReSt, html2textile" 24 ), 25 ) 26 self.parser.add_argument( 27 '--version', action='version', 28 version='%%(prog)s from python-creole v%s' % VERSION_STRING # noqa flynt 29 ) 30 self.parser.add_argument("sourcefile", help="source file to convert") 31 self.parser.add_argument("destination", help="Output filename") 32 self.parser.add_argument("--encoding", 33 default="utf-8", 34 help="Codec for read/write file (default encoding: utf-8)" 35 ) 36 37 args = self.parser.parse_args() 38 39 sourcefile = args.sourcefile 40 destination = args.destination 41 encoding = args.encoding 42 43 self.convert(sourcefile, destination, encoding) 44 45 def convert(self, sourcefile, destination, encoding): 46 print(f"Convert {sourcefile!r} to {destination!r} with {self.convert_func.__name__} (codec: {encoding})") 47 48 with codecs.open(sourcefile, "r", encoding=encoding) as infile: 49 with codecs.open(destination, "w", encoding=encoding) as outfile: 50 content = infile.read() 51 converted = self.convert_func(content) 52 outfile.write(converted) 53 print(f"done. {destination!r} created.") 54 55 56def cli_creole2html(): 57 CreoleCLI(creole2html) 58 59 60def cli_html2creole(): 61 CreoleCLI(html2creole) 62 63 64def cli_html2rest(): 65 CreoleCLI(html2rest) 66 67 68def cli_html2textile(): 69 CreoleCLI(html2textile) 70 71 72if __name__ == "__main__": 73 import sys 74 sys.argv += ["../README.creole", "../test.html"] 75 print(sys.argv) 76 cli_creole2html() 77