aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-12-01 01:15:25 +0000
committerMichael Williamson <mike@zwobble.org>2013-12-01 01:15:25 +0000
commit68047ef381cb08cf43e5a30ca9dd6e40ccde8cdd (patch)
treedcf862340c7c5d65f94264ddb28dde62aa1c1d5e
parent25706255c4a746b6c14afe2d9dd45cf50c2d2208 (diff)
Add basic conversion to HTML
-rw-r--r--mammoth/__init__.py43
-rw-r--r--mammoth/documents.py7
-rw-r--r--mammoth/results.py4
-rw-r--r--tests/__init__.py1
-rw-r--r--tests/mammoth_tests.py13
5 files changed, 67 insertions, 1 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py
index 803273f..195e818 100644
--- a/mammoth/__init__.py
+++ b/mammoth/__init__.py
@@ -1,5 +1,46 @@
from .results import Result
+from . import docx, documents
def convert_to_html(fileobj):
- return Result("", [])
+ document = docx.read(fileobj).value
+ html = _convert_element_to_html(document)
+ return Result(html, [])
+
+
+def _convert_element_to_html(element):
+ return _converters[type(element)](element)
+
+
+_converters = {}
+
+
+def converter(element_type):
+ def add(func):
+ _converters[element_type] = func
+
+ return add
+
+
+@converter(documents.Document)
+def convert_document(document):
+ return _convert_elements_to_html(document.children)
+
+
+@converter(documents.Paragraph)
+def convert_paragraph(paragraph):
+ return "<p>{0}</p>".format(_convert_elements_to_html(paragraph.children))
+
+
+@converter(documents.Run)
+def convert_run(run):
+ return _convert_elements_to_html(run.children)
+
+
+@converter(documents.Text)
+def convert_text(text):
+ return text.value
+
+
+def _convert_elements_to_html(elements):
+ return "".join(map(_convert_element_to_html, elements))
diff --git a/mammoth/documents.py b/mammoth/documents.py
new file mode 100644
index 0000000..50b5e65
--- /dev/null
+++ b/mammoth/documents.py
@@ -0,0 +1,7 @@
+import collections
+
+
+Document = collections.namedtuple("Document", ["children"])
+Paragraph = collections.namedtuple("Paragraph", ["children"])
+Run = collections.namedtuple("Run", ["children"])
+Text = collections.namedtuple("Text", ["value"])
diff --git a/mammoth/results.py b/mammoth/results.py
new file mode 100644
index 0000000..f735df7
--- /dev/null
+++ b/mammoth/results.py
@@ -0,0 +1,4 @@
+class Result(object):
+ def __init__(self, value, messages):
+ self.value = value
+ self.messages = messages
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1 @@
+
diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py
new file mode 100644
index 0000000..4550d91
--- /dev/null
+++ b/tests/mammoth_tests.py
@@ -0,0 +1,13 @@
+from nose.tools import istest, assert_equal
+
+from .testing import test_path
+
+import mammoth
+
+
+@istest
+def docx_containing_one_paragraph_is_converted_to_single_p_element():
+ with open(test_path("single-paragraph.docx")) as fileobj:
+ result = mammoth.convert_to_html(fileobj=fileobj)
+ assert_equal("<p>Walking on imported air</p>", result.value)
+ assert_equal([], result.messages)