org.asciidoctor.ast.StructuralNode Java Examples

The following examples show how to use org.asciidoctor.ast.StructuralNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TerminalCommandTreeprocessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
private void processBlock(StructuralNode block) {

        List<StructuralNode> blocks = block.getBlocks();

        for (int i = 0; i < blocks.size(); i++) {
            final StructuralNode currentBlock = blocks.get(i);
            if(currentBlock instanceof StructuralNode) {
                if ("paragraph".equals(currentBlock.getContext())) {
                    List<String> lines = ((Block) currentBlock).getLines();
                    if (lines.size() > 0 && lines.get(0).startsWith("$")) {
                        blocks.set(i, convertToTerminalListing((Block) currentBlock));
                    }
                } else {
                    // It's not a paragraph, so recursively descent into the child node
                    processBlock(currentBlock);
                }
            }
        }
    }
 
Example #2
Source File: PathsDocument.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
private void appendServersSection(StructuralNode node, List<Server> servers) {
    if (null == servers || servers.isEmpty()) return;

    Section serversSection = new SectionImpl(node);
    serversSection.setTitle(labels.getLabel(SECTION_TITLE_SERVERS));

    servers.forEach(server -> {
        Section serverSection = new SectionImpl(serversSection);
        serverSection.setTitle(italicUnconstrained(labels.getLabel(LABEL_SERVER)) + ": " + server.getUrl());

        appendDescription(serverSection, server.getDescription());
        ServerVariables variables = server.getVariables();
        appendVariables(serverSection, variables);
        serversSection.append(serverSection);
    });
    node.append(serversSection);
}
 
Example #3
Source File: TerminalCommandTreeprocessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
private void processBlock(StructuralNode block) {

        List<StructuralNode> blocks = block.getBlocks();

        for (int i = 0; i < blocks.size(); i++) {
            final StructuralNode currentBlock = blocks.get(i);
            if(currentBlock instanceof StructuralNode) {
                if ("paragraph".equals(currentBlock.getContext())) { // <3>
                    List<String> lines = ((Block) currentBlock).getLines();
                    if (lines != null
                            && lines.size() > 0
                            && lines.get(0).startsWith("$")) {
                        blocks.set(i, convertToTerminalListing((Block) currentBlock));
                    }
                } else {
                    // It's not a paragraph, so recursively descend into the child node
                    processBlock(currentBlock);
                }
            }
        }
    }
 
Example #4
Source File: ArrowsAndBoxesBlock.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Override
    public Object process(StructuralNode parent, Reader reader,
            Map<String, Object> attributes) {

        List<String> lines = reader.lines();

        StringBuilder outputLines = new StringBuilder();
        outputLines.append("<pre class=\"arrows-and-boxes\">");

        for (String line : lines) {
            outputLines.append(line);
        }

        outputLines.append("</pre>");
        attributes.put("!subs", "");

        return null;
//        return createBlock(document, "pass", Arrays.asList(outputLines.toString()),
//                attributes, new HashMap<String, Object>() {
//                    {
//                        put("content_model", ":raw");
//                    }
//                });

    }
 
Example #5
Source File: TextConverter.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Override
public String convert(
        ContentNode node, String transform, Map<Object, Object> o) {  // <3>

    if (transform == null) {                                          // <4>
        transform = node.getNodeName();
    }

    if (node instanceof Document) {
        Document document = (Document) node;
        return document.getContent().toString();                      // <5>
    } else if (node instanceof Section) {
        Section section = (Section) node;
        return new StringBuilder()
                .append("== ").append(section.getTitle()).append(" ==")
                .append(LINE_SEPARATOR).append(LINE_SEPARATOR)
                .append(section.getContent()).toString();             // <5>
    } else if (transform.equals("paragraph")) {
        StructuralNode block = (StructuralNode) node;
        String content = (String) block.getContent();
        return new StringBuilder(content.replaceAll(LINE_SEPARATOR, " "))
                .append(LINE_SEPARATOR).toString();                   // <5>
    }
    return null;
}
 
