private boolean isMacroMarkerBlockAndEmpty(Block block) { return block instanceof MacroMarkerBlock && block.getChildren().isEmpty(); } }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#getChildrenByType(java.lang.Class, boolean) */ public <T extends Block> List<T> getChildrenByType(Class<T> blockClass, boolean recurse) { List<T> typedBlocks = new ArrayList<T>(); for (Block block : getChildren()) { if (blockClass.isAssignableFrom(block.getClass())) { typedBlocks.add(blockClass.cast(block)); } if (recurse && !block.getChildren().isEmpty()) { typedBlocks.addAll(block.getChildrenByType(blockClass, true)); } } return typedBlocks; }
/** * Removes any top level paragraph since for example for the following use case we don't want an extra paragraph * block: <code>= hello {{velocity}}world{{/velocity}}</code>. * * @param blocks the blocks to check and convert */ public void removeTopLevelParagraph(List<Block> blocks) { // Remove any top level paragraph so that the result of a macro can be used inline for example. // We only remove the paragraph if there's only one top level element and if it's a paragraph. if ((blocks.size() == 1) && blocks.get(0) instanceof ParagraphBlock) { Block paragraphBlock = blocks.remove(0); blocks.addAll(0, paragraphBlock.getChildren()); } } }
/** * Removes any top level paragraph since for example for the following use case we don't want an extra paragraph * block: <code>= hello {{velocity}}world{{/velocity}}</code>. * * @param blocks the blocks to check and convert */ private Block removeTopLevelParagraph(Block block) { List<Block> blocks = block.getChildren(); // Remove any top level paragraph so that the result of a macro can be used inline for example. // We only remove the paragraph if there's only one top level element and if it's a paragraph. if ((block.getChildren().size() == 1) && block.getChildren().get(0) instanceof ParagraphBlock) { Block paragraphBlock = blocks.remove(0); blocks.addAll(0, paragraphBlock.getChildren()); return new CompositeBlock(blocks); } return block; }
/** * @param headerBlock the section title. * @return the filtered label to use in toc anchor link. */ public List<Block> generateLabel(HeaderBlock headerBlock) { return headerBlock.clone(this).getChildren(); } }
/** * @param block the block to filter out * @param blockClass the type of Blocks to look for * @param recurse if true also search recursively children * @param <T> the class of the Blocks to return * @return the filtered blocks matching the passed Block class */ public <T extends Block> List<T> getChildrenByType(Block block, Class<T> blockClass, boolean recurse) { List<T> typedBlocks = new ArrayList<T>(); for (Block child : filter(block.getChildren())) { if (blockClass.isAssignableFrom(child.getClass())) { typedBlocks.add(blockClass.cast(child)); } if (recurse && !child.getChildren().isEmpty()) { typedBlocks.addAll(getChildrenByType(child, blockClass, true)); } } return typedBlocks; }
/** * Utility method for recursively traversing the XDOM and extracting a title. * * @param parent parent block. * @return a title if found or null. */ private String getTitle(Block parent) { for (Block block : parent.getChildren()) { if (block instanceof HeaderBlock) { String title = renderTitle((HeaderBlock) block); if (!StringUtils.isBlank(title)) { return title; } } else if (block instanceof SectionBlock) { return getTitle(block); } } return null; }
Block firstBlock = block.getChildren().get(0); if (firstBlock instanceof HeaderBlock) { DocumentResourceReference reference = new DocumentResourceReference(target); return new LinkBlock(clonedHeaderBlock.getChildren(), reference, false); } else if (firstBlock instanceof SectionBlock) { return createLink(firstBlock, target);
} else if (block.getClass() == LinkBlock.class && block.getChildren().size() == 0) { ResourceReference reference = ((LinkBlock) block).getReference(); return this.plainTextParser.parse(new StringReader(label)).getChildren().get(0).getChildren(); } catch (ParseException e) {
@Override public void format(String tokenType, String value, Map<String, Object> style) { List<Block> blockList; if (StringUtils.isNotEmpty(value)) { try { blockList = this.plainTextParser.parse(new StringReader(value)).getChildren().get(0).getChildren(); } catch (ParseException e) { // This shouldn't happen since the parser cannot throw an exception since the source is a memory // String. throw new RuntimeException("Failed to parse [" + value + "] as plain text.", e); } String styleParameter = formatStyle(style); FormatBlock formatBlock = null; if (styleParameter.length() > 0) { formatBlock = new FormatBlock(blockList, Format.NONE); formatBlock.setParameter("style", styleParameter); this.blocks.add(formatBlock); } else { this.blocks.addAll(blockList); } } }
/** * {@inheritDoc} * * @see org.xwiki.rendering.block.Block#getPreviousBlockByType(java.lang.Class, boolean) */ public <T extends Block> T getPreviousBlockByType(Class<T> blockClass, boolean recurse) { if (getParent() == null) { return null; } int index = indexOfBlock(this, getParent().getChildren()); // test previous brothers List<Block> blocks = getParent().getChildren(); for (int i = index - 1; i >= 0; --i) { Block previousBlock = blocks.get(i); if (blockClass.isAssignableFrom(previousBlock.getClass())) { return blockClass.cast(previousBlock); } } // test parent if (blockClass.isAssignableFrom(getParent().getClass())) { return blockClass.cast(getParent()); } // recurse return recurse ? getParent().getPreviousBlockByType(blockClass, true) : null; }
private Block extractResult(Block block, Map<String, Object> macroContext, boolean async) { Object resultObject = macroContext.get(MACRO_RESULT_KEY); Block result; if (resultObject instanceof List) { result = new CompositeBlock((List<Block>) resultObject); } else if (resultObject instanceof Block) { result = (Block) resultObject; } else { if (!async) { // If synchronized the top level block is a temporary macro marker so we need to get rid of it result = new CompositeBlock(block.getChildren()); } else { result = block; } // If in inline mode remove any top level paragraph. if (this.inline) { result = removeTopLevelParagraph(result); } } return result; }
filteredBlocks = clonedChildBlocks.getChildren();
@Override protected List<Block> parseContent(CodeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException { List<Block> result; try { if (LANGUAGE_NONE.equalsIgnoreCase(parameters.getLanguage())) { if (StringUtils.isEmpty(content)) { result = Collections.emptyList(); } else { result = this.plainTextParser.parse(new StringReader(content)).getChildren().get(0).getChildren(); } } else { result = highlight(parameters, content); } } catch (Exception e) { throw new MacroExecutionException("Failed to highlight content", e); } Map<String, String> formatParameters = new LinkedHashMap<String, String>(); formatParameters.put("class", "code"); if (context.isInline()) { result = Arrays.<Block> asList(new FormatBlock(result, Format.NONE, formatParameters)); } else { result = Arrays.<Block> asList(new GroupBlock(result, formatParameters)); } return result; }
blocks = listener.getBlocks(); } else { blocks = this.plainTextParser.parse(new StringReader(code)).getChildren().get(0).getChildren();
int sectionLevel = header.getLevel().getAsInt(); for (int level = 1; level < sectionLevel && blocks.size() == 1 && blocks.get(0) instanceof SectionBlock; ++level) { blocks = blocks.get(0).getChildren();
public String extractTitle() { String title = ""; try { if (is10Syntax()) { title = extractTitle10(); } else { List<HeaderBlock> blocks = getXDOM().getChildrenByType(HeaderBlock.class, true); if (blocks.size() > 0) { HeaderBlock header = blocks.get(0); if (header.getLevel().compareTo(HeaderLevel.LEVEL2) <= 0) { XDOM headerXDOM = new XDOM(Collections.<Block> singletonList(header)); // transform TransformationContext context = new TransformationContext(headerXDOM, getSyntax()); Utils.getComponent(TransformationManager.class).performTransformations(headerXDOM, context); // render Block headerBlock = headerXDOM.getChildren().get(0); if (headerBlock instanceof HeaderBlock) { title = renderXDOM(new XDOM(headerBlock.getChildren()), Syntax.XHTML_1_0); } } } } } catch (Exception e) { // Don't stop when there's a problem rendering the title. } return title; }
private Block tranform(XDOM xdom, Block block) throws RenderingException { TransformationContext transformationContext = new TransformationContext(xdom, this.configuration.getDefaultSyntax(), false); transformationContext.setTargetSyntax(this.configuration.getTargetSyntax()); transformationContext.setId(this.configuration.getTransformationId()); transform(block, transformationContext); // The result is often inserted in a bigger content so we remove the XDOM around it if (block instanceof XDOM) { return new MetaDataBlock(block.getChildren(), ((XDOM) block).getMetaData()); } return block; } }
@Override public List<Block> execute(WikiMacroParameters parameters, String macroContent, MacroTransformationContext context) throws MacroExecutionException { validate(parameters, macroContent); // Create renderer DefaultWikiMacroRenderer renderer; try { renderer = this.componentManager.getInstance(DefaultWikiMacroRenderer.class); } catch (ComponentLookupException e) { throw new MacroExecutionException("Failed to create wiki macro rendeder", e); } // Initialize the renderer renderer.initialize(this, parameters, macroContent, context); AsyncRendererConfiguration configuration = new AsyncRendererConfiguration(); configuration.setSecureReference(getDocumentReference(), getAuthorReference()); configuration.setContextEntries(this.contextEntries); // Execute the renderer Block result; try { result = this.executor.execute(renderer, configuration); } catch (Exception e) { throw new MacroExecutionException("Failed to execute wiki macro", e); } return result instanceof CompositeBlock ? result.getChildren() : Arrays.asList(result); }
blocks = block.getChildren(); } else { blocks = Arrays.asList(block);