org.asciidoctor.ast.Section Java Examples

The following examples show how to use org.asciidoctor.ast.Section. 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: TagsComponent.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
@Override
public Document apply(Document document, TagsComponent.Parameters parameters) {
    List<Tag> openAPITags = parameters.tags;
    if (null == openAPITags || openAPITags.isEmpty()) return document;

    Section tagsSection = new SectionImpl(document);
    tagsSection.setTitle(labels.getLabel(SECTION_TITLE_TAGS));

    DescriptionListImpl tagsList = new DescriptionListImpl(tagsSection);
    openAPITags.forEach(tag -> {
        DescriptionListEntryImpl tagEntry = new DescriptionListEntryImpl(tagsList, Collections.singletonList(new ListItemImpl(tagsList, tag.getName())));
        String description = tag.getDescription();
        if(StringUtils.isNotBlank(description)){
            ListItemImpl tagDesc = new ListItemImpl(tagEntry, "");
            tagDesc.setSource(description);
            externalDocumentationComponent.apply(tagDesc, tag.getExternalDocs());
            tagEntry.setDescription(tagDesc);
        }
        tagsList.addEntry(tagEntry);
    });

    tagsSection.append(tagsList);
    document.append(tagsSection);
    return document;
}
 
Example #2
Source File: Generator.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
private static void processSection(final StructuralNode node, final StringBuilder builder, final String target) {
    node.getBlocks().stream().filter(Section.class::isInstance).map(Section.class::cast).forEach(section -> {
        final boolean root = section.getLevel() == 1;
        builder
                .append(root ? "."
                        : (IntStream.range(0, section.getLevel() - 1).mapToObj(i -> "*").collect(joining())
                                + " xref:" + target + "#" + section.getId() + "["))
                .append(root ? mapTitle(section.getTitle()) : section.getTitle())
                .append(root ? "" : "]")
                .append('\n');
        processSection(section, builder, target);
        if (root) {
            builder.append('\n');
        }
    });
}
 
Example #3
Source File: ComponentsDocument.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
private void appendComponentsSchemasSection(
        Section componentsSection, String componentSectionId,
        @SuppressWarnings("rawtypes") Map<String, Schema> schemas) {
    if (null == schemas || schemas.isEmpty()) return;

    SectionImpl schemasSection = new SectionImpl(componentsSection);
    String schemasSectionId = componentSectionId + "_schemas";
    schemasSection.setTitle(labels.getLabel(SECTION_TITLE_SCHEMAS));
    schemasSection.setId(schemasSectionId);
    schemas.forEach((name, schema) -> {
        String schemaDocumentId = schemasSectionId + "_" + name;
        Document schemaDocument = schemaComponent.apply(schemasSection, schema);
        schemaDocument.setTitle(name);
        schemaDocument.setId(schemaDocumentId);
        schemasSection.append(schemaDocument);
    });
    componentsSection.append(schemasSection);
}
 
Example #4
Source File: OverviewDocument.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
private void appendLicenseInfo(Section overviewDoc, Info info) {
    License license = info.getLicense();
    if (null != license) {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(license.getUrl())) {
            sb.append(license.getUrl()).append("[");
        }
        sb.append(license.getName());
        if (StringUtils.isNotBlank(license.getUrl())) {
            sb.append("]");
        }
        BlockImpl paragraph = new ParagraphBlockImpl(overviewDoc);
        paragraph.setSource(sb.toString());
        overviewDoc.append(paragraph);
    }
}
 
Example #5
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 #6
Source File: PathsDocument.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
private void appendVariables(Section serverSection, ServerVariables variables) {
    if (null == variables || variables.isEmpty()) return;

    TableImpl serverVariables = new TableImpl(serverSection, new HashMap<String, Object>() {{
        put("header-option", "");
        put("cols", ".^2a,.^9a,.^3a,.^4a");
    }}, new ArrayList<>());
    serverVariables.setTitle(labels.getLabel(TABLE_TITLE_SERVER_VARIABLES));

    serverVariables.setHeaderRow(labels.getLabel(TABLE_HEADER_VARIABLE), labels.getLabel(TABLE_HEADER_DESCRIPTION),
            labels.getLabel(TABLE_HEADER_POSSIBLE_VALUES), labels.getLabel(TABLE_HEADER_DEFAULT)
    );

    variables.forEach((name, variable) -> {
        String possibleValues = String.join(", ", Optional.ofNullable(variable.getEnum()).orElse(Collections.singletonList("Any")));
        serverVariables.addRow(name, Optional.ofNullable(variable.getDescription()).orElse(""), possibleValues, variable.getDefault());

    });
    serverSection.append(serverVariables);
}
 
