diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-01-07 22:50:38 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-01-07 22:50:38 +0000 |
| commit | 189b6180fc032c95e49023879fcf328795b45bd3 (patch) | |
| tree | 3d97993388607ffb1d020f1afdc2a6f66ad0ee23 /mammoth | |
| parent | 77b597fc382bc7674f1e4365825aba70814577f4 (diff) | |
Read options in separate module
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/__init__.py | 36 | ||||
| -rw-r--r-- | mammoth/conversion.py | 8 | ||||
| -rw-r--r-- | mammoth/options.py | 36 |
3 files changed, 43 insertions, 37 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 +""") + |
