Export to Markdown

Export to Markdown

Postby leehach » Mon Apr 30, 2012 2:55 am

I would like to export my mind map to Markdown, basically because from there I can use Pandoc to get it into a whole lot of other formats. Unfortunately, I don't know Groovy or Java. I ripped off the exportToCsv to create the following code:

Code: Select all
import javax.swing.*;
import org.freeplane.core.ui.components.UITools;
import org.freeplane.core.util.HtmlTools;

// for cut 'n paste:
def showDialog(String text) {
    def dialog = new JDialog(UITools.getFrame())
    dialog.setSize(350, 450)
    dialog.setLocationRelativeTo(UITools.getFrame())
    dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
    dialog.add(new JScrollPane(new JTextArea(text)))
    UITools.addEscapeActionToDialog(dialog)
    dialog.setVisible(true)
}

def process(thisNode, childPosition) {
    def noteText = thisNode.getNoteText() ? HtmlTools.htmlToPlain(thisNode.getNoteText()) : ""
    def result = [[childPosition, thisNode.text, noteText]]
    thisNode.children.each {
        result += process(it, childPosition + 1)
    }
    return result
}

def result = process(node, 0);
showDialog(result.collect{ "#" * it[0] + it[1] + "\n\n" + it[2]}.join("\n\n"))


This builds Markdown headings and (number of #s indicates heading level) and strips out all HTML. Of course, what I need to do is actually convert the HTML to Markdown. Pointers to basic Groovy tutorials, particularly for text processing, would be helpful. Any other suggestions are welcome, particularly if you know of anything that already does Markdown processing, or can advise on syntax for markup-to-markup conversion.

Thanks,
--Lee
leehach
 
Posts: 8
Joined: Mon Apr 30, 2012 1:32 am

Re: Export to Markdown

Postby boercher » Sun May 06, 2012 6:57 pm

Export of mind maps to a flat document format is challenging since normal ("good") mind maps are deeply hierarchical structured, which doesn't translate well to flat document formats since you are effectively limited to at most 3-4 nesting levels. Scripts offer the possibility to create very personal solutions for that problem. Mark Edgington has created a LaTeX export script, available at /bzr/freeplane/contrib/groovy, also see the post on the mailing list.

Nevertheless, Freeplane support an export to several flat document formats. I guess your simplest option would be to use the HTML export as a basis for a Markdown export and to use these resources: https://www.google.com/search?q=html+to+markdown (or perhaps Pandoc itself?).

Regards, Volker
boercher
 
Posts: 644
Joined: Tue Jul 26, 2011 7:13 am

Re: Export to Markdown

Postby leehach » Mon May 07, 2012 12:08 am

Volker,

Thanks for your response. I looked at a couple of your leads. Export to HTML adds a lot of HTML specific formatting, like Javascript-based branch folding, that seem to make export to other formats dodgy. The mailing list post you link was gated and I couldn't get access since I'm not a member of Freeplane developer. The LaTeX export script did not work out of the box, but maybe I might be able to use some of the HTML parsing code in my own script.

In any event the problem isn't the issue of moving from a hierarchical to a flat format. This is one of the reasons why I want to use Markdown instead of LaTeX. With LaTeX (which, admittedly, I'm not that familiar with), it looks like you're limited to, as you say, 3-4 levels (part for some formats, chapter, section, subsection, subsubsection). With Markdown, you just stick another # in front of the section.

Anyway, thanks for the leads.

--Lee
leehach
 
Posts: 8
Joined: Mon Apr 30, 2012 1:32 am

Re: Export to Markdown

Postby yate » Wed Nov 20, 2013 10:28 pm

I, too, would be interested in an "Export to Markdown" feature. I always use Freeplane for outlining and quick drafts, then frequently write in Markdown, using Pandoc to convert to Word, PDF, HTML, etc. as needed.

Would a simpler solution to this functionality be writing an export xslt file?

20131120_16-10-51.png
(24.39 KiB) Downloaded 150 times


I've attached an image of a mind map (of course!) outlining how the mind map would convert to Markdown. In words:
* All node text corresponds to a header, with the root node being h1, and all child nodes representing h2..h6, based on depth from the root.
* Any note text will be converted to body text within the "header" defined by the note's node.

It shouldn't be too hard to add hash signs (#) for atx-style headers based on node depth. A potential issue could be the interpretation of the html formatting of notes, but Markdown should be able to handle this [1]. I don't have much experience with xsl/xslt, but this may be a good learning experience.

