aboutsummaryrefslogtreecommitdiff
path: root/mammoth/docx/document_xml.py
diff options
context:
space:
mode:
Diffstat (limited to 'mammoth/docx/document_xml.py')
-rw-r--r--mammoth/docx/document_xml.py103
1 files changed, 59 insertions, 44 deletions
diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py
index 1c9a43b..425ba37 100644
--- a/mammoth/docx/document_xml.py
+++ b/mammoth/docx/document_xml.py
@@ -2,54 +2,73 @@ from .. import documents
from .xmlparser import node_types
-_handlers = {}
-
-
-def handler(name):
- def add(func):
- _handlers[name] = func
+def read_document_xml_element(element, numbering=None):
+ reader = _create_reader(numbering)
+ return reader(element)
+
+def _create_reader(numbering):
+ _handlers = {}
+
+ def handler(name):
+ def add(func):
+ _handlers[name] = func
+
+ return add
+
+ @handler("w:t")
+ def text(element):
+ return documents.Text(_inner_text(element))
+
+
+ @handler("w:r")
+ def run(element):
+ return documents.Run(_read_xml_elements(element.children))
+
+
+ @handler("w:p")
+ def paragraph(element):
+ properties = element.find_child_or_null("w:pPr")
+ style_name = properties \
+ .find_child_or_null("w:pStyle") \
+ .attributes.get("w:val")
+ numbering_properties = properties.find_child("w:numPr")
+ if numbering_properties is None:
+ numbering = None
+ else:
+ numbering = _read_numbering_properties(numbering_properties)
- return add
+ children = _read_xml_elements(element.children)
+ return documents.paragraph(children, style_name, numbering)
-
-def read_document_xml_element(element):
- handler = _handlers.get(element.name)
- if handler is None:
- return None
- else:
- return handler(element)
+ def _read_numbering_properties(element):
+ num_id = element.find_child("w:numId").attributes["w:val"]
+ level_index = element.find_child("w:ilvl").attributes["w:val"]
+ return numbering.find_level(num_id, level_index)
-@handler("w:t")
-def text(element):
- return documents.Text(_inner_text(element))
+ @handler("w:body")
+ def body(element):
+ return _read_xml_elements(element.children)
-@handler("w:r")
-def run(element):
- return documents.Run(_read_xml_elements(element.children))
-
-
-@handler("w:p")
-def paragraph(element):
- properties = element.find_child_or_null("w:pPr")
- style_name = properties \
- .find_child_or_null("w:pStyle") \
- .attributes.get("w:val")
+ @handler("w:document")
+ def document(element):
+ body_element = _find_child(element, "w:body")
+ return documents.Document(_read_xml_elements(body_element.children))
- children = _read_xml_elements(element.children)
- return documents.paragraph(children, style_name)
-
-
-@handler("w:body")
-def body(element):
- return _read_xml_elements(element.children)
-
+
+ def read(element):
+ handler = _handlers.get(element.name)
+ if handler is None:
+ return None
+ else:
+ return handler(element)
+
-@handler("w:document")
-def document(element):
- body_element = _find_child(element, "w:body")
- return documents.Document(_read_xml_elements(body_element.children))
+ def _read_xml_elements(elements):
+ return filter(None, map(read, elements))
+
+ return read
def _find(predicate, iterable):
@@ -62,10 +81,6 @@ def _find_child(element, name):
return _find(lambda child: child.name == name, element.children)
-def _read_xml_elements(elements):
- return filter(None, map(read_document_xml_element, elements))
-
-
def _inner_text(node):
if node.node_type == node_types.text:
return node.value