aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-02-07 18:55:24 +0000
committerMichael Williamson <mike@zwobble.org>2015-02-07 18:55:24 +0000
commit57eddaefd1961884cddbaf427459822ad8811aeb (patch)
tree1693604dfbd460f861227915b9734a4b9ebeb3eb
parente0b022e52d720758023bb0248ea0a17a4cbbb822 (diff)
Add scaffolding for markdown
-rw-r--r--mammoth/__init__.py12
-rw-r--r--mammoth/conversion.py5
-rw-r--r--mammoth/writers/__init__.py18
-rw-r--r--mammoth/writers/markdown.py25
-rw-r--r--scripts/mammoth5
-rw-r--r--setup.py4
-rw-r--r--tests/cli_tests.py8
-rw-r--r--tests/mammoth_tests.py8
8 files changed, 76 insertions, 9 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py
index c7b305d..8ec2400 100644
--- a/mammoth/__init__.py
+++ b/mammoth/__init__.py
@@ -3,7 +3,15 @@ from . import docx, conversion, options, documents, images, underline
__all__ = ["convert_to_html", "extract_raw_text", "images", "underline"]
-def convert_to_html(fileobj, transform_document=None, id_prefix=None, **kwargs):
+def convert_to_html(*args, **kwargs):
+ return convert(*args, output_format="html", **kwargs)
+
+
+def convert_to_markdown(*args, **kwargs):
+ return convert(*args, output_format="markdown", **kwargs)
+
+
+def convert(fileobj, transform_document=None, id_prefix=None, **kwargs):
if transform_document is None:
transform_document = lambda x: x
return docx.read(fileobj).map(transform_document).bind(lambda document:
@@ -13,7 +21,7 @@ def convert_to_html(fileobj, transform_document=None, id_prefix=None, **kwargs):
**options.read_options(kwargs)
)
)
-
+
def extract_raw_text(fileobj):
return docx.read(fileobj).map(_extract_raw_text_from_element)
diff --git a/mammoth/conversion.py b/mammoth/conversion.py
index 3c217e0..2932351 100644
--- a/mammoth/conversion.py
+++ b/mammoth/conversion.py
@@ -13,7 +13,8 @@ def convert_document_element_to_html(element,
style_map=None,
convert_image=None,
convert_underline=None,
- id_prefix=None):
+ id_prefix=None,
+ output_format=None):
if style_map is None:
style_map = []
@@ -21,7 +22,7 @@ def convert_document_element_to_html(element,
if id_prefix is None:
id_prefix = str(random.randint(0, 1000000000000000))
- html_generator = HtmlGenerator(writers.writer)
+ html_generator = HtmlGenerator(lambda: writers.writer(output_format))
converter = DocumentConverter(style_map,
convert_image=convert_image,
convert_underline=convert_underline,
diff --git a/mammoth/writers/__init__.py b/mammoth/writers/__init__.py
index 04a64d7..376b322 100644
--- a/mammoth/writers/__init__.py
+++ b/mammoth/writers/__init__.py
@@ -1,5 +1,19 @@
from .html import HtmlWriter
+from .markdown import MarkdownWriter
-def writer():
- return HtmlWriter()
+def writer(output_format=None):
+ if output_format is None:
+ output_format = "html"
+
+ return _writers[output_format]()
+
+
+def formats():
+ return _writers.keys()
+
+
+_writers = {
+ "html": HtmlWriter,
+ "markdown": MarkdownWriter,
+}
diff --git a/mammoth/writers/markdown.py b/mammoth/writers/markdown.py
new file mode 100644
index 0000000..c4e1158
--- /dev/null
+++ b/mammoth/writers/markdown.py
@@ -0,0 +1,25 @@
+from __future__ import unicode_literals
+
+
+class MarkdownWriter(object):
+ def __init__(self):
+ self._fragments = []
+
+ def text(self, text):
+ self._fragments.append(text)
+
+ def start(self, name, attributes=None):
+ pass
+
+ def end(self, name):
+ if name == "p":
+ self._fragments.append("\n\n")
+
+ def self_closing(self, name, attributes=None):
+ pass
+
+ def append(self, other):
+ self._fragments.append(other)
+
+ def as_string(self):
+ return "".join(self._fragments)
diff --git a/scripts/mammoth b/scripts/mammoth
index b6fa056..f4865fb 100644
--- a/scripts/mammoth
+++ b/scripts/mammoth
@@ -7,6 +7,7 @@ import os
import shutil
import mammoth
+from mammoth import writers
def main():
@@ -27,10 +28,11 @@ def main():
output_filename = "{0}.html".format(os.path.basename(args.path).rpartition(".")[0])
output_path = os.path.join(args.output_dir, output_filename)
- result = mammoth.convert_to_html(
+ result = mammoth.convert(
docx_fileobj,
style_map=style_map,
convert_image=convert_image,
+ output_format=args.output_format,
)
for message in result.messages:
sys.stderr.write(message.message)
@@ -89,6 +91,7 @@ def _parse_args():
output_group.add_argument("output", nargs="?")
output_group.add_argument("--output-dir", nargs="?")
+ parser.add_argument("--output-format", required=False, choices=writers.formats())
parser.add_argument("--style-map", required=False)
return parser.parse_args()
diff --git a/setup.py b/setup.py
index 5456f2d..e194468 100644
--- a/setup.py
+++ b/setup.py
@@ -19,14 +19,14 @@ if sys.version_info[:2] <= (2, 6):
setup(
name='mammoth',
version='0.3.9',
- description='Convert Word documents to simple and clean HTML',
+ description='Convert Word documents from docx to simple and clean HTML and Markdown',
long_description=read("README"),
author='Michael Williamson',
author_email='mike@zwobble.org',
url='http://github.com/mwilliamson/python-mammoth',
packages=['mammoth', 'mammoth.docx', 'mammoth.style_reader', 'mammoth.writers'],
scripts=["scripts/mammoth"],
- keywords="docx word office clean html",
+ keywords="docx word office clean html markdown md",
install_requires=_install_requires,
classifiers=[
'Development Status :: 4 - Beta',
diff --git a/tests/cli_tests.py b/tests/cli_tests.py
index f76df45..4728c6b 100644
--- a/tests/cli_tests.py
+++ b/tests/cli_tests.py
@@ -66,3 +66,11 @@ def style_map_is_used_if_set():
result = _local.run(["mammoth", docx_path, "--style-map", style_map_path])
assert_equal(b"", result.stderr_output)
assert_equal(b"<span>Walking on imported air</span>", result.output)
+
+
+@istest
+def output_format_markdown_option_generates_markdown_output():
+ docx_path = test_path("single-paragraph.docx")
+ result = _local.run(["mammoth", docx_path, "--output-format=markdown"])
+ assert_equal(b"", result.stderr_output)
+ assert_equal(b"Walking on imported air\n\n", result.output)
diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py
index c5f2557..ce76221 100644
--- a/tests/mammoth_tests.py
+++ b/tests/mammoth_tests.py
@@ -99,6 +99,14 @@ def transform_document_is_applied_to_document_before_conversion():
@istest
+def docx_containing_one_paragraph_can_be_converted_to_markdown():
+ with open(test_path("single-paragraph.docx"), "rb") as fileobj:
+ result = mammoth.convert_to_markdown(fileobj=fileobj)
+ assert_equal("Walking on imported air\n\n", result.value)
+ assert_equal([], result.messages)
+
+
+@istest
def can_extract_raw_text():
with open(test_path("simple-list.docx"), "rb") as fileobj:
result = mammoth.extract_raw_text(fileobj=fileobj)