Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

preparing stripping of <p> for list items

  • Loading branch information...
commit e70b9eab11d57f83d12d0dd35c414b46fc8a17ba 1 parent fa5068b
@ebookineur authored
View
55 src/java/com/ebookineur/markdown/impl/scanner/BlockList.java
@@ -8,6 +8,7 @@
public class BlockList extends BlockElement {
private final static String BULLETED_MARKERS = "*+-";
+ private final boolean _debug = false;
static boolean isList(String line) {
return checkIfList(line) > 0;
@@ -122,18 +123,12 @@ public BlockList(MdParser parser, MdOutput output) {
public void render(MarkdownRenderer renderer, DocumentInformation di)
throws IOException {
- System.out.println(">>>>>>> list:start>>>>>");
- for (String line : _lines) {
- System.out.println(line);
- }
- System.out.println(">>>>>>> list:end>>>>>");
-
// the first line contains a list starting point
parseList(0, 0, _output, renderer);
}
private int parseList(int index, int level, MdOutput output,
- MarkdownRenderer renderer) {
+ MarkdownRenderer renderer) throws IOException {
boolean withPara = false;
String firstLine = _lines.get(index);
@@ -209,8 +204,7 @@ private int parseList(int index, int level, MdOutput output,
}
if (items.size() > 0) {
- itemOutput(level, output, renderer, type, items, null,
- withPara);
+ itemOutput(level, output, renderer, type, items, null, withPara);
}
output.println(renderer.block_list_end(type, level));
@@ -290,13 +284,50 @@ private int firstChar(String line) {
private void itemOutput(int level, MdOutput output,
MarkdownRenderer renderer, int type, List<String> items,
- List<String> formattedLines, boolean withPara) {
+ List<String> formattedLines, boolean withPara) throws IOException {
+
+ if (_debug) {
+ System.out.println(">>>>>>> list:start>>>>>");
+ for (String line : items) {
+ System.out.println(line);
+ }
+ if (formattedLines != null) {
+ System.out.println("......");
+ for (String line : formattedLines) {
+ System.out.println(line);
+ }
+ }
+ System.out.println(">>>>>>> list:end>>>>>");
+ }
+ MdInputLinesImpl input = new MdInputLinesImpl();
+
+ MdOutputLinesImpl o = new MdOutputLinesImpl();
+
+ for (String line : items) {
+ input.addLine(trimLevel(level, line));
+ }
+
+ _parser.render(input, o);
+
+ List<String> result = o.getLines();
if (formattedLines != null) {
- items.addAll(formattedLines);
+ result.addAll(formattedLines);
}
- output.println(renderer.block_list_item(type, level, items, withPara));
+ output.println(renderer.block_list_item(type, level, result, withPara));
items.clear();
}
+ private String trimLevel(int level, String line) {
+ for (int i = 0; i < line.length(); i++) {
+ char c = line.charAt(i);
+ if (c == ' ') {
+ } else if ((c != ' ') && (c != '\t')) {
+ break;
+ }
+ }
+
+ return line;
+ }
+
}
View
160 src/java/com/ebookineur/markdown/renderer/DefaultXHtmlRenderer.java
@@ -5,6 +5,8 @@
import com.ebookineur.markdown.MarkdownRenderer;
public class DefaultXHtmlRenderer implements MarkdownRenderer {
+ private final boolean _debug = false;
+
public DefaultXHtmlRenderer() {
}
@@ -181,10 +183,25 @@ public String block_list_end(int type, int level) {
@Override
public String block_list_item(int type, int level, List<String> lines,
boolean withPara) {
+
+ if (_debug) {
+ System.out.println("@@ - block_list_item (withPara?:" + withPara
+ + ")");
+ for (String line : lines) {
+ System.out.println(line);
+ }
+ System.out.println("@@ - end");
+ }
StringBuilder sb = new StringBuilder();
- sb.append("<li>");
+ boolean paraAdded = false;
if (withPara) {
- sb.append("<p>");
+ // we don't add a <p> if the content to insert
+ // already starts with <p>
+ String firstLine = lines.get(0);
+ if (!firstLine.startsWith("<p>")) {
+ paraAdded = true;
+ sb.append("<p>");
+ }
}
int lineno = 0;
for (String line : lines) {
@@ -194,12 +211,21 @@ public String block_list_item(int type, int level, List<String> lines,
}
sb.append(line);
}
- if (withPara) {
+ if (paraAdded) {
sb.append("</p>");
}
- sb.append("</li>");
- return sb.toString();
+ // if we don't want paras, we may have to remove it as the
+ // content to process may already be containing this <p>!
+ String content = sb.toString();
+ // if (!withPara) {
+ // if (content.startsWith("<p>")) {
+ // System.out.println("..." + content + "...");
+ // content = content.substring(3, content.length() - 4);
+ // }
+ // }
+
+ return "<li>" + content + "</li>";
}
@Override
@@ -208,4 +234,128 @@ public String hrule() {
sb.append("<hr />");
return sb.toString();
}
+
+ private String removeTag(String line, String tag) {
+ StringBuilder sb = new StringBuilder();
+ StringBuilder tagName = new StringBuilder();
+
+ int level = 0;
+
+ int state = 0;
+
+ for (int i = 0; i < line.length(); i++) {
+ char c = line.charAt(i);
+
+ // System.out.println("[" + c + "," + state + "]:" + sb.toString());
+
+ switch (state) {
+ case 0:
+ if (c == '<') {
+ state = 1;
+ } else {
+ sb.append(c);
+ }
+ break;
+
+ case 1:
+ if (c == '/') {
+ state = 5;
+ } else if (c == '>') {
+ // we have an opening tag
+ if (tagName.toString().trim().equalsIgnoreCase(tag)) {
+ if (level == 0) {
+ // we have our opening tag
+ // we don't output it
+ tagName.setLength(0);
+ state = 0;
+ } else {
+ // we have an embeded one, we need
+ // to output it
+ sb.append('<');
+ sb.append(tagName);
+ sb.append('>');
+ tagName.setLength(0);
+ state = 0;
+ }
+ level++;
+ } else {
+ // not our tag... we output it
+ sb.append('<');
+ sb.append(tagName);
+ sb.append('>');
+ tagName.setLength(0);
+ state = 0;
+ }
+ } else {
+ tagName.append(c);
+ }
+ break;
+
+ case 5:
+ if (c == '>') {
+ // we have a closing tag
+ if (tagName.toString().trim().equalsIgnoreCase(tag)) {
+ level--;
+ if (level == 0) {
+ // this is it! we found our matching
+ // closing tag
+ // we append the rest of the string and
+ // we are done!
+ if (i < (line.length() - 1)) {
+ sb.append(line.substring(i + 1));
+ }
+ return sb.toString();
+ } else {
+ // closing an embeded one.. we
+ // output it
+ sb.append("</");
+ sb.append(tagName);
+ sb.append('>');
+ tagName.setLength(0);
+ state = 0;
+ }
+ } else {
+ // not our tag, we output it
+ sb.append("</");
+ sb.append(tagName);
+ sb.append('>');
+ tagName.setLength(0);
+ state = 0;
+ }
+ } else {
+ tagName.append(c);
+ }
+ break;
+ }
+ }
+
+ if (state == 1) {
+ sb.append('<');
+ sb.append(tagName);
+ } else if (state == 5) {
+ sb.append("</");
+ sb.append(tagName);
+ }
+
+ return sb.toString();
+ }
+
+ private void t(String line, String tag) {
+ System.out.println("Removing <" + tag + "> from:");
+ System.out.println(line);
+ System.out.println(removeTag(line, tag));
+ System.out.println(".");
+ }
+
+ public static void main(String[] args) {
+ DefaultXHtmlRenderer x = new DefaultXHtmlRenderer();
+
+ x.t("allo", "p");
+ x.t("<p>allo</p>", "p");
+ x.t("<p>allo <i>italics</i></p>", "p");
+ x.t("<p>allo <i>italics</i>xx </p>", "p");
+ x.t("<p>allo</p> blah blah", "p");
+ x.t("<p>allo<p>inside</p></p>", "p");
+ x.t("<p>allo<p>inside</p>xxx</p>", "p");
+ }
}
View
4 src/test/com/ebookineur/markdown/TestMarkdownParser.java
@@ -37,8 +37,8 @@ public void testBlock() throws Exception {
//testFile("tests/simple/test60.txt", extensions);
//testFile("tests/simple/test61.txt", extensions);
- //testFile("tests/simple/test62.txt", extensions);
- testFile("tests/simple/test63.txt", extensions);
+ testFile("tests/simple/test62.txt", extensions);
+ //testFile("tests/simple/test63.txt", extensions);
}
@Test
View
1  tests/simple/test63.html
@@ -5,4 +5,5 @@
<p>this is a blockquote
inside a list item.</p>
</blockquote></li>
+<li><p>another item</p></li>
</ul>
View
2  tests/simple/test63.txt
@@ -2,4 +2,4 @@
> this is a blockquote
> inside a list item.
-
+* another item
Please sign in to comment.
Something went wrong with that request. Please try again.