From 178a640539e1af27fb7b64fc540adc17e58c30c4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 8 Jun 2014 21:45:14 +0100 Subject: Add support for reading style names in style maps --- mammoth/document_matchers.py | 12 ++++----- mammoth/style_reader/document_matcher_reader.py | 34 +++++++++++++++++-------- 2 files changed, 30 insertions(+), 16 deletions(-) (limited to 'mammoth') diff --git a/mammoth/document_matchers.py b/mammoth/document_matchers.py index 014e4e3..222ee73 100644 --- a/mammoth/document_matchers.py +++ b/mammoth/document_matchers.py @@ -1,17 +1,17 @@ import collections -def paragraph(style_id=None, numbering=None): - return ParagraphMatcher(style_id, numbering) +def paragraph(style_id=None, style_name=None, numbering=None): + return ParagraphMatcher(style_id, style_name, numbering) -ParagraphMatcher = collections.namedtuple("ParagraphMatcher", ["style_id", "numbering"]) +ParagraphMatcher = collections.namedtuple("ParagraphMatcher", ["style_id", "style_name", "numbering"]) ParagraphMatcher.element_type = "paragraph" -def run(style_id=None): - return RunMatcher(style_id) +def run(style_id=None, style_name=None): + return RunMatcher(style_id, style_name) -RunMatcher = collections.namedtuple("RunMatcher", ["style_id"]) +RunMatcher = collections.namedtuple("RunMatcher", ["style_id", "style_name"]) RunMatcher.element_type = "run" diff --git a/mammoth/style_reader/document_matcher_reader.py b/mammoth/style_reader/document_matcher_reader.py index 5b50f24..0152fd2 100644 --- a/mammoth/style_reader/document_matcher_reader.py +++ b/mammoth/style_reader/document_matcher_reader.py @@ -17,19 +17,29 @@ def read_document_matcher_node(root_node): def _read_paragraph_node(paragraph_node): - style_id = _read_style_node(paragraph_node.children[1]) - numbering = _read_list_node(paragraph_node.children[2]) - return document_matchers.paragraph(style_id=style_id, numbering=numbering) + style_id = _read_style_id_node(paragraph_node.children[1]) + style_name = _read_style_name_node(paragraph_node.children[2]) + numbering = _read_list_node(paragraph_node.children[3]) + return document_matchers.paragraph(style_id=style_id, style_name=style_name, numbering=numbering) def _read_run_node(run_node): - style_id = _read_style_node(run_node.children[1]) - return document_matchers.run(style_id=style_id) + style_id = _read_style_id_node(run_node.children[1]) + style_name = _read_style_name_node(run_node.children[2]) + return document_matchers.run(style_id=style_id, style_name=style_name) -def _read_style_node(style_node): - if style_node.children: - return style_node.children[0].children[1].text +def _read_style_id_node(style_id_node): + if style_id_node.children: + return style_id_node.children[0].children[1].text + else: + return None + + +def _read_style_name_node(style_name_node): + if style_name_node.children: + print style_name_node.children[0].children[1] + return style_name_node.children[0].children[1].text else: return None @@ -46,14 +56,18 @@ def _read_list_node(list_node): grammar_text = r""" document_matcher = paragraph / run -paragraph = "p" style_id? list? +paragraph = "p" style_id? style_name_specifier? list? -run = "r" style_id? +run = "r" style_id? style_name_specifier? style_id = "." style_identifier style_identifier = ~"[A-Z0-9]*"i +style_name_specifier = "[style-name='" style_name "']" + +style_name = ~"[^']+" + list = ":" list_type "(" ~"[0-9]+" ")" list_type = "ordered-list" / "unordered-list" -- cgit v1.2.3