Skip to content

Commit c5fe8f2

Browse files
Merge pull request #215 from nkanaev/metadata-fenceless
make metadata fences optional
2 parents 9aa12d3 + e03852f commit c5fe8f2

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

lib/markdown2.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,18 +371,29 @@ def preprocess(self, text):
371371
"""
372372
return text
373373

374-
# Is metadata if the content starts with '---'-fenced `key: value`
374+
# Is metadata if the content starts with optional '---'-fenced `key: value`
375375
# pairs. E.g. (indented for presentation):
376376
# ---
377377
# foo: bar
378378
# another-var: blah blah
379379
# ---
380-
_metadata_pat = re.compile("""^---[ \t]*\n((?:[ \t]*[^ \t:]+[ \t]*:[^\n]*\n)+)---[ \t]*\n""")
380+
# # header
381+
# or:
382+
# foo: bar
383+
# another-var: blah blah
384+
#
385+
# # header
386+
387+
_metadata_pat = re.compile(r"""
388+
^
389+
(?:---[\ \t]*\n)? # optional "---"
390+
((?:[ \t]*[^ \t:]+[\ \t]*:[^\n]*\n)+) # "key: value" pairs
391+
(?:---[ \t]*)? # optional "---"
392+
\n""",
393+
re.VERBOSE
394+
)
381395

382396
def _extract_metadata(self, text):
383-
# fast test
384-
if not text.startswith("---"):
385-
return text
386397
match = self._metadata_pat.match(text)
387398
if not match:
388399
return text

test/tm-cases/metadata2.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>The real text</h1>
2+
3+
<p>This should be parsed as before</p>

test/tm-cases/metadata2.metadata

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"test": "abc",
3+
"leading~space": "is okay",
4+
"And": "some, cvs, data, which, you, must, parse, yourself",
5+
"this-is": "a hyphen test",
6+
"empty": ""
7+
}

test/tm-cases/metadata2.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["metadata"]}

test/tm-cases/metadata2.tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extra metadata issue78

test/tm-cases/metadata2.text

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test: abc
2+
this-is : a hyphen test
3+
leading~space : is okay
4+
And: some, cvs, data, which, you, must, parse, yourself
5+
empty :
6+
7+
# The real text
8+
9+
This should be parsed as before

0 commit comments

Comments
 (0)