Skip to content

Commit

Permalink
Merge pull request #50 from oliviermartin/github-code-block
Browse files Browse the repository at this point in the history
markdown/pagemodel: Added support for Github code block
Thanks Olivier!
We can probably make GitHub-mode the default.
  • Loading branch information
winterstein committed Dec 28, 2014
2 parents 045864e + bb772b1 commit 50e4cd4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
28 changes: 28 additions & 0 deletions plugin/src/winterwell/markdown/pagemodel/MarkdownPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ private void setText(String text) {
Header currentHeader = dummyTopHeader;
// Identify line types
int lineNum = 0;
IPreferenceStore pStore = Activator.getDefault().getPreferenceStore();

// Multi-markdown header
if (multiMarkdownSupport) {
// The key is the text before the colon, and the data is the text
Expand Down Expand Up @@ -328,6 +330,32 @@ private void setText(String text) {
if (dummyTopHeader.getSubHeaders().size() == 0) {
level1Headers.remove(dummyTopHeader);
}

boolean githubSyntaxSupport =
pStore.getBoolean(MarkdownPreferencePage.PREF_GITHUB_SYNTAX);
if (githubSyntaxSupport) {
/*
* Support Code block
*/
boolean inCodeBlock = false;
for (lineNum = 0; lineNum < lines.size(); lineNum++) {
String line = lines.get(lineNum);
// Found the start or end of a code block
if (line.matches("^```.*\n")) {
// We reverse the boolean value
inCodeBlock = !inCodeBlock;

// We force the line to be blank. But we mark it as normal
// to prevent to be stripped
lines.set(lineNum, "\n");
lineTypes.set(lineNum, KLineType.NORMAL);
continue;
}
if (inCodeBlock) {
lines.set(lineNum, " " + line);
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class MarkdownPreferencePage
public static final String PREF_CODE = "Pref_Code";
public static final String PREF_CODE_BG = "Pref_Code_Background";

public static final String PREF_GITHUB_SYNTAX = "Pref_Github_Syntax";

private static final RGB DEF_DEFAULT = new RGB(0, 0, 0);
private static final RGB DEF_COMMENT = new RGB(128, 0, 0);
private static final RGB DEF_HEADER = new RGB(0, 128, 0);
Expand All @@ -71,6 +73,7 @@ public static void setDefaultPreferences(IPreferenceStore pStore) {
pStore.setDefault(PREF_TASK_TAGS_DEFINED, "TODO,FIXME,??");
pStore.setDefault(PREF_MARKDOWN_COMMAND, MARKDOWNJ);
pStore.setDefault(PREF_SECTION_NUMBERS, true);
pStore.setDefault(PREF_GITHUB_SYNTAX, true);

PreferenceConverter.setDefault(pStore, PREF_DEFUALT, DEF_DEFAULT);
PreferenceConverter.setDefault(pStore, PREF_COMMENT, DEF_COMMENT);
Expand Down Expand Up @@ -141,6 +144,16 @@ public void createFieldEditors() {

ColorFieldEditor codeBg = new ColorFieldEditor(PREF_CODE_BG, "Code Background", getFieldEditorParent());
addField(codeBg);

/*
* Fields for the preview window
*/

// Github Syntax support
fd = new BooleanFieldEditor(PREF_GITHUB_SYNTAX,
"Support Github Syntax",
getFieldEditorParent());
addField(fd);
}

/* (non-Javadoc)
Expand Down

0 comments on commit 50e4cd4

Please sign in to comment.