Skip to content

Commit

Permalink
Merge pull request support-project#11 from support-project/develop
Browse files Browse the repository at this point in the history
Release v1.4.0
  • Loading branch information
koda-masaru committed May 23, 2016
2 parents 126bed2 + 84a09b2 commit 7607b78
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 70 deletions.
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.3.0</version>
<version>1.4.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
3 changes: 2 additions & 1 deletion src/main/java/io/github/gitbucket/markedj/Grammer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class Grammer {
public static String BLOCK_HEADING = "^ *(#{1,6}) *([^\\n]+?) *#* *(?:\\n+|$)";
public static String BLOCK_LHEADING = "^([^\\n]+)\\n *(=|-){2,} *(?:\\n+|$)";
public static String BLOCK_BLOCKQUOTE = "^( *>[^\\n]+(\\n(?!" + removeLineStart(DEF) + ")[^\\n]+)*\\n*)+";
public static String BLOCK_LIST = "^( *)(" + BULLET + ") [\\s\\S]+?(?:" + HR + "|\\n+(?=" + removeLineStart(DEF) + ")|\\n{2,}(?! )(?!\\1" + BULLET + " )\\n*|\\s*$)";
//public static String BLOCK_LIST = "^( *)(" + BULLET + ") [\\s\\S]+?(?:" + HR + "|\\n+(?=" + removeLineStart(DEF) + ")|\\n{2,}(?! )(?!\\1" + BULLET + " )\\n*|\\s*$)";
public static String BLOCK_LIST = "^( *)(" + BULLET + ") [\\s\\S]+?(?:\\n{2,}(?! )(?!\\1" + BULLET + " )\\n*|\\s*$)";
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+|$)";
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/gitbucket/markedj/InlineLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public String output(String src) {
} else {
key = cap.get(1).replaceAll("\\s+", "");
}
Lexer.Link link = links.get(key);
Lexer.Link link = links.get(key.toLowerCase());
if(link == null || isEmpty(link.getHref())){
out.append(renderer.nolink(cap.get(0)));
continue;
Expand Down
82 changes: 54 additions & 28 deletions src/main/java/io/github/gitbucket/markedj/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.util.*;

import static io.github.gitbucket.markedj.Utils.*;

public class Lexer {

protected Options options;
Expand Down Expand Up @@ -100,31 +102,43 @@ protected void token(String src, boolean top, boolean bq, LexerContext context){
if(!cap.isEmpty()){
src = src.substring(cap.get(0).length());

String[] header = cap.get(1).replaceAll("^ *| *\\| *$", "").split(" *\\| *");
String[] align = cap.get(2).replaceAll("^ *|\\| *$", "").split(" *\\| *");
String[] cells = cap.get(3).replaceAll("\n$", "").split("\n");
String[] headers = cap.get(1).replaceAll("^ *| *\\| *$", "").split(" *\\| *");
String[] aligns = cap.get(2).replaceAll("^ *|\\| *$", "").split(" *\\| *");
String[] rows = cap.get(3).replaceAll("\n$", "").split("\n");

List<String> header2 = Arrays.asList(header);
List<String> headerList = array2list(headers);

List<String> align2 = new ArrayList<>();
for (String s : align) {
List<String> alignList = new ArrayList<>();
for (String s : aligns) {
if(s.matches("^ *-+: *$")){
align2.add("right");
alignList.add("right");
} else if(s.matches("^ *:-+: *$")){
align2.add("center");
alignList.add("center");
} else if(s.matches("^ *:-+ *$")){
align2.add("left");
alignList.add("left");
} else {
align2.add(null);
alignList.add(null);
}
}

int maxColumns = Math.max(headers.length, aligns.length);

List<List<String>> rowList = new ArrayList<>();
for (String row : rows) {
String[] columns = row.split(" *\\| *");
if(maxColumns < columns.length){
maxColumns = columns.length;
}
rowList.add(array2list(columns));
}

List<List<String>> cells2 = new ArrayList<>();
for (String cell : cells) {
cells2.add(Arrays.asList(cell.split(" *\\| *")));
fillList(headerList, maxColumns, "");
fillList(alignList, maxColumns, null);
for(List<String> row: rowList){
fillList(row, maxColumns, "");
}

context.pushToken(new TableToken(header2, align2, cells2));
context.pushToken(new TableToken(headerList, alignList, rowList));
continue;
}
}
Expand Down Expand Up @@ -264,31 +278,43 @@ protected void token(String src, boolean top, boolean bq, LexerContext context){
if(!cap.isEmpty()){
src = src.substring(cap.get(0).length());

String[] header = cap.get(1).replaceAll("^ *| *\\| *$", "").split(" *\\| *");
String[] align = cap.get(2).replaceAll("^ *|\\| *$", "").split(" *\\| *");
String[] cells = cap.get(3).replaceAll("(?: *\\| *)?\\n$", "").split("\\n");
String[] headers = cap.get(1).replaceAll("^ *| *\\| *$", "").split(" *\\| *");
String[] aligns = cap.get(2).replaceAll("^ *|\\| *$", "").split(" *\\| *");
String[] rows = cap.get(3).replaceAll("(?: *\\| *)?\\n$", "").split("\\n");

List<String> header2 = Arrays.asList(header);
List<String> headerList = array2list(headers);

List<String> align2 = new ArrayList<>();
for (String s : align) {
List<String> alignList = new ArrayList<>();
for (String s : aligns) {
if(s.matches("^ *-+: *$")){
align2.add("right");
alignList.add("right");
} else if(s.matches("^ *:-+: *$")){
align2.add("center");
alignList.add("center");
} else if(s.matches("^ *:-+ *$")){
align2.add("left");
alignList.add("left");
} else {
align2.add(null);
alignList.add(null);
}
}

int maxColumns = Math.max(headers.length, aligns.length);

List<List<String>> rowList = new ArrayList<>();
for (String row : rows) {
String[] columns = row.replaceAll("^ *\\| *| *\\| *$", "").split(" *\\| *");
if(maxColumns < columns.length){
maxColumns = columns.length;
}
rowList.add(array2list(columns));
}

List<List<String>> cells2 = new ArrayList<>();
for (String cell : cells) {
cells2.add(Arrays.asList(cell.replaceAll("^ *\\| *| *\\| *$", "").split(" *\\| *")));
fillList(headerList, maxColumns, "");
fillList(alignList, maxColumns, null);
for(List<String> row: rowList){
fillList(row, maxColumns, "");
}

context.pushToken(new TableToken(header2, align2, cells2));
context.pushToken(new TableToken(headerList, alignList, rowList));
continue;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/github/gitbucket/markedj/Marked.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public static String marked(String src) {
}

public static String marked(String src, Options options) {
if (options.isHeaderIdSequential()) {
options.initSequentialId();
}
return marked(src, options, new Renderer(options));
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/io/github/gitbucket/markedj/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ public class Options {
private String langPrefix = "lang-";
// private boolean smartypants = false;
private String headerPrefix = "";
private boolean headerIdSequential;
private boolean xhtml = false;
private boolean linkTargetBlank;

private int sequentialId = 0;
public void initSequentialId() {
sequentialId = 0;
}
public int SequentialId() {
sequentialId++;
return sequentialId;
}


public void setGfm(boolean gfm) {
this.gfm = gfm;
}
Expand Down Expand Up @@ -127,4 +138,20 @@ public boolean isLinkTargetBlank() {
public void setLinkTargetBlank(boolean linkTargetBlank) {
this.linkTargetBlank = linkTargetBlank;
}

/**
* Get headerIdSequential
* @return the headerIdSequential
*/
public boolean isHeaderIdSequential() {
return headerIdSequential;
}

/**
* Set headerIdSequential
* @param headerIdSequential the headerIdSequential to set
*/
public void setHeaderIdSequential(boolean headerIdSequential) {
this.headerIdSequential = headerIdSequential;
}
}
12 changes: 2 additions & 10 deletions src/main/java/io/github/gitbucket/markedj/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,16 @@ protected String tok(ParserContext context) {
StringBuilder outBody = new StringBuilder();

for(int i = 0; i < t.getHeader().size(); i++){
String align = null;
if(t.getAlign().size() > i){
align = t.getAlign().get(i);
}
outCell.append(renderer.tablecell(
context.getInlineLexer().output(t.getHeader().get(i)), new Renderer.TableCellFlags(true, align)));
context.getInlineLexer().output(t.getHeader().get(i)), new Renderer.TableCellFlags(true, t.getAlign().get(i))));
}
outHeader.append(renderer.tablerow(outCell.toString()));

for(int i = 0; i < t.getCells().size(); i++){
outCell.setLength(0);
for(int j = 0; j < t.getCells().get(i).size(); j++){
String align = null;
if(t.getAlign().size() > j){
align = t.getAlign().get(j);
}
outCell.append(renderer.tablecell(
context.getInlineLexer().output(t.getCells().get(i).get(j)), new Renderer.TableCellFlags(false, align)));
context.getInlineLexer().output(t.getCells().get(i).get(j)), new Renderer.TableCellFlags(false, t.getAlign().get(j))));
}
outBody.append(renderer.tablerow(outCell.toString()));
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/github/gitbucket/markedj/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public String html(String html){
}

public String heading(String text, int level, String raw){
if (options.isHeaderIdSequential()) {
return "<h" + level + " id=\"" + options.getHeaderPrefix() +
options.SequentialId() + "\">" + text + "</h" + level + ">\n";
}
return "<h" + level + " id=\"" + options.getHeaderPrefix() +
raw.toLowerCase().replaceAll("[^\\w]+", "-") + "\">" + text + "</h" + level + ">\n";
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/github/gitbucket/markedj/Utils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.gitbucket.markedj;

import java.util.ArrayList;
import java.util.List;

public class Utils {

public static String escape(String html){
Expand Down Expand Up @@ -27,4 +30,18 @@ public static boolean isEmpty(String str){
return str == null || str.length() == 0;
}

public static void fillList(List<String> list, int length, String value){
while(list.size() < length){
list.add(value);
}
}

public static List<String> array2list(String[] array){
List<String> list = new ArrayList<>(array.length);
for(String value: array){
list.add(value);
}
return list;
}

}
93 changes: 65 additions & 28 deletions src/test/java/io/github/gitbucket/markedj/ExtendParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,71 @@
import org.junit.Test;

public class ExtendParseTest {

public static void main(String[] args) throws IOException {
ExtendParseTest test = new ExtendParseTest();
test.debug();
}

private void debug() throws IOException {
Options options = new Options();
options.setBreaks(true);
options.setLinkTargetBlank(true);
options.setHeaderPrefix("markdown-agenda-");
options.setHeaderIdSequential(true);

@Test
public void testLinkTarget() throws Exception {
Options options = new Options();
options.setBreaks(true);
options.setLinkTargetBlank(true);

String md = loadResourceAsString("link_target.md");
String result = Marked.marked(md, options);
System.out.println(result);
String expect = loadResourceAsString("link_target.html");
System.out.println(expect);
assertEquals(expect, result);
}

private String loadResourceAsString(String path) throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024 * 8];
int length = 0;
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
return new String(out.toByteArray(), "UTF-8");
} finally {
in.close();
}
}
String md = loadResourceAsString("debug.md");
String result = Marked.marked(md, options);
System.out.println(result);
}

@Test
public void testHeaderId() throws Exception {
Options options = new Options();
options.setBreaks(true);
options.setLinkTargetBlank(true);
options.setHeaderPrefix("markdown-agenda-");
options.setHeaderIdSequential(true);

String md = loadResourceAsString("header_id.md");
String result = Marked.marked(md, options);
System.out.println(result);
String expect = loadResourceAsString("header_id.html");
System.out.println(expect);
assertEquals(expect, result);
}





@Test
public void testLinkTarget() throws Exception {
Options options = new Options();
options.setBreaks(true);
options.setLinkTargetBlank(true);

String md = loadResourceAsString("link_target.md");
String result = Marked.marked(md, options);
// System.out.println(result);
String expect = loadResourceAsString("link_target.html");
// System.out.println(expect);
assertEquals(expect, result);
}

private String loadResourceAsString(String path) throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024 * 8];
int length = 0;
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
return new String(out.toByteArray(), "UTF-8");
} finally {
in.close();
}
}

}
Loading

0 comments on commit 7607b78

Please sign in to comment.