Fix IndexError in metadata extra on documents with no metadata split#715
Fix IndexError in metadata extra on documents with no metadata split#715santhreal wants to merge 1 commit into
Conversation
_extract_metadata assumed re.split always yields enough parts to index: - the non-fenced branch did tail = metadata_split[1], which raised IndexError when the document had no blank line to split on (e.g. a tab-indented code block with no trailing blank line); - the fenced branch did fence_splits[1]/[2], which raised IndexError when a document opened with '---' (a horizontal rule) but had no closing '---' fence. Guard both: a leading '---' with no closing fence is left as-is (rendered as an <hr>), and a missing blank-line tail is treated as an empty body. Added regression tests for both crash cases and for unchanged parsing of fenced and leading key: value metadata.
| '<h2>%s</h2>\n' % ko) | ||
| test_russian.tags = ["unicode", "issue3"] | ||
|
|
||
| def test_metadata_no_blank_line(self): |
There was a problem hiding this comment.
I don't think this file is actually run as part of the test suite. The test suite runs on the test cases in test/tm-cases so maybe worth putting these in there?
There was a problem hiding this comment.
Or amending the test runner to run this. Probably more work this way, but if you want to give it a go, it's triggered via the make test and make testone Makefile rules
There was a problem hiding this comment.
Resolved. This test file is run.
|
LGTM as long as the tests pass |
|
It does run on CI. The test step is So the new case gates the fix as-is. |
|
LGTM |
_extract_metadataassumesre.splityields enough parts to index, so two common inputs crash themetadataextra withIndexError: list index out of range:---(a horizontal rule) but with no closing---fence: the fenced branch indexesfence_splits[1]/[2]but the split returns fewer than three parts. Example:markdown2.markdown('---\n# My Document\n', extras=['metadata']).metadata_split[1]but the split returns a single part. Example:markdown2.markdown('\tsome indented code', extras=['metadata']).This guards both branches: a leading
---with no closing fence is left unchanged (rendered as an<hr>), and a missing tail is treated as an empty body. Valid---fenced and leadingkey: valuemetadata still parse. Added regression tests for both crash cases and the two preservation cases; the full suite passes.