diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-07-15 16:23:27 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-07-15 16:24:29 +0100 |
| commit | e135a91d3c76eb29a274a2f608b41787bf49e5ee (patch) | |
| tree | 625d7856710045600938451c8d75d55b0bf9e99e | |
| parent | 7d8e8197b03f87a8cac2eed8f75e8a41136cc760 (diff) | |
Fix: cannot extract text from elements without children
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | mammoth/raw_text.py | 2 | ||||
| -rw-r--r-- | tests/raw_text_tests.py | 22 |
3 files changed, 27 insertions, 1 deletions
@@ -1,3 +1,7 @@ +# 0.3.19 + +* Fix: cannot extract raw text from elements without children + # 0.3.18 * Support links and images in footnotes and endnotes. diff --git a/mammoth/raw_text.py b/mammoth/raw_text.py index fb39acf..ccf50ed 100644 --- a/mammoth/raw_text.py +++ b/mammoth/raw_text.py @@ -5,7 +5,7 @@ def extract_raw_text_from_element(element): if isinstance(element, documents.Text): return element.value else: - text = "".join(map(extract_raw_text_from_element, element.children)) + text = "".join(map(extract_raw_text_from_element, getattr(element, "children", []))) if isinstance(element, documents.Paragraph): return text + "\n\n" else: diff --git a/tests/raw_text_tests.py b/tests/raw_text_tests.py new file mode 100644 index 0000000..c2537c5 --- /dev/null +++ b/tests/raw_text_tests.py @@ -0,0 +1,22 @@ +from nose.tools import istest, assert_equal + +from mammoth.raw_text import extract_raw_text_from_element +from mammoth import documents + + +@istest +def raw_text_of_text_element_is_value(): + assert_equal("Hello", extract_raw_text_from_element(documents.Text("Hello"))) + + +@istest +def raw_text_of_paragraph_is_terminated_with_newlines(): + paragraph = documents.paragraph(children=[documents.Text("Hello")]) + assert_equal("Hello\n\n", extract_raw_text_from_element(paragraph)) + + +@istest +def non_text_element_without_children_has_no_raw_text(): + tab = documents.Tab() + assert not hasattr(tab, "children") + assert_equal("", extract_raw_text_from_element(documents.Tab())) |
