Search in sources :

Example 1 with Parser

use of org.xwiki.rendering.parser.Parser in project OsmAnd-tools by osmandapp.

the class WikiDatabasePreparation method mainTest.

public static void mainTest(String[] args) throws ConversionException, ComponentLookupException, ParseException, IOException {
    EmbeddableComponentManager cm = new EmbeddableComponentManager();
    cm.initialize(WikiDatabasePreparation.class.getClassLoader());
    Parser parser = cm.getInstance(Parser.class, Syntax.MEDIAWIKI_1_0.toIdString());
    FileReader fr = new FileReader(new File("/Users/victorshcherb/Documents/b.src.html"));
    BufferedReader br = new BufferedReader(fr);
    String content = "";
    String s;
    while ((s = br.readLine()) != null) {
        content += s;
    }
    content = removeMacroBlocks(content, new HashMap<>());
    XDOM xdom = parser.parse(new StringReader(content));
    // Find all links and make them italic
    for (Block block : xdom.getBlocks(new ClassBlockMatcher(LinkBlock.class), Block.Axes.DESCENDANT)) {
        Block parentBlock = block.getParent();
        Block newBlock = new FormatBlock(Collections.<Block>singletonList(block), Format.ITALIC);
        parentBlock.replaceChild(newBlock, block);
    }
    // for (Block block : xdom.getBlocks(new ClassBlockMatcher(ParagraphBlock.class), Block.Axes.DESCENDANT)) {
    // ParagraphBlock b = (ParagraphBlock) block;
    // block.getParent().removeBlock(block);
    // }
    WikiPrinter printer = new DefaultWikiPrinter();
    // BlockRenderer renderer = cm.getInstance(BlockRenderer.class, Syntax.XHTML_1_0.toIdString());
    // renderer.render(xdom, printer);
    // System.out.println(printer.toString());
    Converter converter = cm.getInstance(Converter.class);
    // Convert input in XWiki Syntax 2.1 into XHTML. The result is stored in the printer.
    printer = new DefaultWikiPrinter();
    converter.convert(new FileReader(new File("/Users/victorshcherb/Documents/a.src.html")), Syntax.MEDIAWIKI_1_0, Syntax.XHTML_1_0, printer);
    System.out.println(printer.toString());
    final HTMLConverter nconverter = new HTMLConverter(false);
    String lang = "be";
    WikiModel wikiModel = new WikiModel("http://" + lang + ".wikipedia.com/wiki/${image}", "http://" + lang + ".wikipedia.com/wiki/${title}");
// String plainStr = wikiModel.render(nconverter, content);
// System.out.println(plainStr);
// downloadPage("https://be.m.wikipedia.org/wiki/%D0%93%D0%BE%D1%80%D0%B0%D0%B4_%D0%9C%D1%96%D0%BD%D1%81%D0%BA",
// "/Users/victorshcherb/Documents/a.wiki.html");
}
Also used : HTMLConverter(info.bliki.wiki.filter.HTMLConverter) XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FormatBlock(org.xwiki.rendering.block.FormatBlock) WikiModel(info.bliki.wiki.model.WikiModel) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) SAXParser(javax.xml.parsers.SAXParser) Parser(org.xwiki.rendering.parser.Parser) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) LinkBlock(org.xwiki.rendering.block.LinkBlock) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) FormatBlock(org.xwiki.rendering.block.FormatBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) HTMLConverter(info.bliki.wiki.filter.HTMLConverter) Converter(org.xwiki.rendering.converter.Converter) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) FileReader(java.io.FileReader) File(java.io.File)

Example 2 with Parser

use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.

the class DefaultIOTargetService method getTransformedXDOM.

private XDOM getTransformedXDOM(String content, String sourceSyntaxId) throws ParseException, org.xwiki.component.manager.ComponentLookupException, TransformationException {
    Parser parser = componentManager.getInstance(Parser.class, sourceSyntaxId);
    XDOM xdom = parser.parse(new StringReader(content));
    // run transformations
    TransformationContext txContext = new TransformationContext(xdom, Syntax.valueOf(sourceSyntaxId));
    TransformationManager transformationManager = componentManager.getInstance(TransformationManager.class);
    transformationManager.performTransformations(xdom, txContext);
    return xdom;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Parser(org.xwiki.rendering.parser.Parser)

Example 3 with Parser

use of org.xwiki.rendering.parser.Parser 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 4 with Parser

use of org.xwiki.rendering.parser.Parser in project xwiki-platform by xwiki.

the class RenderingScriptServiceTest method parseAndRenderWhenErrorInRender.

@Test
public void parseAndRenderWhenErrorInRender() throws Exception {
    Parser parser = this.mocker.registerMockComponent(Parser.class, "plain/1.0");
    when(parser.parse(new StringReader("some [[TODO]] stuff"))).thenReturn(new XDOM(Collections.<Block>emptyList()));
    XDOM xdom = this.mocker.getComponentUnderTest().parse("some [[TODO]] stuff", "plain/1.0");
    Assert.assertNull(this.mocker.getComponentUnderTest().render(xdom, "unknown"));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) Parser(org.xwiki.rendering.parser.Parser) Test(org.junit.Test)

Example 5 with Parser

use of org.xwiki.rendering.parser.Parser 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)

Aggregations

Parser (org.xwiki.rendering.parser.Parser)24 XDOM (org.xwiki.rendering.block.XDOM)19 StringReader (java.io.StringReader)17 Test (org.junit.Test)13 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)8 Block (org.xwiki.rendering.block.Block)5 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)5 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)5 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 Syntax (org.xwiki.rendering.syntax.Syntax)4 TransformationManager (org.xwiki.rendering.transformation.TransformationManager)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)3 ConfigurationSource (org.xwiki.configuration.ConfigurationSource)3 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)3 SyntaxType (org.xwiki.rendering.syntax.SyntaxType)3 Expectations (org.jmock.Expectations)2 ModelContext (org.xwiki.model.ModelContext)2 EntityReference (org.xwiki.model.reference.EntityReference)2 EntityReferenceProvider (org.xwiki.model.reference.EntityReferenceProvider)2