org.asciidoctor.ast.ContentNode Java Examples

The following examples show how to use org.asciidoctor.ast.ContentNode. 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: ObjectWrapper.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
@Override
public TemplateModel wrap(Object obj) throws TemplateModelException {
    if (obj == null) {
        return super.wrap(obj);
    }
    if (obj instanceof ContentNode) {
        return new ContentNodeHashModel(this, (ContentNode) obj);
    }
    if (obj instanceof RubyAttributesMapDecorator) {
        return new ContentNodeAttributesModel(this,
                (RubyAttributesMapDecorator) obj);
    }
    if (obj instanceof Document){
        return new SimpleObjectModel(obj);
    }
    return super.wrap(obj);
}
 
Example #2
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, String text, Map<String, Object> attributes, Map<String, Object> options) {

    Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

    options.put(Options.ATTRIBUTES, RubyHashUtil.convertMapToRubyHashWithStrings(rubyRuntime, attributes));

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

    IRubyObject[] parameters = {
            ((ContentNodeImpl) parent).getRubyObject(),
            RubyUtils.toSymbol(rubyRuntime, context),
            text == null ? rubyRuntime.getNil() : rubyRuntime.newString(text),
            convertedOptions};
    return (PhraseNode) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.INLINE_CLASS, parameters);
}
 
Example #3
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, List<String> text, Map<String, Object> attributes, Map<Object, Object> options) {

    Ruby rubyRuntime = JRubyRuntimeContext.get(parent);

    options.put(Options.ATTRIBUTES, attributes);

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

    RubyArray rubyText = rubyRuntime.newArray();
    rubyText.addAll(text);

    IRubyObject[] parameters = {
            ((ContentNodeImpl) parent).getRubyObject(),
            RubyUtils.toSymbol(rubyRuntime, context),
            rubyText,
            convertMapToRubyHashWithSymbols};
    return (PhraseNode) NodeConverter.createASTNode(rubyRuntime, NodeConverter.NodeType.INLINE_CLASS, parameters);
}
 
Example #4
Source File: ConverterProxy.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@JRubyMethod(required = 1, optional = 2)
public IRubyObject convert(ThreadContext context, IRubyObject[] args) {
    ContentNode node = NodeConverter.createASTNode(args[0]);

    T ret = null;
    if (args.length == 1) {
        ret = delegate.convert(
                node,
                null,
                Collections.emptyMap());
    } else if (args.length == 2) {
        ret = delegate.convert(
                node,
                (String) JavaEmbedUtils.rubyToJava(getRuntime(), args[1], String.class),
                Collections.emptyMap());//RubyString.objAsString(context, args[1]).asJavaString());
    } else if (args.length == 3) {
        ret = (T) delegate.convert(
                node,
                (String) JavaEmbedUtils.rubyToJava(getRuntime(), args[1], String.class),
                (Map) JavaEmbedUtils.rubyToJava(getRuntime(), args[2], Map.class));
    }
    this.output = ret;
    return JavaEmbedUtils.javaToRuby(getRuntime(), ret);
}
 
Example #5
Source File: IssueInlineMacroProcessor.java    From asciidoctorj with Apache License 2.0 6 votes vote down vote up
@Override
public Object process(                                               // <3>
        ContentNode parent, String target, Map<String, Object> attributes) {

    String href =
            new StringBuilder()
                .append("https://github.com/")
                .append(attributes.containsKey("repo") ?
                        attributes.get("repo") :
                        parent.getDocument().getAttribute("repo"))
                .append("/issues/")
                .append(target).toString();

    Map<String, Object> options = new HashMap<>();
    options.put("type", ":link");
    options.put("target", href);
    return createPhraseNode(parent, "anchor", target, attributes, options); // <4>
}
 
Example #6
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 #7
Source File: ContentNodeHashModel.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance of {@link ContentNodeHashModel}.
 * @param objectWrapper the object wrapper to use
 * @param node the {@link ContentNode} to expose as {@link TemplateHashModel}
 */
