Search in sources :

Example 6 with WikiPrinter

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"));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StringReaderMatcher(org.xwiki.test.mockito.StringReaderMatcher) Block(org.xwiki.rendering.block.Block) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Parser(org.xwiki.rendering.parser.Parser) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 7 with WikiPrinter

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();
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 8 with WikiPrinter

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;
}
Also used : Locale(java.util.Locale) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) XWikiContext(com.xpn.xwiki.XWikiContext) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)

Example 9 with WikiPrinter

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);
    }
}
Also used : ConversionException(org.xwiki.properties.converter.ConversionException) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiComponentRuntimeException(org.xwiki.component.wiki.WikiComponentRuntimeException)

Example 10 with WikiPrinter

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();
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)

Aggregations

WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)33 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)28 XDOM (org.xwiki.rendering.block.XDOM)12 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)11 StringReader (java.io.StringReader)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 Parser (org.xwiki.rendering.parser.Parser)6 Test (org.junit.Test)5 Block (org.xwiki.rendering.block.Block)5 ParseException (org.xwiki.rendering.parser.ParseException)5 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)3 XWikiException (com.xpn.xwiki.XWikiException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 WordBlock (org.xwiki.rendering.block.WordBlock)2 Converter (org.xwiki.rendering.converter.Converter)2 TransformationManager (org.xwiki.rendering.transformation.TransformationManager)2