Example #6
Source File: CukedoctorMinMaxExtension.java    From cukedoctor with Apache License 2.0 6 votes vote down vote up
@Override
public Object process(StructuralNode parent, String target, Map<String, Object> map) {
    String backend = parent.getAttribute("backend") != null ? parent.getAttribute("backend").toString() : "";
    if (backend.contains("html")) {
        StringBuilder minMax = new StringBuilder();
        minMax.append("<span class=\"fa fa-minus-square fa-fw\" style=\"cursor:pointer;float:right;margin-top:-30px\" ").
                append(" title=\"Minimize\" onclick=\"hideFeatureScenarios('").
                append(target).
                append("');document.getElementById('hidden-").
                append(target).
                append("').style.display = 'inline';this.style.display = 'none'\">  </span>\n\n").
                append("<span id=\"hidden-").append(target).append("\"").
                append(" class=\"fa fa-plus-square fa-fw\" style=\"cursor:pointer;float:right;display:none;margin-top:-30px\" title=\"Maximize feature\" onclick=\"showFeatureScenarios('").
                append(target).append("');").append("this.style.display = 'none'\">  </span>");

        return createBlock(parent, "pass", Arrays.asList(minMax.toString()), this.getConfig(),new HashMap<Object, Object>());
    } else {
        return parent;
    }
}
 
Example #7
Source File: GistBlockMacroProcessor.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Block process(StructuralNode parent, String target,
                     Map<String, Object> attributes) {

  String content = "<div class=\"content\">\n" +
          "<script src=\"https://gist.github.com/"+target+".js\"></script>\n" +
          "</div>";

  return createBlock(parent, "pass", Arrays.asList(content), attributes);
}
 
Example #8
Source File: LinkComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
@Override
public Document apply(StructuralNode parent, LinkComponent.Parameters parameters) {
    DocumentImpl linksDocument = new DocumentImpl(parent);
    ParagraphBlockImpl linkParagraph = new ParagraphBlockImpl(linksDocument);

    Map<String, Link> links = parameters.links;
    if (null == links || links.isEmpty()) {
        linkParagraph.setSource(labels.getLabel(LABEL_NO_LINKS));
    } else {
        StringBuilder sb = new StringBuilder();
        links.forEach((name, link) -> {
            sb.append(name).append(" +").append(LINE_SEPARATOR);
            sb.append(italicUnconstrained(labels.getLabel(LABEL_OPERATION))).append(' ')
                    .append(italicUnconstrained(link.getOperationId())).append(" +").append(LINE_SEPARATOR);
            Map<String, String> linkParameters = link.getParameters();
            if (null != linkParameters && !linkParameters.isEmpty()) {
                sb.append(italicUnconstrained(labels.getLabel(LABEL_PARAMETERS))).append(" {").append(" +").append(LINE_SEPARATOR);
                linkParameters.forEach((param, value) ->
                        sb.append('"').append(param).append("\": \"").append(value).append('"').append(" +").append(LINE_SEPARATOR)
                );
                sb.append('}').append(" +").append(LINE_SEPARATOR);
            }
        });
        linkParagraph.setSource(sb.toString());
    }
    linksDocument.append(linkParagraph);
    return linksDocument;
}
 
Example #9
Source File: WhenAsciiDocIsRenderedToDocument.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_get_attributes() {

    final String documentWithAttributes = "= Document Title\n" +
        ":docattr: docvalue\n" +
        "\n" +
        "preamble\n" +
        "\n" +
        "== Section A\n" +
        "\n" +
        "paragraph\n" +
        "\n";

    Document document = asciidoctor.load(documentWithAttributes, new HashMap<String, Object>());
    List<StructuralNode> blocks = document.getBlocks();

    Section section = (Section) blocks.get(1);
    section.setAttribute("testattr", "testvalue", true);

    assertThat(document.hasAttribute("testattr"), is(false));

    assertThat(section.hasAttribute("testattr"), is(true));
    assertThat(section.hasAttribute("testattr", true), is(true));
    assertThat(section.hasAttribute("testattr", false), is(true));
    assertThat(section.isAttribute("testattr", "testvalue"), is(true));

    assertThat(section.hasAttribute("docattr", true), is(true));
    assertThat(section.hasAttribute("docattr", false), is(false));
}
 
Example #10
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public org.asciidoctor.ast.List createList(StructuralNode parent, String context,
                                           Map<Object, Object> options) {

    Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

    RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbolsIfNecessary(rubyRuntime,
        filterBlockOptions(parent, options, "subs", ContentModel.KEY));

    IRubyObject[] parameters = {
        ((StructuralNodeImpl) parent).getRubyObject(),
        RubyUtils.toSymbol(rubyRuntime, context),
        convertMapToRubyHashWithSymbols};
    return (org.asciidoctor.ast.List) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.LIST_CLASS, parameters);
}
 
