Search in sources :

Example 1 with PrintRenderer

use of org.xwiki.rendering.renderer.PrintRenderer in project xwiki-platform by xwiki.

the class AbstractAnnotationMaintainer method renderPlainText.

/**
 * Helper method to render the plain text version of the passed content.
 *
 * @param content the content to render in plain text
 * @param syntaxId the source syntax of the content to render
 * @throws Exception if anything goes wrong while rendering the content
 * @return the normalized plain text rendered content
 */
private String renderPlainText(String content, String syntaxId) throws Exception {
    PrintRenderer renderer = componentManager.getInstance(PrintRenderer.class, "normalizer-plain/1.0");
    // parse
    Parser parser = componentManager.getInstance(Parser.class, syntaxId);
    XDOM xdom = parser.parse(new StringReader(content));
    // run transformations -> although it's going to be at least strange to handle rendered content since there
    // is no context
    Syntax sourceSyntax = Syntax.valueOf(syntaxId);
    TransformationManager transformationManager = componentManager.getInstance(TransformationManager.class);
    transformationManager.performTransformations(xdom, sourceSyntax);
    // render
    WikiPrinter printer = new DefaultWikiPrinter();
    renderer.setPrinter(printer);
    xdom.traverse(renderer);
    return printer.toString();
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) PrintRenderer(org.xwiki.rendering.renderer.PrintRenderer) StringReader(java.io.StringReader) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) Syntax(org.xwiki.rendering.syntax.Syntax) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Parser(org.xwiki.rendering.parser.Parser)

Example 2 with PrintRenderer

use of org.xwiki.rendering.renderer.PrintRenderer in project xwiki-platform by xwiki.

the class DefaultHTMLConverterTest method fromHTML.

/**
 * Unit test for {@link DefaultHTMLConverter#fromHTML(String, String)}.
 */
@Test
public void fromHTML() throws Exception {
    String html = "some HTML";
    String syntaxId = "syntax/x.y";
    // Verify the HTML is cleaned.
    HTMLCleaner cleaner = mocker.getInstance(HTMLCleaner.class);
    when(cleaner.clean(html)).thenReturn(html);
    PrintRendererFactory printRendererFactory = this.mocker.registerMockComponent(PrintRendererFactory.class, syntaxId);
    PrintRenderer printRenderer = mock(PrintRenderer.class);
    when(printRendererFactory.createRenderer(any(WikiPrinter.class))).thenReturn(printRenderer);
    Assert.assertEquals("", mocker.getComponentUnderTest().fromHTML(html, syntaxId));
    // Verify the HTML is converted to the specified syntax.
    StreamParser xhtmlStreamParser = mocker.getInstance(StreamParser.class, "xhtml/1.0");
    verify(xhtmlStreamParser).parse(any(StringReader.class), same(printRenderer));
}
Also used : PrintRenderer(org.xwiki.rendering.renderer.PrintRenderer) StreamParser(org.xwiki.rendering.parser.StreamParser) StringReader(java.io.StringReader) PrintRendererFactory(org.xwiki.rendering.renderer.PrintRendererFactory) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) HTMLCleaner(org.xwiki.wysiwyg.cleaner.HTMLCleaner) Test(org.junit.Test)

Aggregations

StringReader (java.io.StringReader)2 PrintRenderer (org.xwiki.rendering.renderer.PrintRenderer)2 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)2 Test (org.junit.Test)1 XDOM (org.xwiki.rendering.block.XDOM)1 Parser (org.xwiki.rendering.parser.Parser)1 StreamParser (org.xwiki.rendering.parser.StreamParser)1 PrintRendererFactory (org.xwiki.rendering.renderer.PrintRendererFactory)1 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)1 Syntax (org.xwiki.rendering.syntax.Syntax)1 TransformationManager (org.xwiki.rendering.transformation.TransformationManager)1 HTMLCleaner (org.xwiki.wysiwyg.cleaner.HTMLCleaner)1