对于一个具有易于使用且可配置的 api 的漂亮解析器有什么想法吗?我希望向其提供数据,例如 http://wikitravel.org/wiki/en/api.php?format=xml&action=parse&prop=wikitext&page=San%20Francisco,选择我想要的数据部分,然后输出自定义html 为每个独特类型的元素? Java 是首选,但如果有一个 php/js 解决方案与大多数(99%+)wikitext 兼容,那也可以。
您可以尝试他们网站上的文档,但我无法弄清楚。只需查看示例源代码即可。在
https://repo1.maven.org/maven2/org/sweble/wikitext/swc-example-basic/2.0.0/swc-example-basic-2.0.0- 下载 swc-example-basic 的源 jar resources.jar 并查看 App.java 和 TextConverter.java。
基本上,要解析页面并将其转换为另一种形式,首先将以下依赖项添加到您的项目中:
<dependency>
<groupId>org.sweble.wikitext</groupId>
<artifactId>swc-engine</artifactId>
<version>2.0.0</version>
</dependency>
然后,执行以下操作:
public String convertWikiText(String title, String wikiText, int maxLineLength) throws LinkTargetException, EngineException {
// Set-up a simple wiki configuration
WikiConfig config = DefaultConfigEnWp.generate();
// Instantiate a compiler for wiki pages
WtEngineImpl engine = new WtEngineImpl(config);
// Retrieve a page
PageTitle pageTitle = PageTitle.make(config, title);
PageId pageId = new PageId(pageTitle, -1);
// Compile the retrieved page
EngProcessedPage cp = engine.postprocess(pageId, wikiText, null);
TextConverter p = new TextConverter(config, maxLineLength);
return (String)p.go(cp.getPage());
}
TextConverter 是一个您可以在我上面提到的示例中找到的类。自定义它来做任何你想做的事情。例如,以下内容确保所有粗体文本都被“**”包围:
public void visit(WtBold b)
{
write("**");
iterate(b);
write("**");
}
对于您将遇到的每种类型的元素,该类都有许多访问方法。
https://bitbucket.org/axelclk/info.bliki.wiki/wiki/Mediawiki2HTML
Bliki 是 XWiki 使用的,使用非常简单:
String htmlText = WikiModel.toHtml("This is a simple [[Hello World]] wiki tag");
以下是下载列表:
https://oss.sonatype.org/content/repositories/snapshots/info/bliki/wiki/bliki-core/
但是与 Maven 一起使用要容易得多。
http://rendering.xwiki.org)。以下是如何解析某些 mediawiki 内容的示例:
// Initialize Rendering components and allow getting instances
EmbeddableComponentManager componentManager = new EmbeddableComponentManager();
componentManager.initialize(this.getClass().getClassLoader());
// Get the MediaWiki Parser
Parser parser = componentManager.getInstance(Parser.class, "mediawiki/1.0");
// Parse the content in mediawiki markup and generate an AST (it's also possible to use a streaming parser for large content)
XDOM xdom = parser.parse(new StringReader("... input here"));
// Perform any transformation you wish to the XDOM here
...
// Generate XHTML out of the modified XDOM
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = componentManager.getInstance(BlockRenderer.class, "xhtml/1.0");
renderer.render(xdom, printer);
// The result is now in the printer object
printer.toString();
查看更多示例http://rendering.xwiki.org/xwiki/bin/view/Main/GettingStarted
希望有帮助。