如何将Confluence存储格式XML转换为HTML?

问题描述 投票:2回答:3

我使用REST API从汇合中提取内容。

https://docs.atlassian.com/atlassian-confluence/REST/3.2/

API返回带有content属性的confluence页面。内容是XHTML和专有XML标签的混合。 XML是Confluence存储格式:

https://confluence.atlassian.com/display/DOC/Confluence+Storage+Format

自定义XML标记用于图像,相对链接和附件等内容。如果我直接呈现内容,则自定义XML将不会呈现。

我发现看起来像应该转换格式的端点:https://docs.atlassian.com/confluence/latest/com/atlassian/confluence/xhtml/api/XhtmlContent.html

我认为不再支持它。

我也找到了这个项目:

http://www.amnet.net.au/~ghannington/confluence/readme.html#wikifier

它将汇合XML转换为汇总wiki标记。该项目附带两个.xsl表单一张是confluence2wiki.xsl处理标记转换,另一张是confluence2xhtml.xsl,听起来它会起作用但不幸的是实施很差。它实际上将汇合XML转换为看起来像XML的XHTML。因此,不幸的是,来自confluence XML的图像标记变为:

<div class="extension-element">
  <p class="extension-element-markup">
    <span class="element-name">ac:image</span>
    <span class="attribute-name">ac:alt</span>
    <span class="markup">="</span>
    <span class="attribute-value">Example1.png</span>
    <span class="markup">"</span>
  </p>
  <div class="extension-element-contents">
    <div class="extension-element">
      <p class="extension-element-markup">
        <span class="element-name">ri:url</span>
        <span class="attribute-name">ri:value</span>
        <span class="markup">="</span>
        <span class="attribute-value">https://example.com/attachments/token/2ujwb0dm4jsorgk/?name=Omniata_Docs_Projects_Example1.png</span>
        <span class="markup">"</span>
      </p>
    </div>
  </div>
</div>

哪个不是很有帮助。目前看起来我将不得不根据xsl编写我自己的wkik xsl sheet表。我希望有一个较少的手动解决方案,或者之前有人这样做过。

xml xslt xml-serialization confluence
3个回答
1
投票

通过Remote API调用renderContent方法。如果您的内容是旧格式,还有一个convertWikiToStorageFormat方法。


0
投票

如果您需要使用REST API执行此操作,目前最好的方法是编写插件并实现自己的REST API,为您进行转换。

这样的事情应该通过将存储格式转换为HTML来启动你:

public String convertStorageToView(int pageId)
{
    Page page = pageManager.getById(pageId);        
    String storage = page.getBodyAsString();

    try
    {
        final ConversionContext conversionContext = new DefaultConversionContext(page.toPageContext());

        return xhtmlContent.convertStorageToView(storage, conversionContext);
    }
    catch (XhtmlException e)
    {
        e.printStackTrace();
    }
    catch (XMLStreamException e)
    {
        e.printStackTrace();
    }

    return null;
}

你还必须写REST module


0
投票

类似于它已经在另一个答案中显示,但对我来说,不清楚你必须使用xhtmlContent。您可以通过构造函数轻松获取该实例。

public class MyServlet extends HttpServlet {

    private Logger LOGGER = Logger.getLogger(getClass());

    private final PageManager pageManager;
    private final XhtmlContent xhtmlContent;


    public static final String PARAM_PAGE_ID = "pageId";


    public MyServlet( PageManager pageManager, XhtmlContent xhtmlContent) {
    this.pageManager = pageManager;
        this.xhtmlContent = xhtmlContent;
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        //fetch page or blogpost
        ContentEntityObject page = pageManager.getPage(Long.parseLong(request.getParameter(PARAM_PAGE_ID)));
        if (page == null) {
            page = pageManager.getBlogPost(Long.parseLong(request.getParameter(PARAM_PAGE_ID)));
        }
        if (page != null) {
            String htmlContent = "";
            final ConversionContext conversionContext = new DefaultConversionContext(page.toPageContext());
            try {
                htmlContent = xhtmlContent.convertStorageToView(page.getBodyAsString(), conversionContext);
            } catch (XMLStreamException e) {
                htmlContent = "ERROR ON EXPORT";
                LOGGER.error(e);
            } catch (XhtmlException e) {
                htmlContent = "ERROR ON EXPORT";
                LOGGER.error(e);
            }

        } else {
        //do some errorhandling here
    }
    //.. do something with the content .. render it in a velocity file for example
    }

例如,您可以在速度文件中呈现数据,或者直接在响应中写入数据。

© www.soinside.com 2019 - 2024. All rights reserved.