From dbc3dac871616c8549cc3ddce01cd0e98f83c1f6 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 19 Nov 2016 20:02:19 +0000 Subject: Include embedded style map when explicit style map is set --- mammoth/__init__.py | 11 ++++++++--- mammoth/options.py | 23 +++++++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'mammoth') diff --git a/mammoth/__init__.py b/mammoth/__init__.py index 7735fcf..fe480ec 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -5,6 +5,9 @@ from .docx.style_map import write_style_map, read_style_map __all__ = ["convert_to_html", "extract_raw_text", "images", "underline"] +_undefined = object() + + def convert_to_html(*args, **kwargs): return convert(*args, output_format="html", **kwargs) @@ -13,11 +16,13 @@ def convert_to_markdown(*args, **kwargs): return convert(*args, output_format="markdown", **kwargs) -def convert(fileobj, transform_document=None, id_prefix=None, **kwargs): +def convert(fileobj, transform_document=None, id_prefix=None, include_embedded_style_map=_undefined, **kwargs): + if include_embedded_style_map is _undefined: + include_embedded_style_map = True if transform_document is None: transform_document = lambda x: x - if kwargs.get("style_map") is None: - kwargs["style_map"] = read_style_map(fileobj) + if include_embedded_style_map: + kwargs["embedded_style_map"] = read_style_map(fileobj) return options.read_options(kwargs).bind(lambda convert_options: docx.read(fileobj).map(transform_document).bind(lambda document: conversion.convert_document_element_to_html( diff --git a/mammoth/options.py b/mammoth/options.py index ddac3de..d33b7ba 100644 --- a/mammoth/options.py +++ b/mammoth/options.py @@ -2,13 +2,24 @@ from . import style_reader, lists, results def read_options(options): - custom_style_map_result = _read_style_map(options.get("style_map") or "") - include_default_style_map = options.get("include_default_style_map", True) - options["ignore_empty_paragraphs"] = options.get("ignore_empty_paragraphs", True) + custom_style_map_text = options.pop("style_map", "") or "" + embedded_style_map_text = options.pop("embedded_style_map", "") or "" + include_default_style_map = options.pop("include_default_style_map", True) + + read_style_map_result = results.combine([ + _read_style_map(custom_style_map_text), + _read_style_map(embedded_style_map_text), + ]) + + custom_style_map, embedded_style_map = read_style_map_result.value + style_map = custom_style_map + embedded_style_map - options["style_map"] = custom_style_map_result.value + \ - (_default_style_map if include_default_style_map else []) - return custom_style_map_result.map(lambda _: options) + if include_default_style_map: + style_map += _default_style_map + + options["ignore_empty_paragraphs"] = options.get("ignore_empty_paragraphs", True) + options["style_map"] = style_map + return read_style_map_result.map(lambda _: options) def _read_style_map(style_text): -- cgit v1.2.3