Search in sources :

Example 1 with ParagraphBlock

use of org.xwiki.rendering.block.ParagraphBlock 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 ParagraphBlock

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

the class RssMacro method generateBoxTitle.

/**
 * Renders the RSS's title.
 *
 * @param cssClass the CSS sheet
 * @param feed the RSS feed data
 * @return the list of blocks making the RSS Box title
 */
private List<? extends Block> generateBoxTitle(String cssClass, SyndFeed feed) {
    List<Block> titleBlocks;
    if (feed.getLink() == null) {
        titleBlocks = parsePlainText(feed.getTitle());
    } else {
        // Title link.
        ResourceReference titleResourceReference = new ResourceReference(feed.getLink(), ResourceType.URL);
        // Title text link.
        Block titleTextLinkBlock = new LinkBlock(parsePlainText(feed.getTitle()), titleResourceReference, true);
        // Rss icon.
        String imagePath = this.skinAccessBridge.getSkinFile(FEED_ICON_RESOURCE_PATH);
        ImageBlock imageBlock = new ImageBlock(new ResourceReference(imagePath, ResourceType.URL), false);
        // Title rss icon link.
        Block titleImageLinkBlock = new LinkBlock(Arrays.<Block>asList(imageBlock), titleResourceReference, true);
        titleBlocks = Arrays.<Block>asList(titleTextLinkBlock, titleImageLinkBlock);
    }
    ParagraphBlock titleBlock = new ParagraphBlock(titleBlocks);
    titleBlock.setParameter(CLASS_ATTRIBUTE, cssClass);
    return Collections.singletonList(titleBlock);
}
Also used : ImageBlock(org.xwiki.rendering.block.ImageBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) 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 3 with ParagraphBlock

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

the class ContextMacroTest method executeOk.

@Test
public void executeOk() throws Exception {
    MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
    MacroTransformationContext macroContext = new MacroTransformationContext();
    macroContext.setSyntax(Syntax.XWIKI_2_0);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    DocumentReference referencedDocumentReference = new DocumentReference("wiki", "space", "page");
    when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(referencedDocumentReference);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
    MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
    XDOM xdom = new XDOM(Arrays.asList((Block) new ParagraphBlock(Arrays.asList((Block) new LinkBlock(Collections.emptyList(), new ResourceReference("", ResourceType.DOCUMENT), false)))));
    when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(xdom);
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("wiki:space.page");
    // Note: we're not testing the returned value here since this is done in integation tests.
    this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) LinkBlock(org.xwiki.rendering.block.LinkBlock) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 4 with ParagraphBlock

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

the class ChartMacro method execute.

@Override
public List<Block> execute(ChartMacroParameters macroParams, String content, MacroTransformationContext context) throws MacroExecutionException {
    // Generate the chart image in a temporary location.
    generateChart(macroParams, content, context);
    String imageLocation = this.imageWriter.getURL(new ImageId(macroParams));
    String title = macroParams.getTitle();
    ResourceReference reference = new ResourceReference(imageLocation, ResourceType.URL);
    ImageBlock imageBlock = new ImageBlock(new ResourceReference(imageLocation, ResourceType.URL), true);
    imageBlock.setParameter("alt", title);
    LinkBlock linkBlock = new LinkBlock(Collections.singletonList((Block) imageBlock), reference, true);
    linkBlock.setParameter("title", title);
    // If the macro is used standalone then we need to wrap it in a paragraph block.
    Block resultBlock;
    if (context.isInline()) {
        resultBlock = linkBlock;
    } else {
        resultBlock = new ParagraphBlock(Collections.singletonList((Block) linkBlock));
    }
    return Collections.singletonList(resultBlock);
}
Also used : ImageBlock(org.xwiki.rendering.block.ImageBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference)

Example 5 with ParagraphBlock

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

the class FormulaMacro method execute.

@Override
public List<Block> execute(FormulaMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    if (StringUtils.isEmpty(content)) {
        throw new MacroExecutionException(CONTENT_MISSING_ERROR);
    }
    String rendererHint = this.configuration.getRenderer();
    FontSize size = parameters.getFontSize();
    Type type = parameters.getImageType();
    Block result;
    try {
        result = render(content, context.isInline(), size, type, rendererHint);
    } catch (MacroExecutionException ex) {
        this.logger.debug("Failed to render content with the [{}] renderer. Falling back to the safe renderer.", rendererHint, ex);
        try {
            result = render(content, context.isInline(), size, type, this.configuration.getSafeRenderer());
        } catch (IllegalArgumentException ex2) {
            throw new MacroExecutionException(WRONG_CONTENT_ERROR);
        }
    } catch (IllegalArgumentException ex) {
        throw new MacroExecutionException(WRONG_CONTENT_ERROR);
    }
    // If no image was generated, just return the original text
    if (result == null) {
        result = new WordBlock(content);
    }
    // Block level formulae should be wrapped in a paragraph element
    if (!context.isInline()) {
        result = new ParagraphBlock(Collections.<Block>singletonList(result));
    }
    return Collections.singletonList(result);
}
Also used : Type(org.xwiki.formula.FormulaRenderer.Type) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) FontSize(org.xwiki.formula.FormulaRenderer.FontSize) WordBlock(org.xwiki.rendering.block.WordBlock) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Block(org.xwiki.rendering.block.Block) ImageBlock(org.xwiki.rendering.block.ImageBlock) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock)

Aggregations

ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)9 Block (org.xwiki.rendering.block.Block)8 LinkBlock (org.xwiki.rendering.block.LinkBlock)6 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)5 ImageBlock (org.xwiki.rendering.block.ImageBlock)4 WordBlock (org.xwiki.rendering.block.WordBlock)4 XDOM (org.xwiki.rendering.block.XDOM)4 Test (org.junit.Test)3 DocumentReference (org.xwiki.model.reference.DocumentReference)2 WikiDocument (org.xwiki.refactoring.WikiDocument)2 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)2 NamingCriterion (org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion)2 GroupBlock (org.xwiki.rendering.block.GroupBlock)2 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)2 IdBlock (org.xwiki.rendering.block.IdBlock)2 MacroBlock (org.xwiki.rendering.block.MacroBlock)2 RawBlock (org.xwiki.rendering.block.RawBlock)2 SectionBlock (org.xwiki.rendering.block.SectionBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)1