Skip to content

Commit

Permalink
markdown/pagemodel: Only parse the Multimarkdown metadata if there ar…
Browse files Browse the repository at this point in the history
…e valid

Prior to this change, metadata were parsed even if the metadata block
was not valid.

Example of non valid metadata block:
---------
Author: Olivier
---------

The MultiMarkdown format requires an empty line to be added at the
end of the metadata block.
  • Loading branch information
oliviermartin committed Dec 28, 2014
1 parent 0e294f7 commit b570849
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions plugin/src/winterwell/markdown/pagemodel/MarkdownPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,28 +266,59 @@ private void setText(String text) {
// ends with the first whitespace only line. The metadata is
// stripped from the
// document before it is passed on to the syntax parser.
String data = "";
String tag = "";
for (; lineNum < lines.size(); lineNum++) {

//
// Check if the Metdatas are valid
//
boolean validMetadata = true;
for (lineNum = 0; lineNum < lines.size(); lineNum++) {
String line = lines.get(lineNum);
if (Utils.isBlank(line)) {
break;
}
Matcher m = multiMarkdownTag.matcher(line);
if (!m.find()) {
if (lineNum == 0)
if (lineNum == 0) {
// No MultiMarkdown metadata
validMetadata = false;
break;
} else if (!line.matches("^\\s.*\n")) {
// The next line was not intended (ie. it does not start
// with a whitespace)
validMetadata = false;
break;
}
}
}

// Valid Metadatas have been found. We need to retrieve these keys/values.
if (validMetadata) {
String data = "";
String tag = "";
for (lineNum = 0; lineNum < lines.size(); lineNum++) {
String line = lines.get(lineNum);
if (Utils.isBlank(line)) {
break;
// Multi-line tag
lineTypes.add(KLineType.META);
data += StrUtils.LINEEND + line.trim();
multiMarkdownTags.put(tag, data);
} else {
lineTypes.add(KLineType.META);
tag = m.group(0);
data = m.group(1).trim();
if (m.group(1).endsWith(line))
}
Matcher m = multiMarkdownTag.matcher(line);
if (!m.find()) {
if (lineNum == 0) {
break;
}
// Multi-line tag
lineTypes.add(KLineType.META);
data += StrUtils.LINEEND + line.trim();
multiMarkdownTags.put(tag, data);
} else {
lineTypes.add(KLineType.META);
tag = m.group(0);
data = m.group(1).trim();
if (m.group(1).endsWith(line))
multiMarkdownTags.put(tag, data);
}
}
} else {
lineNum = 0;
}
}
for (; lineNum < lines.size(); lineNum++) {
Expand Down

0 comments on commit b570849

Please sign in to comment.