Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * #%L
    * JRst :: Api
    * 
    * $Id: JRSTReader.java 597 2011-06-01 09:30:30Z kcardineaud $
    * $HeadURL: http://svn.nuiton.org/svn/jrst/tags/jrst-1.4/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java $
    * %%
    * Copyright (C) 2004 - 2010 CodeLutin
    * %%
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as 
   * published by the Free Software Foundation, either version 3 of the 
   * License, or (at your option) any later version.
   * 
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Lesser Public License for more details.
   * 
   * You should have received a copy of the GNU General Lesser Public 
   * License along with this program.  If not, see
   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
   * #L%
   */
  
  /* *
   * JRSTReader.java
   *
   * Created: 27 oct. 06 00:15:34
   *
   * @author poussin
   * @version $Revision: 597 $
   *
   * Last update: $Date: 2011-06-01 11:30:30 +0200 (Wed, 01 Jun 2011) $
   * by : $Author: kcardineaud $
   */
  
  package org.nuiton.jrst;
  
  import static org.nuiton.i18n.I18n._;
  import static org.nuiton.jrst.ReStructuredText.ADDRESS;
  import static org.nuiton.jrst.ReStructuredText.ADMONITION;
  import static org.nuiton.jrst.ReStructuredText.ATTRIBUTION;
  import static org.nuiton.jrst.ReStructuredText.AUTHOR;
  import static org.nuiton.jrst.ReStructuredText.AUTHORS;
  import static org.nuiton.jrst.ReStructuredText.BLOCK_QUOTE;
  import static org.nuiton.jrst.ReStructuredText.BULLET_LIST;
  import static org.nuiton.jrst.ReStructuredText.COLSPEC;
  import static org.nuiton.jrst.ReStructuredText.COMMENT;
  import static org.nuiton.jrst.ReStructuredText.CONTACT;
  import static org.nuiton.jrst.ReStructuredText.COPYRIGHT;
  import static org.nuiton.jrst.ReStructuredText.DATE;
  import static org.nuiton.jrst.ReStructuredText.DECORATION;
  import static org.nuiton.jrst.ReStructuredText.DEFINITION;
  import static org.nuiton.jrst.ReStructuredText.DEFINITION_LIST;
  import static org.nuiton.jrst.ReStructuredText.DEFINITION_LIST_ITEM;
  import static org.nuiton.jrst.ReStructuredText.DESCRIPTION;
  import static org.nuiton.jrst.ReStructuredText.DOCINFO;
  import static org.nuiton.jrst.ReStructuredText.DOCTEST_BLOCK;
  import static org.nuiton.jrst.ReStructuredText.DOCUMENT;
  import static org.nuiton.jrst.ReStructuredText.EMPHASIS;
  import static org.nuiton.jrst.ReStructuredText.ENTRY;
  import static org.nuiton.jrst.ReStructuredText.ENUMERATED_LIST;
  import static org.nuiton.jrst.ReStructuredText.FIELD;
  import static org.nuiton.jrst.ReStructuredText.FIELD_BODY;
  import static org.nuiton.jrst.ReStructuredText.FIELD_LIST;
  import static org.nuiton.jrst.ReStructuredText.FIELD_NAME;
  import static org.nuiton.jrst.ReStructuredText.FOOTER;
  import static org.nuiton.jrst.ReStructuredText.FOOTNOTE;
  import static org.nuiton.jrst.ReStructuredText.FOOTNOTE_REFERENCE;
  import static org.nuiton.jrst.ReStructuredText.FOOTNOTE_SYMBOL;
  import static org.nuiton.jrst.ReStructuredText.GENERATED;
  import static org.nuiton.jrst.ReStructuredText.HEADER;
  import static org.nuiton.jrst.ReStructuredText.IMAGE;
  import static org.nuiton.jrst.ReStructuredText.LABEL;
  import static org.nuiton.jrst.ReStructuredText.LINE;
  import static org.nuiton.jrst.ReStructuredText.LINE_BLOCK;
  import static org.nuiton.jrst.ReStructuredText.LIST_ITEM;
  import static org.nuiton.jrst.ReStructuredText.LITERAL;
  import static org.nuiton.jrst.ReStructuredText.LITERAL_BLOCK;
  import static org.nuiton.jrst.ReStructuredText.OPTION;
  import static org.nuiton.jrst.ReStructuredText.OPTION_ARGUMENT;
  import static org.nuiton.jrst.ReStructuredText.OPTION_GROUP;
  import static org.nuiton.jrst.ReStructuredText.OPTION_LIST;
  import static org.nuiton.jrst.ReStructuredText.OPTION_LIST_ITEM;
  import static org.nuiton.jrst.ReStructuredText.OPTION_STRING;
  import static org.nuiton.jrst.ReStructuredText.ORGANIZATION;
  import static org.nuiton.jrst.ReStructuredText.PARAGRAPH;
  import static org.nuiton.jrst.ReStructuredText.REFERENCE;
  import static org.nuiton.jrst.ReStructuredText.REGEX_ANONYMOUS_HYPERLINK_REFERENCE;
  import static org.nuiton.jrst.ReStructuredText.REGEX_EMAIL;
  import static org.nuiton.jrst.ReStructuredText.REGEX_EMPHASIS;
  import static org.nuiton.jrst.ReStructuredText.REGEX_FOOTNOTE_REFERENCE;
  import static org.nuiton.jrst.ReStructuredText.REGEX_HYPERLINK_REFERENCE;
  import static org.nuiton.jrst.ReStructuredText.REGEX_INLINE_REFERENCE;
  import static org.nuiton.jrst.ReStructuredText.REGEX_LITERAL;
  import static org.nuiton.jrst.ReStructuredText.REGEX_REFERENCE;
  import static org.nuiton.jrst.ReStructuredText.REGEX_STRONG;
  import static org.nuiton.jrst.ReStructuredText.REGEX_SUBSTITUTION_REFERENCE;
 import static org.nuiton.jrst.ReStructuredText.REVISION;
 import static org.nuiton.jrst.ReStructuredText.ROW;
 import static org.nuiton.jrst.ReStructuredText.SECTION;
 import static org.nuiton.jrst.ReStructuredText.SIDEBAR;
 import static org.nuiton.jrst.ReStructuredText.STATUS;
 import static org.nuiton.jrst.ReStructuredText.STRONG;
 import static org.nuiton.jrst.ReStructuredText.SUBSTITUTION_DEFINITION;
 import static org.nuiton.jrst.ReStructuredText.SUBTITLE;
 import static org.nuiton.jrst.ReStructuredText.TABLE;
 import static org.nuiton.jrst.ReStructuredText.TARGET;
 import static org.nuiton.jrst.ReStructuredText.TBODY;
 import static org.nuiton.jrst.ReStructuredText.TERM;
 import static org.nuiton.jrst.ReStructuredText.TGROUP;
 import static org.nuiton.jrst.ReStructuredText.THEAD;
 import static org.nuiton.jrst.ReStructuredText.TITLE;
 import static org.nuiton.jrst.ReStructuredText.TOPIC;
 import static org.nuiton.jrst.ReStructuredText.TRANSITION;
 import static org.nuiton.jrst.ReStructuredText.VERSION;
 
 
 import java.io.File;
 import java.net.URL;
 import java.util.Map;
 
 
 /*
  * 
  * <pre> +--------------------------------------------------------------------+ |
  * document [may begin with a title, subtitle, decoration, docinfo] | |
  * +--------------------------------------+ | | sections [each begins with a
  * title] |
  * +-----------------------------+-------------------------+------------+ |
  * [body elements:] | (sections) | | | - literal | - lists | | - hyperlink
  * +------------+ | | blocks | - tables | | targets | | para- | - doctest | -
  * block | foot- | - sub. defs | | graphs | blocks | quotes | notes | - comments |
  * +---------+-----------+----------+-------+--------------+ | [text]+ | [text] |
  * (body elements) | [text] | | (inline
  * +-----------+------------------+--------------+ | markup) | +---------+
  * </pre>
  * 
  * 
  * Inline support: http://docutils.sourceforge.net/docs/user/rst/quickref.html
  * 
  * <li> STRUCTURAL ELEMENTS: document, section, topic, sidebar <li> STRUCTURAL
  * SUBELEMENTS: title, subtitle, decoration, docinfo, transition <li> docinfo:
  * address, author, authors, contact, copyright, date, field, organization,
  * revision, status, version <li> decoration: footer, header <li> BODY ELEMENTS:
  * admonition, attention, block_quote, bullet_list, caution, citation, comment,
  * compound, container, danger, definition_list, doctest_block, enumerated_list,
  * error, field_list, figure, footnote, hint, image, important, line_block,
  * literal_block, note, option_list, paragraph, pending, raw, rubric,
  * substitution_definition, system_message, table, target, tip, warning <li>
  * SIMPLE BODY ELEMENTS: comment, doctest_block, image, literal_block,
  * paragraph, pending, raw, rubric, substitution_definition, target <li>
  * COMPOUND BODY ELEMENTS: admonition, attention, block_quote, bullet_list,
  * caution, citation, compound, container, danger, definition_list,
  * enumerated_list, error, field_list, figure, footnote, hint, important,
  * line_block, note, option_list, system_message, table, tip, warning <li> BODY
  * SUBELEMENTS: attribution, caption, classifier, colspec, field_name, label,
  * line, option_argument, option_string, term definition, definition_list_item,
  * description, entry, field, field_body, legend, list_item, option,
  * option_group, option_list_item, row, tbody, tgroup, thead <li> INLINE
  * ELEMENTS: abbreviation, acronym, citation_reference, emphasis,
  * footnote_reference, generated, image, inline, literal, problematic,
  * reference, strong, subscript, substitution_reference, superscript, target,
  * title_reference, raw
  * 
  * <pre> DOCUMENT :: ( (title, subtitle?)?, decoration?, (docinfo,
  * transition?)?, STRUCTURE.MODEL; ) decoration :: (header?, footer?) header,
  * footer, definition, description, attention, caution, danger, error, hint,
  * important, note, tip, warning :: (BODY.ELEMENTS;)+ transition :: EMPTY
  * docinfo :: (BIBLIOGRAPHIC.ELEMENTS;)+ BIBLIOGRAPHIC.ELEMENTS :: author |
  * authors | organization | contact | address | version | revision | status |
  * date | copyright | field authors :: ( (author)+ ) field :: (field_name,
  * field_body) field_body, list_item :: (BODY.ELEMENTS;)* STRUCTURE.MODEL :: ( (
  * (BODY.ELEMENTS; | topic | sidebar)+, transition? )*, ( (section),
  * (transition?, (section) )* )? ) BODY.ELEMENTS :: paragraph | compound |
  * container | literal_block | doctest_block | line_block | block_quote | table |
  * figure | image | footnote | citation | rubric | bullet_list | enumerated_list |
  * definition_list | field_list | option_list | attention | caution | danger |
  * error | hint | important | note | tip | warning | admonition | reference |
  * target | substitution_definition | comment | pending | system_message | raw
  * topic :: (title?, (BODY.ELEMENTS;)+) sidebar :: (title, subtitle?,
  * (BODY.ELEMENTS; | topic)+) section :: (title, STRUCTURE.MODEL;) line_block ::
  * (line | line_block)+ block_quote:: ((BODY.ELEMENTS;)+, attribution?)
  * bullet_list, enumerated_list :: (list_item +) definition_list ::
  * (definition_list_item +) definition_list_item :: (term, classifier?,
  * definition) field_list :: (field +) option_list :: (option_list_item +)
  * option_list_item :: (option_string, option_argument *, description)
  * option_string, option_argument :: (#PCDATA) admonition :: (title,
  * (BODY.ELEMENTS;)+)
  * 
  * title, subtitle, author, organization, contact, address, version, revision,
  * status, date, copyright, field_name, paragraph, compound, container,
  * literal_block, doctest_block, attribution, line, term, classifier ::
  * TEXT.MODEL;
  * 
  * TEXT.MODEL :: (#PCDATA | INLINE.ELEMENTS;)* INLINE.ELEMENTS :: emphasis |
  * strong | literal | reference | footnote_reference | citation_reference |
  * substitution_reference | title_reference | abbreviation | acronym | subscript |
  * superscript | inline | problematic | generated | target | image | raw
  * emphasis :: '*' #PCDATA '*' strong :: '**' #PCDATA '**' literal :: '``'
  * #PCDATA '``' footnote_reference :: '[' ([0-9]+|#) ']' citation_reference ::
  * '[' [a-zA-Z]+ ']'
  * 
  * </pre>
  */

