我想使用Java编写从Mylyn wiki支持的格式到HTML的转换代码。我没有发现任何有用的在线示例来演示如何使用Java为Mylyn WikiText编写代码。我只在Eclipse上找到了这个页面,但它使用了Ant。我需要一些示例代码
虽然您总是可以编写Ant脚本并从Java启动它们,但下面是我刚刚编写的一个完整的实用程序类,它将允许您通过注册到WikiText独立部署的各种核心语言转换wiki文本。
值得注意的是,最简单的方法就是下载Standalone Deployment,展开它,然后通读API,看看还需要应用哪些配置。
将wikittext转换为HTML
package com.stackoverflow.mylyn;
import java.io.StringWriter;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
import org.eclipse.mylyn.wikitext.core.parser.builder.HtmlDocumentBuilder;
import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage;
import org.eclipse.mylyn.wikitext.core.util.ServiceLocator;
/**
* Utility to parse Wiki Text of varying languages and convert to HTML.
*/
public final class ParseWikiToHTMLUtility {
public static final String NAME_TEXTILE = "Textile";
public static final String NAME_TRACWIKI = "TracWiki";
public static final String NAME_MEDIAWIKI = "MediaWiki";
public static final String NAME_CONFLUENCE = "Confluence";
public static final String NAME_TWIKI = "TWiki";
private ParseWikiToHTMLUtility() {
/* Do not instantiate utility classes */
}
public static String parseTextile(String wikiText) {
return parseByLanguage(NAME_TEXTILE, wikiText);
}
public static String parseTracWiki(String wikiText) {
return parseByLanguage(NAME_TRACWIKI, wikiText);
}
public static String parseMediaWiki(String wikiText) {
return parseByLanguage(NAME_MEDIAWIKI, wikiText);
}
public static String parseConfluence(String wikiText) {
return parseByLanguage(NAME_CONFLUENCE, wikiText);
}
public static String parseTWiki(String wikiText) {
return parseByLanguage(NAME_TWIKI, wikiText);
}
public static String parseByLanguage(String name, String wikiText) {
return parseByLanguage(ServiceLocator.getInstance().getMarkupLanguage(name), wikiText);
}
public static String parseByLanguage(MarkupLanguage language, String wikiText) {
StringWriter writer = new StringWriter();
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer);
MarkupParser parser = new MarkupParser(language, builder);
parser.parse(wikiText);
return writer.toString();
}
/**
* MarkupLanguage API prefers we retrieve the MarkupLanguge by name from
* the ServiceLocator; since there are no name constants, we obtain the names
* from this method or alternately use the hard-coded names from this utility class,
* which were pulled directly from a prior call to this very method.
*/
public static Set<String> getLanguageNames() {
Set<String> languages = new TreeSet<String>();
for (MarkupLanguage s : ServiceLocator.getInstance().getAllMarkupLanguages()) {
languages.add(s.getName());
}
return languages;
}
}
从HTML转换,使用HtmlParser.parse()
方法。将HTML作为InputSource
提交,并提供DocumentBuilder
目标。DocumentBuilder
实现包括XslfoDocumentBuilder
(用于XSL-Fo和最终转换为PDF或PostScript), DocBookDocumentBuilder
(用于Docbook格式)和扩展AbstractMarkupDocumentBuilder
的类(几乎每种标记都有一个:TextileDocumentBuilder
, ConfluenceDocumentBuilder
等)。
其他WikiText链接:
如何在Java中使用Mylyn WikiText(有一些示例代码)
关于使用WikiText解析的问题(有一些示例代码)
WikiText Eclipse集成开发指南
WikiText入门教程(1 of 2)
高级wikittext (2 of 2)
WikiText胜出的转换器比较