Java源码示例:org.asciidoctor.ast.ContentNode
示例1
@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);
}
示例2
@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;
}
示例3
@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>
}
示例4
@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);
}
示例5
@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);
}
示例6
@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);
}
示例7
/**
* 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);
}
示例8
/**
* 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;
}
示例9
@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());
}
示例10
@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());
}
示例11
@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 "";
}
}
示例12
@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>
}
示例13
@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>
}
示例14
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);
}
}
示例15
@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}"));
}
示例16
@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);
}
示例17
@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);
}
示例18
@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);
}
示例19
@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();
}
示例20
@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");
}
String title = null;
if (params.containsKey("title")) {
Object titleParam = params.get("title");
if (!(titleParam instanceof TemplateScalarModel)) {
throw new TemplateModelException(
"The title parameter must be a string");
}
title = ((TemplateScalarModel) titleParam).getAsString();
} else if (node instanceof StructuralNode) {
title = stripHtmlMarkups(((StructuralNode) node).getTitle());
}
if (title == null) {
throw new TemplateModelException("missing title");
}
Object pageAttr = node.getDocument().getAttribute("page");
if (!(pageAttr instanceof Page)) {
throw new TemplateModelException(
"document attribute page is not valid");
}
Page page = (Page) pageAttr;
if (body == null) {
throw new TemplateModelException("Body is null");
}
StringWriter writer = new StringWriter();
body.render(writer);
SearchEntry entry = new SearchEntry(
page.getTargetPath(), stripHtmlMarkups(writer.toString()), title);
entries.add(entry);
}
示例21
@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);
}
}
示例22
public IconNode(ContentNode node) {
super(node.getAttributes());
alt = pop("alt", "default-alt");
}
示例23
public BlockImageNode(ContentNode node) {
super(node.getAttributes());
target = pop("target").replaceAll("\\s", "{sp}");
}
示例24
public PhraseNodeImpl(ContentNode parent, String context, Map<String, Object> attributes, List<String> roles, String type, String text, String target) {
super(parent, context, attributes, roles);
this.type = type;
this.text = text;
this.target = target;
}
示例25
public ContentNodeImpl(ContentNode parent, String context) {
this(parent, context, new HashMap<>(), new ArrayList<>());
}
示例26
public ContentNodeImpl(ContentNode parent, String context, Map<String, Object> attributes, List<String> roles) {
this.parent = parent;
this.context = context;
this.attributes = attributes;
this.roles = roles;
}
示例27
@Override
@Deprecated
public ContentNode parent() {
return getParent();
}
示例28
@Override
public ContentNode getParent() {
return parent;
}
示例29
@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, List<String> text, Map<String, Object> attributes, Map<Object, Object> options) {
return delegate.createPhraseNode(parent, context, text, attributes, options);
}
示例30
@Override
public PhraseNode createPhraseNode(ContentNode parent, String context, String text, Map<String, Object> attributes, Map<String, Object> options) {
return delegate.createPhraseNode(parent, context, text, attributes, options);
}