Example #11
Source File: WhenJavaExtensionGroupIsRegistered.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_create_toc_with_treeprocessor() throws Exception {
    this.asciidoctor.createGroup()
        .treeprocessor(new Treeprocessor() {
            @Override
            public Document process(Document document) {
                List<StructuralNode> blocks=document.getBlocks();
                for (StructuralNode block : blocks) {
                    for (StructuralNode block2 : block.getBlocks()) {
                        if(block2 instanceof Section)
                            System.out.println(((Section) block2).getId());
                    }
                }
                return document;
            }
        })
        .register();

    String content = asciidoctor.convertFile(
            classpath.getResource("documentwithtoc.adoc"),
            options().headerFooter(true).toFile(false).safe(SafeMode.UNSAFE).get());

    org.jsoup.nodes.Document doc = Jsoup.parse(content, "UTF-8");
    Element toc = doc.getElementById("toc");
    assertThat(toc, notNullValue());
    Elements elements = toc.getElementsByAttributeValue("href", "#TestId");
    assertThat(elements.size(), is(1));
}
 
Example #12
Source File: YellBlockProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(                                 // <5>
        StructuralNode parent, Reader reader, Map<String, Object> attributes) {

    String content = reader.read();
    String yellContent = content.toUpperCase();

    return createBlock(parent, "paragraph", yellContent, attributes);
}
 
Example #13
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public org.asciidoctor.ast.List createList(StructuralNode parent, String context,
                                           Map<String, Object> attributes,
                                           Map<Object, Object> options) {

    options.put(Options.ATTRIBUTES, new HashMap<>(attributes));
    return createList(parent, context, options);
}
 
Example #14
Source File: YellBlock.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
    List<String> lines = reader.readLines();
    String upperLines = null;
    for (String line : lines) {
        if (upperLines == null) {
            upperLines = line.toUpperCase();
        } else {
            upperLines = upperLines + "\n" + line.toUpperCase();
        }
    }

    return createBlock(parent, "paragraph", Arrays.asList(upperLines), attributes, new HashMap<>());
}
 
Example #15
Source File: ExamplesComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
@Override
public StructuralNode apply(StructuralNode node, ExamplesComponent.Parameters parameters) {
    Map<String, Example> examples = parameters.examples;
    if (examples == null || examples.isEmpty()) return node;

    DescriptionListImpl examplesList = new DescriptionListImpl(node);
    examplesList.setTitle(labels.getLabel(LABEL_EXAMPLES));

    examples.forEach((name, example) -> {
        DescriptionListEntryImpl exampleEntry = new DescriptionListEntryImpl(examplesList, Collections.singletonList(new ListItemImpl(examplesList, name)));
        ListItemImpl tagDesc = new ListItemImpl(exampleEntry, "");

        ParagraphBlockImpl exampleBlock = new ParagraphBlockImpl(tagDesc);

        appendDescription(exampleBlock, example.getSummary());
        appendDescription(exampleBlock, example.getDescription());
        mediaTypeExampleComponent.apply(tagDesc, example.getValue());

        ParagraphBlockImpl paragraphBlock = new ParagraphBlockImpl(tagDesc);
        String source = "";
        generateRefLink(source, example.getExternalValue(), labels.getLabel(LABEL_EXTERNAL_VALUE));
        generateRefLink(source, example.get$ref(), "");
        if(StringUtils.isNotBlank(source)){
            paragraphBlock.setSource(source);
            tagDesc.append(paragraphBlock);
        }

        exampleEntry.setDescription(tagDesc);

        examplesList.addEntry(exampleEntry);
    });
    node.append(examplesList);

    return node;
}
 
Example #16
Source File: PillarsBlockProcessor.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(StructuralNode parent,
                      Reader reader,
                      Map<String, Object> attributes) {

    Map<Object, Object> opts = new HashMap<>();
    // means it can have nested blocks
    opts.put("content_model", "compound");
    // create an empty block with context "pillars"
    Block block = this.createBlock(parent, "pillars",
            Collections.emptyList(), attributes, opts);
    return block;
}
 
Example #17
Source File: WhenAsciiDocIsRenderedToDocument.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_be_able_to_get_roles() {
    Document document = asciidoctor.load(ROLE, new HashMap<String, Object>());
    StructuralNode abstractBlock = document.getBlocks().get(0);
    assertThat(abstractBlock.getRole(), is("famous"));
    assertThat(abstractBlock.hasRole("famous"), is(true));
    assertThat(abstractBlock.isRole(), is(true));
    assertThat(abstractBlock.getRoles(), contains("famous"));
}
 