Example #7
Source File: WhenJavaExtensionIsRegistered.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Test
public void should_create_toc_with_treeprocessor() {
    asciidoctor.javaExtensionRegistry().treeprocessor(new Treeprocessor() {
        @Override
        public org.asciidoctor.ast.Document process(org.asciidoctor.ast.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;
        }
    });

    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 #8
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 #9
Source File: WhenAsciiDocIsRenderedToDocument.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_get_content_model() {
	final String documentWithPreambleAndSection = ""
			+ "= Document Title\n"
			+ "\n"
			+ "A test document with a preamble and a section.\n"
			+ "\n"
			+ "The preamble contains multiple paragraphs to force the outer block to be compound.\n"
			+ "\n"
			+ "== First Section\n"
			+ "\n"
			+ "And herein lies the problem.\n"
			+ "\n";

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

    StructuralNode preambleContainer = blocks.get(0);
    assertThat(preambleContainer.getContentModel(), is("compound"));

    assertThat(preambleContainer.getBlocks().get(0).getContentModel(), is("simple"));
    assertThat(preambleContainer.getBlocks().get(1).getContentModel(), is("simple"));

    Section section = (Section) blocks.get(1);
    assertThat(section.getContentModel(), is("compound"));
    assertThat(section.getBlocks().get(0).getContentModel(), is("simple"));
}
 
Example #10
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 #11
Source File: WhenAsciiDocIsRenderedToDocument.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void should_return_section_blocks() {
    Document document = asciidoctor.load(DOCUMENT, new HashMap<String, Object>());
    Section section = (Section) document.getBlocks().get(1);
    assertThat(section.getIndex(), is(0));
    assertThat(section.getSectionName(), either(is("sect1")).or(is("section")));
    assertThat(section.isSpecial(), is(false));
}
 
Example #12
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 #13
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Section createSection(StructuralNode parent, Integer level, boolean numbered, Map<Object, Object> options) {

    Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

    RubyHash convertMapToRubyHashWithSymbols = RubyHashUtil.convertMapToRubyHashWithSymbolsIfNecessary(rubyRuntime,
            options);

    IRubyObject[] parameters = {
            ((StructuralNodeImpl) parent).getRubyObject(),
            level == null ? rubyRuntime.getNil() : rubyRuntime.newFixnum(level),
            rubyRuntime.newBoolean(numbered),
            convertMapToRubyHashWithSymbols};
    return (Section) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.SECTION_CLASS, parameters);
}
 
Example #14
Source File: ComponentsDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private void appendComponentsSection(Document document, Components components) {
    if (null == components) return;

    Section componentsSection = new SectionImpl(document);
    componentsSection.setTitle(labels.getLabel(SECTION_TITLE_COMPONENTS));
    String componentSectionId = "_components";
    componentsSection.setId(componentSectionId);

    appendComponentsSchemasSection(componentsSection, componentSectionId, components.getSchemas());
    Map<String, Parameter> parameters = components.getParameters();
    if (null != parameters && !parameters.isEmpty()) {
        appendSubSection(componentsSection, componentSectionId, parametersComponent, SECTION_TITLE_PARAMETERS,
                new ParametersComponent.Parameters(parameters));
    }
    Map<String, ApiResponse> responses = components.getResponses();
    if (null != responses && !responses.isEmpty()) {
        appendSubSection(componentsSection, componentSectionId, responseComponent, SECTION_TITLE_RESPONSES,
                new ResponseComponent.Parameters(responses));
    }
    Map<String, Header> headers = components.getHeaders();
    if (null != headers && !headers.isEmpty()) {
        appendSubSection(componentsSection, componentSectionId, headersComponent, SECTION_TITLE_HEADERS,
                new HeadersComponent.Parameters(headers));
    }
    Map<String, Link> links = components.getLinks();
    if (null != links && !links.isEmpty()) {
        appendSubSection(componentsSection, componentSectionId, linkComponent, SECTION_TITLE_LINKS,
                new LinkComponent.Parameters(links));
    }
    document.append(componentsSection);
}
 
Example #15
Source File: ComponentsDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private <T> void appendSubSection(Section componentsSection, String componentSectionId,
                                  MarkupComponent<StructuralNode, T, StructuralNode> markupComponent,
                                  String sectionLabel, T parameters) {
    SectionImpl parametersSection = new SectionImpl(componentsSection);
    String parametersSectionId = componentSectionId + "_parameters";
    parametersSection.setTitle(labels.getLabel(sectionLabel));
    parametersSection.setId(parametersSectionId);
    markupComponent.apply(parametersSection, parameters);
    componentsSection.append(parametersSection);
}
 
