Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Mylyn » API for generation of wiki markup
API for generation of wiki markup [message #920942] Sun, 23 September 2012 16:43 Go to next message
saurav sarkar is currently offline saurav sarkarFriend
Messages: 428
Registered: July 2009
Senior Member
Hi,

Have been reading about Mylyn wiki text for last few hours.

Would like to know if there are any APIs or ANT tasks available for generation of wiki markup for e.g. for Confluence wiki

cheers,
Saurav


[Updated on: Sun, 23 September 2012 17:26]

Report message to a moderator

Re: API for generation of wiki markup [message #920974 is a reply to message #920942] Sun, 23 September 2012 17:33 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 124
Registered: November 2010
Senior Member
Do you want to parse or to generate Confluence Wiki?

From wiki.eclipse.org/Mylyn/WikiText:

Quote:
WikiText has parsers for MediaWiki, Textile, Confluence, TracWiki and TWiki markup, and can be extended to support other languages. WikiText provides Ant tasks for converting lightweight markup to HTML, Eclipse Help, DocBook, DITA and XSL-FO.


Do not hesitate to ask here for help.
Re: API for generation of wiki markup [message #921314 is a reply to message #920974] Mon, 24 September 2012 02:32 Go to previous messageGo to next message
saurav sarkar is currently offline saurav sarkarFriend
Messages: 428
Registered: July 2009
Senior Member
Hi Jeremie,

Thanks for the reply.
Yes, I would like to generate Confluence wiki.

cheers,
Saurav


Re: API for generation of wiki markup [message #921507 is a reply to message #921314] Mon, 24 September 2012 07:12 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 124
Registered: November 2010
Senior Member
I think you can use the ConfluenceDocumentBuilder.

If your input is HTML, you can probably use the html-to-wikitext ant task (Java code HtmlToMarkupTask)
WikiText User Guide > Markup Conversion

If your have other needs (like an other input as HTML), you can use the public API of WikiText to write some code that suit exactly your needs.

[Updated on: Mon, 24 September 2012 07:15]

Report message to a moderator

Re: API for generation of wiki markup [message #921678 is a reply to message #920942] Mon, 24 September 2012 10:33 Go to previous messageGo to next message
saurav sarkar is currently offline saurav sarkarFriend
Messages: 428
Registered: July 2009
Senior Member
Thanks Jeremie for the reply.

I imported the org.eclipse.mylyn.wikitext.confluence.core and org.eclipse.mylyn.wikitext.confluence.ui plugin also the other wikitext plug-ins .Could not find the ConfluenceDocumentBuilder.

Is there anywhere else i have to look into ?

cheers,
Saurav


Re: API for generation of wiki markup [message #922027 is a reply to message #921678] Mon, 24 September 2012 17:06 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 124
Registered: November 2010
Senior Member
Simple JavaSE example:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;

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

public class Main {
	public static void main(String[] args) {
		StringWriter out = new StringWriter();
		ConfluenceDocumentBuilder builder = new ConfluenceDocumentBuilder(out);

		HtmlParser parser = new HtmlParser();
		String content = "<html><body><h1>Hello world</h1>"
				+ "<p>this <i>is</i> a <b>good</b> test</p>"
				+ "<ul><li>lorem</li><li>ipsum</li></ul></body></html>";
		InputSource input = new InputSource(new ByteArrayInputStream(
				content.getBytes(Charset.forName("UTF-8"))));
		try {
			parser.parse(input, builder);
			String markup = out.toString();
			System.out.println(markup);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
	}
}


On the classpath you need to have:
* org.eclipse.mylyn.wikitext.confluence.core_1.8.0.I20120907-2057.jar
* org.eclipse.mylyn.wikitext.core_1.8.0.I20120907-2057.jar

You also can have JSoup on the classpath. This will allow you to have not strict HTML as input.

Very important:
Notice that ConfluenceDocumentBuilder is an API internal class. It means that if you use it, you accept some changes (functions, features...) from a release to an other one.

Output:
h1. Hello world

this _is_ a *good* test

* lorem
* ipsum



Re: API for generation of wiki markup [message #922092 is a reply to message #922027] Mon, 24 September 2012 18:29 Go to previous messageGo to next message
saurav sarkar is currently offline saurav sarkarFriend
Messages: 428
Registered: July 2009
Senior Member
Hi Jeremie,

Had the older version.
Got the newer version and its working fine Smile.

Is it also possible to write the content back to confluence hosting server by giving the path ?.

cheers,
Saurav


Re: API for generation of wiki markup [message #922178 is a reply to message #922092] Mon, 24 September 2012 20:18 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 124
Registered: November 2010
Senior Member
saurav sarkar wrote on Mon, 24 September 2012 20:29

Is it also possible to write the content back to confluence hosting server by giving the path ?.


I am the wrong person for this question. I do not know.
I only use/know a little bit the WikiText library of Mylyn Doc.

Maybe someone else has an idea how you could do this.
Re: API for generation of wiki markup [message #922737 is a reply to message #920942] Tue, 25 September 2012 09:27 Go to previous messageGo to next message
saurav sarkar is currently offline saurav sarkarFriend
Messages: 428
Registered: July 2009
Senior Member
Thanks Jeremie.

Your help is very useful and much appreciated.

cheers,
Saurav


Re: API for generation of wiki markup [message #945388 is a reply to message #921507] Mon, 15 October 2012 11:28 Go to previous messageGo to next message
Subhankar Chattopadhyay is currently offline Subhankar ChattopadhyayFriend
Messages: 5
Registered: October 2012
Junior Member
Hi Jeremie,
Can I generate a wiki content out of an xml which I have ? That xml is very specific to my use case.
Re: API for generation of wiki markup [message #945763 is a reply to message #945388] Mon, 15 October 2012 16:11 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 124
Registered: November 2010
Senior Member
I am not sure to understand:

You have a propritary xml that contains formatted text. Something like:
<article><paragraphe>Lorem ipsum dolor <italique>sit amet</italique>, consectetur adipiscing elit.</paragraphe>
<paragraphe> Etiam laoreet est vel nulla sagittis in ornare tellus commodo.
<liste>
<element>Nullam</element>
<element>Metus</element>
<element>Magna</element>
</liste>
</paragraphe></article>

In this example, the XML tags are here in French.

If you want to convert this to one of the supported WikiText output language (something that extends org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder), you will need to write a parser. Depending on how your XML looks like, it might be more or less complex. I imagine it is possible.
Re: API for generation of wiki markup [message #946213 is a reply to message #945763] Tue, 16 October 2012 02:52 Go to previous messageGo to next message
Subhankar Chattopadhyay is currently offline Subhankar ChattopadhyayFriend
Messages: 5
Registered: October 2012
Junior Member
Hi Jeremie,
My xml looks like this :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<CodeReview>
<File name="AbstractTestContract.java">
<Project name="com.sap.mts.junit.test"/>
<ShortText name="as"/>
<Description name="Please Add Your Comment for the java class here"/>
</File>
<File name="TestWizard.java">
<Project name="com.sap.mts.junit.test"/>
<ShortText name="sa"/>
<Description name="Please Add Your Comment for the java class here"/>
</File>
<File>
.....
.....
</File>
</CodeReview>



I want to generate a wiki content out of it which gives me a table consisting of all the data in the xml. Columns of the table will be File name, project name, Short text, Description. Can you please tell me how can I parse it? Code snippet will be of great help. I want to save the output in a .confluence file.
Re: API for generation of wiki markup [message #947052 is a reply to message #946213] Tue, 16 October 2012 19:22 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 124
Registered: November 2010
Senior Member
Here an example, how you can call a builder (in your case the ConfluenceDocumentBuilder) directly:
import java.io.StringWriter;

import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;
import org.eclipse.mylyn.wikitext.core.parser.TableAttributes;
import org.eclipse.mylyn.wikitext.core.parser.TableCellAttributes;
import org.eclipse.mylyn.wikitext.core.util.ServiceLocator;

public class UseBuilder {
	public static void main(String[] args) {
		StringWriter out = new StringWriter();
		DocumentBuilder builder = ServiceLocator.getInstance().getMarkupLanguage("Confluence").createDocumentBuilder(out);
		
		openTable(builder);
		
		openRow(builder);
		addHeaderCell(builder, "File name");
		addHeaderCell(builder, "Project name");
		addHeaderCell(builder, "Short text");
		addHeaderCell(builder, "Description");
		closeRow(builder);
		
		openRow(builder);
		addCell(builder, "AbstractTestContract.java");
		addCell(builder, "com.sap.mts.junit.test");
		addCell(builder, "Du noci suhum imi");
		addCell(builder, "Lango umidi te ipa, hinne abuni ubo pe. Ika teka imagi gonyo on. Cobi zaga paimoda tu sun.");
		closeRow(builder);
		
		openRow(builder);
		addCell(builder, "TestWizard.java");
		addCell(builder, "com.sap.mts.junit.test");
		addCell(builder, "Ume muga cebi kasin du");
		addCell(builder, "Kayo kidon jesio xen zn. Ura sane ceika unaua di, dun lindi xolada di, jaben kizinda zin si.");
		closeRow(builder);
		
		closeTable(builder);
		
		String markup = out.toString();
		System.out.println(markup);
	}
	
	private static void openTable(DocumentBuilder builder) {
		builder.beginBlock(BlockType.TABLE, new TableAttributes());
	}
	
	private static void closeTable(DocumentBuilder builder) {
		builder.endBlock();
	}
	
	private static void openRow(DocumentBuilder builder) {
		builder.beginBlock(BlockType.TABLE_ROW, new TableAttributes());
	}
	
	private static void closeRow(DocumentBuilder builder) {
		builder.endBlock();
	}
	private static void addHeaderCell(DocumentBuilder builder, String content) {
		builder.beginBlock(BlockType.TABLE_CELL_HEADER, new TableCellAttributes());
		builder.characters(content);
		builder.endBlock();
	}
	
	private static void addCell(DocumentBuilder builder, String content) {
		builder.beginBlock(BlockType.TABLE_CELL_NORMAL, new TableCellAttributes());
		builder.characters(content);
		builder.endBlock();
	}
}


"(new ConfluenceLanguage()).createDocumentBuilder(out)" is the correct way to instantiate the ConfluenceDocumentBuilder. It uses only public API.

Now in your case, I have try to parse your XML. The idea is to write a new parser MyCustomParser that will replace the HtmlParser of the first example. I have tried something, now I am publishing it here, but I am not really proud of this code...

import java.io.IOException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
import org.eclipse.mylyn.wikitext.core.parser.TableCellAttributes;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;
import org.eclipse.mylyn.wikitext.core.parser.TableAttributes;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class MyCustomParser {

	public void parse(InputSource input, DocumentBuilder builder) throws IOException, SAXException, ParserConfigurationException{
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		javax.xml.parsers.DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		Document doc = docBuilder.parse(input);
		
		openTable(builder);
		
		openRow(builder);
		addHeaderCell(builder, "File name");
		addHeaderCell(builder, "Project name");
		addHeaderCell(builder, "Short text");
		addHeaderCell(builder, "Description");
		closeRow(builder);
		
		NodeList rootNodes = doc.getChildNodes();
		Node codeReviewNode = rootNodes.item(0);
		NodeList fileNodes = codeReviewNode.getChildNodes();
		for (int i = 0; i < fileNodes.getLength(); i++) {
			Node fileNode = fileNodes.item(i);
			Node projectNode = null; 
			Node shortTextNode = null;
			Node descriptionNode = null;
			
			NodeList childNodes = fileNode.getChildNodes();
			for (int j = 0; j < childNodes.getLength(); j++) {
				Node node = childNodes.item(j);
				if("Project".equals(node.getNodeName())) {
					projectNode = node; 
				} else if("ShortText".equals(node.getNodeName())) {
					shortTextNode = node;
				} else if("Description".equals(node.getNodeName())) {
					descriptionNode = node;
				} else {
					throw new IllegalStateException("unexpected node: "+ node.getNodeName());
				}
			}
			if(projectNode == null || shortTextNode == null || descriptionNode == null) {
				throw new IllegalStateException("missing one of the child node.");
			}

			openRow(builder);
			addCell(builder, fileNode.getAttributes().getNamedItem("name").getTextContent());
			addCell(builder, projectNode.getAttributes().getNamedItem("name").getTextContent());
			addCell(builder, shortTextNode.getAttributes().getNamedItem("name").getTextContent());
			addCell(builder, descriptionNode.getAttributes().getNamedItem("name").getTextContent());
			closeRow(builder);
		}
		 
		closeTable(builder);
	}

	private static void openTable(DocumentBuilder builder) {
		builder.beginBlock(BlockType.TABLE, new TableAttributes());
	}
	
	private static void closeTable(DocumentBuilder builder) {
		builder.endBlock();
	}
	
	private static void openRow(DocumentBuilder builder) {
		builder.beginBlock(BlockType.TABLE_ROW, new TableAttributes());
	}
	
	private static void closeRow(DocumentBuilder builder) {
		builder.endBlock();
	}
	private static void addHeaderCell(DocumentBuilder builder, String content) {
		builder.beginBlock(BlockType.TABLE_CELL_HEADER, new TableCellAttributes());
		builder.characters(content);
		builder.endBlock();
	}
	
	private static void addCell(DocumentBuilder builder, String content) {
		builder.beginBlock(BlockType.TABLE_CELL_NORMAL, new TableCellAttributes());
		builder.characters(content);
		builder.endBlock();
	}
}


Testing this with the same Main class as in the first example:
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.nio.charset.Charset;

import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
import org.eclipse.mylyn.wikitext.core.util.ServiceLocator;
import org.xml.sax.InputSource;

public class Main {
	public static void main(String[] args) {
		StringWriter out = new StringWriter();
		DocumentBuilder builder = ServiceLocator.getInstance().getMarkupLanguage("Confluence").createDocumentBuilder(out);

		MyCustomParser parser = new MyCustomParser();
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
				"<CodeReview>" +
				"<File name=\"AbstractTestContract.java\">" +
				"<Project name=\"com.sap.mts.junit.test\"/>" +
				"<ShortText name=\"Du noci suhum imi\"/>" +
				"<Description name=\"Lango umidi te ipa, hinne abuni ubo pe. Ika teka imagi gonyo on. Cobi zaga paimoda tu sun.\"/>" +
				"</File>" +
				"<File name=\"TestWizard.java\">" +
				"<Project name=\"com.sap.mts.junit.test\"/>" +
				"<ShortText name=\"Ume muga cebi kasin du\"/>" +
				"<Description name=\"Kayo kidon jesio xen zn. Ura sane ceika unaua di, dun lindi xolada di, jaben kizinda zin si.\"/>" +
				"</File>" +
				"</CodeReview>";
		InputSource input = new InputSource(new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8"))));
		try {
			parser.parse(input, builder);
			String markup = out.toString();
			System.out.println(markup);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


I hope you get the idea...

---
Update: use the correct way to get ConfluenceDocumentBuilder given by David Green

[Updated on: Sun, 02 December 2012 17:00]

Report message to a moderator

Re: API for generation of wiki markup [message #987130 is a reply to message #922027] Fri, 23 November 2012 17:16 Go to previous message
David Green is currently offline David GreenFriend
Messages: 136
Registered: July 2009
Senior Member
Jeremie,

Thanks for the amazing support. The correct way to get
ConfluenceDocumentBuilder without using internal API is as follows:

DocumentBuilder builder =
ServiceLocator.getInstance().getMarkupLanguage("Confluence").createDocumentBuilder(writer);

David

On 9/24/2012 10:06 AM, Jeremie Bresson wrote:
> Simple JavaSE example:
>
> import java.io.ByteArrayInputStream;
> import java.io.IOException;
> import java.io.StringWriter;
> import java.nio.charset.Charset;
>
> import
> org.eclipse.mylyn.internal.wikitext.confluence.core.ConfluenceDocumentBuilder;
>
> import org.eclipse.mylyn.wikitext.core.parser.HtmlParser;
> import org.xml.sax.InputSource;
> import org.xml.sax.SAXException;
>
> public class Main {
> public static void main(String[] args) {
> StringWriter out = new StringWriter();
> ConfluenceDocumentBuilder builder = new
> ConfluenceDocumentBuilder(out);
>
> HtmlParser parser = new HtmlParser();
> String content = "<html><body><h1>Hello world</h1>"
> + "<p>this <i>is</i> a <b>good</b> test</p>"
> + "<ul><li>lorem</li><li>ipsum</li></ul></body></html>";
> InputSource input = new InputSource(new ByteArrayInputStream(
> content.getBytes(Charset.forName("UTF-8"))));
> try {
> parser.parse(input, builder);
> String markup = out.toString();
> System.out.println(markup);
> } catch (IOException e) {
> e.printStackTrace();
> } catch (SAXException e) {
> e.printStackTrace();
> }
> }
> }
>
>
> On the classpath you need to have:
> * org.eclipse.mylyn.wikitext.confluence.core_1.8.0.I20120907-2057.jar
> * org.eclipse.mylyn.wikitext.core_1.8.0.I20120907-2057.jar
>
> You also can have http://jsoup.org/ on the classpath. This will allow
> you to have not strict HTML as input.
>
> Very important:
> Notice that ConfluenceDocumentBuilder is an API internal class. It means
> that if you use it, you accept some changes (functions, features...)
> from a release to an other one.
>
> Output:
>
> h1. Hello world
>
> this _is_ a *good* test
>
> * lorem
> * ipsum
>
>
>
>
Previous Topic:Adding Elements to the active context of the task
Next Topic:How do I "install dependencies" using "File > Import > Install Software Item&qu
Goto Forum:
  


Current Time: Fri Apr 19 16:59:09 GMT 2024

Powered by FUDForum. Page generated in 0.02483 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top