org.asciidoctor.ast.Block Java Examples

The following examples show how to use org.asciidoctor.ast.Block. 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
public Block convertToTerminalListing(Block block) {

        Map<String, Object> attributes = block.getAttributes();
        attributes.put("role", "terminal");
        StringBuilder resultLines = new StringBuilder();

        List<String> lines = block.getLines();

        for (String line : lines) {
            if (line.startsWith("$")) {
                resultLines.append("<span class=\"command\">")
                        .append(line.substring(2, line.length()))
                        .append("</span>");
            } else {
                resultLines.append(line);
            }
        }

        return createBlock(this.document, "listing", Arrays.asList(resultLines.toString()), attributes,
                new HashMap<Object, Object>());
    }
 
Example #2
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 #3
Source File: SyntaxHighlighterProxy.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@JRubyMethod(name = "highlight", required = 4)
public IRubyObject highlight(ThreadContext context, IRubyObject[] args) {
  Highlighter highlighter = (Highlighter) delegate;
  IRubyObject blockRuby = args[0];
  Block block = (Block) NodeConverter.createASTNode(blockRuby);

  IRubyObject sourceRuby = args[1];
  String source = sourceRuby.asJavaString();

  IRubyObject langRuby = args[2];
  String lang = langRuby.asJavaString();

  RubyHash optionsRuby = (RubyHash) args[3];
  Map<String, Object> options = new RubyHashMapDecorator(optionsRuby);

  HighlightResult result = highlighter.highlight(block, source, lang, options);
  if (result.getLineOffset() != null) {
    return getRuntime().newArray(getRuntime().newString(result.getHighlightedSource()), getRuntime().newFixnum(result.getLineOffset()));
  } else {
    return getRuntime().newString(result.getHighlightedSource());
  }
}
 
Example #4
Source File: PrismJsHighlighter.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Override
public HighlightResult highlight(Block node,
                                 String source,
                                 String lang,
                                 Map<String, Object> options) {
    ScriptContext ctx = scriptEngine.getContext();                                     // <4>
    Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("text", source);
    bindings.put("language", lang);

    try {
        String result = (String) scriptEngine.eval(
            "Prism.highlight(text, Prism.languages[language], language)", bindings);
        return new HighlightResult(result);
    } catch (ScriptException e) {
        throw new RuntimeException(e);
    }
}
 
Example #5
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 #6
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 #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: 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 #9
Source File: SyntaxHighlighterProxy.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@JRubyMethod(name = "format", required = 3)
public IRubyObject format(ThreadContext context, IRubyObject blockRuby, IRubyObject langRuby, IRubyObject optionsRuby) {
  Formatter formatter = (Formatter) delegate;

  Block block = (Block) NodeConverter.createASTNode(blockRuby);
  String lang = langRuby.asJavaString();
  Map<String, Object> options = new RubyHashMapDecorator((RubyHash) optionsRuby);

  String result = formatter.format(block, lang, options);

  return getRuntime().newString(result);
}
 
Example #10
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 #11
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, List<String> content, Map<String, Object> attributes,
                         Map<Object, Object> options) {

    options.put(Options.SOURCE, content);
    options.put(Options.ATTRIBUTES, new HashMap<>(attributes));
    return createBlock(parent, context, options);
}
 
Example #12
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, String content, Map<String, Object> attributes,
                         Map<Object, Object> options) {

    options.put(Options.SOURCE, content);
    options.put(Options.ATTRIBUTES, attributes);

    return createBlock(parent, context, options);
}
 
Example #13
Source File: OrderDocumentingHighlighter.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public HighlightResult highlight(Block node,
                                 String source,
                                 String lang,
                                 Map<String, Object> options) {
    messages.add("highlight `" + source + "`");
    return new HighlightResult(source);
}
 
Example #14
Source File: OrderDocumentingHighlighter.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public String format(Block node, String lang, Map<String, Object> opts) {
    messages.add("format `" + lang + "`");
    return "<pre class='highlight'><code>"
        + node.getContent()
        + "</code></pre>";
}
 
Example #15
Source File: TerminalCommandTreeprocessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public Block convertToTerminalListing(Block originalBlock) {
    Map<Object, Object> options = new HashMap<Object, Object>();
    options.put("subs", ":specialcharacters");

    Block block = createBlock(                                   // <4>
            (StructuralNode) originalBlock.getParent(),
            "listing",
            originalBlock.getLines(),
            originalBlock.getAttributes(),
            options);

    block.addRole("terminal");                                   // <5>
    return block;
}
 
