Index: jrst2/src/java/org/codelutin/jrst/JRSTReader.java diff -u jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.13 jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.14 --- jrst2/src/java/org/codelutin/jrst/JRSTReader.java:1.13 Tue Apr 24 14:28:59 2007 +++ jrst2/src/java/org/codelutin/jrst/JRSTReader.java Wed Apr 25 13:29:17 2007 @@ -23,30 +23,24 @@ * Created: 27 oct. 06 00:15:34 * * @author poussin - * @version $Revision: 1.13 $ + * @version $Revision: 1.14 $ * - * Last update: $Date: 2007/04/24 14:28:59 $ + * Last update: $Date: 2007/04/25 13:29:17 $ * by : $Author: sletellier $ */ package org.codelutin.jrst; import static org.codelutin.jrst.ReStructuredText.*; - import static org.codelutin.i18n.I18n._; - import java.io.IOException; import java.io.Reader; import java.io.StringReader; -import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.TreeSet; import java.util.regex.Matcher; -import java.util.regex.Pattern; - import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.logging.Log; @@ -282,7 +276,7 @@ * warning (done) * * - * @author poussin + * @author poussin, letellier */ public class JRSTReader { @@ -380,8 +374,8 @@ } /** - * @param in - * @param root + * @param lexer + * @return Element * @throws Exception */ private Element composeDocument(JRSTLexer lexer) throws Exception { @@ -396,8 +390,6 @@ // le header item = lexer.peekHeader(); if (itemEquals(HEADER, item)){ - // TODO elever la ligne pour quelle ne soit pas lu 2 fois - lexer.removeLine(Integer.valueOf(item.attributeValue("line"))); Element decoration = result.addElement(DECORATION); Element header = decoration.addElement(HEADER); JRSTReader reader = new JRSTReader(); @@ -406,7 +398,10 @@ header.appendContent(doc.getRootElement()); } - + item = lexer.peekRemove(); + if (itemEquals("remove", item)) + lexer.remove(); + // le titre du doc item = lexer.peekTitle(); if (itemEquals(TITLE, item)) { @@ -500,7 +495,11 @@ return result; } - + /** + * @param lexer + * @throws DocumentException + * @throws IOException + */ private void skipBlankLine(JRSTLexer lexer) throws IOException, DocumentException { Element item = lexer.peekBlankLine(); // skip blank line @@ -513,8 +512,10 @@ /** - * *@param root - * @return + * * + * @param lexer + * @param root + * @return Element * @throws DocumentException * @throws IOException */ @@ -529,21 +530,23 @@ if (itemEquals(JRSTLexer.BLANK_LINE, item)) { // go to the next element lexer.remove(); - } else if (itemEquals(DOCTEST_BLOCK, item)) { + }else if (itemEquals("remove", item)) { + lexer.remove(); + } else if (itemEquals(DOCTEST_BLOCK, item)) { lexer.remove(); - Element list = composeDoctestBlock(lexer, item); + Element list = composeDoctestBlock(item); parent.add(list); } else if (itemEquals(ADMONITION, item)) { lexer.remove(); - Element list = composeAdmonition(lexer, item); + Element list = composeAdmonition(item); parent.add(list); } else if (itemEquals(SIDEBAR, item)) { lexer.remove(); - Element list = composeSidebar(lexer, item); + Element list = composeSidebar(item); parent.add(list); } else if (itemEquals(TOPIC, item)) { lexer.remove(); - Element list = composeTopic(lexer, item); + Element list = composeTopic(item); parent.add(list); } else if (itemEquals(PARAGRAPH, item)) { lexer.remove(); @@ -569,7 +572,7 @@ para.setText(item.getText()); } else if (itemEquals(JRSTLexer.TABLE, item)) { lexer.remove(); - Element table = composeTable(lexer, item); + Element table = composeTable(item); parent.add(table); // Element para = parent.addElement(TABLE); // copyLevel(item, para); @@ -592,7 +595,7 @@ parent.add(list); } else if (itemEquals(BLOCK_QUOTE, item)) { lexer.remove(); - Element list =composeBlockQuote(lexer, item); + Element list =composeBlockQuote(item); parent.add(list); } else if (itemEquals(OPTION_LIST, item)) { Element list =composeOptionList(lexer); @@ -612,48 +615,45 @@ System.out.println(item.asXML());*/ item = lexer.peekTitleOrBodyElement(); - - - } return parent; } - - - private Element composeOptionList(JRSTLexer lexer) throws Exception { + /** + * @param lexer + * @return Element + * @throws Exception + * @throws DocumentException + */ + private Element composeOptionList(JRSTLexer lexer) throws DocumentException, Exception{ Element item = lexer.peekOption(); Element result = DocumentHelper.createElement(OPTION_LIST); while (itemEquals(OPTION_LIST, item) ) { lexer.remove(); Element optionListItem = result.addElement(OPTION_LIST_ITEM); - - Element optionGroup = optionListItem.addElement(OPTION_GROUP); - List option = (List)item.selectNodes("option"); - for (Element e : option){ Element eOption = optionGroup.addElement(OPTION); eOption.addElement("option_string").setText(e.attributeValue("option_string")); if (e.attributeValue("delimiterExiste").equals("true")){ eOption.addElement("option_argument").addAttribute("delimiter", e.attributeValue("delimiter")).setText(e.attributeValue("option_argument")); } - - } JRSTReader reader = new JRSTReader(); String text = item.getText(); Document doc = reader.read(new StringReader(text)); optionListItem.addElement("description").appendContent(doc.getRootElement()); - - item = lexer.peekOption(); } return result; } + /** + * @param Element item + * @return Element + * @throws Exception + */ - - private Element composeTopic(JRSTLexer lexer, Element item) throws Exception { + private Element composeTopic(Element item) throws Exception { Element result = null; result=DocumentHelper.createElement(TOPIC); Element title = result.addElement("title"); @@ -668,9 +668,13 @@ return result; } + /** + * @param Element item + * @return Element + * @throws Exception + */ - - private Element composeSidebar(JRSTLexer lexer, Element item) throws Exception { + private Element composeSidebar(Element item) throws Exception { Element result = null; result=DocumentHelper.createElement(SIDEBAR); Element title = result.addElement("title"); @@ -692,7 +696,12 @@ return result; } - + /** + * @param lexer + * @param item + * @return Element + * @throws Exception + */ private Element composeLineBlock(JRSTLexer lexer, Element item) throws Exception { Element result = null; @@ -721,7 +730,6 @@ Element newItem = DocumentHelper.createElement(LINE_BLOCK); Boolean done = false; for (int i=cnt;i0){ Element eLine = newItem.addElement("line"); eLine.addAttribute("level", ""+(levels[i]-1)); @@ -733,30 +741,29 @@ } Element eLineBlock= result.addElement(LINE_BLOCK); + // Appel recursif eLineBlock.appendContent(composeLineBlock(lexer,newItem)); } } cnt++; } - - - /*result=DocumentHelper.createElement(LINE_BLOCK); - JRSTReader reader = new JRSTReader(); - String text = item.getText(); - Document doc = reader.read(new StringReader(text)); - result.appendContent(doc.getRootElement());*/ - return result; } - - - private Element composeDoctestBlock(JRSTLexer lexer, Element item) { + /** + * @param Element item + * @return Element + */ + private Element composeDoctestBlock(Element item) { return item; } + /** + * @param Element item + * @return Element + * @throws Exception + */ - - private Element composeBlockQuote(JRSTLexer lexer, Element item) throws Exception { + private Element composeBlockQuote(Element item) throws Exception { Element result = null; result=DocumentHelper.createElement(BLOCK_QUOTE); JRSTReader reader = new JRSTReader(); @@ -767,9 +774,12 @@ attribution.setText(item.attributeValue(ATTRIBUTION)); return result; } - - - private Element composeAdmonition(JRSTLexer lexer, Element item) throws Exception { + /** + * @param Element item + * @return Element + * @throws Exception + */ + private Element composeAdmonition(Element item) throws Exception { Element result = null; if (item.attributeValue("type").equalsIgnoreCase(ADMONITION)){ result=DocumentHelper.createElement(ADMONITION); @@ -799,7 +809,7 @@ /** * @param item - * @return + * @return Node */ private Node composeDirective(Element item) { Node result = item; @@ -818,9 +828,8 @@ /** - * @param lexer - * @param item - * @return + * @param Element item + * @return Element */ private Element composeSubstitutionDefinition(Element item) { Element result = item; @@ -833,11 +842,10 @@ /** - * @param lexer - * @param item - * @return + * @param Element item + * @return Element */ - private Element composeTable(JRSTLexer lexer, Element item) throws Exception { + private Element composeTable(Element item) throws Exception { Element result = DocumentHelper.createElement(TABLE); @@ -929,7 +937,11 @@ return result; } - + /** + * @param lexer + * @return Element + * @throws Exception + */ private Element composeBulletList(JRSTLexer lexer) throws Exception { Element item = lexer.peekBulletList(); Element result = DocumentHelper.createElement(BULLET_LIST); @@ -947,7 +959,11 @@ } return result; } - + /** + * @param lexer + * @return Element + * @throws Exception + */ private Element composeEnumeratedList(JRSTLexer lexer) throws Exception { Element item = lexer.peekEnumeratedList(); Element result = DocumentHelper.createElement(ENUMERATED_LIST); @@ -970,7 +986,11 @@ } return result; } - + /** + * @param lexer + * @return Element + * @throws Exception + */ private Element composeDefinitionList(JRSTLexer lexer) throws Exception { Element item = lexer.peekBodyElement(); Element result = DocumentHelper.createElement(DEFINITION_LIST); @@ -1001,7 +1021,11 @@ } return result; } - + /** + * @param lexer + * @return Element + * @throws Exception + */ private Element composeFieldList(JRSTLexer lexer) throws Exception { Element item = lexer.peekBodyElement(); Element result = DocumentHelper.createElement(FIELD_LIST); @@ -1013,7 +1037,11 @@ } return result; } - + /** + * @param lexer + * @return Element + * @throws Exception + */ private Element composeFieldItemList(JRSTLexer lexer) throws Exception { Element item = lexer.peekFieldList(); if (itemEquals(FIELD_LIST, item)) { @@ -1037,10 +1065,9 @@ /** - * @param items - * @return - * @throws DocumentException - * @throws IOException + * @param lexer + * @return Element + * @throws Exception */ private Element composeSection(JRSTLexer lexer) throws Exception { Element result = DocumentHelper.createElement(SECTION); @@ -1085,7 +1112,7 @@ * * @param item * @param firstTitle - * @return + * @return boolean * @throws DocumentException */ private boolean isUpperLevel(Element subSection, Element section) throws DocumentException { @@ -1105,7 +1132,7 @@ * * @param item * @param firstTitle - * @return + * @return boolean * @throws DocumentException */ private boolean isSameLevel(Element subSection, Element section) throws DocumentException { @@ -1118,7 +1145,12 @@ boolean result = subSectionLevel == sectionLevel; return result; } - + /** + * @param Element e1 + * @param Element e2 + * @param String ... attnames + * @return boolean + */ private boolean hasSameAttribute(Element e1, Element e2, String ... attnames) { boolean result = true; for (String attname : attnames) { @@ -1131,7 +1163,11 @@ } return result; } - + /** + * @param Element from + * @param Element to + * @throws DocumentException + */ private void copyLevel(Element from, Element to) throws DocumentException { String level = from.attributeValue("level"); if (level == null) { @@ -1139,12 +1175,24 @@ } to.addAttribute("level", level); } - + /** + * @param String name + * @param Element e + * @return boolean + * @throws DocumentException + */ private boolean itemEquals(String name, Element e) throws DocumentException { boolean result = itemEquals(name, e, false, false); return result; } - + /** + * @param String name + * @param Element e + * @param boolean throwError + * @param boolean eof + * @return boolean + * @throws DocumentException + */ private boolean itemEquals(String name, Element e, boolean throwError, boolean eof) throws DocumentException { boolean result = e != null && name.equals(e.getName()); if (ERROR_MISSING_ITEM && !result && throwError && !eof) { @@ -1152,7 +1200,11 @@ } return result; } - + /** + * @param String name + * @param Element e + * @return boolean + */ private boolean itemNotEquals(String name, Element e) { boolean result = e == null || !name.equals(e.getName()); return result; @@ -1160,8 +1212,7 @@ /** * Parse text in element and replace text with parse result - * @param text - * @return + * @param Element e * @throws DocumentException */ private void inline(Element e) throws DocumentException { @@ -1243,6 +1294,4 @@ e.setText(""); e.appendContent(result); } -} - - +} \ No newline at end of file Index: jrst2/src/java/org/codelutin/jrst/JRSTLexer.java diff -u jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.13 jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.14 --- jrst2/src/java/org/codelutin/jrst/JRSTLexer.java:1.13 Tue Apr 24 14:28:59 2007 +++ jrst2/src/java/org/codelutin/jrst/JRSTLexer.java Wed Apr 25 13:29:17 2007 @@ -23,9 +23,9 @@ * Created: 28 oct. 06 00:44:20 * * @author poussin - * @version $Revision: 1.13 $ + * @version $Revision: 1.14 $ * - * Last update: $Date: 2007/04/24 14:28:59 $ + * Last update: $Date: 2007/04/25 13:29:17 $ * by : $Author: sletellier $ */ @@ -62,7 +62,7 @@ * * * - * @author poussin + * @author poussin, letellier */ public class JRSTLexer { @@ -72,7 +72,7 @@ static final public String TITLE_CHAR = "-=-~'`^+:!\"#$%&*,./;|?@\\_[\\]{}<>()"; static final public String DOCINFO_ITEM = "author|authors|organization|address|contact|version|revision|status|date|copyright"; - public static final String ADMONITION = "admonition|attention|caution|danger|error|hint|important|note|tip|warning"; + public static final String ADMONITION_PATTERN = "admonition|attention|caution|danger|error|hint|important|note|tip|warning"; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Title Elements @@ -85,7 +85,14 @@ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ static final public String DOCINFO = "docinfo"; - + + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + // Decoration Elements + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + public static final String DECORATION = "decoration"; + public static final String HEADER = "header"; + //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Structural Elements //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -107,6 +114,12 @@ static final public String DEFINITION_LIST = "definition_list"; static final public String ENUMERATED_LIST = "enumerated_list"; static final public String OPTION_LIST = "option_list"; + public static final String LINE_BLOCK = "line_block"; + public static final String LINE = "line"; + public static final String BLOCK_QUOTE = "block_quote"; + public static final String ATTRIBUTION = "attribution"; + public static final String DOCTEST_BLOCK = "doctest_block"; + public static final String ADMONITION = "admonition"; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Table Elements @@ -159,7 +172,7 @@ /** * true if no more element to read - * @return + * @return boolean * @throws IOException */ public boolean eof() throws IOException { @@ -178,14 +191,18 @@ public void remove() throws IOException { in.skip(elementLength); } - public void removeLine(int l) throws IOException { - // TODO - } + /** + * start peek + * @throws IOException + */ private void beginPeek() throws IOException { elementLength = 0; in.mark(); } - + /** + * end peek + * @throws IOException + */ private void endPeek() throws IOException { elementLength = in.readSinceMark(); in.reset(); @@ -196,7 +213,7 @@ * blank line) * * @param minLeftMargin min left blank needed to accept to read block - * @return + * @return String[] * @throws IOException */ private String [] readBlock(int minLeftMargin) throws IOException { @@ -216,14 +233,22 @@ /** * All lines are joined and left and right spaces are removed during join * - * @param lines - * @return + * @param String[] lines + * @return String */ private String joinBlock(String [] lines) { String result = joinBlock(lines, " ", true); return result; } - + /** + * All lines are joined whith the String joinSep + * and left and right spaces are removed if trim + * + * @param String[] lines + * @param String joinSep + * @param Boolean trim + * @return String + */ private String joinBlock(String [] lines, String joinSep, boolean trim) { String result = ""; String sep = ""; @@ -236,18 +261,27 @@ } return result; } + /** + * search if the doc have an header + *
+     *    .. header:: This space for rent. aaaa **aaaa**
+     * 
+ * + * @return Element + * @throws IOException + */ public Element peekHeader() throws IOException { - beginPeek(); // TODO elever la ligne pour quelle ne soit pas lu 2 fois + beginPeek(); Element result = null; String[] line = in.readAll(); if (line != null){ int i=0; for (String l : line){ i++; - if (l.matches("^\\s*.. header:: .*")){ + if (l.matches("^\\s*.. "+HEADER+":: .*")){ int level = level(l); - l=l.replaceAll("^\\s*.. header:: ", ""); - result = DocumentHelper.createElement("header").addAttribute("level", String.valueOf(level)); + l=l.replaceAll("^\\s*.. "+HEADER+":: ", ""); + result = DocumentHelper.createElement(HEADER).addAttribute("level", String.valueOf(level)); result.addAttribute("line", ""+i); result.setText(l); } @@ -259,7 +293,8 @@ } /** * Return title or para - * @return + * + * @return Element * @throws IOException */ public Element peekTitleOrBodyElement() throws IOException { @@ -283,7 +318,7 @@ * Dans le monde * * - * @return + * @return Element * @throws IOException */ public Element peekDocInfo() throws IOException { @@ -302,7 +337,7 @@ /** * Return para - * @return + * @return Element * @throws IOException */ public Element peekBodyElement() throws IOException { @@ -356,6 +391,9 @@ result = peekBlockQuote(); } if (result == null) { + result = peekRemove(); + } + if (result == null) { result = peekPara(); } @@ -363,11 +401,58 @@ return result; } - - public Element peekOption() throws IOException { + /** + * Elements already read + * + * @return Element + * @throws IOException + */ + public Element peekRemove() throws IOException { beginPeek(); Element result = null; String line = in.readLine(); + if (line != null){ + // Le header est parsé des le debut + if (line.matches("^\\s*.. "+HEADER+":: .*")){ + result= DocumentHelper.createElement("remove").addAttribute("level", ""+level(line)); + } + } + endPeek(); + return result; + } + /** + * read options + *
+     * -a            command-line option "a"
+	 * -1 file, --one=file, --two file
+     *        Multiple options with arguments.
+     * 
+ * + * @return Element + * @throws IOException + */ + public Element peekOption() throws IOException { + /* + -a command-line option "a" + -1 file, --one=file, --two file + Multiple options with arguments. + + ________________________________ + v | | + -{1,2}\w+ ->|',' | + |'='-----|-> \w+ --->|',' + |' '-----| |' '---+ + |" " -----> \w+ ---> end | + ^ | + |_________________________| + + -{1,2} --> 1 or 2 tirets + \w+ ------> word characters one or more times + + */ + beginPeek(); + Element result = null; + String line = in.readLine(); if (line != null){ if (line.matches("^\\s*-{1,2}.\\s+.*$")){ result = DocumentHelper.createElement(OPTION_LIST).addAttribute("level", ""+level(line)); @@ -385,13 +470,11 @@ option_stringTmp=option_stringTmp.substring(matcher.end()+1, option_stringTmp.length()); option.addAttribute("delimiterExiste", "false"); boolean done=false; - if (delimiter==' '){ - + if (delimiter==' '){ // S'il ya 2 espace a suivre, l'option est finit if (option_stringTmp.charAt(0)==' '){ done=true; } } - if ((delimiter=='=' || delimiter==' ')&&!done){ option.addAttribute("delimiterExiste", "true"); option.addAttribute("delimiter", ""+delimiter); @@ -399,39 +482,40 @@ String option_argument; if (matcher.find()){ option_argument= matcher.group().substring(1, matcher.group().length()); - - option.addAttribute("option_argument", option_argument); + option.addAttribute("option_argument", option_argument); if (option_stringTmp.charAt(option_argument.length())==','){ delimiter = ','; line=line.substring(option_string.length()+option_argument.length()+3,line.length()); - } else if (option_stringTmp.charAt(option_argument.length())==' ') done=true; } - else{ + else{ // Si la description n'est pas sur la meme ligne option_argument=option_stringTmp; option.addAttribute("option_argument", option_argument); line = in.readLine(); result.setText(line.trim()); } } - if (done) - result.setText(option_stringTmp.substring(matcher.end(),option_stringTmp.length()).trim()); - - }while (delimiter==','); - - - - - } + result.setText(option_stringTmp.substring(matcher.end()-1,option_stringTmp.length()).trim()); + }while (delimiter==','); + } } - endPeek(); return result; } - + /** + * read topic + *
+     * -.. topic:: Title
+     *
+     *     Body.
+     * 
+ * + * @return Element + * @throws IOException + */ private Element peekTopic() throws IOException { beginPeek(); Element result = null; @@ -453,7 +537,18 @@ endPeek(); return result; } - + /** + * read sidebar + *
+     * .. sidebar:: Title
+     *    :subtitle: If Desired
+     *
+     *    Body.
+     * 
+ * + * @return Element + * @throws IOException + */ private Element peekSidebar() throws IOException { beginPeek(); Element result = null; @@ -485,7 +580,20 @@ endPeek(); return result; } - + /** + * read line block + *
+     * |        A one, two, a one two three four
+     * |
+     * | Half a bee, philosophically,
+     * |     must, *ipso facto*, half not be.
+     * | But half the bee has got to be,
+     * |               *vis a vis* its entity.  D'you see?
+     * 
+ * + * @return Element + * @throws IOException + */ private Element peekLineBlock() throws IOException { beginPeek(); Element result = null; @@ -497,12 +605,9 @@ lines[0]=line; for (int i=0;i + * >>> print 'this is a Doctest block' + * this is a Doctest block + * + * + * @return Element + * @throws IOException + */ private Element peekDoctestBlock() throws IOException { - // A voir les >>> sont transforme en >>> beginPeek(); Element result = null; String line = in.readLine(); if (line != null){ if (line.matches("^\\s*>>>\\s.*")){ int level = level(line); - result = DocumentHelper.createElement("doctest_block").addAttribute("level", String.valueOf(level)); + result = DocumentHelper.createElement(DOCTEST_BLOCK).addAttribute("level", String.valueOf(level)); result.addAttribute("xml:space", "preserve"); line += "\n"+joinBlock(readBlock(level)); result.setText(line); @@ -579,6 +690,19 @@ endPeek(); return result; } + /** + * read block quote + *
+     * As a great paleontologist once said,
+     * 
+     *     This theory, that is mine, is mine.
+     * 
+     *     -- Anne Elk (Miss)
+     * 
+ * + * @return Element + * @throws IOException + */ private Element peekBlockQuote() throws IOException { beginPeek(); Element result = null; @@ -607,32 +731,43 @@ done=true; } if (blockQuote!=null){ - result = DocumentHelper.createElement("block_quote").addAttribute("level", String.valueOf(level)); - result.addAttribute("attribution", blockQuote); + result = DocumentHelper.createElement(BLOCK_QUOTE).addAttribute("level", String.valueOf(level)); + result.addAttribute(ATTRIBUTION, blockQuote); result.setText(txt.trim()); } } endPeek(); return result; } - + /** + * read admonitions : admonition|attention|caution|danger|error|hint|important|note|tip|warning + *
+     * .. Attention:: All your base are belong to us.
+     * .. admonition:: And, by the way...
+     * 
+     *    You can make up your own admonition too.
+     * 
+ * + * @return Element + * @throws IOException + */ private Element peekAdmonition() throws IOException { beginPeek(); Element result = null; String line = in.readLine(); if (line != null) { String lineTest = line.toLowerCase(); - Pattern pAdmonition = Pattern.compile("^\\.\\.\\s*("+ADMONITION+")::\\s*(.*)$"); + Pattern pAdmonition = Pattern.compile("^\\.\\.\\s*("+ADMONITION_PATTERN+")::\\s*(.*)$"); Matcher matcher = pAdmonition.matcher(lineTest); if (matcher.matches()) { boolean admonition=false; - matcher = Pattern.compile(ADMONITION).matcher(lineTest); + matcher = Pattern.compile(ADMONITION_PATTERN).matcher(lineTest); matcher.find(); - result = DocumentHelper.createElement("admonition").addAttribute("level", String.valueOf(0)); + result = DocumentHelper.createElement(ADMONITION).addAttribute("level", String.valueOf(0)); int level; - if (matcher.group().equals("admonition")){ + if (matcher.group().equals(ADMONITION)){ // Il y a un titre pour un admonition general admonition=true; - result.addAttribute("type", "admonition"); + result.addAttribute("type", ADMONITION); String title=line.substring(matcher.end()+2,line.length()); String tmp=in.readLine(); if (!tmp.matches("\\s*")){ @@ -644,7 +779,7 @@ } else result.addAttribute("type",matcher.group()); - matcher = Pattern.compile(ADMONITION).matcher(lineTest); + matcher = Pattern.compile(ADMONITION_PATTERN).matcher(lineTest); matcher.find(); line=line.trim(); String firstLine=""; @@ -662,48 +797,15 @@ result.setText(firstLine); } } - /*if (result!=null) - System.out.println(result.asXML());*/ endPeek(); return result; } - public String readBlockWithBlankLine(int level) throws IOException{ - String txt=""; - String [] lines = in.readWhile("(^ {"+level+"}.*)|(\\s*)"); - while (lines.length > 0) { - for (String l : lines) { - l=l.trim(); - txt += l + "\n"; - - } - lines = in.readWhile("(^ {"+level+"}.*)|(\\s*)"); - } - return txt; - } - /** - * Lit les premieres ligne non vide et les retourne, rien n'est modifier par rapport - * aux positions dans le fichier. Util pour afficher a l'utilisateur les lignes - * qui ont produit une erreur - * - * @return + /** + * read blank line + * + * @return Element * @throws IOException */ - public String readNotBlanckLine() throws IOException { - beginPeek(); - in.skipBlankLines(); - String line = joinBlock(in.readUntilBlank(), "\n", false); - endPeek(); - return line; - } - - public int getLineNumber() { - return in.getLineNumber(); - } - - public int getCharNumber() { - return in.getCharNumber(); - } - public Element peekBlankLine() throws IOException { beginPeek(); Element result = null; @@ -721,16 +823,15 @@ } /** - * @return + * read directive or reference + * + * @return Element * @throws IOException */ public Element peekDirectiveOrReference() throws IOException { beginPeek(); - - Element result = null; -// in.skipBlankLines(); - - String line = in.readLine(); + Element result = null; + String line = in.readLine(); if (line != null) { Pattern pImage = Pattern.compile("^\\.\\.\\s*(?:\\|([^|]+)\\|)?\\s*(\\w+)::\\s*(.*)$"); Matcher matcher = pImage.matcher(line); @@ -758,11 +859,15 @@ directive.setText(text); } } - endPeek(); return result; } - + /** + * read transition + * + * @return Element + * @throws IOException + */ public Element peekTransition() throws IOException { beginPeek(); @@ -783,11 +888,9 @@ } } } - endPeek(); return result; } - /** * read paragraph with attribut level that represente the space numbers * at left side @@ -837,7 +940,18 @@ endPeek(); return result; } - + /** + * read literal block + * + *
+     * ::
+     * 
+     *     LiteralBlock
+     * 
+ * + * @return Element + * @throws IOException + */ public Element peekLiteralBlock() throws IOException { beginPeek(); @@ -882,7 +996,7 @@ * Dans le monde * * - * @return + * @return Element * @throws IOException */ public Element peekDocInfoItem() throws IOException { @@ -913,8 +1027,22 @@ return result; } - - + /** + * read table simple and complexe + *
+     * +------------------------+------------+----------+----------+
+     * | Header row, column 1   | Header 2   | Header 3 | Header 4 |
+     * | (header rows optional) |            |          |          |
+     * +========================+============+==========+==========+
+     * | body row 1, column 1   | column 2   | column 3 | column 4 |
+     * +------------------------+------------+----------+----------+
+     * | body row 2             | Cells may span columns.          |
+     * +------------------------+------------+---------------------+
+     * 
+ * + * @return Element + * @throws IOException + */ public Element peekTable() throws IOException { beginPeek(); @@ -1545,11 +1673,63 @@ endPeek(); return result; } - + /** + * Read block text, block text have same indentation + * + * @param minLeftMargin min left blank needed to accept to read block + * @return String + * @throws IOException + */ + private String readBlockWithBlankLine(int level) throws IOException{ + String txt=""; + String [] lines = in.readWhile("(^ {"+level+"}.*)|(\\s*)"); + while (lines.length > 0) { + for (String l : lines) { + l=l.trim(); + txt += l + "\n"; + + } + lines = in.readWhile("(^ {"+level+"}.*)|(\\s*)"); + } + return txt; + } + /** + * Lit les premieres ligne non vide et les retourne, rien n'est modifier par rapport + * aux positions dans le fichier. Util pour afficher a l'utilisateur les lignes + * qui ont produit une erreur + * + * @return + * @throws IOException + */ + public String readNotBlanckLine() throws IOException { + beginPeek(); + in.skipBlankLines(); + String line = joinBlock(in.readUntilBlank(), "\n", false); + endPeek(); + return line; + } + /** + * return the number of line read + * + * @return int + * @throws IOException + */ + public int getLineNumber() { + return in.getLineNumber(); + } + /** + * return the number of char read + * + * @return int + * @throws IOException + */ + public int getCharNumber() { + return in.getCharNumber(); + } /** * return true if line can be underline or overline for title * @param line - * @return + * @return boolean */ private boolean startsWithTitleChar(String line) { if (line == null || line.length() < 2) { @@ -1560,16 +1740,19 @@ boolean result = line.matches("(["+escapeRegex(TITLE_CHAR)+"])\\1+"); return result; } - /** * @param title_char - * @return + * @return String */ private String escapeRegex(String text) { String result = text.replaceAll("([()[\\\\]*+?.])", "\\\\$1"); return result; } - + /** + * @param String line + * @return int + * @throws IOException + */ private int level(String line) { int result = 0; while (line.length() > result && line.charAt(result) == ' ') { @@ -1577,8 +1760,6 @@ } return result; } - - }