public ContentNodeHashModel(ObjectWrapper objectWrapper, ContentNode node) {
    Objects.requireNonNull(objectWrapper);
    this.objectWrapper = objectWrapper;
    Objects.requireNonNull(node);
    this.contentNode = node;
}
 
Example #8
Source File: ManpageInlineMacroProcessor.java    From asciidoctor-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public String process(ContentNode parent, String target, Map<String, Object> attributes) {

    Map<String, Object> options = new HashMap<String, Object>();
    options.put("type", ":link");
    options.put("target", target + ".html");
    return createPhraseNode(parent, "anchor", target, attributes, options).convert();
}
 
Example #9
Source File: InlineMacroRegistrationTest.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
    String transformed = target.chars()
        .mapToObj(c -> Character.isUpperCase(c) ? " " + (char) c : Character.toString((char) c))
        .collect(joining())
        .toUpperCase().trim();
    return createPhraseNode(parent, "quoted", transformed);
}
 
Example #10
Source File: InlineMacroRegistrationTest.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
    String transformed = target.chars()
        .mapToObj(c -> Character.isUpperCase(c) ? " " + (char) c : Character.toString((char) c))
        .collect(joining())
        .toUpperCase().trim();
    return createPhraseNode(parent, "quoted", transformed);
}
 
Example #11
Source File: ManpageMacro.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(ContentNode parent, String target,
        Map<String, Object> attributes) {
    Map<String, Object> options = new HashMap<String, Object>();
    options.put("type", ":link");
    options.put("target", target + ".html");
    return createPhraseNode(parent, "anchor", target, attributes, options);
}
 
Example #12
Source File: WhenTheInlineMacroProcessorRunsTwice.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Test
public void inlineMacroAttributes() {
    Asciidoctor asciidoctor = Asciidoctor.Factory.create();
    asciidoctor.javaExtensionRegistry().inlineMacro(new InlineMacroProcessor("example") {

        @Override
        public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
            return createPhraseNode(parent, "quoted", attributes.toString(), attributes, new HashMap<>());
        }

    });
    assertThat(convert(asciidoctor), containsString("{format=test}"));
    assertThat(convert(asciidoctor), containsString("{format=test}"));
}
 
Example #13
Source File: JRubyRuntimeContext.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public static Ruby get(ContentNode node) {
    if (node instanceof IRubyObject) {
        return ((IRubyObject) node).getRuntime();
    } else if (node instanceof RubyObjectHolderProxy) {
        return ((RubyObjectHolderProxy) node).__ruby_object().getRuntime();
    } else if (node instanceof ContentNodeImpl) {
        IRubyObject nodeDelegate = ((ContentNodeImpl) node).getRubyObject();
        return nodeDelegate.getRuntime();
    } else {
        throw new IllegalArgumentException("Don't know what to with a " + node);
    }
}
 
Example #14
Source File: ImageInlineMacroProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {

    Map<String, Object> options = new HashMap<String, Object>();
    options.put("type", "image");                                            // <1>
    options.put("target", "http://foo.bar/" + target);                       // <2>

    String[] items = target.split("\\|");
    Map<String, Object> attrs = new HashMap<String, Object>();
    attrs.put("alt", "Image not available");                                 // <3>
    attrs.put("width", "64");
    attrs.put("height", "64");

    return createPhraseNode(parent, "image", (String) null, attrs, options); // <4>
}
 
Example #15
Source File: ContextMenuInlineMacroProcessor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public Object process(ContentNode parent, String target, Map<String, Object> attributes) {
    String[] items = target.split("\\|");
    Map<String, Object> attrs = new HashMap<String, Object>();
    attrs.put("menu", "Right click");                              // <1>
    List<String> submenus = new ArrayList<String>();
    for (int i = 0; i < items.length - 1; i++) {
        submenus.add(items[i]);
    }
    attrs.put("submenus", submenus);
    attrs.put("menuitem", items[items.length - 1]);

    return createPhraseNode(parent, "menu", (String) null, attrs); // <2>
}
 
