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")));
}
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations