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

refactor some codes #53

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ target/
*.releaseBackup
release.properties
*.pyc

*.iml
.idea/
39 changes: 17 additions & 22 deletions src/main/java/com/github/rjeschke/txtmark/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,19 @@ class Line
public Line xmlEndLine;

/** Constructor. */
public Line()
public Line(String value)
{
//
this.value = value;
init();
}

/**
* Calculates leading and trailing spaces. Also sets empty if needed.
*/
public void init()
private void init()
{
this.leading = 0;
while (this.leading < this.value.length() && this.value.charAt(this.leading) == ' ')
{
this.leading++;
}

if (this.leading == this.value.length())
{
this.setEmpty();
}
else
{
this.isEmpty = false;
this.trailing = 0;
while (this.value.charAt(this.value.length() - this.trailing - 1) == ' ')
{
this.trailing++;
}
}
initLeading();
initTailing();
}

/**
Expand All @@ -92,6 +76,17 @@ public void initLeading()
}
}

private void initTailing()
{
if (this.leading != this.value.length()) {
this.isEmpty = false;
this.trailing = 0;
while (this.value.charAt(this.value.length() - this.trailing - 1) == ' ') {
this.trailing++;
}
}
}

/**
* Skips spaces.
*
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/github/rjeschke/txtmark/LineReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.github.rjeschke.txtmark;

import java.io.IOException;
import java.io.Reader;

/**
* Created by caosh on 2016/6/11.
*/
class LineReader {
private final Reader reader;
private final Configuration config;
private int nextChar;

public LineReader(Reader reader, Configuration config) throws IOException {
this.reader = reader;
this.config = config;
this.nextChar = reader.read();
}

public boolean eof() {
return nextChar == -1;
}

public Line read() throws IOException {
final StringBuilder sb = new StringBuilder(80);
int pos = 0;
boolean eol = false;
while (!eol) {
switch (nextChar) {
case -1:
eol = true;
break;
case '\n':
nextChar = reader.read();
if (nextChar == '\r') {
nextChar = reader.read();
}
eol = true;
break;
case '\r':
nextChar = reader.read();
if (nextChar == '\n') {
nextChar = reader.read();
}
eol = true;
break;
case '\t': {
final int np = pos + (4 - (pos & 3));
while (pos < np) {
sb.append(' ');
pos++;
}
nextChar = reader.read();
break;
}
default:
if (nextChar != '<' || !config.panicMode) {
pos++;
sb.append((char) nextChar);
} else {
pos += 4;
sb.append("&lt;");
}
nextChar = reader.read();
break;
}
}
return new Line(sb.toString());

}
}
63 changes: 4 additions & 59 deletions src/main/java/com/github/rjeschke/txtmark/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,67 +588,11 @@ public final static String process(final Reader reader, final Decorator decorato
private Block readLines() throws IOException
{
final Block block = new Block();
final StringBuilder sb = new StringBuilder(80);
int c = this.reader.read();
LineReader lineReader = new LineReader(reader, config);
LinkRef lastLinkRef = null;
while (c != -1)
while (!lineReader.eof())
{
sb.setLength(0);
int pos = 0;
boolean eol = false;
while (!eol)
{
switch (c)
{
case -1:
eol = true;
break;
case '\n':
c = this.reader.read();
if (c == '\r')
{
c = this.reader.read();
}
eol = true;
break;
case '\r':
c = this.reader.read();
if (c == '\n')
{
c = this.reader.read();
}
eol = true;
break;
case '\t':
{
final int np = pos + (4 - (pos & 3));
while (pos < np)
{
sb.append(' ');
pos++;
}
c = this.reader.read();
break;
}
default:
if (c != '<' || !this.config.panicMode)
{
pos++;
sb.append((char)c);
}
else
{
pos += 4;
sb.append("&lt;");
}
c = this.reader.read();
break;
}
}

final Line line = new Line();
line.value = sb.toString();
line.init();
final Line line = lineReader.read();

// Check for link definitions
boolean isLinkRef = false;
Expand Down Expand Up @@ -1011,4 +955,5 @@ private String process() throws IOException

return out.toString();
}

}
82 changes: 46 additions & 36 deletions src/test/java/com/github/rjeschke/txtmark/ConformityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,38 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;

import org.junit.Assert;
import org.junit.Test;

import com.github.rjeschke.txtmark.Processor;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class ConformityTest
{
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final String RES = "/com/github/rjeschke/txtmark/testsuite/";
private static final String[] TESTS =
{
"Amps and angle encoding", "Auto links", "Backslash escapes", "Blockquotes with code blocks", "Code Blocks",
"Code Spans", "Hard-wrapped paragraphs with list-like lines", "Horizontal rules", "Images",
"Inline HTML (Advanced)", "Inline HTML (Simple)", "Inline HTML comments", "Links, inline style",
"Links, reference style", "Links, shortcut references", "Markdown Documentation - Basics",
"Markdown Documentation - Syntax", "Nested blockquotes", "Ordered and unordered lists",
"Strong and em together", "Tabs", "Tidyness",
};

private final String name;

public ConformityTest(String name) {
this.name = name;
}

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> parameters() {
return Arrays.asList(new Object[][]{
{"Amps and angle encoding"}, {"Auto links"}, {"Backslash escapes"}, {"Blockquotes with code blocks"}, {"Code Blocks"},
{"Code Spans"}, {"Hard-wrapped paragraphs with list-like lines"}, {"Horizontal rules"}, {"Images"},
{"Inline HTML (Advanced)"}, {"Inline HTML (Simple)"}, {"Inline HTML comments"}, {"Links, inline style"},
{"Links, reference style"}, {"Links, shortcut references"}, {"Markdown Documentation - Basics"},
{"Markdown Documentation - Syntax"}, {"Nested blockquotes"}, {"Ordered and unordered lists"},
{"Strong and em together"}, {"Tabs"}, {"Tidyness"},
});
}

private final static String readTextUTF_8(final InputStream in) throws IOException
{
Expand Down Expand Up @@ -106,37 +119,34 @@ public final static String tidy(final String str)
@Test
public void test() throws IOException
{
for (final String name : TESTS)
final InputStream txtIn = ConformityTest.class.getResourceAsStream(RES + name + ".text");
final InputStream cmpIn = ConformityTest.class.getResourceAsStream(RES + name + ".html");
if (txtIn == null || cmpIn == null)
{
final InputStream txtIn = ConformityTest.class.getResourceAsStream(RES + name + ".text");
final InputStream cmpIn = ConformityTest.class.getResourceAsStream(RES + name + ".html");
if (txtIn == null || cmpIn == null)
{
Assert.fail("Unmatched test resources");
}
Assert.fail("Unmatched test resources");
}

final String text = readTextUTF_8(txtIn);
final String compare = readTextUTF_8(cmpIn);
final String text = readTextUTF_8(txtIn);
final String compare = readTextUTF_8(cmpIn);

final String processed = Processor.process(text);
final String processed = Processor.process(text);

final String tCompare = tidy(compare);
final String tProcessed = tidy(processed);
final String tCompare = tidy(compare);
final String tProcessed = tidy(processed);

if (!tCompare.equals(tProcessed))
{
System.out.println("Test: " + name);
System.out.println("=============================================");
System.out.println(tProcessed);
System.out.println("---------------------------------------------");
System.out.println(tCompare);
System.out.println("=============================================");
System.out.println(processed);
System.out.println("---------------------------------------------");
System.out.println(compare);
System.out.println("=============================================");
Assert.fail("Test '" + name + "' failed");
}
if (!tCompare.equals(tProcessed))
{
System.out.println("Test: " + name);
System.out.println("=============================================");
System.out.println(tProcessed);
System.out.println("---------------------------------------------");
System.out.println(tCompare);
System.out.println("=============================================");
System.out.println(processed);
System.out.println("---------------------------------------------");
System.out.println(compare);
System.out.println("=============================================");
Assert.fail("Test '" + name + "' failed");
}
}
}