From 47c7a524e001bd0d9aef872b4a12c0b8efd0d6ff Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 14 Dec 2013 15:55:19 +0000 Subject: Move document to HTML conversion to conversion module --- mammoth/__init__.py | 42 ++---------------------------------------- mammoth/conversion.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 mammoth/conversion.py diff --git a/mammoth/__init__.py b/mammoth/__init__.py index 195e818..df032b2 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -1,46 +1,8 @@ from .results import Result -from . import docx, documents +from . import docx, conversion def convert_to_html(fileobj): document = docx.read(fileobj).value - html = _convert_element_to_html(document) + html = conversion.convert_document_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 "

{0}

".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/conversion.py b/mammoth/conversion.py new file mode 100644 index 0000000..0d6dd6d --- /dev/null +++ b/mammoth/conversion.py @@ -0,0 +1,41 @@ +from .results import Result +from . import documents + + +def convert_document_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 "

{0}

".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_document_element_to_html, elements)) + -- cgit v1.2.3