Index: jrst2/src/java/org/codelutin/jrst/JRSTLexer.java diff -u jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.6 jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.7 --- jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.6 Thu Feb 8 11:48:15 2007 +++ jrst2/src/java/org/codelutin/jrst/JRSTLexer.java Thu Mar 8 15:05:07 2007 @@ -23,9 +23,9 @@ * Created: 28 oct. 06 00:44:20 * * @author poussin - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * - * Last update: $Date: 2007/02/08 11:48:15 $ + * Last update: $Date: 2007/03/08 15:05:07 $ * by : $Author: bpoussin $ */ @@ -68,24 +68,26 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private Log log = LogFactory.getLog(JRSTLexer.class); - static final public String TABLE = "table"; - static final public String ROW = "row"; - static final public String CELL = "cell"; - - static final public String TABLE_HEADER = "header"; - static final public String TABLE_WIDTH = "width"; - - static final public String ROW_END_HEADER = "endHeader"; - - static final public String CELL_INDEX_START = "indexStart"; - static final public String CELL_INDEX_END = "indexEnd"; - static final public String CELL_BEGIN = "begin"; - static final public String CELL_END = "end"; - - static final public String DIRECTIVE = "directive"; - static final public String DIRECTIVE_TYPE = "type"; - static final public String DIRECTIVE_VALUE = "value"; - + static final public String BLANK_LINE = "blankLine"; + + static final public String TABLE = "table"; + static final public String ROW = "row"; + static final public String CELL = "cell"; + + static final public String TABLE_HEADER = "header"; + static final public String TABLE_WIDTH = "width"; + + static final public String ROW_END_HEADER = "endHeader"; + + static final public String CELL_INDEX_START = "indexStart"; + static final public String CELL_INDEX_END = "indexEnd"; + static final public String CELL_BEGIN = "begin"; + static final public String CELL_END = "end"; + + static final public String DIRECTIVE = "directive"; + static final public String DIRECTIVE_TYPE = "type"; + static final public String DIRECTIVE_VALUE = "value"; + /** * retient le niveau du titre, pour un titre de type double, on met * deux fois le caratere dans la chaine, sinon on le met une seul fois. @@ -263,6 +265,9 @@ result = peekLiteralBlock(); } if (result == null) { + result = peekBlankLine(); + } + if (result == null) { result = peekPara(); } @@ -293,6 +298,22 @@ return in.getCharNumber(); } + public Element peekBlankLine() throws IOException { + beginPeek(); + Element result = null; + + // must have one blank line before + String line = in.readLine(); + if (line != null && line.matches("\\s*")) { + int level = level(line); + result = DocumentHelper.createElement(BLANK_LINE) + .addAttribute("level", String.valueOf(level)); + } + + endPeek(); + return result; + } + /** * @return * @throws IOException @@ -301,7 +322,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String line = in.readLine(); if (line != null) { @@ -345,7 +366,7 @@ // must have one blank line before String line = in.readLine(); if (line != null && line.matches("\\s*")) { - in.skipBlankLines(); +// in.skipBlankLines(); line = in.readLine(); if (line != null && line.matches("-{3,}\\s*")) { line = in.readLine(); @@ -372,25 +393,40 @@ beginPeek(); Element result = null; - in.skipBlankLines(); - - String [] lines = readBlock(0); - if (lines.length > 0) { - int level = level(lines[0]); - String para = joinBlock(lines); - - if (para.endsWith(": ::")) { - para = para.substring(0, para.length() - " ::".length()); - in.unread("::", true); - } else if (para.endsWith("::")) { - para = para.substring(0, para.length() - ":".length()); // keep one : - in.unread("::", true); +// in.skipBlankLines(); + + String [] lines; + do { + lines = readBlock(0); + if (lines.length > 0) { + int level = level(lines[0]); + String para = joinBlock(lines); + + boolean literal = false; + if (para.endsWith(": ::")) { + para = para.substring(0, para.length() - " ::".length()); + in.unread("::", true); + literal = true; + } else if (para.endsWith("::")) { + para = para.substring(0, para.length() - ":".length()); // keep one : + in.unread("::", true); + literal = true; + } + + if (para.length() == 0 || ":".equals(para)) { + if (literal) { + in.readLine(); // eat "::" + } + } else { + // if para is empty, there are error and possible + // infiny loop on para, force read next line + result = DocumentHelper.createElement(PARAGRAPH) + .addAttribute("level", String.valueOf(level)) + .addText(para); + } } + } while (result == null && lines.length > 0); - result = DocumentHelper.createElement(PARAGRAPH) - .addAttribute("level", String.valueOf(level)) - .addText(para); - } endPeek(); return result; @@ -400,7 +436,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String [] prefix = in.readLines(2); if (prefix.length == 2 && @@ -447,7 +483,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String line = in.readLine(); // (?i) case inensitive on docinfo item if (line != null && line.matches("^:((?i)"+DOCINFO_ITEM+"):.*$")) { @@ -476,7 +512,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String line = in.readLine(); if (line != null) { @@ -707,7 +743,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String line = in.readLine(); if (line != null && line.matches("^\\s*["+escapeRegex(BULLET_CHAR)+"] \\S.*")) { int level = level(line); @@ -746,7 +782,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String line = in.readLine(); if (line != null) { Pattern pattern = Pattern.compile("^\\s*:([^:]+): [^\\s].*"); @@ -794,7 +830,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String [] lines = in.readLines(2); if (lines.length == 2) { int level = level(lines[0]); @@ -852,7 +888,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String line = in.readLine(); if (line != null) { Pattern pattern = Pattern.compile("^\\s*(\\(?)(#|\\d+|[a-z]|[A-Z]|[ivxlcdm]+|[IVXLCDM]+)([\\.)]) [^\\s].*"); @@ -926,7 +962,7 @@ beginPeek(); Element result = null; - in.skipBlankLines(); +// in.skipBlankLines(); String line = in.readLine(); if (line != null) { if (startsWithTitleChar(line)) { Index: jrst2/src/java/org/codelutin/jrst/JRSTReader.java diff -u jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.6 jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.7 --- jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.6 Thu Feb 8 11:48:15 2007 +++ jrst2/src/java/org/codelutin/jrst/JRSTReader.java Thu Mar 8 15:05:07 2007 @@ -23,9 +23,9 @@ * Created: 27 oct. 06 00:15:34 * * @author poussin - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * - * Last update: $Date: 2007/02/08 11:48:15 $ + * Last update: $Date: 2007/03/08 15:05:07 $ * by : $Author: bpoussin $ */ @@ -362,7 +362,7 @@ inline(e); } catch (DocumentException eee) { if (log.isWarnEnabled()) { - log.warn("Can inline text for " + e, eee); + log.warn("Can't inline text for " + e, eee); } } } @@ -474,7 +474,10 @@ System.out.println(item); } while (!lexer.eof() && itemNotEquals(TITLE, item) && isUpperLevel(item, parent)) { - if (itemEquals(PARAGRAPH, item)) { + if (itemEquals(JRSTLexer.BLANK_LINE, item)) { + // go to the next element + lexer.remove(); + } else if (itemEquals(PARAGRAPH, item)) { lexer.remove(); Element para = parent.addElement(PARAGRAPH); copyLevel(item,para); @@ -903,15 +906,32 @@ // search all LITERAL and replace it with special mark // this prevent substitution in literal, example **something** must not // change in literal - ArrayList literals = new ArrayList(); + Map temporaries = new HashMap(); Matcher matcher = REGEX_LITERAL.matcher(text); + int index = 0; while(matcher.find()) { int start = matcher.start(); int end = matcher.end(); - String literal = matcher.group(1); - literals.add(literal); + String literal = "<"+LITERAL+">" + matcher.group(1) + ""; + String key = "literal" + index++; + temporaries.put(key, literal); + + text = text.substring(0, start) + "``" + key + "``" + text.substring(end); + } - text = text.substring(0, start) + "``" + (literals.size() - 1) + "``" + text.substring(end); + // search all REGEX_INLINE_REFERENCE and replace it with special mark + // this prevent substitution of URL with REGEX_REFERENCE. Use same + // mechanisme as literal for that + matcher = REGEX_INLINE_REFERENCE.matcher(text); + index = 0; + while(matcher.find()) { + int start = matcher.start(); + int end = matcher.end(); + String reference = "<"+REFERENCE+" refuri='"+matcher.group(2)+"'>"+matcher.group(1)+""; + String key = "inlineReference" + index++; + temporaries.put(key, reference); + + text = text.substring(0, start) + "``" + key + "``" + text.substring(end); } @@ -949,8 +969,8 @@ String start = text.substring(0, matcher.start()); String end = text.substring(matcher.end()); - int literalIndex = Integer.parseInt(matcher.group(1)); - text = start + "<"+LITERAL+">" + literals.get(literalIndex) + "" + end; + String tempKey = matcher.group(1); + text = start + temporaries.get(tempKey) + end; } Element result = DocumentHelper.parseText(""+text+"").getRootElement(); Index: jrst2/src/java/org/codelutin/jrst/ReStructuredText.java diff -u jrst2/src/java/org/codelutin/jrst/ReStructuredText.java:1.2 jrst2/src/java/org/codelutin/jrst/ReStructuredText.java:1.3 --- jrst2/src/java/org/codelutin/jrst/ReStructuredText.java:1.2 Mon Nov 6 15:08:47 2006 +++ jrst2/src/java/org/codelutin/jrst/ReStructuredText.java Thu Mar 8 15:05:07 2007 @@ -23,9 +23,9 @@ * Created: 27 oct. 06 11:10:30 * * @author poussin - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * - * Last update: $Date: 2006/11/06 15:08:47 $ + * Last update: $Date: 2007/03/08 15:05:07 $ * by : $Author: bpoussin $ */ @@ -182,6 +182,7 @@ public static final Pattern REGEX_STRONG = Pattern.compile("\\*\\*(.+?)\\*\\*"); public static final Pattern REGEX_LITERAL = Pattern.compile("``(.+?)``"); public static final Pattern REGEX_REFERENCE = Pattern.compile("(http://[-/%#&\\._\\w]+)([^-/%#&\\._\\w]|$)"); + public static final Pattern REGEX_INLINE_REFERENCE = Pattern.compile("`(.+) ?<(http://[-/%#&\\._\\w]+)>`_"); public static final Pattern REGEX_EMAIL = Pattern.compile("(^|[^_\\w])([-\\._\\w]+@[-\\._\\w]+)([^-\\._\\w]|$)"); public static final Pattern REGEX_FOOTNOTE_REFERENCE = Pattern.compile("\\[([0-9]+?|#)\\]"); public static final Pattern REGEX_CITATION_REFERENCE = Pattern.compile("\\[([^\\]]+?)\\]");