Ensure exported entries end in a newline for Markdown and YAML exporters (#908)

* [Markdown Exporter] [YAML Exporter] Ensure exported entires end in a newline

Fixes #768, Fixes #881.

If the exported entry does not have a final empty line, this will add one on export. Some Markdown parsers get picky about not having a empty line above a heading....

* fix black formatting issues
* explicitly sort filenames

to deal with inconsistent default file ordering on different OS's
* Update .gitignore
* Update test for typo fix

Co-authored-by: Jonathan Wren <jonathan@nowandwren.com>
This commit is contained in:
MinchinWeb 2020-04-11 16:14:08 -06:00
parent 62ea1e2b23
commit 68ad5c0c1a
5 changed files with 52 additions and 1 deletions

3
.gitignore vendored
View file

@ -42,11 +42,12 @@ obj
# virtaulenv # virtaulenv
env/ env/
env*/ env*/
venv*/
# PyCharm Project files # PyCharm Project files
.idea/ .idea/
# export testing director # export testing directories
exp/ exp/
_extras/ _extras/

View file

@ -123,6 +123,43 @@ Feature: Zapped bugs should stay dead.
Then the output should contain "Adding an entry right now." Then the output should contain "Adding an entry right now."
Then the output should not contain "A future entry." Then the output should not contain "A future entry."
# See issues #768 and #881
# the "deletion" journal is used because it doesn't have a newline at the
# end of the last entry
Scenario: Add a blank line to Markdown export is there isn't one already
Given we use the config "deletion.yaml"
When we run "jrnl --export markdown"
Then the output should be
"""
# 2019
## October
### 2019-10-29 11:11 First entry.
### 2019-10-29 11:11 Second entry.
### 2019-10-29 11:13 Third entry.
"""
# See issues #768 and #881
Scenario: Add a blank line to YAML export is there isn't one already
Given we use the config "deletion.yaml"
And we created a directory named "bug768"
When we run "jrnl --export yaml -o bug768"
Then "bug768" should contain the files ["2019-10-29_first-entry.md", "2019-10-29_second-entry.md", "2019-10-29_third-entry.md"]
And the content of exported yaml "bug768/2019-10-29_third-entry.md" should be
"""
title: Third entry.
date: 2019-10-29 11:13
starred: False
tags:
"""
@deployment_tests @deployment_tests
Scenario: Version numbers should stay in sync Scenario: Version numbers should stay in sync
Given we use the config "basic.yaml" Given we use the config "basic.yaml"

View file

@ -96,6 +96,11 @@ def create_directory(context, dir_name):
def assert_dir_contains_files(context, dir_name, expected_files_json_list): def assert_dir_contains_files(context, dir_name, expected_files_json_list):
actual_files = os.listdir(dir_name) actual_files = os.listdir(dir_name)
expected_files = json.loads(expected_files_json_list) expected_files = json.loads(expected_files_json_list)
# sort to deal with inconsistent default file ordering on different OS's
actual_files.sort()
expected_files.sort()
assert actual_files == expected_files, [actual_files, expected_files] assert actual_files == expected_files, [actual_files, expected_files]

View file

@ -53,6 +53,10 @@ class MarkdownExporter(TextExporter):
previous_line = line previous_line = line
newbody = newbody + previous_line # add very last line newbody = newbody + previous_line # add very last line
# make sure the export ends with a blank line
if previous_line not in ["\r", "\n", "\r\n", "\n\r"]:
newbody = newbody + os.linesep
if warn_on_heading_level is True: if warn_on_heading_level is True:
print( print(
f"{WARNING_COLOR}WARNING{RESET_COLOR}: " f"{WARNING_COLOR}WARNING{RESET_COLOR}: "

View file

@ -64,6 +64,10 @@ class YAMLExporter(TextExporter):
previous_line = line previous_line = line
newbody = newbody + previous_line # add very last line newbody = newbody + previous_line # add very last line
# make sure the export ends with a blank line
if previous_line not in ["\r", "\n", "\r\n", "\n\r"]:
newbody = newbody + os.linesep
if warn_on_heading_level is True: if warn_on_heading_level is True:
print( print(
"{}WARNING{}: Headings increased past H6 on export - {} {}".format( "{}WARNING{}: Headings increased past H6 on export - {} {}".format(