Search in sources :

Example 1 with FormatBlock

use of org.xwiki.rendering.block.FormatBlock 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 FormatBlock

use of org.xwiki.rendering.block.FormatBlock in project xwiki-platform by xwiki.

the class CodeMacro method parseContent.

@Override
protected List<Block> parseContent(CodeMacroParameters parameters, String content, MacroTransformationContext context) throws MacroExecutionException {
    List<Block> result;
    try {
        if (LANGUAGE_NONE.equalsIgnoreCase(parameters.getLanguage())) {
            if (StringUtils.isEmpty(content)) {
                result = Collections.emptyList();
            } else {
                result = this.plainTextParser.parse(new StringReader(content)).getChildren().get(0).getChildren();
            }
        } else {
            result = highlight(parameters, content);
        }
    } catch (Exception e) {
        throw new MacroExecutionException("Failed to highlight content", e);
    }
    Map<String, String> formatParameters = new LinkedHashMap<String, String>();
    formatParameters.put("class", "code");
    if (context.isInline()) {
        result = Arrays.<Block>asList(new FormatBlock(result, Format.NONE, formatParameters));
    } else {
        result = Arrays.<Block>asList(new GroupBlock(result, formatParameters));
    }
    return result;
}
Also used : StringReader(java.io.StringReader) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) GroupBlock(org.xwiki.rendering.block.GroupBlock) FormatBlock(org.xwiki.rendering.block.FormatBlock) Block(org.xwiki.rendering.block.Block) GroupBlock(org.xwiki.rendering.block.GroupBlock) FormatBlock(org.xwiki.rendering.block.FormatBlock) ParseException(org.xwiki.rendering.parser.ParseException) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with FormatBlock

use of org.xwiki.rendering.block.FormatBlock in project xwiki-platform by xwiki.

the class BlocksGeneratorPygmentsListener method format.

@Override
public void format(String tokenType, String value, Map<String, Object> style) {
    List<Block> blockList;
    if (StringUtils.isNotEmpty(value)) {
        try {
            blockList = this.plainTextParser.parse(new StringReader(value)).getChildren().get(0).getChildren();
        } catch (ParseException e) {
            // String.
            throw new RuntimeException("Failed to parse [" + value + "] as plain text.", e);
        }
        String styleParameter = formatStyle(style);
        FormatBlock formatBlock = null;
        if (styleParameter.length() > 0) {
            formatBlock = new FormatBlock(blockList, Format.NONE);
            formatBlock.setParameter("style", styleParameter);
            this.blocks.add(formatBlock);
        } else {
            this.blocks.addAll(blockList);
        }
    }
}
Also used : StringReader(java.io.StringReader) Block(org.xwiki.rendering.block.Block) FormatBlock(org.xwiki.rendering.block.FormatBlock) ParseException(org.xwiki.rendering.parser.ParseException) FormatBlock(org.xwiki.rendering.block.FormatBlock)

Aggregations

StringReader (java.io.StringReader)3 Block (org.xwiki.rendering.block.Block)3 FormatBlock (org.xwiki.rendering.block.FormatBlock)3 LinkedHashMap (java.util.LinkedHashMap)2 ParseException (org.xwiki.rendering.parser.ParseException)2 HTMLConverter (info.bliki.wiki.filter.HTMLConverter)1 WikiModel (info.bliki.wiki.model.WikiModel)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 HashMap (java.util.HashMap)1 SAXParser (javax.xml.parsers.SAXParser)1 EmbeddableComponentManager (org.xwiki.component.embed.EmbeddableComponentManager)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 GroupBlock (org.xwiki.rendering.block.GroupBlock)1 LinkBlock (org.xwiki.rendering.block.LinkBlock)1 XDOM (org.xwiki.rendering.block.XDOM)1 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)1 Converter (org.xwiki.rendering.converter.Converter)1 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)1