Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert break elements to \n in raw text #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mammoth/raw_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ def extract_raw_text_from_element(element):
text = "".join(map(extract_raw_text_from_element, getattr(element, "children", [])))
if isinstance(element, documents.Paragraph):
return text + "\n\n"
if isinstance(element, documents.Break):
return text + "\n"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's possible for breaks to contain text? If not, I guess we can just return \n.

else:
return text
11 changes: 11 additions & 0 deletions tests/raw_text_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
def raw_text_of_text_element_is_value():
assert_equal("Hello", extract_raw_text_from_element(documents.Text("Hello")))

@istest
def raw_text_of_line_break_element_is_newline():
assert_equal("\n", extract_raw_text_from_element(documents.line_break))

@istest
def raw_text_of_page_break_element_is_newline():
assert_equal("\n", extract_raw_text_from_element(documents.page_break))

@istest
def raw_text_of_column_break_element_is_newline():
assert_equal("\n", extract_raw_text_from_element(documents.column_break))

@istest
def raw_text_of_paragraph_is_terminated_with_newlines():
Expand Down