use of org.xwiki.rendering.renderer.printer.WikiPrinter in project xwiki-platform by xwiki.
the class RenderingScriptServiceTest method parseAndRender.
@Test
public void parseAndRender() throws Exception {
Parser parser = this.mocker.registerMockComponent(Parser.class, "plain/1.0");
when(parser.parse(argThat(new StringReaderMatcher("some [[TODO]] stuff")))).thenReturn(new XDOM(Collections.<Block>emptyList()));
BlockRenderer blockRenderer = this.mocker.registerMockComponent(BlockRenderer.class, "xwiki/2.0");
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
WikiPrinter printer = (WikiPrinter) invocationOnMock.getArguments()[1];
printer.print("some ~[~[TODO]] stuff");
return null;
}
}).when(blockRenderer).render(any(XDOM.class), any());
XDOM xdom = this.mocker.getComponentUnderTest().parse("some [[TODO]] stuff", "plain/1.0");
assertEquals("some ~[~[TODO]] stuff", this.mocker.getComponentUnderTest().render(xdom, "xwiki/2.0"));
}
use of org.xwiki.rendering.renderer.printer.WikiPrinter in project xwiki-platform by xwiki.
the class DefaultOfficeViewerScriptService method render.
/**
* Renders the given XDOM into specified syntax.
*
* @param xdom the {@link XDOM} to be rendered
* @param fromSyntax the syntax for which to perform the transformations
* @param toSyntax expected output syntax
* @return string holding the result of rendering
* @throws Exception if an error occurs during rendering
*/
private String render(XDOM xdom, Syntax fromSyntax, Syntax toSyntax) throws Exception {
// Perform the transformations. This is required for office presentations which use the gallery macro to display
// the slide images.
TransformationContext context = new TransformationContext(xdom, fromSyntax);
context.setTargetSyntax(toSyntax);
this.transformationManager.performTransformations(xdom, context);
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, toSyntax.toIdString());
renderer.render(xdom, printer);
return printer.toString();
}
use of org.xwiki.rendering.renderer.printer.WikiPrinter in project xwiki-platform by xwiki.
the class DocumentSolrMetadataExtractor method setFieldsInternal.
@Override
public boolean setFieldsInternal(LengthSolrInputDocument solrDocument, EntityReference entityReference) throws Exception {
DocumentReference documentReference = new DocumentReference(entityReference);
XWikiContext xcontext = this.xcontextProvider.get();
XWikiDocument translatedDocument = getTranslatedDocument(documentReference);
if (translatedDocument == null) {
return false;
}
Locale locale = getLocale(documentReference);
solrDocument.setField(FieldUtils.FULLNAME, localSerializer.serialize(documentReference));
// Rendered title.
String plainTitle = translatedDocument.getRenderedTitle(Syntax.PLAIN_1_0, xcontext);
solrDocument.setField(FieldUtils.getFieldName(FieldUtils.TITLE, locale), plainTitle);
// Raw Content
solrDocument.setField(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RAW_CONTENT, locale), translatedDocument.getContent());
// Rendered content
WikiPrinter plainContentPrinter = new DefaultWikiPrinter();
this.renderer.render(translatedDocument.getXDOM(), plainContentPrinter);
solrDocument.setField(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RENDERED_CONTENT, locale), plainContentPrinter.toString());
solrDocument.setField(FieldUtils.VERSION, translatedDocument.getVersion());
solrDocument.setField(FieldUtils.COMMENT, translatedDocument.getComment());
solrDocument.setField(FieldUtils.DOCUMENT_LOCALE, translatedDocument.getLocale().toString());
// Add locale inheritance
addLocales(translatedDocument, translatedDocument.getLocale(), solrDocument);
// Get both serialized user reference string and pretty user name
setAuthors(solrDocument, translatedDocument, entityReference);
// Document dates.
solrDocument.setField(FieldUtils.CREATIONDATE, translatedDocument.getCreationDate());
solrDocument.setField(FieldUtils.DATE, translatedDocument.getContentUpdateDate());
// Document translations have their own hidden fields
solrDocument.setField(FieldUtils.HIDDEN, translatedDocument.isHidden());
// Add any extra fields (about objects, etc.) that can improve the findability of the document.
setExtras(documentReference, solrDocument, locale);
return true;
}
use of org.xwiki.rendering.renderer.printer.WikiPrinter in project xwiki-platform by xwiki.
the class DefaultWikiComponentMethodExecutor method castRenderedContent.
/**
* Render a XDOM and return a value converted from the rendered content. The type matches the return value of the
* passed method.
*
* @param xdom The XDOM to render
* @param method The method called
* @return A value matching the method return type
* @throws WikiComponentRuntimeException When the conversion fails
*/
private Object castRenderedContent(XDOM xdom, Method method) throws WikiComponentRuntimeException {
// Since no return value has been explicitly provided, we try to convert the result of the rendering
// into the expected return type using a Converter.
WikiPrinter printer = new DefaultWikiPrinter();
blockRenderer.render(xdom, printer);
String contentResult = printer.toString();
// Do the conversion!
try {
return converterManager.convert(method.getGenericReturnType(), contentResult);
} catch (ConversionException e) {
// Surrender!
throw new WikiComponentRuntimeException(String.format("Failed to convert result [%s] to type [%s] for method [%s.%s]", contentResult, method.getGenericReturnType(), method.getDeclaringClass().getName(), method.getName()), e);
}
}
use of org.xwiki.rendering.renderer.printer.WikiPrinter in project xwiki-platform by xwiki.
the class FeedPlugin method stripHtmlTags.
/**
* @param originalString the string to strip the HTML tags from
* @return the passed string, stripped from HTML markup
* @throws ConversionException when the conversion fails
*/
private String stripHtmlTags(String originalString) throws ConversionException {
if (this.syntaxConverter == null) {
this.syntaxConverter = Utils.getComponent(Converter.class);
}
WikiPrinter printer = new DefaultWikiPrinter();
this.syntaxConverter.convert(new StringReader(originalString), Syntax.HTML_4_01, Syntax.PLAIN_1_0, printer);
return printer.toString();
}
Aggregations