Search in sources :

Example 1 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter 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 DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter 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 3 with DefaultWikiPrinter

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

the class XDOMOfficeDocument method getContentAsString.

/**
 * Renders the XDOM encapsulated by this document into the given syntax.
 *
 * @param syntaxId string identifier of the syntax.
 * @return content of this document in the given syntax or null if the syntax is invalid.
 */
public String getContentAsString(String syntaxId) {
    try {
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer renderer = this.componentManager.getInstance(BlockRenderer.class, syntaxId);
        renderer.render(this.xdom, printer);
        return printer.toString();
    } catch (ComponentLookupException ex) {
    // Nothing to do here.
    }
    return null;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 4 with DefaultWikiPrinter

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

the class HeadingNameNamingCriterion method getDocumentName.

@Override
public String getDocumentName(XDOM newDoc) {
    String documentName = null;
    String prefix = spaceName + ".";
    if (newDoc.getChildren().size() > 0) {
        Block firstChild = newDoc.getChildren().get(0);
        if (firstChild instanceof HeaderBlock) {
            // Clone the header block and remove any unwanted stuff
            Block clonedHeaderBlock = firstChild.clone(new BlockFilter() {

                public List<Block> filter(Block block) {
                    List<Block> blocks = new ArrayList<Block>();
                    if (block instanceof WordBlock || block instanceof SpaceBlock || block instanceof SpecialSymbolBlock) {
                        blocks.add(block);
                    }
                    return blocks;
                }
            });
            XDOM xdom = new XDOM(clonedHeaderBlock.getChildren());
            WikiPrinter printer = new DefaultWikiPrinter();
            this.plainSyntaxRenderer.render(xdom, printer);
            documentName = cleanPageName(printer.toString());
        }
    }
    // Fall back if necessary.
    if (null == documentName || documentName.equals("")) {
        documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc);
    } else if (prependBasePageName) {
        documentName = prefix + basePageName + INDEX_SEPERATOR + documentName;
    } else {
        documentName = prefix + documentName;
    }
    // Truncate long document names.
    int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255;
    if (documentName.length() > maxWidth) {
        documentName = documentName.substring(0, maxWidth);
    }
    // Resolve any name clashes.
    String newDocumentName = documentName;
    int localIndex = 0;
    while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) {
        // Append a trailing local index if the page already exists
        newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex);
    }
    // Add the newly generated document name into the pool of generated document names.
    documentNames.add(newDocumentName);
    return newDocumentName;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) ArrayList(java.util.ArrayList) List(java.util.List) BlockFilter(org.xwiki.rendering.block.BlockFilter)

Example 5 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter 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)

Aggregations

DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)41 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)28 StringReader (java.io.StringReader)18 Test (org.junit.Test)12 Converter (org.xwiki.rendering.converter.Converter)12 XDOM (org.xwiki.rendering.block.XDOM)10 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Block (org.xwiki.rendering.block.Block)6 ParseException (org.xwiki.rendering.parser.ParseException)5 Parser (org.xwiki.rendering.parser.Parser)5 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 XWikiException (com.xpn.xwiki.XWikiException)3 HashMap (java.util.HashMap)3 Expectations (org.jmock.Expectations)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 Reader (java.io.Reader)2