Skip to content

Commit

Permalink
Merge pull request support-project#20 from support-project/develop
Browse files Browse the repository at this point in the history
Release v1.6.0
  • Loading branch information
koda-masaru authored Oct 31, 2016
2 parents 1a5a46f + 26aaa70 commit ffeae42
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 56 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ markedj is JVM port of graceful markdown processor marked.js

[Original project](https://github.com/gitbucket/markedj).


2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.support-project</groupId>
<artifactId>markedj</artifactId>
<packaging>jar</packaging>
<version>1.5.0</version>
<version>1.6.0</version>
<name>markedj</name>
<url>https://github.com/gitbucket/markedj</url>
<description>Fork from gitbucket markedj because knowledge's issue. markedj is JVM port of graceful markdown processor marked.js</description>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/io/github/gitbucket/markedj/Grammer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Grammer {
public static String BLOCK_DEF = "^ *\\[([^\\]]+)\\]: *<?([^\\s>]+)>?(?: +[\"(]([^\\n]+)[\")])? *(?:\\n+|$)";
public static String BLOCK_PARAGRAPH = "^((?:[^\\n]+\\n?(?!" + removeLineStart(BLOCK_HR) + "|" + removeLineStart(BLOCK_HEADING) + "|" + removeLineStart(BLOCK_LHEADING) + "|" + removeLineStart(BLOCK_BLOCKQUOTE) + "|<" + TAG + "|" + removeLineStart(BLOCK_DEF) + "))+)\\n*";
public static String BLOCK_GFM_FENCES = "^ *(`{3,}|~{3,})[ \\.]*(\\S+)? *\\n([\\s\\S]*?)\\s*\\1 *(?:\\n+|$)";
public static String BLOCK_MATH = "^ *(\\${2,}|~{2,})[ \\.]*(\\S+)? *\\n([\\s\\S]*?)\\s*\\1 *(?:\\n+|$)";
public static String BLOCK_FOOTNOTES = "^\\[\\^([0-9]+)\\]: *([^\n]*(?:\n+|$)(?: {1,}[^\n]*(?:\n+|$))*)";

public static Map<String, Rule> BLOCK_RULES = new HashMap<>();
Expand All @@ -36,6 +37,7 @@ public class Grammer {
BLOCK_RULES.put("newline", new FindFirstRule("^\n+"));
BLOCK_RULES.put("code", new FindFirstRule("^( {4}[^\n]+\n*)+"));
BLOCK_RULES.put("fences", new NoopRule());
BLOCK_RULES.put("math", new NoopRule());
BLOCK_RULES.put("hr", new FindFirstRule(BLOCK_HR));
BLOCK_RULES.put("heading", new FindFirstRule(BLOCK_HEADING));
BLOCK_RULES.put("nptable", new NoopRule());
Expand All @@ -52,6 +54,7 @@ public class Grammer {

BLOCK_GFM_RULES.putAll(BLOCK_RULES);
BLOCK_GFM_RULES.put("fences", new FindFirstRule(BLOCK_GFM_FENCES));
BLOCK_GFM_RULES.put("math", new FindFirstRule(BLOCK_MATH));
BLOCK_GFM_RULES.put("paragraph", new FindFirstRule(BLOCK_PARAGRAPH.replace("(?!", "(?!" + removeLineStart(BLOCK_GFM_FENCES).replace("\\1", "\\2") + "|" + removeLineStart(BLOCK_LIST).replace("\\1", "\\3") + "|")));
BLOCK_GFM_RULES.put("heading", new FindFirstRule("^ *(#{1,6}) +([^\\n]+?) *#* *(?:\\n+|$)"));

Expand Down Expand Up @@ -89,17 +92,20 @@ private static String removeLineStart(String regex){
INLINE_RULES.put("url", new NoopRule());
INLINE_RULES.put("tag", new FindFirstRule("^<!--[\\s\\S]*?-->|^<\\/?\\w+(?:\"[^\"]*\"|'[^']*'|[^'\">])*?>"));
INLINE_RULES.put("oembed", new FindFirstRule("^\\[oembed\\s(" + HREF + ")\\]"));
INLINE_RULES.put("slide", new FindFirstRule("^\\[slide\\s(.+?)\\]"));
INLINE_RULES.put("link", new FindFirstRule(("^!?\\[(" + INSIDE + ")\\]\\(" + HREF + "\\)")));
INLINE_RULES.put("reflink", new FindFirstRule(("^!?\\[(" + INSIDE + ")\\]\\s*\\[([^\\]]*)\\]")));
INLINE_RULES.put("nolink", new FindFirstRule("^!?\\[((?:\\[[^\\]]*\\]|[^\\[\\]])*)\\]"));
INLINE_RULES.put("strong", new FindFirstRule("^__([\\s\\S]+?)__(?!_)|^\\*\\*([\\s\\S]+?)\\*\\*(?!\\*)"));
INLINE_RULES.put("em", new FindFirstRule("^\\b_((?:[^_]|__)++)_\\b|^\\*((?:\\*\\*|[\\s\\S])+?)\\*(?!\\*)"));
INLINE_RULES.put("em", new FindFirstRule("^\\b_((?:[^_]|__)+?)_\\b|^\\*((?:\\*\\*|[\\s\\S])+?)\\*(?!\\*)"));
INLINE_RULES.put("code", new FindFirstRule("^(`+)\\s*([\\s\\S]*?[^`])\\s*\\1(?!`)"));
INLINE_RULES.put("br", new FindFirstRule(INLINE_BR));
INLINE_RULES.put("del", new NoopRule());
INLINE_RULES.put("text", new FindFirstRule(INLINE_TEXT));
INLINE_RULES.put("footnote", new FindFirstRule(INLINE_FOOTNOTE));


INLINE_RULES.put("internallink", new FindFirstRule("^#[0-9]+"));

INLINE_GFM_RULES.putAll(INLINE_RULES);
INLINE_GFM_RULES.put("escape", new FindFirstRule(INLINE_ESCAPE.replace("])", "~|])")));
INLINE_GFM_RULES.put("url", new FindFirstRule("^(https?:\\/\\/[^\\s<]+[^<.,:;\"')\\]\\s])"));
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/github/gitbucket/markedj/InlineLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ public String output(String src) {
}
}

// internallink
{
List<String> cap = rules.get("internallink").exec(src);
if(!cap.isEmpty()){
src = src.substring(cap.get(0).length());
out.append(renderer.internallink(cap.get(0)));
continue;
}
}

// slide
{
List<String> cap = rules.get("slide").exec(src);
if(!cap.isEmpty()){
src = src.substring(cap.get(0).length());
out.append(renderer.slide(cap.get(1)));
continue;
}
}

// autolink
{
List<String> cap = rules.get("autolink").exec(src);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/io/github/gitbucket/markedj/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ protected void token(String src, boolean top, boolean bq, LexerContext context){
}
}

// math
{
List<String> cap = rules.get("math").exec(src);
if(!cap.isEmpty()){
src = src.substring(cap.get(0).length());
context.pushToken(new MathToken(cap.get(0)));
continue;
}
}

// heading
{
List<String> cap = rules.get("heading").exec(src);
Expand Down Expand Up @@ -173,7 +183,7 @@ protected void token(String src, boolean top, boolean bq, LexerContext context){
if(!cap.isEmpty()){
src = src.substring(cap.get(0).length());
context.pushToken(new BlockquoteStartToken());
token(cap.get(0).replaceAll("(?m) *> ?", ""), top, true, context);
token(cap.get(0).replaceAll("(?m)^ *> ?", ""), top, true, context);
context.pushToken(new BlockquoteEndToken());
continue;
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/io/github/gitbucket/markedj/Parser.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package io.github.gitbucket.markedj;

import io.github.gitbucket.markedj.rule.Rule;
import io.github.gitbucket.markedj.token.*;

import java.util.Map;
import java.util.Stack;

import io.github.gitbucket.markedj.rule.Rule;
import io.github.gitbucket.markedj.token.CodeToken;
import io.github.gitbucket.markedj.token.HeadingToken;
import io.github.gitbucket.markedj.token.HtmlToken;
import io.github.gitbucket.markedj.token.ListStartToken;
import io.github.gitbucket.markedj.token.MathToken;
import io.github.gitbucket.markedj.token.ParagraphToken;
import io.github.gitbucket.markedj.token.TableToken;
import io.github.gitbucket.markedj.token.TextToken;
import io.github.gitbucket.markedj.token.Token;

public class Parser {

protected Options options;
Expand Down Expand Up @@ -70,6 +78,10 @@ protected String tok(ParserContext context) {
CodeToken t = (CodeToken) context.currentToken();
return renderer.code(t.getCode(), t.getLang(), t.isEscaped());
}
case "MathToken": {
MathToken t = (MathToken) context.currentToken();
return renderer.mathBlock(t.getCode());
}
case "TableToken": {
TableToken t = (TableToken) context.currentToken();
StringBuilder outCell = new StringBuilder();
Expand Down
40 changes: 33 additions & 7 deletions src/main/java/io/github/gitbucket/markedj/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ public Renderer(Options options){
public String code(String code, String lang, boolean escaped){
if(lang != null){
StringBuilder sb = new StringBuilder();
sb.append("<pre><code class=\"" + options.getLangPrefix() + escape(lang, true) + "\">");
if(escaped){
sb.append(code);
if (escape(lang, true).equals("math")) {
sb.append("<div class=\"" + options.getLangPrefix() + escape(lang, true) + " hljs\">\n$$\n");
sb.append(code); // Markdownのエスケープはしない
sb.append("\n$$</div>\n");
} else {
sb.append(escape(code, true));
sb.append("<pre><code class=\"" + options.getLangPrefix() + escape(lang, true) + " hljs\">");
if(escaped){
sb.append(code);
} else {
sb.append(escape(code, true));
}
sb.append("\n</code></pre>\n");
}
sb.append("\n</code></pre>\n");
return sb.toString();
} else {
StringBuilder sb = new StringBuilder();
sb.append("<pre><code>");
sb.append("<pre><code class=\"hljs\">");
if(escaped){
sb.append(code);
} else {
Expand All @@ -36,6 +42,15 @@ public String code(String code, String lang, boolean escaped){
}
}

public String mathBlock(String code){
StringBuilder sb = new StringBuilder();
sb.append("<div>\n");
sb.append(code); // Markdownのエスケープはしない
sb.append("\n</div>\n");
return sb.toString();
}


public String blockquote(String quote){
return "<blockquote>\n" + quote + "</blockquote>\n";
}
Expand Down Expand Up @@ -112,7 +127,7 @@ public String em(String text){
}

public String codespan(String text){
return "<code>" + text + "</code>";
return "<code class=\"hljs\">" + text + "</code>";
}

public String br(){
Expand All @@ -131,6 +146,16 @@ public String oembed(String href) {
return "<a class=\"oembed\" href=\"" + href + "\">" + href + "</a>";
}

public String slide(String fileNo) {
return "<var class=\"slideshow\" id=\"slide-" + fileNo +"\" slide=\"" + fileNo + "\">" + fileNo + "</var>";
}

public Object internallink(String noTerm) {
String no = noTerm.substring("#".length());
return "<var class=\"internallink\" id=\"internallink-" + no +"\" internallink=\"" + no + "\">" + noTerm + "</var>";
}


public String footnoteref(String key) {
return "<sup class=\"footnote-ref\" id=\"fnref-" + escape(key) + "\">"
+ "<a href=\"#fn-" + escape(key) + "\">" + key + "</a></sup>";
Expand Down Expand Up @@ -214,4 +239,5 @@ public String getAlign() {
}
}


}
21 changes: 12 additions & 9 deletions src/main/java/io/github/gitbucket/markedj/rule/FindAllRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ public FindAllRule(String regex){
}

public List<String> exec(String src) {
Matcher matcher = pattern.matcher(src);
List<String> result = new ArrayList<>();
while(matcher.find()){
result.add(matcher.group(0));
}
if(!result.isEmpty()){
return result;
} else {
return Collections.emptyList();
try {
Matcher matcher = pattern.matcher(src);
List<String> result = new ArrayList<>();
while (matcher.find()) {
result.add(matcher.group(0));
}
if (!result.isEmpty()) {
return result;
}
} catch(StackOverflowError e){
// ignore
}
return Collections.emptyList();
}
}
19 changes: 11 additions & 8 deletions src/main/java/io/github/gitbucket/markedj/rule/FindFirstRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ public FindFirstRule(String regex){
}

public List<String> exec(String src) {
Matcher matcher = pattern.matcher(src);
if(matcher.find()){
List<String> result = new ArrayList<>();
for(int i = 0; i <= matcher.groupCount(); i++){
result.add(matcher.group(i));
try {
Matcher matcher = pattern.matcher(src);
if (matcher.find()) {
List<String> result = new ArrayList<>();
for (int i = 0; i <= matcher.groupCount(); i++) {
result.add(matcher.group(i));
}
return result;
}
return result;
} else {
return Collections.emptyList();
} catch(StackOverflowError e){
// ignore
}
return Collections.emptyList();
}
}
20 changes: 20 additions & 0 deletions src/main/java/io/github/gitbucket/markedj/token/MathToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.gitbucket.markedj.token;

public class MathToken implements Token {

@Override
public String getType() {
return "MathToken";
}

private String code;

public MathToken(String code){
this.code = code;
}

public String getCode() {
return code;
}

}
42 changes: 41 additions & 1 deletion src/test/java/io/github/gitbucket/markedj/ExtendParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,47 @@ public void testSmb() throws Exception {
public void testAmp() {
String markdown = "```\n&read_data\n```";
String result = Marked.marked(markdown);
String check = "<pre><code>&amp;read_data\n</code></pre>\n";
String check = "<pre><code class=\"hljs\">&amp;read_data\n</code></pre>\n";
org.junit.Assert.assertEquals(check, result);
}


@Test
public void testSlide() throws Exception {
String md = Marked.marked("[slide 41]", new Options());
String result = Marked.marked(md, new Options());
assertEquals("<p><var class=\"slideshow\" id=\"slide-41\" slide=\"41\">41</var></p>\n", result);
}

@Test
public void testMath() {
String markdown = "```math\n\\(ax^2 + bx + c = 0\\)\n```";
String result = Marked.marked(markdown);
String check = "<div class=\"lang-math hljs\">\n$$\n\\(ax^2 + bx + c = 0\\)\n$$</div>\n";
org.junit.Assert.assertEquals(check, result);
}

@Test
public void testMath2() {
String markdown = "$$\\boldsymbol{x} = \\left[ a, b, c \\right] ^{T} \\tag{1} \\label{aaa}$$";
String result = Marked.marked(markdown);
String check = "<p>$$\\boldsymbol{x} = \\left[ a, b, c \\right] ^{T} \\tag{1} \\label{aaa}$$</p>\n";
org.junit.Assert.assertEquals(check, result);
}


@Test
public void testInternallink() {
String markdown = "#123";
String result = Marked.marked(markdown);
String check = "<p><var class=\"internallink\" id=\"internallink-123\" internallink=\"123\">#123</var></p>\n";
org.junit.Assert.assertEquals(check, result);
}
@Test
public void testInternallink2() {
String markdown = "#123 へリンク";
String result = Marked.marked(markdown);
String check = "<p><var class=\"internallink\" id=\"internallink-123\" internallink=\"123\">#123</var> へリンク</p>\n";
org.junit.Assert.assertEquals(check, result);
}

Expand Down
6 changes: 5 additions & 1 deletion src/test/java/io/github/gitbucket/markedj/MarkedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ public void testEm() throws Exception {

@Test
public void testStackoverFlow() throws Exception {
// Make sure StackOverflowError does not occur by em regular expression
Marked.marked(loadResourceAsString("stackoverflow.txt"), new Options());
}

@Test
public void testStackoverFlow2() throws Exception {
Marked.marked(loadResourceAsString("stackoverflow2.txt"), new Options());
}

@Test
public void testNptable() throws Exception {
String result = Marked.marked(loadResourceAsString("nptable.md"), new Options());
Expand Down
Loading

0 comments on commit ffeae42

Please sign in to comment.