Search in sources :

Example 1 with RawBlock

use of org.xwiki.rendering.block.RawBlock in project xwiki-platform by xwiki.

the class RssMacro method generateEntries.

/**
 * Renders the given RSS's entries.
 *
 * @param parentBlock the parent Block to which the output is going to be added
 * @param feed the RSS Channel we retrieved via the Feed URL
 * @param parameters our parameter helper object
 * @throws MacroExecutionException if the content cannot be rendered
 */
private void generateEntries(Block parentBlock, SyndFeed feed, RssMacroParameters parameters) throws MacroExecutionException {
    int maxElements = parameters.getCount();
    int count = 0;
    for (Object item : feed.getEntries()) {
        ++count;
        if (count > maxElements) {
            break;
        }
        SyndEntry entry = (SyndEntry) item;
        ResourceReference titleResourceReference = new ResourceReference(entry.getLink(), ResourceType.URL);
        Block titleBlock = new LinkBlock(parsePlainText(entry.getTitle()), titleResourceReference, true);
        ParagraphBlock paragraphTitleBlock = new ParagraphBlock(Collections.singletonList(titleBlock));
        paragraphTitleBlock.setParameter(CLASS_ATTRIBUTE, "rssitemtitle");
        parentBlock.addChild(paragraphTitleBlock);
        if (parameters.isContent() && entry.getDescription() != null) {
            // We are wrapping the feed entry content in a HTML macro, not considering what the declared content
            // is, because some feed will declare text while they actually contain HTML.
            // See http://stuffthathappens.com/blog/2007/10/29/i-hate-rss/
            // A case where doing this might hurt is if a feed declares "text" and has any XML inside it does
            // not want to be interpreted as such, but displayed as is instead. But this certainly is too rare
            // compared to mis-formed feeds that say text while they want to say HTML.
            Block html = new RawBlock(entry.getDescription().getValue(), Syntax.XHTML_1_0);
            parentBlock.addChild(new GroupBlock(Arrays.asList(html), Collections.singletonMap(CLASS_ATTRIBUTE, "rssitemdescription")));
        }
    }
}
Also used : SyndEntry(com.sun.syndication.feed.synd.SyndEntry) LinkBlock(org.xwiki.rendering.block.LinkBlock) RawBlock(org.xwiki.rendering.block.RawBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) RawBlock(org.xwiki.rendering.block.RawBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference)

Example 2 with RawBlock

use of org.xwiki.rendering.block.RawBlock in project xwiki-platform by xwiki.

the class EditableGadgetRenderer method getGadgetEditMetadata.

/**
 * @param gadget the gadget to decorate
 * @return the block containing the metadata that will allow clients to edit this gadget
 */
protected Block getGadgetEditMetadata(Gadget gadget) {
    GroupBlock metadataBlock = new GroupBlock();
    metadataBlock.setParameter(CLASS, METADATA);
    // look at the content of the gadget and store whether it's a macro or not
    boolean isMacro = gadget.getContent().size() == 1 && gadget.getContent().get(0) instanceof MacroMarkerBlock;
    GroupBlock isMacroBlock = new GroupBlock();
    isMacroBlock.setParameter(CLASS, "isMacro");
    isMacroBlock.addChild(new WordBlock(Boolean.toString(isMacro)));
    metadataBlock.addChild(isMacroBlock);
    if (isMacro) {
        // render the annotated macro call in the page, to be able to edit it. Only the macro call comments will be
        // rendered, since transformations are not ran, so there is no content in the macro. But annotation is
        // enough.
        GroupBlock renderedContentBlock = new GroupBlock();
        renderedContentBlock.setParameter(CLASS, "content");
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer gadgetContentRenderer = getGadgetContentRenderer();
        gadgetContentRenderer.render(gadget.getContent(), printer);
        RawBlock rawBlock = new RawBlock(printer.toString(), getRawBlockSyntax(gadgetContentRenderer));
        renderedContentBlock.addChild(rawBlock);
        // render the title in the page as well, to be edited as source
        GroupBlock gadgetTitleBlock = new GroupBlock();
        gadgetTitleBlock.setParameter(CLASS, "title");
        // even if it's not a word, it's fine since it will be rendered in one piece
        gadgetTitleBlock.addChild(new WordBlock(gadget.getTitleSource()));
        metadataBlock.addChild(renderedContentBlock);
        metadataBlock.addChild(gadgetTitleBlock);
    }
    return metadataBlock;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WordBlock(org.xwiki.rendering.block.WordBlock) RawBlock(org.xwiki.rendering.block.RawBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Aggregations

GroupBlock (org.xwiki.rendering.block.GroupBlock)2 RawBlock (org.xwiki.rendering.block.RawBlock)2 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)1 Block (org.xwiki.rendering.block.Block)1 ImageBlock (org.xwiki.rendering.block.ImageBlock)1 LinkBlock (org.xwiki.rendering.block.LinkBlock)1 MacroMarkerBlock (org.xwiki.rendering.block.MacroMarkerBlock)1 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)1 WordBlock (org.xwiki.rendering.block.WordBlock)1 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)1 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)1 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)1 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)1