Does this sound plausible to someone with more Freeplane dev experience?


[1] http://johnmacfarlane.net/pandoc/demo/e ... l#raw-html
yate
 
Posts: 6
Joined: Wed Nov 20, 2013 10:05 pm

Re: Export to Markdown

Postby leehach » Thu Nov 21, 2013 12:29 am

yate wrote:I, too, would be interested in an "Export to Markdown" feature. I always use Freeplane for outlining and quick drafts, then frequently write in Markdown, using Pandoc to convert to Word, PDF, HTML, etc. as needed.

Would a simpler solution to this functionality be writing an export xslt file?


As it turns out I have done just that, just hadn't gotten around to putting it up for people to see. Forum is not letting me upload, so please let me know where this can be submitted, or email me for a copy. Please use and let me know what you think. (More details below.)

yate wrote:* All node text corresponds to a header, with the root node being h1, and all child nodes representing h2..h6, based on depth from the root.
* Any note text will be converted to body text within the "header" defined by the note's node.


I've set the root node as a title block (preceded by `%` in Pandoc-flavored markdown). I haven't figured out how to get it to work with the standard 3-line title block:
Code: Select all
% My Article
% Lee Hachadoorian
% November 20, 2013


...because line breaks inserting in the node are not honored. Need to fix that so that line breaks in the root node (only) are honored and preceded by `%`.

Since the root node is a title block, top level child nodes are h1, then h2, etc. The XSLT does not pay attention to the number of levels, so it *will* produce a subhead of 7 or 8 hashes if you nest your map that deeply, which will of course be ignored by the markdown parsers I am familiar with.

Also, Markdown allows code blocks which must be prefixed by a tab (don't know how to insert a tab character in a Freeplane Note) or four or more blank spaces. Multiple spaces in Freeplane notes are stored as a nonbreaking space ` `, but Pandoc chokes on nonbreaking spaces. So first I use normalize-space(.) to turn any amount of whitespace (tabs and spaces, but *not* nonbreaking spaces) into a single space, *then* I translate() the nonbreaking spaces to regular spaces. This seems to work, but YMMV. Please let me know if you run into problems or have suggestions for improvement.
leehach
 
Posts: 8
Joined: Mon Apr 30, 2012 1:32 am

Re: Export to Markdown

Postby yate » Thu Nov 21, 2013 12:55 am

leehach wrote:As it turns out I have done just that, just hadn't gotten around to putting it up for people to see. Forum is not letting me upload, so please let me know where this can be submitted, or email me for a copy. Please use and let me know what you think. (More details below.)


I knew I couldn't be the only one - excited to see your implementation. Can you post it as a Github Gist? https://gist.github.com/
yate
 
Posts: 6
Joined: Wed Nov 20, 2013 10:05 pm

Re: Export to Markdown

Postby yate » Thu Nov 21, 2013 1:45 am

Here's my initial version: https://gist.github.com/py/7574625

It does the basics - create headers and put in note text. Any markdown within the note body does not work at this point... I like to put lists in my text, so I'll have to see if I can't figure out how to parse those so that pandoc can find them.
yate
 
Posts: 6
Joined: Wed Nov 20, 2013 10:05 pm

Re: Export to Markdown

Postby leehach » Thu Nov 21, 2013 4:50 am

I think mine is a little more full-featured at the moment. It *does* preserver markdown inside Notes, and also will convert some basic HTML, like changing <em>...</em> to *...*.

https://gist.github.com/leehach/7576227
leehach
 
Posts: 8
Joined: Mon Apr 30, 2012 1:32 am

Re: Export to Markdown

Postby yate » Thu Nov 21, 2013 3:20 pm

leehach wrote:I think mine is a little more full-featured at the moment. It *does* preserver markdown inside Notes, and also will convert some basic HTML, like changing <em>...</em> to *...*.

https://gist.github.com/leehach/7576227


I posted a comment on your gist - might as well keep the comments with the code.
yate
 
Posts: 6
Joined: Wed Nov 20, 2013 10:05 pm

Re: Export to Markdown

Postby yate » Fri Jan 24, 2014 9:20 pm

For those interested, we've consolidated our work and hosted it here: https://github.com/py/mm2markdown

Please feel free to use and contribute.
yate
 
Posts: 6
Joined: Wed Nov 20, 2013 10:05 pm

Next

Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 1 guest