diff options
-rw-r--r-- | README.creole | 34 | ||||
-rw-r--r-- | creole/__init__.py | 2 | ||||
-rw-r--r-- | creole/cmdline.py | 81 | ||||
-rw-r--r-- | creole/tests/test_cli.py | 81 | ||||
-rwxr-xr-x | setup.py | 8 |
5 files changed, 204 insertions, 2 deletions
diff --git a/README.creole b/README.creole index 86cd2e2..50868ea 100644 --- a/README.creole +++ b/README.creole @@ -84,6 +84,36 @@ u'This is *textile __markup__*!' See also: [[http://github.com/jedie/python-creole/blob/master/demo.py]] += commandline interface = + +If you have python-creole installed, you will get these simple CLI scripts: +* creole2html +* html2creole +* html2rest +* html2textile + +Here the {{{--help}}} output from {{{html2creole}}}: +{{{ +$ html2creole --help +usage: html2creole [-h] [-v] [--encoding ENCODING] sourcefile destination + +python-creole is an open-source (GPL) markup converter in pure Python for: +creole2html, html2creole, html2ReSt, html2textile + +positional arguments: + sourcefile source file to convert + destination Output filename + +optional arguments: + -h, --help show this help message and exit + -v, --version show program's version number and exit + --encoding ENCODING Codec for read/write file (default encoding: utf-8) +}}} + +Example to convert a html file into a creole file: +{{{ +$ html2creole foobar.html foobar.creole +}}} = documentation = @@ -163,7 +193,9 @@ Note: In this case you must install **docutils**! See above. = history = -* v1.0.7 - 2013-08.07 +* v1.1.0 - 2013-10-28 +** NEW: Simple commandline interface added. +* v1.0.7 - 2013-08-07 ** Bugfix in 'clean reStructuredText html writer' if docutils => v0.11 used. ** Bugfix for PyPy 2.1 usage * v1.0.6 - 2012-10-15 diff --git a/creole/__init__.py b/creole/__init__.py index bde72a3..06d8304 100644 --- a/creole/__init__.py +++ b/creole/__init__.py @@ -20,7 +20,7 @@ from __future__ import division, absolute_import, print_function, unicode_literals -__version__ = (1, 0, 7) +__version__ = (1, 1, 0) __api__ = (1, 0) # Creole 1.0 spec - http://wikicreole.org/ diff --git a/creole/cmdline.py b/creole/cmdline.py new file mode 100644 index 0000000..0245078 --- /dev/null +++ b/creole/cmdline.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# coding: utf-8 + +""" + python-creole commandline interface + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyleft: 2013 by the python-creole team, see AUTHORS for more details. + :license: GNU GPL v3 or above, see LICENSE for more details. +""" + +from __future__ import division, absolute_import, print_function, unicode_literals +import argparse +import codecs + +from creole import creole2html, html2creole, html2rest, html2textile +from creole import VERSION_STRING + + +class CreoleCLI(object): + def __init__(self, convert_func): + self.convert_func = convert_func + self.parser = argparse.ArgumentParser( + description=( + "python-creole is an open-source (GPL) markup converter" + " in pure Python for:" + " creole2html, html2creole, html2ReSt, html2textile" + ), + version=VERSION_STRING, + ) + + self.parser.add_argument("sourcefile", help="source file to convert") + self.parser.add_argument("destination", help="Output filename") + self.parser.add_argument("--encoding", + default="utf-8", + help="Codec for read/write file (default encoding: utf-8)" + ) + + args = self.parser.parse_args() + + sourcefile = args.sourcefile + destination = args.destination + encoding = args.encoding + + self.convert(sourcefile, destination, encoding) + + def convert(self, sourcefile, destination, encoding): + print("Convert %r to %r with %s (codec: %s)" % ( + sourcefile, destination, self.convert_func.__name__, encoding + )) + + with codecs.open(sourcefile, "r", encoding=encoding) as infile: + with codecs.open(destination, "w", encoding=encoding) as outfile: + content = infile.read() + converted = self.convert_func(content) + outfile.write(converted) + print("done. %r created." % destination) + + +def cli_creole2html(): + cli = CreoleCLI(creole2html) +# cli.convert() + +def cli_html2creole(): + cli = CreoleCLI(html2creole) +# cli.convert() + +def cli_html2rest(): + cli = CreoleCLI(html2rest) +# cli.convert() + +def cli_html2textile(): + cli = CreoleCLI(html2textile) +# cli.convert() + + +if __name__ == "__main__": + import sys + sys.argv += ["../README.creole", "../test.html"] + print(sys.argv) + cli_creole2html() diff --git a/creole/tests/test_cli.py b/creole/tests/test_cli.py new file mode 100644 index 0000000..6febeff --- /dev/null +++ b/creole/tests/test_cli.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# coding: utf-8 + +""" + unittest for CLI + ~~~~~~~~~~~~~~~~ + + :copyleft: 2013 by python-creole team, see AUTHORS for more details. + :license: GNU GPL v3 or above, see LICENSE for more details. +""" + +from __future__ import division, absolute_import, print_function, unicode_literals + +import unittest +import sys +import tempfile + +from creole.tests.utils.base_unittest import BaseCreoleTest +from creole.cmdline import cli_creole2html, cli_html2creole, cli_html2rest, \ + cli_html2textile + + +class CreoleCLITests(BaseCreoleTest): + def setUp(self): + super(CreoleCLITests, self).setUp() + self._old_argv = sys.argv[:] + def tearDown(self): + super(CreoleCLITests, self).tearDown() + sys.argv = self._old_argv + + def _test_convert(self, source_content, dest_content, cli_func): + source_file = tempfile.NamedTemporaryFile() + sourcefilepath = source_file.name + source_file.write(source_content) + source_file.seek(0) + + dest_file = tempfile.NamedTemporaryFile() + destfilepath = dest_file.name + + sys.argv += [sourcefilepath, destfilepath] + cli_func() + + dest_file.seek(0) + result_content = dest_file.read() + +# print(dest_content) + self.assertEqual(result_content, dest_content) + + def test_creole2html(self): + self._test_convert( + source_content=b"= test creole2html =", + dest_content=b"<h1>test creole2html</h1>", + cli_func=cli_creole2html + ) + + def test_html2creole(self): + self._test_convert( + source_content=b"<h1>test html2creole</h1>", + dest_content=b"= test html2creole", + cli_func=cli_html2creole + ) + + def test_html2rest(self): + self._test_convert( + source_content=b"<h1>test html2rest</h1>", + dest_content=(b"==============\n" + "test html2rest\n" + "==============" + ), + cli_func=cli_html2rest + ) + + def test_html2textile(self): + self._test_convert( + source_content=b"<h1>test html2textile</h1>", + dest_content=b"h1. test html2textile", + cli_func=cli_html2textile + ) + +if __name__ == '__main__': + unittest.main() @@ -45,6 +45,14 @@ setup( packages=find_packages(), include_package_data=True, # include package data under svn source control data_files=[("", ["README.creole"])], # README used in unittest test_setup_utils.py + entry_points={ + "console_scripts": [ + "creole2html = creole.cmdline:cli_creole2html", + "html2creole = creole.cmdline:cli_html2creole", + "html2rest = creole.cmdline:cli_html2rest", + "html2textile = creole.cmdline:cli_html2textile", + ], + }, zip_safe=True, # http://packages.python.org/distribute/setuptools.html#setting-the-zip-safe-flag keywords="creole markup creole2html html2creole rest2html html2rest html2textile", classifiers=[ |