Ejemplo n.º 1
0
  private static Document extractBetween(
      SectionsMarkdownVisitor v, Node parent, Header header1, Header header2) {
    /*Node parent1 = header1 != null ? header1.jjtGetParent() : null;
    Node parent2 = header2 != null ? header2.jjtGetParent() : null;
    /*if (parent1 != null
            && parent2 != null
            && parent1 != parent2) {
        throw new RuntimeException();
    }* /
    Node parent = parent1 != null ? parent1 : parent2;*/

    Document doc = new Document(Parser.JJTDOCUMENT);
    final int numChildren = parent.jjtGetNumChildren();
    final int start = header1 != null ? Markdown.getIndexInParent(header1) + 1 : 0;
    final int end = header2 != null ? Markdown.getIndexInParent(header2) - 1 : numChildren - 1;
    for (int nodeIndex = start; nodeIndex <= end; nodeIndex++) {
      Node child = parent.jjtGetChild(nodeIndex);
      child.jjtSetParent(doc);
      doc.jjtAddChild(child, nodeIndex - start);
    }
    // Add a copy of all the resource definitions
    // to each document
    for (ResourceDefinition rd : v.resourceDefinitions) {
      ResourceDefinition copy = copy(rd);
      copy.jjtSetParent(doc);
      doc.jjtAddChild(copy, doc.jjtGetNumChildren());
    }
    return doc;
  }
Ejemplo n.º 2
0
  /**
   * Nasty method for extracting doc sections from a markdown document.
   *
   * <ol>
   *   <li>The most prominent heading(s) are identified
   *   <li>The document is split into a list of sections corresponding to the tree following
   *       headings of that level. If the document didn't begin with a heading of that level, the
   *       first Section in the result list will have a null heading.
   * </ol>
   *
   * @param document
   * @return
   */
  public static List<Section> extractSections(Document document) {
    SectionsMarkdownVisitor v = new SectionsMarkdownVisitor();
    document.accept(v);
    List<Section> result = new ArrayList<>();
    for (int levelIndex = 1; levelIndex <= 6; levelIndex++) {
      List<Header> sections = v.sections.get(levelIndex);
      if (sections != null) {
        Node parent;
        {
          Header header = sections.get(0);
          parent = header.jjtGetParent();
          Document doc = extractBetween(v, parent, null, header);
          result.add(new Section(null, doc));
        }
        for (int sectionIndex = 0; sectionIndex < sections.size(); sectionIndex++) {
          Header header = sections.get(sectionIndex);
          Header next = sectionIndex < sections.size() - 1 ? sections.get(sectionIndex + 1) : null;
          Document doc = extractBetween(v, parent, header, next);
          result.add(new Section(header, doc));
        }

        break;
      }
    }
    return result;
  }
Ejemplo n.º 3
0
 /** Adjusts the heading levels in the document */
 public static void adjustHeadings(Document document, final int increment) {
   // adjust heading levels to h2 max
   document.accept(
       new AbstractMarkdownVisitor() {
         @Override
         public void visit(Header header) {
           header.setLevel(header.getLevel() + increment);
         }
       });
 }