Search in sources :

Example 1 with Converter

use of org.xwiki.rendering.converter.Converter 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 Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenWikiMacroBinding.

@Test
public void testExecuteWhenWikiMacroBinding() throws Exception {
    registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}");
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 3 with Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenWikiRequiringPRAfterDropPermission.

@Test
public void testExecuteWhenWikiRequiringPRAfterDropPermission() throws Exception {
    registerWikiMacro("wikimacrobindings", "{{groovy}}" + "print xcontext.macro.doc" + "{{/groovy}}");
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    getContext().dropPermissions();
    this.wikiMacroDocument.newDocument(getContext()).dropPermissions();
    converter.convert(new StringReader("{{wikimacrobindings param1=\"value2\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>" + this.wikiMacroDocument.toString() + "</p>", printer.toString());
    Assert.assertTrue("Wiki macro did not properly restord persmission dropping", getContext().hasDroppedPermissions());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 4 with Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenInnerMacro.

/**
 * Check that macro used inside wiki macro are executed as part of the document.
 */
@Test
public void testExecuteWhenInnerMacro() throws Exception {
    registerWikiMacro("wikimacro1", "{{toc/}}", Syntax.XWIKI_2_0);
    getMockery().checking(new Expectations() {

        {
            DocumentResourceReference reference = new DocumentResourceReference(null);
            reference.setAnchor("Hheading");
            allowing(mockWikiModel).getDocumentViewURL(reference);
            will(returnValue("url"));
        }
    });
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("= heading\n\n{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<h1 id=\"Hheading\" class=\"wikigeneratedid\"><span>heading</span></h1>" + "<ul><li><span class=\"wikilink\"><a href=\"#Hheading\">heading</a></span></li></ul>", printer.toString());
}
Also used : Expectations(org.jmock.Expectations) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) Test(org.junit.Test)

Example 5 with Converter

use of org.xwiki.rendering.converter.Converter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenInlineAndWithMacro.

/**
 * When a wiki macro is used in inline mode and its code starts with a macro, that nested macro is made inline. In
 * other words, the nested macro should not generate extra paragraph elements.
 */
@Test
public void testExecuteWhenInlineAndWithMacro() throws Exception {
    registerWikiMacro("wikimacro1", "This is **bold**", Syntax.XWIKI_2_0);
    registerWikiMacro("wikimacro2", "{{wikimacro1 param1=\"v1\" param2=\"v2\"/}}", Syntax.XWIKI_2_0);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    // Note: We're putting the macro after the "Hello" text to force it as an inline macro.
    converter.convert(new StringReader("Hello {{wikimacro2 param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>Hello This is <strong>bold</strong></p>", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Aggregations

StringReader (java.io.StringReader)11 Converter (org.xwiki.rendering.converter.Converter)11 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)11 Test (org.junit.Test)10 HashMap (java.util.HashMap)3 Expectations (org.jmock.Expectations)3 Reader (java.io.Reader)2 Writer (java.io.Writer)2 Map (java.util.Map)2 Properties (java.util.Properties)2 VelocityContext (org.apache.velocity.VelocityContext)2 Description (org.hamcrest.Description)2 Action (org.jmock.api.Action)2 Invocation (org.jmock.api.Invocation)2 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)2 Execution (org.xwiki.context.Execution)2 VelocityEngine (org.xwiki.velocity.VelocityEngine)2 VelocityManager (org.xwiki.velocity.VelocityManager)2 HTMLConverter (info.bliki.wiki.filter.HTMLConverter)1 WikiModel (info.bliki.wiki.model.WikiModel)1