Le principe est d'utiliser les methodes peek* JRSTLexer pour prendre l'element que l'on attend, si la methode retourne null ou un autre element et bien c que ce n'est pas le bon choix, cela veut dire que l'element courant est fini d'etre lu (plus de paragraphe dans la section par exemple) ou qu'il y a une erreur dans le fichier en entre.

On construit un arbre XML representant le RST au fur et a mesure, on peut ensuite appliquer une feuille de style ou autre chose avec JRSTGenerator

Tous les elements ont un attribut level qui permet de savoir on il est dans la hierarchie. Le Document a le level -1001, et les sections/titres on pour level les valeurs 1000, -999, -998, ...

de cette facon les methods isUpperLevel et isSameLevel fonctionne pour tous les elements de la meme facon

   abbreviation
   acronym
   address (done)
   admonition (done)
   attention (done)
   attribution
   author (done)
   authors (done)
   block_quote (done)
   bullet_list (done)
   caption
   caution (done)
   citation
   citation_reference
   classifier (done)
   colspec (done)
   comment
   compound
   contact (done)
   container
   copyright (done)
   danger (done)
   date (done)
   decoration (done)
   definition (done)
   definition_list (done)
   definition_list_item (done)
   description (done)
   docinfo (done)
   doctest_block (done)
   document (done)
   emphasis (done)
   entry (done)
   enumerated_list (done)
   error (done)
   field (done)
   field_body (done)
   field_list (done)
   field_name (done)
   figure
   footer (done)
   footnote (done)
   footnote_reference (done)
   generated
   header (done)
   hint (done)
   image (done)
   important (done)
   inline
   label
   legend
   line (done)
   line_block (done)
   list_item (done)
   literal (done)
   literal_block (done)
   note (done)
   option (done)
   option_argument (done)
   option_group (done)
   option_list (done)
   option_list_item (done)
   option_string (done)
   organization (done)
   paragraph (done)
   pending
   problematic
   raw
   reference (done)
   revision (done)
   row (done)
   rubric
   section (done)
   sidebar (done)
   status (done)
   strong (done)
   subscript
   substitution_definition
   substitution_reference
   subtitle (done)
   superscript
   system_message
   table (done)
   target (done)
   tbody (done)
   term (done)
   tgroup (done)
   thead (done)
   tip (done)
   title (done)
   title_reference
   topic (done)
   transition (done)
   version (done)
   warning (done)
 
