### Eclipse Workspace Patch 1.0 #P jrst Index: src/main/java/org/nuiton/jrst/JRSTLexer.java =================================================================== --- src/main/java/org/nuiton/jrst/JRSTLexer.java (revision 552) +++ src/main/java/org/nuiton/jrst/JRSTLexer.java (working copy) @@ -204,9 +204,12 @@ */ private int elementLength; + private int lastLevel; + public JRSTLexer(Reader reader) { titleLevels = new ArrayList(); in = new AdvancedReader(reader); + lastLevel = -1; } /** @@ -429,6 +432,9 @@ if (result == null) { result = peekBodyElement(); } + if((result != null) && !result.getQName().getName().equals(BLANK_LINE)) { + lastLevel = Integer.valueOf(result.attributeValue("level")); + } return result; } @@ -718,7 +724,7 @@ Element result = null; String line = in.readLine(); if (line != null) { - if (line.matches("^\\.\\.\\s*(" + TOPIC + ")::\\s*(.*)$")) { + if (line.matches("^\\.\\.\\s(" + TOPIC + ")::\\s(.*)$")) { Matcher matcher = Pattern.compile(TOPIC + "::").matcher(line); matcher.find(); result = DocumentHelper.createElement(TOPIC).addAttribute( @@ -733,7 +739,7 @@ String[] lines = null; if (level != 0) { lines = in.readWhile("(^ {" + level + "}.*)|(\\s*)"); - String txt = line; + String txt = line.trim(); for (String txtTmp : lines) { txt += "\n" + txtTmp.trim(); } @@ -939,37 +945,52 @@ private Element peekBlockQuote() throws IOException { beginPeek(); Element result = null; + + // blank line before block quote String line = in.readLine(); if (line != null) { - if (line.matches("\\s.*")) { - String savedLine = line; - line = in.readLine(); + if(line.matches("\\s*")) { + line = in.readLine(); + } + else if (lastLevel != -1) { + in.unread(line, true); + line = null; + } + } - if (line != null) { - int level = level(line); - String blockQuote = null; - if (level != 0) { - String txt = line; - String[] lines = in.readWhile("(^ {" + level - + "}.*)|(\\s*)"); - for (String l : lines) { - if (l.matches("^ {" + level + "}--\\s*.*")) { - blockQuote = l; - blockQuote = blockQuote.replaceAll("--", "") - .trim(); - } else { - txt += "\n" + l; - } - } - result = DocumentHelper.createElement(BLOCK_QUOTE) - .addAttribute("level", String.valueOf(level)); - if (blockQuote != null) { - result.addAttribute(ATTRIBUTION, blockQuote); - } - result.setText(savedLine + txt); + if (line != null) { + // block quote line + if(line.matches("\\s.*")) { + int blockQuoteLevel = level(line); + if(blockQuoteLevel > lastLevel) { + // block quote text + String blockQuoteText = line.trim(); + String[] lines = in.readWhile("(^\\s{" + blockQuoteLevel + "}[^--].*)|(\\s*)"); + for (String l : lines) { + if((l != null) && (l.length() > 0)) { + blockQuoteText += "\n" + l.trim(); + } } + result = DocumentHelper.createElement(BLOCK_QUOTE).addAttribute("level", String.valueOf(blockQuoteLevel)); + + // attribution text + line = in.readLine(); + if (line != null) { + if(line.matches("^\\s{" + blockQuoteLevel + "}--\\s*.*")) { + line = line.replaceAll("--", "") + .trim(); + result.addAttribute(ATTRIBUTION, line); + } + else { + in.unread(line, true); + } + } + result.setText(blockQuoteText); } } + else { + in.unread(line, true); + } } endPeek(); return result; @@ -1411,7 +1432,7 @@ content = tmpcontent; } else { - content += tmpcontent; + content += tmpcontent; } if (start == -1) { start = tmpstart; @@ -1432,7 +1453,7 @@ // of cell content = ""; } else { - // start = tmpstart; + // start = tmpstart; if (start == -1) { start = tmpstart; } @@ -1823,7 +1844,7 @@ if (lines.length == 2) { int level = level(lines[0]); int levelDef = level(lines[1]); - if (level < levelDef) { + if ((levelDef != lines[1].length()) && (level < levelDef)) { in.unread(lines[1], true); Pattern pattern = Pattern.compile("^\\s*([^:]+)(?: : (.*))?"); Matcher matcher = pattern.matcher(lines[0]); @@ -1831,10 +1852,10 @@ String term = matcher.group(1); String classifiers = matcher.group(2); - result = DocumentHelper.createElement(DEFINITION_LIST) - .addAttribute("level", String.valueOf(level)) - .addAttribute("term", term).addAttribute( - "classifiers", classifiers); + result = DocumentHelper.createElement(DEFINITION_LIST) + .addAttribute("level", String.valueOf(level)) + .addAttribute("term", term).addAttribute( + "classifiers", classifiers); // poussin 20070207 don't read block here because can't // interpret it correctly in JRSTReader @@ -2110,7 +2131,7 @@ Element result = null; String line = in.readLine(); if (line != null) { - if (line.matches("^\\.\\.\\s*.*$")) { + if (line.matches("^\\.\\.\\s+.*$")) { result = DocumentHelper.createElement("comment"); result.addAttribute("level", "0"); result.addAttribute("xml:space", "preserve"); @@ -2118,14 +2139,16 @@ // first line is part of comment result.setText(line.substring(2).trim()); line = in.readLine(); - int level = level(line); - if (level > 0) { - String[] lines = readBlock(level); - String text = line.substring(level); - for (String l : lines) { - text += "\n" + l.substring(level); - } - result.addText(text); + if(line != null) { + int level = level(line); + if (level > 0) { + String[] lines = readBlock(level); + String text = line.substring(level); + for (String l : lines) { + text += "\n" + l.substring(level); + } + result.addText(text); + } } } } @@ -2372,4 +2395,8 @@ } return result; } + + public void setLastLevel(int level) { + this.lastLevel = level; + } }