Example #16
Source File: CardBlockProcessor.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 a block with context "card", and put the parsed content into it
    Block block = this.createBlock(parent, "card", reader.readLines(),
            attributes, opts);

    // if the link attribute is present
    // add a link into the content with a marker as text
    String link = (String) attributes.get("link");
    if (link != null) {
        String linkPhrase;
        String linkType = (String) attributes.get("link-type");
        if (linkType == null || linkType.equals("xref")) {
            linkPhrase = "<<" + link + "," + BLOCKLINK_TEXT + ">>";
        } else if (linkType.equals("url")) {
            linkPhrase = "link:" + link + "[" + BLOCKLINK_TEXT + "]";
        } else {
            linkPhrase = null;
            LOGGER.warning(link);
        }
        if (linkPhrase != null){
            parseContent(block, Arrays.asList(linkPhrase));
            // trigger rendering for the nested content here to trigger the
            // converter so that the converter can catch the generated
            // phrase node and add it as an attribute named _link to the
            // block
            block.getContent();
        }
    }
    return block;
}
 
Example #17
Source File: OpenApiHelpers.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
public static void appendDescription(StructuralNode node, String description) {
    if (StringUtils.isNotBlank(description)) {
        Block paragraph = new ParagraphBlockImpl(node);
        paragraph.setSource(description);
        node.append(paragraph);
    }
}
 
Example #18
Source File: OpenApiHelpers.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
public static Document generateInnerDoc(Table table, String documentContent, String id) {
    Document innerDoc = new DocumentImpl(table);
    if (StringUtils.isNotBlank(id)) {
        innerDoc.setId(id);
    }

    Block paragraph = new ParagraphBlockImpl(innerDoc);
    paragraph.setSource(documentContent);
    innerDoc.append(paragraph);
    return innerDoc;
}
 
Example #19
Source File: ExternalDocumentationComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
@Override
public StructuralNode apply(StructuralNode node, Parameters params) {
    ExternalDocumentation externalDocs = params.externalDocs;
    if (externalDocs == null) return node;

    String url = externalDocs.getUrl();
    if (StringUtils.isNotBlank(url)) {
        Block paragraph = new ParagraphBlockImpl(node);
        String desc = externalDocs.getDescription();
        paragraph.setSource(url + (StringUtils.isNotBlank(desc) ? "[" + desc + "]" : ""));
        node.append(paragraph);
    }

    return node;
}
 
Example #20
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 #21
Source File: BaseProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Block createBlock(StructuralNode parent, String context, List<String> content, Map<String, Object> attributes,
                         Map<Object, Object> options) {

    options.put(Options.SOURCE, content);
    options.put(Options.ATTRIBUTES, new HashMap<>(attributes));

    return createBlock(parent, context, options);
}
 
Example #22
Source File: PrismJsHighlighter.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public String format(Block node, String lang, Map<String, Object> opts) {
    return "<pre class='highlight'><code>"  // <3>
        + node.getContent()
        + "</code></pre>";
}
 
Example #23
Source File: PassthroughFixDirective.java    From helidon-build-tools with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(Environment env,
        Map params, TemplateModel[] loopVars,
        TemplateDirectiveBody body)
        throws TemplateException, IOException {

    if (loopVars.length != 0) {
            throw new TemplateModelException(
                "This directive does not allow loop variables.");
    }

    // Check if no parameters were given:
    if (body != null) {
        throw new TemplateModelException(
                "This directive does not allow body content.");
    }

    TemplateModel textVar = env.getVariable("text");
    if (!(textVar instanceof TemplateScalarModel)) {
        throw new TemplateModelException(
                "text variable is not a TemplateScalarModel");
    }

    String text = ((TemplateScalarModel) textVar).getAsString();

    if (PLACEHOLDER.equals(text)) {

        TemplateModel parentVar = env.getVariable("parent");
        if (!(parentVar instanceof ContentNodeHashModel)) {
            throw new TemplateModelException(
                    "pareant variable is not a ContentNodeHashModel");
        }

        ContentNode parent = ((ContentNodeHashModel) parentVar).getContentNode();

        String source;
        if (parent instanceof Block) {
            source = ((Block) parent).getSource();
        } else if (parent instanceof Cell) {
            source = ((Cell) parent).getSource();
        } else {
            throw new TemplateModelException(
                    "parent is not a Block or a Cell");
        }

        if (source == null || source.isEmpty()) {
            throw new TemplateModelException(
                    "source is null or empty");
        }

        String fixed = formatInlineSource(source);
        env.getOut().write(fixed);

    } else {
        // nothing to do, just write the text out
        env.getOut().write(text);
    }
}
 
Example #24
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 #25
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) {
    return createBlock(parent, context, content, new HashMap<>(), new HashMap<>());
}
 
Example #26
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, String content, Map<String, Object> attributes) {
    return createBlock(parent, context, content, attributes, 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, String content) {
    return createBlock(parent, context, content, new HashMap<>(), new HashMap<>());
}
 
Example #28
Source File: BlockListingNode.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public BlockListingNode(Block node) {
    super(node);
    this.node = node;
}
 
Example #29
Source File: SourceNode.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public SourceNode(Block node) {
    super(node);
    this.node = node;
}
 
Example #30
Source File: BaseProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public Block createBlock(StructuralNode parent, String context, Map<Object, Object> options) {
    return delegate.createBlock(parent, context, options);
}