java 如何将 HTML 代码转换为 Confluence 风格的 Wiki 标记?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10483999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:15:07  来源:igfitidea点击:

How do I convert HTML code to Confluence-style Wiki Markup?

javahtmlconfluencemylynwiki-markup

提问by Vinay Bedre

The API documentation for Mylyn Wikitext has functions to convert Wiki Markup to HTML, but I cannot find functions to convert / parse HTML code to Wiki Markup. Class MarkupParserhas method parseToHTML, but where can I find the reverse?

Mylyn Wikitext 的 API 文档具有将 Wiki 标记转换为 HTML 的功能,但我找不到将 HTML 代码转换/解析为 Wiki 标记的功能。类MarkupParser有方法parseToHTML,但我在哪里可以找到相反的方法?

回答by Graham Hannington

Try Wikifier.

试试维基百科

It doesn't do exactly what you want, but you might find it does enough, or is a useful starting point.

它并不完全符合您的要求,但您可能会发现它已经足够了,或者是一个有用的起点。

Wikifier converts snippets of the Confluence 4 XML storage format (that is, as presented by the Confluence Source Editor plugin, without a single document root element) into Confluence 3 wiki markup.

Wikiifier 将 Confluence 4 XML 存储格式的片段(即由 Confluence Source Editor 插件提供的,没有单个文档根元素)转换为 Confluence 3 wiki 标记。

Why is this at all relevant to your question? The Confluence 4 XML storage format includes some elements and attributes that have the same names as XHTML elements and attributes.

为什么这与您的问题完全相关?Confluence 4 XML 存储格式包括一些与 XHTML 元素和属性同名的元素和属性。

For more information, click the Help link on the Wikifier web page.

有关更多信息,请单击 Wikiifier 网页上的帮助链接。

Note: The XSLT stylesheet used by the Wikifier web page is slightly more recent than the XSLT stylesheet bundled with the related schema package.

注意: Wikiifier 网页使用的 XSLT 样式表比与相关模式包捆绑的 XSLT 样式表稍新。

This added later: Wikifier RTis even closer to what you want.

这是后来添加的:Wikiifier RT更接近您想要的。

回答by JoshDM

Here is how you do it in Mylyn using the WikiText Standalone. Substitute the appropriate DocumentBuilderfor your desired Wiki markup (you'll have to check the API to see what's available; TextileDocumentBuilderalso exists).

以下是您如何使用WikiText Standalone在 Mylyn 中执行此操作。将合适的替换DocumentBuilder为您想要的 Wiki 标记(您必须检查 API 以查看可用的内容;TextileDocumentBuilder也存在)。

File ConvertToConfluence.java:

文件ConvertToConfluence.java

package com.stackoverflow.mylyn;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

import org.eclipse.mylyn.internal.wikitext.confluence.core.ConfluenceDocumentBuilder;
import org.eclipse.mylyn.wikitext.core.parser.HtmlParser;
import org.xml.sax.InputSource;

public class ConvertToConfluence {

    public static String convertHTML(File htmlFile) {

        InputStream in = null;

        try {

            in = new FileInputStream(htmlFile);

        } catch (Exception ex) {

            // TODO: handle or re-throw file exception
        }

        InputSource inputSource = new InputSource(new InputStreamReader(in));
        StringWriter writer = new StringWriter();
        ConfluenceDocumentBuilder builder = new ConfluenceDocumentBuilder(writer);
        HtmlParser parser = new HtmlParser();

        try {

            parser.parse(inputSource, builder);

        } catch (Exception ex) {

            // TODO: handle or re-throw parsing exception
        }

        return writer.toString();       
    }   

    public static void main(String args[]) {

        File file = new File("c:\filename.html");
        System.out.println(convertHTML(file));
    }
}

File filename.html:

文件文件名.html

<HTML>
<BODY>
<p>This is <b>bold text</b> and some <i>italic text</i>.<br/><br/>TEST!</p>
</BODY>
</HTML>

Produces Confluence output:

产生 Confluence 输出:

This is *bold text* and some _italic text_.
\TEST!

回答by dokaspar

I was able to achieve HTML to Confluence-style WikiMarkup using the DefaultWysiwygConverterfrom Atlassian's own Java libraries. Here's a simplified unit test:

我能够使用DefaultWysiwygConverter来自 Atlassian 自己的 Java 库实现 HTML 到 Confluence 风格的 WikiMarkup 。这是一个简化的单元测试:

import com.atlassian.renderer.wysiwyg.converter.DefaultWysiwygConverter;

String htmlString = "This is <em>emphasized</em> and <b>bold</b>";
DefaultWysiwygConverter converter = new DefaultWysiwygConverter();
String wikiMarkupString = converter.convertXHtmlToWikiMarkup(htmlString);
Assert.assertEquals("This is _emphasized_ and *bold*", wikiMarkupString);

The POM must include the correct repositories and dependencies

POM 必须包含正确的存储库和依赖项

    <dependency>
        <groupId>com.atlassian.renderer</groupId>
        <artifactId>atlassian-renderer</artifactId>
        <version>8.0.5</version>
        <exclusions>
            <exclusion>
                <!-- This exclusion is necessary if you are in a situation which 
                     it conflicts, EG: using spring-boot -->
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <repositories>
        <repository>
            <!-- https://developer.atlassian.com/docs/advanced-topics/working-with-maven/atlassian-maven-repositories -->
            <id>atlassian-public</id>
            <url>https://packages.atlassian.com/maven/repository/public</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>never</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
            </snapshots>
            <releases>
                <enabled>true</enabled>
                <checksumPolicy>warn</checksumPolicy>
            </releases>
        </repository>
    </repositories>

回答by Christian Koch

As far as I know there is no way to convert HTML to Confluence wiki markup. And since Atlassian stops using textile as wiki markup in Confluence 4.x there is no need for a conversion. The page format ist XHTML.

据我所知,没有办法将 HTML 转换为 Confluence wiki 标记。由于 Atlassian 在 Confluence 4.x 中停止使用纺织品作为 wiki 标记,因此无需进行转换。页面格式是 XHTML。