File tree 4 files changed +40
-7
lines changed 4 files changed +40
-7
lines changed Original file line number Diff line number Diff line change 1
1
[pytest]
2
- addopts = " -v "
2
+ addopts = " -vv "
3
3
filterwarnings =
4
4
ignore::DeprecationWarning:invoke.*:
Original file line number Diff line number Diff line change 4
4
import re
5
5
import toml
6
6
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 ()
8
14
9
15
10
16
def get_toml_and_content (filename ):
11
17
"""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 )
16
24
17
25
18
26
def get_toml (filename ):
Original file line number Diff line number Diff line change @@ -11,4 +11,12 @@ Some content.
11
11
12
12
## A section in the content
13
13
14
+ Content that looks like frontmatter:
15
+ ```
16
+ +++
17
+ but this is
18
+ not really frontmatter
19
+ +++
20
+ ```
21
+
14
22
More content.
Original file line number Diff line number Diff line change 6
6
7
7
TEST_ENTRY = os .path .join (os .path .dirname (__file__ ), "test_entry.md" )
8
8
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
+
9
26
10
27
def test_get_toml_and_content ():
11
28
(toml , content ) = entry .get_toml_and_content (TEST_ENTRY )
@@ -14,7 +31,7 @@ def test_get_toml_and_content():
14
31
'tags' : ["books" , "stuff" ],
15
32
'book' : {'title' : 'The Sorcerer of the Wildeeps' , 'rating' : 4 }
16
33
}
17
- assert content == " \n \n Some content. \n \n ## A section in the content \n \n More content. \n "
34
+ assert content == TEST_ENTRY_CONTENT
18
35
19
36
20
37
def test_get_toml ():
You can’t perform that action at this time.
0 commit comments