aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mammoth/__init__.py36
-rw-r--r--mammoth/conversion.py8
-rw-r--r--mammoth/options.py36
-rw-r--r--tests/conversion_tests.py10
4 files changed, 48 insertions, 42 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py
index 84849ce..90ab764 100644
--- a/mammoth/__init__.py
+++ b/mammoth/__init__.py
@@ -1,41 +1,11 @@
from .results import Result
-from . import docx, conversion, style_reader, lists
+from . import docx, conversion, options
-def convert_to_html(fileobj, style_map=None, convert_image=None):
- if style_map is None:
- style_map = _default_style_map
- else:
- style_map = _read_style_map(style_map)
-
+def convert_to_html(fileobj, **kwargs):
return docx.read(fileobj).bind(lambda document:
conversion.convert_document_element_to_html(
document,
- styles=style_map,
- convert_image=convert_image,
+ **options.read_options(kwargs)
)
)
-
-
-def _read_style_map(style_text):
- lines = filter(None, map(lambda line: line.strip(), style_text.split("\n")))
- return lists.map(style_reader.read_style, lines)
-
-
-
-_default_style_map = _read_style_map("""
-p.Heading1 => h1:fresh
-p.Heading2 => h2:fresh
-p.Heading3 => h3:fresh
-p.Heading4 => h4:fresh
-p:unordered-list(1) => ul > li:fresh
-p:unordered-list(2) => ul|ol > li > ul > li:fresh
-p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh
-p:unordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh
-p:unordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh
-p:ordered-list(1) => ol > li:fresh
-p:ordered-list(2) => ul|ol > li > ol > li:fresh
-p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh
-p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh
-p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh
-""")
diff --git a/mammoth/conversion.py b/mammoth/conversion.py
index 835cb6b..8123ebd 100644
--- a/mammoth/conversion.py
+++ b/mammoth/conversion.py
@@ -5,11 +5,11 @@ from . import documents, results, html_paths
from .html_generation import HtmlGenerator, satisfy_html_path
-def convert_document_element_to_html(element, styles=None, convert_image=None):
- if styles is None:
- styles = []
+def convert_document_element_to_html(element, style_map=None, convert_image=None):
+ if style_map is None:
+ style_map = []
html_generator = HtmlGenerator()
- converter = DocumentConverter(styles, convert_image=convert_image)
+ converter = DocumentConverter(style_map, convert_image=convert_image)
converter.convert_element_to_html(element, html_generator,)
html_generator.end_all()
return results.Result(html_generator.html_string(), converter.messages)
diff --git a/mammoth/options.py b/mammoth/options.py
new file mode 100644
index 0000000..4e4d5f1
--- /dev/null
+++ b/mammoth/options.py
@@ -0,0 +1,36 @@
+from . import style_reader, lists
+
+
+def read_options(options):
+ if options.get("style_map") is None:
+ style_map = _default_style_map
+ else:
+ style_map = _read_style_map(style_map)
+
+ options["style_map"] = style_map
+ return options
+
+
+def _read_style_map(style_text):
+ lines = filter(None, map(lambda line: line.strip(), style_text.split("\n")))
+ return lists.map(style_reader.read_style, lines)
+
+
+
+_default_style_map = _read_style_map("""
+p.Heading1 => h1:fresh
+p.Heading2 => h2:fresh
+p.Heading3 => h3:fresh
+p.Heading4 => h4:fresh
+p:unordered-list(1) => ul > li:fresh
+p:unordered-list(2) => ul|ol > li > ul > li:fresh
+p:unordered-list(3) => ul|ol > li > ul|ol > li > ul > li:fresh
+p:unordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh
+p:unordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ul > li:fresh
+p:ordered-list(1) => ol > li:fresh
+p:ordered-list(2) => ul|ol > li > ol > li:fresh
+p:ordered-list(3) => ul|ol > li > ul|ol > li > ol > li:fresh
+p:ordered-list(4) => ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh
+p:ordered-list(5) => ul|ol > li > ul|ol > li > ul|ol > li > ul|ol > li > ol > li:fresh
+""")
+
diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py
index e3b4c9e..5912a0e 100644
--- a/tests/conversion_tests.py
+++ b/tests/conversion_tests.py
@@ -40,7 +40,7 @@ def paragraphs_are_converted_by_satisfying_matching_paths():
documents.paragraph(style_name="TipsParagraph", children=[
_run_with_text("Tip")
]),
- styles=[
+ style_map=[
style_reader.read_style("p.TipsParagraph => p.tip")
]
)
@@ -63,7 +63,7 @@ def default_paragraph_style_is_specified_by_mapping_plain_paragraphs():
documents.paragraph(style_name="TipsParagraph", children=[
_run_with_text("Tip")
]),
- styles=[
+ style_map=[
style_reader.read_style("p => p.tip")
]
)
@@ -94,7 +94,7 @@ def bulleted_paragraphs_are_converted_using_matching_styles():
documents.paragraph(children=[
_run_with_text("Hello")
], numbering=documents.numbering_level(level_index=0, is_ordered=False)),
- styles=[
+ style_map=[
style_reader.read_style("p:unordered-list(1) => ul > li:fresh")
]
)
@@ -107,7 +107,7 @@ def bulleted_styles_dont_match_plain_paragraph():
documents.paragraph(children=[
_run_with_text("Hello")
]),
- styles=[
+ style_map=[
style_reader.read_style("p:unordered-list(1) => ul > li:fresh")
]
)
@@ -134,7 +134,7 @@ def italic_runs_are_wrapped_in_emphasis_tags():
def runs_are_converted_by_satisfying_matching_paths():
result = convert_document_element_to_html(
documents.run(style_name="TipsRun", children=[documents.Text("Tip")]),
- styles=[
+ style_map=[
style_reader.read_style("r.TipsRun => span.tip")
]
)