Permalink
Browse files

Handle case where a soft-page-break is inserted

  • Loading branch information...
1 parent be00ce0 commit a8ef51399bebc2b7615e311940e6a34871b4a46e @agateau committed Sep 9, 2011
Showing with 43 additions and 4 deletions.
  1. +19 −4 replacer.py
  2. +24 −0 tests/unit/replacertestcase.py
View
@@ -17,10 +17,25 @@
def replace_placeholders(tree, dct):
def do_replace(element, eat_span=False):
- if element.text is None:
- return
-
- result = PLACEHOLDER_RX.match(element.text)
+ result = None
+ # placeholder can be either the text of an element, as in:
+ #
+ # <text:p>${file.cpp}</text:p>"
+ #
+ # or there can be a soft-page-break element in:
+ #
+ # <text:p>
+ # <text:soft-page-break/>
+ # ${file.cpp}
+ # </text:p>
+ #
+ #
+ if element.text is not None:
+ result = PLACEHOLDER_RX.match(element.text)
+ else:
+ page_break = element.find("{%s}soft-page-break" % TEXT_URI)
+ if page_break is not None:
+ result = PLACEHOLDER_RX.match(page_break.tail)
if not result:
return
@@ -66,6 +66,30 @@ def test_replace_text_p_span(self):
self._compare_trees(tree, expected_tree)
+ def test_replace_text_soft_page_break(self):
+ input_xml = HEADER \
+ + "<text:p>Before</text:p>" \
+ + "<text:p text:style-name='code'>" \
+ + "<text:soft-page-break/>${test.cpp}" \
+ + "</text:p>" \
+ + "<text:p>After</text:p>" \
+ + FOOTER
+ tree = etree.fromstring(input_xml)
+
+ expected_xml = HEADER \
+ + "<text:p>Before</text:p>" \
+ + "<text:p text:style-name='code'>line1</text:p>" \
+ + "<text:p text:style-name='code'>line2</text:p>" \
+ + "<text:p>After</text:p>" \
+ + FOOTER
+ expected_tree = etree.fromstring(expected_xml)
+
+ dct = {"test.cpp": "line1\nline2"}
+
+ replacer.replace_placeholders(tree, dct)
+
+ self._compare_trees(tree, expected_tree)
+
def _compare_trees(self, output, expected):
def dump(tree): return etree.tostring(tree)
output_str = dump(output)

0 comments on commit a8ef513

Please sign in to comment.