Created: 27 oct. 06 00:15:34

Author(s):
poussin, letellier
Version:
$Revision: 597 $ Last update: $Date: 2011-06-01 11:30:30 +0200 (Wed, 01 Jun 2011) $ by : $Author: kcardineaud $
 
 public class JRSTReader {

    
to use log facility, just put in your code: log.info(\"...\");
 
     private static Log log = LogFactory.getLog(JRSTReader.class);
 
     protected static final String ANONYMOUS = "anonymous";
     
     protected static final String AUTO = "auto";
     
     protected static final String AUTONUM = "autoNum";
     
     protected static final String AUTONUMLABEL = "autoNumLabel";
     
     protected static final String AUTOSYMBOL = "autoSymbol";
     
     protected static final String ATTR_REFID = "refid";
     
     protected static final String ATTR_INLINE = "inline";
     
     protected static final String ATTR_IDS = "ids";
     
     protected static final String BACKREFS = "backrefs";
     
     protected static final String BULLET = "bullet";
     
     protected static final String CLASS = "class";
     
     protected static final String CONTENTS = "contents";
     
     protected static final String DELIMITER = "delimiter";
     
     protected static final String DELIMITEREXISTE ="delimiterExiste";
     
     protected static final String ENUMTYPE = "enumtype";
     
     protected static final String FOOTNOTES = "footnotes";
     
     protected static final String ID = "id";
     
     protected static final String INCLUDE = "include";
     
     protected static final String LEVEL = "level";
     
     protected static final String NAME = "name";
     
     protected static final String NAMES = "names";
     
     protected static final String NUM = "num";
     
     protected static final String REFURI = "refuri";
     
     protected static final String PREFIX = "prefix";
     
     protected static final String REMOVE = "remove";
 
     protected static final String START = "start";
     
     protected static final String SECTNUM = "sectnum";
     
     protected static final String SUBEXISTE = "subExiste";
     
     protected static final String SUFFIX = "suffix";
     
     protected static final String TRUE = "true";
     
     protected static final String TYPE = "type";
     
     protected static final String TARGETANONYMOUS = "targetAnonymous";
     
     protected static final String VALUE = "value";
     
     protected boolean ERROR_MISSING_ITEM;
 
     protected static int MAX_SECTION_DEPTH = -1000;
 
     protected static Map<StringJRSTDirectivedefaultDirectives;
 
     protected Map<StringJRSTDirectivedirectives = new HashMap<StringJRSTDirective>();
 
     private boolean sectnum;
 
     private Element footer;
 
     private int idMax;
 
     private int symbolMax;
 
     private int symbolMaxRef;
 
     private LinkedList<IntegerlblFootnotes = new LinkedList<Integer>();
 
     private LinkedList<IntegerlblFootnotesRef = new LinkedList<Integer>();
 
     private LinkedList<ElementeFootnotes = new LinkedList<Element>();
 
     private LinkedList<ElementeTarget = new LinkedList<Element>();
 
     private LinkedList<ElementeTargetAnonymous = new LinkedList<Element>();
 
     private LinkedList<ElementeTargetAnonymousCopy = new LinkedList<Element>();
 
     private LinkedList<ElementeTitle = new LinkedList<Element>();
 
     static {
          = new HashMap<StringJRSTDirective>();
         .put(new ImageDirective());
         .put(new DateDirective());
         .put("time"new DateDirective());
         .put(new ContentDirective());
         // defaultDirectives.put("calc", new CalcDirective());
         .put(new SectnumDirective());
         // TODO put here all other directive
     }

    
 
     public JRSTReader() {
     }

    

Parameters:
name
Returns:
the defaultDirectives
 
     public static JRSTDirective getDefaultDirective(String name) {
         return .get(name);
     }

    

Parameters:
name
directive the defaultDirectives to set
 
     public static void addDefaultDirectives(String nameJRSTDirective directive) {
         ..put(namedirective);
     }

    

Parameters:
name
Returns:
the defaultDirectives
 
     public JRSTDirective getDirective(String name) {
         return .get(name);
     }

    

Parameters:
name
directive the defaultDirectives to set
 
     public void addDirectives(String nameJRSTDirective directive) {
         .put(namedirective);
     }

    
On commence par decouper tout le document en Element, puis on construit l'article a partir de ces elements.

Parameters:
reader
Returns:
le document cree
Throws:
java.lang.Exception
 
     public Document read(Reader readerthrows Exception {
         JRSTLexer lexer = new JRSTLexer(reader);
         try {
             Element root = composeDocument(lexer);
 
             Document result = DocumentHelper.createDocument();
             result.setRootElement(root);
 
             root.accept(new VisitorSupport() {
                 @Override
                 public void visit(Element e) {
                     // remove all level attribute
                     e.addAttribute(null);
                     // Constrution du sommaire
                     String type = e.attributeValue();
                     if (type != null) {
                         if (type.equals()) {
                             composeContents(e);
                             e.addAttribute(null);
                         }
                     }
 
                     if (.equalsIgnoreCase(e.attributeValue())) {
                         e.addAttribute(null);
                         try {
                             inline(e);
                         } catch (DocumentException eee) {
                             if (.isWarnEnabled()) {
                                 .warn("Can't inline text for " + eeee);
                             }
                         } catch (UnsupportedEncodingException ee) {
 			    if (.isWarnEnabled()) {
 				.warn("Unsupported encoding " + eee);
 			    }
 			}
                     }
                 }
             });
 
             return result;
         } catch (Exception eee) {
             .error(_("JRST parsing error line %d char %s:\n%s"lexer
                     .getLineNumber(), lexer.getCharNumber(), lexer
                     .readNotBlanckLine()));
             throw eee;
         }
     }

    

exemple :

 ..contents : Sommaire
   depth: 3
 

depth sert a limiter la profondeur du sommaire

Parameters:
Element
 
     private void composeContents(Element e) {
         Element result = DocumentHelper.createElement();
         String option = e.getText();
         int depth = -1;
         // depth: 3
         Pattern pattern = Pattern.compile("\\s*\\:depth\\:\\s*\\p{Digit}+");
         Matcher matcher = pattern.matcher(option);
         if (matcher.matches()) {
             pattern = Pattern.compile("\\p{Digit}+");
             matcher = pattern.matcher(matcher.group());
             if (matcher.find()) {
                 depth = Integer.parseInt(matcher.group());
             }
         }
         int levelInit = 0;
         boolean noTitle = false;
         
         try {
             levelInit = Integer.parseInt(.getFirst().attributeValue(
             		));
         } catch (NumberFormatException eee) {
             .error("Can't parse level in: "
                                     + .getFirst().asXML(), eee);
             return;
         } catch (NoSuchElementException eee) {
             noTitle = true;
         }
 
         LinkedList<Elementtitle = new LinkedList<Element>();
         // on rajoute les refid
         for (int i = 0; i < .size(); i++) {
             ++;
             .get(i).addAttribute( + );
         }
         // on enleve les titres limites par depth
         for (Element el : ) {
             int level = Integer.parseInt(el.attributeValue());
             level = level - levelInit;
             el.addAttribute("" + level);
             if (depth == -1) {
                 title.add(el);
             }
             else {
                 if (depth > level) {
                     title.add(el);
                 }
             }
         }
         e.addAttribute();
         String titleValue = e.attributeValue();
         e.addAttribute(null);
         String value = titleValue.trim().toLowerCase();
         // sans titre c "contents" par default
         if (value.matches("\\s*")) {
             value = ;
             titleValue = "Contents";
         }
         e.addAttribute(value);
         e.addAttribute(value);
         result.addElement().setText(titleValue);
         // on compose les lignes
         if (!noTitle) { //Si il y a des titres à lier à la table des matières
             result.add(composeLineContent(title""));
         }
         e.setText("");
         e.appendContent(result);
     }

    

Parameters:
title <Element> title, String num
Returns:
Element
 
     private Element composeLineContent(LinkedList<ElementtitleString num) {
         Element result = DocumentHelper.createElement();
         if () {
             result.addAttribute("auto-toc");
         }
         Element item = null;
         int cnt = 0;
         while (!title.isEmpty()) {
 
             Element e = title.getFirst();
             int level = Integer.parseInt(e.attributeValue());
             LinkedList<Elementchild = new LinkedList<Element>();
 
             if (level <= 0) {
                 cnt++;
                 title.removeFirst();
                 item = result.addElement();
                 Element para = item.addElement();
                 Element reference = para.addElement();
                 String text = e.getText();
                 String id = e.attributeValue();
                 reference.addAttribute(id);
                 reference.addAttribute(text.replaceAll("\\W+"" ")
                         .trim().toLowerCase().replaceAll("\\W+""-"));
                 // si l'on doit les numeroter
                 if () {
                     Element generated = reference.addElement()
                             .addAttribute();
                     generated.setText(num + cnt + "   ");
                     for (int i = 0; i < .size(); i++) {
                         if (.get(i).attributeValue().equals(id)) {
                             Element generatedTitle = .get(i).addElement(
                                     );
                             generatedTitle.addAttribute();
                             generatedTitle.setText(num + cnt + "   ");
                         }
 
                     }
                 }
                 reference.setText(text.replaceAll("_""").trim());
 
             } else {
                 do {
                     e.addAttribute("" + (level - 1));
                     child.add(e);
                     title.removeFirst();
                     if (!title.isEmpty()) {
                         e = title.getFirst();
                         level = Integer.parseInt(e.attributeValue());
                     }
                 } while (!title.isEmpty() && level > 0);
                 String numTmp = "";
                 // numerotation
                 if () {
                     numTmp = num + cnt + ".";
                 }
                 if (item != null) {
                     item.add(composeLineContent(childnumTmp)); // Appel
                     // recursif
                 } else {
                     result.add(composeLineContent(childnumTmp)); // Appel
                     // recursif
                 }
             }
         }
         return result;
     }

    

Parameters:
lexer
Returns:
Element
Throws:
java.lang.Exception
 
     private Element composeDocument(JRSTLexer lexerthrows Exception {
         Element result = DocumentHelper.createElement();
         result.addAttribute(, String.valueOf( - 1));
 
         Element item = null;
 
         // skip blank line
         skipBlankLine(lexer);
 
         // les liens anonymes
         LinkedList<Elementitems = lexer.refTarget();
         for (Element e : items) {
             .add(e);
 
         }
 
         // le header
         item = lexer.peekHeader();
         if (itemEquals(item)) {
             Element decoration = result.addElement();
             Element header = decoration.addElement();
             header.addAttribute().setText(item.getText());
         }
 
         // le footer
         item = lexer.peekFooter();
         if (itemEquals(item)) {
              = DocumentHelper.createElement();
             Element header = .addElement();
             header.addAttribute().setText(item.getText());
         }
 
         // les hyperlinks
         LinkedList<ElementlistItem = lexer.peekTargetAnonymous();
         if (listItem != null) {
             for (Element e : listItem) {
                 Element anonym = DocumentHelper.createElement();
                 anonym.addAttribute("1");
                 ++;
                 anonym.addAttribute( + );
 
                 anonym.addAttribute(e.attributeValue().trim());
 
                 .add(anonym);
                 .add(anonym);
             }
         }
 
         // les eléments a enlever (deja parser : header, footer...)
         item = lexer.peekRemove();
         if (itemEquals(item)) {
             lexer.remove();
         }
 
         // skip blank line
         skipBlankLine(lexer);
         
         // les commentaires
         List<Elementcomments = lexer.peekAllComment();        
 
         // le titre du doc
         item = lexer.peekTitle();
         if (itemEquals(item)) {
             lexer.remove();
             Element title = result.addElement();
             String txt = item.getText();
             result.addAttribute(txt.replaceAll("[(\\W+)_]"" ")
                     .toLowerCase().trim().replaceAll("\\s+""-"));
             result.addAttribute(txt.toLowerCase().replaceAll(
                     "[(\\W+)_&&[^\\:]]+"" ").trim());
             copyLevel(itemtitle);
             title.addAttribute().setText(txt.trim());
         }
 
         // skip blank line
         skipBlankLine(lexer);
 
         // le sous titre du doc
         item = lexer.peekTitle();
         if (itemEquals(item)) {
             lexer.remove();
             Element subtitle = result.addElement();
             String txt = item.getText();
             subtitle.addAttribute(txt.replaceAll("[(\\W+)_]"" ")
                     .toLowerCase().trim().replaceAll("\\s+""-"));
             subtitle.addAttribute(txt.toLowerCase().replaceAll(
                     "[(\\W+)_]"" ").trim());
             copyLevel(itemsubtitle);
             DocumentHelper.createElement();
             subtitle.addAttribute().setText(txt.trim());
         }
 
         // skip blank line
         skipBlankLine(lexer);
 
         // les infos du doc
         item = lexer.peekDocInfo();
         Element documentinfo = null;
         while (itemEquals(item) || itemEquals(item)) {
 
             if (documentinfo == null) {
                 documentinfo = result.addElement();
             }
             skipBlankLine(lexer);
             if (itemEquals(item)) {
                 Element field = composeFieldItemList(lexer);
                 documentinfo.add(field);
             } else {
                 if ("author".equalsIgnoreCase(item.attributeValue())) {
                     documentinfo.addElement().addAttribute(,
                             ).setText(item.getText());
                 } else if ("date".equalsIgnoreCase(item.attributeValue())) {
                     documentinfo.addElement()
                             .addAttribute().setText(
                                     item.getText().trim());
                 } else if ("organization".equalsIgnoreCase(item
                         .attributeValue())) {
                     documentinfo.addElement().addAttribute(
                             ).setText(item.getText().trim());
                 } else if ("contact".equalsIgnoreCase(item
                         .attributeValue())) {
                     documentinfo.addElement().addAttribute(,
                             ).setText(item.getText().trim());
                 } else if ("address".equalsIgnoreCase(item
                         .attributeValue())) {
                     documentinfo.addElement().addAttribute(,
                             ).setText(item.getText().trim());
                 } else if ("version".equalsIgnoreCase(item
                         .attributeValue())) {
                     documentinfo.addElement().addAttribute(,
                             ).setText(item.getText().trim());
                 } else if ("revision".equalsIgnoreCase(item
                         .attributeValue())) {
                     documentinfo.addElement().addAttribute(,
                             ).setText(item.getText().trim());
                 } else if ("status".equalsIgnoreCase(item
                         .attributeValue())) {
                     documentinfo.addElement().addAttribute(,
                             ).setText(item.getText().trim());
                 } else if ("copyright".equalsIgnoreCase(item
                         .attributeValue())) {
                     documentinfo.addElement().addAttribute(,
                             ).setText(item.getText().trim());
                 } else if ("authors".equalsIgnoreCase(item
                         .attributeValue())) {
                     Element authors = documentinfo.addElement();
                     int t = 0;
                     String line = item.getText();
                     for (int i = 0; i < line.length(); i++) {
                         if (line.charAt(i) == ';' || line.charAt(i) == ',') {
                             authors.addElement().addAttribute(,
                                     )
                                     .setText(line.substring(ti).trim());
                             t = i + 1;
                         }
 
                     }
                     authors.addElement().addAttribute()
                             .setText(line.substring(tline.length()).trim());
                 }
                 lexer.remove();
             }
             // skip blank line
             // skipBlankLine(lexer);
             item = lexer.peekDocInfo();
 
         }
 
          // Ajout des commentaires
     //        System.out.println(comment.asXML());               
         for (Element comment : comments){
             result.add(composeComment(comment));
         }
 
         // l'abstract du doc
         item = lexer.peekTitle();
         while (itemNotEquals(item) && !lexer.eof()) {
             composeBody(lexerresult);
             item = lexer.peekTitle();
         }
 
         // les sections
         item = lexer.peekTitle();
         while (itemEquals(itemtruelexer.eof())) {
             Element section = composeSection(lexer);
             result.add(section);
             item = lexer.peekTitle();
         }
 
         // on ajoute le footer a la fin
         if ( != null) {
             result.add();
         }
 
         return result;
     }

    

skip blank line

 
     private void skipBlankLine(JRSTLexer lexerthrows IOException,
             DocumentException {
         Element item = lexer.peekBlankLine();
         // skip blank line
         while (itemEquals(.item)) {
             // go to the next element
             lexer.remove();
             item = lexer.peekBlankLine();
         }
     }

    
*

Corps du document

Parameters:
lexer
Returns:
Element
Throws:
org.dom4j.DocumentException
java.io.IOException
 
     private Element composeBody(JRSTLexer lexerElement parent)
             throws Exception {
 
         Element item = lexer.peekTitleOrBodyElement();
         if (item == null && !lexer.eof()) {
             item = lexer.peekTitleOrBodyElement();
         }
 
         while (!lexer.eof() && itemNotEquals(item)
                 && isUpperLevel(itemparent)) {
             if (itemEquals(.item)) {
                 // go to the next element
                 lexer.remove();
             } else if (itemEquals(item)) {
                 lexer.remove();
             } else if (itemEquals(item)) {
                 lexer.remove();
                 Element list = composeInclude(item);
                 parent.add(list);
             } else if (itemEquals(item)) {
                 lexer.remove();
                 Element list = composeDoctestBlock(item);
                 parent.add(list);
             } else if (itemEquals(item)) {
                 lexer.remove();
                 Element list = composeAdmonition(item);
                 parent.add(list);
             } else if (itemEquals(item)) {
                 lexer.remove();
                 Element list = composeSidebar(item);
                 parent.add(list);
             } else if (itemEquals(item)) {
                 lexer.remove();
                 Element list = composeTopic(item);
                 parent.add(list);
             } else if (itemEquals(item)) {
                 lexer.remove();
                Element para = parent.addElement();
                copyLevel(itempara);
            } else if (itemEquals(item)) {
                lexer.remove();
                Element para = parent.addElement();
                copyLevel(itempara);
                para.addAttribute().setText(item.getText());
            } else if (itemEquals(.item)) {
                lexer.remove();
                Node directive = composeDirective(item);
                parent.add(directive);
            } else if (itemEquals(item)) {
                lexer.remove();
                Element subst = composeSubstitutionDefinition(item);
                parent.add(subst);
            } else if (itemEquals(item)) {
                lexer.remove();
                Element para = parent.addElement();
                copyLevel(itempara);
                para.setText(item.getText());
            } else if (itemEquals(.item)) {
                lexer.remove();
                Element table = composeTable(item);
                parent.add(table);
                // Element para = parent.addElement(TABLE);
                // copyLevel(item, para);
                // para.setText(item.getText());
            } else if (itemEquals(item)) {
                lexer.remove();
                Element list = composeLineBlock(lexeritem);
                parent.add(list);
            } else if (itemEquals(item)) {
                Element list = composeBulletList(lexer);
                parent.add(list);
            } else if (itemEquals(item)) {
                Element list = composeEnumeratedList(lexer);
                parent.add(list);
            } else if (itemEquals(item)) {
                Element list = composeDefinitionList(lexer);
                parent.add(list);
            } else if (itemEquals(item)) {
                Element list = composeFieldList(lexer);
                parent.add(list);
            } else if (itemEquals(item)) {
                lexer.remove();
                Element list = composeBlockQuote(item);
                parent.add(list);
            } else if (itemEquals(item)) {
                Element list = composeOptionList(lexer);
                parent.add(list);
            } else if (itemEquals(item)) {
                lexer.remove();
                Element list = composeTarget(item);
                if (list != null) {
                    try {
                	parent.add(list);
                    } catch (IllegalAddException e) {}
                } else
                    ..println("Unknown target name : \"" + item.attributeValue() + "\"");
            } else if (itemEquals(item)) {
                lexer.remove();
                Element list = composeTargetAnonymous(item);
                parent.add(list);
            } else if (itemEquals(item)) {
                lexer.remove();
                Element[] list = composeFootnote(item);
                for (Element l : list) {
                    parent.add(l);
                }
            } else if (itemEquals(item)) {
                lexer.remove();
                Element list = composeComment(item);
                parent.add(list);
            }
            else {
                if () {
                    throw new DocumentException("Unknow item type: "
                            + item.getName());
                } else {
                    lexer.remove();
                }
            }
            // Pour afficher le "PseudoXML"
            // if (item!=null) System.out.println(item.asXML());
            item = lexer.peekTitleOrBodyElement();
        }
        return parent;
    }

    

include un document rst

 .. include:: doc.rst
 

include un document literal (code...)

 .. include:: literal
       doc.rst
 

Parameters:
item
Returns:
Element
Throws:
java.lang.Exception
    private Element composeInclude(Element itemthrows Exception {
        String option = item.attributeValue();
        String path = item.getText();
        Element result = null;
        if (option.equals()) {
            result = DocumentHelper.createElement();
            FileReader reader = new FileReader(path);
            BufferedReader bf = new BufferedReader(reader);
            String line = "";
            String lineTmp = bf.readLine();
            while (lineTmp != null) {
                line += '\n' + lineTmp;
                lineTmp = bf.readLine();
            }
            result.setText(line);
        } else {
            File fileIn = new File(path);
            URL url = fileIn.toURI().toURL();
            Reader in = new InputStreamReader(url.openStream());
            Document doc = newJRSTReader(in);
            result = doc.getRootElement();
        }
        return result;
    }

    
 ..
   So this block is not "lost",
   despite its indentation.
 

Parameters:
item
Returns:
Element
    private Element composeComment(Element item) {
        return item;
    }

    
 __ http://truc.html
 

Parameters:
item
Returns:
Element
    private Element composeTargetAnonymous(Element item) {
        Element result = null;
        result = .getFirst();
        return result;
    }

    

 
 

Parameters:
item
Returns:
Element
    private Element composeTarget(Element item) {
        Element result = null;
        for (Element e : ) {
            if (e.attributeValue().equals(item.attributeValue())) {
                result = e;
            }
        }
        return result;
    }

    
 .. [#] This is a footnote
 

Parameters:
item
Returns:
Element
Throws:
java.lang.Exception
    private Element[] composeFootnote(Element itemthrows Exception {
        Element[] result = null;
        if (itemEquals(item)) {
            List<Elementfootnotes = (List<Element>) item
                    .selectNodes();
            result = new Element[footnotes.size()];
            int cnt = 0;
            for (Element footnote : footnotes) {
                result[cnt] = DocumentHelper.createElement();
                Element efootnote = DocumentHelper.createElement();
                int labelMax = 0;
                for (int i = 0; i < .size(); i++) {
                    int lbl = .get(i);
                    labelMax = Math.max(lbllabelMax);
                }
                boolean[] labels = new boolean[labelMax];
                for (int i = 0; i < labels.lengthi++) {
                    labels[i] = false;
                }
                for (int i = 0; i < .size(); i++) {
                    labels[.get(i) - 1] = true;
                }
                ++;
                String name = null;
                String id = "";
                String label = null;
                String type = footnote.attributeValue();
                if (type.equals() || type.equals()) {
                    result[cnt].addAttribute("1");
                }
                if (type.equals()) {
                    result[cnt].addAttribute("*");
                }
                result[cnt].addAttribute( + );
                efootnote.addAttribute( + );
                if (type.equals() || type.equals()) {
                    name = footnote.attributeValue();
                    if (type.equals()) {
                        id = name;
                    }
                    else {
                        label = name;
                    }
                }
                if (type.equals() || type.equals()) {
                    boolean done = false;
                    for (int i = 0; i < labels.length && !donei++) {
                        if (!labels[i]) {
                            done = true;
                            label = "" + (i + 1);
                        }
                    }
                    if (!done) {
                        label = "" + (labels.length + 1);
                    }
                    if (type.equals()) {
                        name = label;
                    }
                }
                if (type.equals()) {
                    int nb = Math.abs( / 10) + 1;
                    char symbol = .charAt( % 10);
                    label = "";
                    for (int j = 0; j < nbj++) {
                        label += symbol;
                    }
                    ++;
                }
                result[cnt].addAttribute("" + id);
                efootnote.addAttribute("" + id);
                if (!type.equals()) {
                    result[cnt].addAttribute("" + name);