Example #16
Source File: FreemarkerEngine.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Render a template.
 *
 * @param template the relative path of the template to render
 * @param node the asciidoctor node to use as model for the template
 * @return the rendered output
 * @throws RenderingException if an error occurred
 */
public String renderString(String template, ContentNode node)
        throws RenderingException {

    Object session = node.getDocument().getAttribute("templateSession");
    checkNonNull(session, "document attribute 'templateSession'");
    if (!(session instanceof TemplateSession)) {
        throw new IllegalStateException(
                "document attribute 'templateSession' is not valid");
    }
    // TODO extract page, pages, templateSession
    // and set them as variables
    return renderString(template, node, (TemplateSession) session);
}
 
Example #17
Source File: CustomLayoutDirective.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Environment env,
                    Map params,
                    TemplateModel[] loopVars,
                    TemplateDirectiveBody body)
        throws TemplateException, IOException {

    TemplateModel dataModel = env.getDataModel().get("this");
    if (!(dataModel instanceof ContentNodeHashModel)) {
        throw new TemplateModelException(
                "Data model is not a ContentNodeHashModel");
    }
    ContentNode node = ((ContentNodeHashModel) dataModel).getContentNode();
    if (node == null) {
        throw new TemplateModelException("'this' has a null content-node");
    }

    Object page = node.getDocument().getAttribute("page");
    if (page == null || !(page instanceof Page)) {
        throw new TemplateModelException("Unable to get page instance");
    }

    if (body == null) {
        throw new TemplateModelException("Body is null");
    }
    StringWriter writer = new StringWriter();
    body.render(writer);
    mappings.put(((Page) page).getSourcePath(), writer.toString());
}
 
Example #18
Source File: VueBindingsDirective.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Environment env,
                    Map params,
                    TemplateModel[] loopVars,
                    TemplateDirectiveBody body)
        throws TemplateException, IOException {

    TemplateModel dataModel = env.getDataModel().get("this");
    if (!(dataModel instanceof ContentNodeHashModel)) {
        throw new TemplateModelException(
                "Data model is not a ContentNodeHashModel");
    }
    ContentNode node = ((ContentNodeHashModel) dataModel).getContentNode();
    if (node == null) {
        throw new TemplateModelException("'this' has a null content-node");
    }

    Object page = node.getDocument().getAttribute("page");
    if (page == null || !(page instanceof Page)) {
        throw new TemplateModelException("Unable to get page instance");
    }

    if (body == null) {
        throw new TemplateModelException("Body is null");
    }
    StringWriter writer = new StringWriter();
    body.render(writer);
    bindings.put(((Page) page).getSourcePath(), writer.toString());
}
 
Example #19
Source File: AsciidocConverter.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
@Override
public String convert(ContentNode node,
                      String transform,
                      Map<Object, Object> opts) {

    if (node != null && node.getNodeName() != null) {
        String templateName;
        if (node.equals(node.getDocument())) {
            templateName = "document";
        } else if (node.isBlock()) {
            templateName = "block_" + node.getNodeName();
        } else {
            // detect phrase node for generated block links
            if (node.getNodeName().equals("inline_anchor")
                    && BLOCKLINK_TEXT.equals(((PhraseNode) node).getText())) {
                // store the link model as an attribute in the corresponding
                // block
                node.getParent().getParent().getAttributes()
                        .put("_link", (PhraseNode) node);
                // the template for the block is responsible for rendering
                // the link, discard the output
                return "";
            }
            templateName = node.getNodeName();
        }
        LOGGER.debug("Rendering node: {}", node);
        return templateEngine.renderString(templateName, node);
    } else {
        return "";
    }
}
 