Example #16
Source File: OverviewDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
@Override
public Document apply(Document document, Parameters parameters) {
    Info apiInfo = parameters.openAPI.getInfo();
    document.setAttribute("openapi", parameters.openAPI.getOpenapi(), true);
    addDocumentTitle(document, apiInfo);
    addAuthorInfo(document, apiInfo);
    addVersionInfo(document, apiInfo);

    applyOverviewDocumentExtension(new Context(OverviewDocumentExtension.Position.DOCUMENT_BEFORE, document));
    Document subDocument = new DocumentImpl(document);
    Section overviewDoc = new SectionImpl(subDocument, "section", new HashMap<>(), new ArrayList<>(),
            null, new ArrayList<>(), 1, "", new ArrayList<>(),
            null, null, "", "", false, false);
    applyOverviewDocumentExtension(new Context(OverviewDocumentExtension.Position.DOCUMENT_BEGIN, subDocument));
    overviewDoc.setTitle(labels.getLabel(SECTION_TITLE_OVERVIEW));

    appendDescription(overviewDoc, apiInfo.getDescription());
    appendTermsOfServiceInfo(overviewDoc, apiInfo);
    appendLicenseInfo(overviewDoc, apiInfo);
    subDocument.append(overviewDoc);
    applyOverviewDocumentExtension(new Context(OverviewDocumentExtension.Position.DOCUMENT_END, subDocument));
    document.append(subDocument);

    externalDocumentationComponent.apply(document, parameters.openAPI.getExternalDocs());
    tagsComponent.apply(document, parameters.openAPI.getTags());
    applyOverviewDocumentExtension(new Context(OverviewDocumentExtension.Position.DOCUMENT_AFTER, document));
    return document;
}
 
Example #17
Source File: OverviewDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private void appendTermsOfServiceInfo(Section overviewDoc, Info info) {
    String termsOfService = info.getTermsOfService();
    if (StringUtils.isNotBlank(termsOfService)) {
        Block paragraph = new ParagraphBlockImpl(overviewDoc);
        paragraph.setSource(termsOfService + "[" + labels.getLabel(LABEL_TERMS_OF_SERVICE) + "]");
        overviewDoc.append(paragraph);
    }
}
 
Example #18
Source File: SecurityDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
@Override
public Document apply(Document document, SecurityDocument.Parameters parameters) {
    List<SecurityRequirement> securityRequirements = parameters.schema.getSecurity();
    if (null == securityRequirements || securityRequirements.isEmpty()) return document;

    Section securityRequirementsSection = new SectionImpl(document);
    securityRequirementsSection.setTitle(labels.getLabel(SECTION_TITLE_SECURITY));
    securityRequirementTableComponent.apply(securityRequirementsSection, securityRequirements, false);
    document.append(securityRequirementsSection);

    return document;
}
 
Example #19
Source File: BaseProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent, Integer level, boolean numbered, Map<Object, Object> options) {
    return delegate.createSection(parent, level, numbered, options);
}
 
Example #20
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent, int level, boolean numbered, Map<Object, Object> options) {
    return createSection(parent, Integer.valueOf(level), numbered, options);
}
 
Example #21
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent, boolean numbered, Map<Object, Object> options) {
    return createSection(parent, null, numbered, options);
}
 
Example #22
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent, Map<Object, Object> options) {
    return createSection(parent, null, true, options);
}
 
Example #23
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent) {
    return createSection(parent, null, true, new HashMap<>());
}
 
Example #24
Source File: BaseProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent, Map<Object, Object> options) {
    return createSection(parent, null, true, options);
}
 
Example #25
Source File: BaseProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent) {
    return createSection(parent, null, true, new HashMap<>());
}
 
Example #26
Source File: BaseProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent, int level, boolean numbered, Map<Object, Object> options) {
    return createSection(parent, Integer.valueOf(level), numbered, options);
}
 
Example #27
Source File: BaseProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Section createSection(StructuralNode parent, boolean numbered, Map<Object, Object> options) {
    return createSection(parent, null, numbered, options);
}
 
Example #28
Source File: Processor.java    From asciidoctorj with Apache License 2.0 votes vote down vote up
Section createSection(StructuralNode parent, Integer level, boolean numbered, Map<Object, Object> options); 
Example #29
Source File: Processor.java    From asciidoctorj with Apache License 2.0 votes vote down vote up
Section createSection(StructuralNode parent, int level, boolean numbered, Map<Object, Object> options); 
Example #30
Source File: Processor.java    From asciidoctorj with Apache License 2.0 votes vote down vote up
Section createSection(StructuralNode parent, boolean numbered, Map<Object, Object> options);