Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed and Improved Multi-Markdown metadata support #49

Merged
merged 3 commits into from
Jan 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions plugin/src/winterwell/markdown/pagemodel/MarkdownPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,8 @@ public enum KLineType {
private List<KLineType> lineTypes;
private Map<Integer,Object> pageObjects = new HashMap<Integer, Object>();

private boolean multiMarkdownSupport = true;
// TODO meta-data, footnotes, tables, link & image attributes
private static Pattern multiMarkdownTag = Pattern.compile("(.+):(.*)");
private static Pattern multiMarkdownTag = Pattern.compile("^([\\w].*):(.*)");
private Map<String, String> multiMarkdownTags = new HashMap<String, String>();

// Regular expression for Github support
Expand Down Expand Up @@ -252,8 +251,12 @@ private void setText(String text) {
// Identify line types
int lineNum = 0;

// Check if we should support the Multi-Markdown Metadata
boolean multiMarkdownMetadataSupport =
pStore.getBoolean(MarkdownPreferencePage.PREF_MULTIMARKDOWN_METADATA);

// Multi-markdown header
if (multiMarkdownSupport) {
if (multiMarkdownMetadataSupport) {
// The key is the text before the colon, and the data is the text
// after the
// colon. In the above example, notice that there are two lines of
Expand All @@ -267,28 +270,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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class MarkdownPreferencePage
public static final String PREF_CODE_BG = "Pref_Code_Background";

public static final String PREF_GITHUB_SYNTAX = "Pref_Github_Syntax";
public static final String PREF_MULTIMARKDOWN_METADATA = "Pref_MultiMarkdown_Metadata";

private static final RGB DEF_DEFAULT = new RGB(0, 0, 0);
private static final RGB DEF_COMMENT = new RGB(128, 0, 0);
Expand All @@ -74,6 +75,7 @@ public static void setDefaultPreferences(IPreferenceStore pStore) {
pStore.setDefault(PREF_MARKDOWN_COMMAND, MARKDOWNJ);
pStore.setDefault(PREF_SECTION_NUMBERS, true);
pStore.setDefault(PREF_GITHUB_SYNTAX, true);
pStore.setDefault(PREF_MULTIMARKDOWN_METADATA, false);

PreferenceConverter.setDefault(pStore, PREF_DEFUALT, DEF_DEFAULT);
PreferenceConverter.setDefault(pStore, PREF_COMMENT, DEF_COMMENT);
Expand Down Expand Up @@ -154,6 +156,12 @@ public void createFieldEditors() {
"Support Github Syntax",
getFieldEditorParent());
addField(fd);

// Multi-Markdown support
fd = new BooleanFieldEditor(PREF_MULTIMARKDOWN_METADATA,
"Support Multi-Markdown Metadata",
getFieldEditorParent());
addField(fd);
}

/* (non-Javadoc)
Expand Down