diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-12-26 11:01:22 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-12-26 11:08:18 +0000 |
| commit | 03f7d77da6ce807effa5494f161aab07fdd2498c (patch) | |
| tree | 631d62cfc647e139a9ca27097c250af834d1ff59 /mammoth | |
| parent | 5a5112f8c0286da6875e54b2e61a32288135d1e8 (diff) | |
Always use mc:Fallback when reading mc:AlternateContent elements
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/docx/office_xml.py | 15 | ||||
| -rw-r--r-- | mammoth/lists.py | 8 |
2 files changed, 21 insertions, 2 deletions
diff --git a/mammoth/docx/office_xml.py b/mammoth/docx/office_xml.py index 4dffb44..0a6a8a8 100644 --- a/mammoth/docx/office_xml.py +++ b/mammoth/docx/office_xml.py @@ -1,4 +1,5 @@ -from .xmlparser import parse_xml +from ..lists import flat_map +from .xmlparser import parse_xml, XmlElement _namespaces = [ @@ -16,5 +17,15 @@ _namespaces = [ def read(fileobj): - return parse_xml(fileobj, _namespaces) + return _collapse_alternate_content(parse_xml(fileobj, _namespaces))[0] + +def _collapse_alternate_content(node): + if isinstance(node, XmlElement): + if node.name == "mc:AlternateContent": + return node.find_child("mc:Fallback").children + else: + node.children = flat_map(_collapse_alternate_content, node.children) + return [node] + else: + return [node] diff --git a/mammoth/lists.py b/mammoth/lists.py index 6e39ae0..e8477cb 100644 --- a/mammoth/lists.py +++ b/mammoth/lists.py @@ -24,6 +24,14 @@ def unique(values): return output +def flat_map(func, values): + return [ + element + for value in values + for element in func(value) + ] + + if sys.version_info[0] == 2: map = map filter = filter |
