Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 MarkupParser has method parseToHTML, but where can I find the reverse?

like image 252
Vinay Bedre Avatar asked May 07 '12 14:05

Vinay Bedre


People also ask

How do I convert HTML to wiki markup?

Click on the "Convert HTML to wiki markup" button (if it doesn't work, use the "Fetch from URL" option or another HTML converter). Copy/paste the resulting text into the edit box of the wiki page you have created. Preview the page and check that everything looks correct.

What markup language does Confluence use?

Confluence stores the content of pages and blog posts in an XHTML-based format. Advanced users can view the storage format of a page and even edit it, provided their Confluence site is configured to allow that. Wiki markup. Confluence allows data entry via a shorthand code called wiki markup.


2 Answers

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.

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.

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

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

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

like image 149
Graham Hannington Avatar answered Nov 04 '22 07:11

Graham Hannington


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

File 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>
<BODY>
<p>This is <b>bold text</b> and some <i>italic text</i>.<br/><br/>TEST!</p>
</BODY>
</HTML>

Produces Confluence output:

This is *bold text* and some _italic text_.
\\TEST!
like image 20
JoshDM Avatar answered Nov 04 '22 07:11

JoshDM