Example #20
Source File: IconNode.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public IconNode(ContentNode node) {
    super(node.getAttributes());
    alt = pop("alt", "default-alt");
}
 
Example #21
Source File: BlockImageNode.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public BlockImageNode(ContentNode node) {
    super(node.getAttributes());
    target = pop("target").replaceAll("\\s", "{sp}");
}
 
Example #22
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, List<String> text) {
    return createPhraseNode(parent, context, text, new HashMap<>());
}
 
Example #23
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, List<String> text, Map<String, Object> attributes) {
    return createPhraseNode(parent, context, text, attributes, new HashMap<>());
}
 
Example #24
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, String text) {
    return createPhraseNode(parent, context, text, new HashMap<>());
}
 
Example #25
Source File: JRubyProcessor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, String text, Map<String, Object> attributes) {
    return createPhraseNode(parent, context, text, attributes, new HashMap<>());
}
 
Example #26
Source File: NodeConverter.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public static ContentNode createASTNode(Ruby runtime, NodeType nodeType, IRubyObject... args) {
    IRubyObject node = nodeType.getRubyClass(runtime).callMethod(runtime.getCurrentContext(), "new", args);
    return createASTNode(node);
}
 
Example #27
Source File: NodeConverter.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public static ContentNode createASTNode(Object object) {

        if (object instanceof IRubyObject || object instanceof RubyObjectHolderProxy) {
            IRubyObject rubyObject = asRubyObject(object);
            if (rubyObject.isNil()) {
                return null;
            }
            NodeCache nodeCache = NodeCache.get(rubyObject);
            ContentNode cachedNode = nodeCache.getASTNode();
            if (cachedNode != null) {
                return cachedNode;
            }

            ContentNode ret;

            switch (NodeType.getNodeType(rubyObject)) {
                case BLOCK_CLASS:
                    ret = new BlockImpl(rubyObject);
                    break;
                case SECTION_CLASS:
                    ret = new SectionImpl(rubyObject);
                    break;
                case DOCUMENT_CLASS:
                    ret = new DocumentImpl(rubyObject);
                    break;
                case INLINE_CLASS:
                    ret = new PhraseNodeImpl(rubyObject);
                    break;
                case LIST_CLASS:
                    ret = new ListImpl(rubyObject);
                    break;
                case LIST_ITEM_CLASS:
                    ret = new ListItemImpl(rubyObject);
                    break;
                case DEFINITIONLIST_CLASS:
                    ret = new DescriptionListImpl(rubyObject);
                    break;
                case DEFINITIONLIST_ITEM_CLASS:
                    ret = new DescriptionListEntryImpl(rubyObject);
                    break;
                case TABLE_CLASS:
                    ret = new TableImpl(rubyObject);
                    break;
                case TABLE_COLUMN_CLASS:
                    ret = new ColumnImpl(rubyObject);
                    break;
                case TABLE_CELL_CLASS:
                    ret = new CellImpl(rubyObject);
                    break;
                default:
                    throw new IllegalArgumentException("Don't know what to do with a " + rubyObject);
            }

            nodeCache.setASTNode(ret);

            return ret;
        } else if (object instanceof ContentNode) {

            return (ContentNode) object;

        } else {
            throw new IllegalArgumentException(object != null ? object.toString() : "null");
        }
    }
 
Example #28
Source File: NodeCache.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public ContentNode getASTNode() {
    ContentNode astNode = (ContentNode) cache.get(getRuntime().newSymbol(KEY_AST_NODE));
    return astNode;
}
 
Example #29
Source File: NodeCache.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public void setASTNode(ContentNode astNode) {
    cache.put(getRuntime().newSymbol(KEY_AST_NODE), astNode);
}
 
Example #30
Source File: ContentNodeImpl.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
@Override
@Deprecated
public ContentNode parent() {
    return getParent();
}