Skip to content

Commit 3e14c84

Browse files
committed
Fix but in frontmatter and content extraction.
1 parent cc4193a commit 3e14c84

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[pytest]
2-
addopts = "-v"
2+
addopts = "-vv"
33
filterwarnings =
44
ignore::DeprecationWarning:invoke.*:

tasks/entry.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@
44
import re
55
import toml
66

7-
RE_TOML_SEPARATOR = r"\+\+\+"
7+
TOML_SEPARATOR = "+++"
8+
9+
10+
def get_file_as_str(filename):
11+
"""Returns the content of the filename as a string."""
12+
with open(filename, 'r') as file:
13+
return file.read()
814

915

1016
def get_toml_and_content(filename):
1117
"""Returns a tuple with the entry TOML frontmatter and the markdown content."""
12-
with open(filename, 'r') as file:
13-
file_str = file.read()
14-
splits = re.split(RE_TOML_SEPARATOR, file_str)
15-
return (toml.loads(splits[1].strip()), splits[2])
18+
file_str = get_file_as_str(filename)
19+
front_start = file_str.index(TOML_SEPARATOR) + len(TOML_SEPARATOR)
20+
frontmatter_end = file_str.index(TOML_SEPARATOR, front_start)
21+
toml_str = file_str[front_start:frontmatter_end]
22+
content_str = file_str[frontmatter_end + len(TOML_SEPARATOR):]
23+
return (toml.loads(toml_str), content_str)
1624

1725

1826
def get_toml(filename):

tests/test_entry.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ Some content.
1111

1212
## A section in the content
1313

14+
Content that looks like frontmatter:
15+
```
16+
+++
17+
but this is
18+
not really frontmatter
19+
+++
20+
```
21+
1422
More content.

tests/test_entry.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66

77
TEST_ENTRY = os.path.join(os.path.dirname(__file__), "test_entry.md")
88

9+
TEST_ENTRY_CONTENT = """
10+
11+
Some content.
12+
13+
## A section in the content
14+
15+
Content that looks like frontmatter:
16+
```
17+
+++
18+
but this is
19+
not really frontmatter
20+
+++
21+
```
22+
23+
More content.
24+
"""
25+
926

1027
def test_get_toml_and_content():
1128
(toml, content) = entry.get_toml_and_content(TEST_ENTRY)
@@ -14,7 +31,7 @@ def test_get_toml_and_content():
1431
'tags': ["books", "stuff"],
1532
'book': {'title': 'The Sorcerer of the Wildeeps', 'rating': 4}
1633
}
17-
assert content == "\n\nSome content.\n\n## A section in the content\n\nMore content.\n"
34+
assert content == TEST_ENTRY_CONTENT
1835

1936

2037
def test_get_toml():

0 commit comments

Comments
 (0)