Example #18
Source File: WhenAsciiDocIsRenderedToDocument.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_find_elements_from_document() {

    Document document = asciidoctor.load(DOCUMENT, new HashMap<String, Object>());
    Map<Object, Object> selector = new HashMap<Object, Object>();
    selector.put("context", ":image");
    List<StructuralNode> findBy = document.findBy(selector);
    assertThat(findBy, hasSize(2));

    assertThat((String)findBy.get(0).getAttributes().get("target"), is("tiger.png"));
    assertThat(findBy.get(0).getLevel(), greaterThan(0));

}
 
Example #19
Source File: BlockMacroProcessorProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@JRubyMethod(name = "process", required = 3)
public IRubyObject process(ThreadContext context, IRubyObject parent, IRubyObject target, IRubyObject attributes) {
    Object o = getProcessor().process(
            (StructuralNode) NodeConverter.createASTNode(parent),
            RubyUtils.rubyToJava(getRuntime(), target, String.class),
            new RubyAttributesMapDecorator((RubyHash) attributes));

    return convertProcessorResult(o);

}
 
Example #20
Source File: WhenAsciidoctorLogsToConsole.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
    log(new LogRecord(Severity.INFO, parent.getSourceLocation(), "Hello Log"));
    final List<String> strings = reader.readLines().stream()
            .map(String::toUpperCase)
            .collect(Collectors.toList());

    return createBlock(parent, "paragraph", strings);
}
 
Example #21
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Block createBlock(StructuralNode parent, String context,
                         Map<Object, Object> options) {

    Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

    RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbolsIfNecessary(rubyRuntime,
            filterBlockOptions(parent, options, "subs", ContentModel.KEY));

    IRubyObject[] parameters = {
            ((StructuralNodeImpl) parent).getRubyObject(),
            RubyUtils.toSymbol(rubyRuntime, context),
            convertMapToRubyHashWithSymbols};
    return (Block) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.BLOCK_CLASS, parameters);
}
 
Example #22
Source File: DescriptionListEntryImpl.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
public DescriptionListEntryImpl(StructuralNode parent, Map<String, Object> attributes, List<String> roles,
                                Object content, List<StructuralNode> blocks, Integer level, String contentModel,
                                List<String> subs, List<ListItem> terms, ListItem description) {
    super(parent, "dlist_item", attributes, roles, content, blocks, level, contentModel, subs);
    this.terms = terms;
    this.description = description;
}
 
Example #23
Source File: GistMacro.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Block process(StructuralNode parent, String target, Map<String, Object> attributes) {
   
   String content = "<div class=\"content\">\n" + 
   		"<script src=\"https://gist.github.com/"+target+".js\"></script>\n" + 
   		"</div>"; 
   
   return createBlock(parent, "pass", Arrays.asList(content), attributes);
}
 
Example #24
Source File: BlockProcessorRegistrationTest.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
    List<String> s = reader.readLines().stream()
        .map(line -> 
            line.chars()
                .mapToObj(c -> Character.toString((char) c))
                .collect(joining(" ")))
        .collect(toList());
    return createBlock(parent, "paragraph", s);
}
 
Example #25
Source File: BlockProcessorRegistrationTest.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
    List<String> s = reader.readLines().stream()
        .map(line ->
            line.chars()
                .mapToObj(c -> Character.toString((char) c))
                .collect(joining(" ")))
        .collect(toList());
    return createBlock(parent, "paragraph", s);
}
 
Example #26
Source File: StructuralNodeImpl.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public StructuralNodeImpl(StructuralNode parent, String context) {
    this(parent, context, new HashMap<>());
}
 
Example #27
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Block createBlock(StructuralNode parent, String context, List<String> content, Map<String, Object> attributes) {
    return createBlock(parent, context, content, attributes, new HashMap<>());
}
 
Example #28
Source File: StructuralNodeImpl.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public StructuralNodeImpl(StructuralNode parent, String context, Object content) {
    this(parent, context, new HashMap<>(), content);
}
 
Example #29
Source File: StructuralNodeImpl.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public StructuralNodeImpl(StructuralNode parent, String context, Map<String, Object> attributes) {
    this(parent, context, attributes, null);
}
 
Example #30
Source File: SectionImpl.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public SectionImpl(StructuralNode parent, String context, Object content, String sectionName) {
    this(parent, context, content, null, null, "", sectionName, false, false);
}