是否有使用Mylyn WikiText的Java示例?

时间:2013-05-07 17:05:34

标签: java html mylyn-wikitext

我想使用Java来编码从Mylyn Wiki支持的格式到HTML的转换。我没有找到任何有用的在线示例演示如何使用Java来编写Mylyn WikiText的代码。我只找到了this page on Eclipse,但它使用了Ant。我请求一些示例代码。

1 个答案:

答案 0 :(得分:7)

虽然你总是code Ant Scripts and launch them from Java,但下面是我刚刚编写的一个完整的实用工具类,它允许你通过注册到WikiText独立部署的各种核心语言转换wiki文本。

值得注意的是,最简单的事情是download the Standalone Deployment,展开它并阅读API以了解您需要应用的其他配置。

将WikiText转换为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()方法。以InputSource的身份提交您的HTML并提供DocumentBuilder目标。 DocumentBuilder实现包括XslfoDocumentBuilder(对于XSL-Fo和最终转换为PDF或PostScript),DocBookDocumentBuilder(对于Docbook格式),以及扩展AbstractMarkupDocumentBuilder的类(有一个用于几乎所有标记:TextileDocumentBuilderConfluenceDocumentBuilder等。)

其他WikiText链接: