Skip to content

Instantly share code, notes, and snippets.

@leehach
Created November 21, 2013 04:47
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save leehach/7576227 to your computer and use it in GitHub Desktop.
Save leehach/7576227 to your computer and use it in GitHub Desktop.
Transforms freeplane mm to markdown md. Nodes become headings and subheadings, Notes become paragraphs. Details are not handled. Tested with Pandoc-flavored markdown. May not work: 1. Title blocks 2. Formatting which requires a specific number of spaces
<?xml version="1.0" encoding="UTF-8" ?>
<!--
MINDMAPEXPORTFILTER md;markdown Markdown
v. 0.1
This code released under the GPL. : (http://www.gnu.org/copyleft/gpl.html)
Document : mm2markdown.xsl
Created on : 20 November, 2013
Author : Lee Hachadoorian Lee.Hachadoorian@gmail.com
Description: Transforms freeplane mm to markdown md. Nodes become headings
and subheadings, Notes become paragraphs. Details are not handled. Tested
with Pandoc-flavored markdown.
May not work:
* Title blocks
* Formatting which requires a specific number of spaces
Please test and suggest improvements to author, or feel free to customize
while crediting previous authors.
******************************************************************************
Based on mm2text.xsl, original notice appears below
******************************************************************************
Document : mm2text.xsl
Created on : 01 February 2004, 17:17
Author : joerg feuerhake joerg.feuerhake@free-penguin.org
Description: transforms freeplane mm
format to html, handles crossrefs and adds numbering. feel free to
customize it while leaving the ancient authors mentioned. thank you
ChangeLog: See: http://freeplane.sourceforge.net/
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="map node" />
<xsl:key name="refid" match="node" use="@ID" />
<xsl:template name="numberSign">
<xsl:param name="howMany">1</xsl:param>
<xsl:if test="$howMany &gt; 0">
<!-- Add 1 number signs (#) to result tree. -->
<xsl:text>#</xsl:text>
<!-- Print remaining ($howMany - 1) number signs. -->
<xsl:call-template name="numberSign">
<xsl:with-param name="howMany" select="$howMany - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/map">
<xsl:apply-templates select="node"/>
</xsl:template>
<xsl:template match="richcontent">
<xsl:if test="@TYPE='DETAILS'">
<xsl:text>&#xA;DETAILS: </xsl:text>
</xsl:if>
<xsl:if test="@TYPE='NOTE'">
<xsl:text>&#xA;</xsl:text>
</xsl:if>
<xsl:apply-templates/>
<xsl:text>&#xA;</xsl:text>
</xsl:template>
<xsl:template match="child::text()">
<xsl:value-of select="translate(normalize-space(.),'&#160;',' ')" />
</xsl:template>
<xsl:template match="p|br|tr|div|li|pre">
<xsl:if test="preceding-sibling::*">
<xsl:text>&#xA;</xsl:text>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="i|em">
<xsl:text> *</xsl:text>
<xsl:apply-templates/>
<xsl:text>* </xsl:text>
</xsl:template>
<xsl:template match="b|strong">
<xsl:text> **</xsl:text>
<xsl:apply-templates/>
<xsl:text>** </xsl:text>
</xsl:template>
<xsl:template match="node">
<xsl:variable name="thisid" select="@ID" />
<xsl:variable name="target" select="arrowlink/@DESTINATION" />
<xsl:choose>
<xsl:when test="count(ancestor::*) = 1">
<xsl:text>%</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>&#xA;</xsl:text>
<xsl:call-template name="numberSign">
<xsl:with-param name="howMany" select="count(ancestor::*) - 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:text> </xsl:text>
<xsl:if test="@TEXT">
<xsl:value-of select="normalize-space(@TEXT)" />
<xsl:text>&#xA;</xsl:text>
</xsl:if>
<xsl:apply-templates select="richcontent[@TYPE='NODE']"/>
<xsl:apply-templates select="richcontent[@TYPE='DETAILS']"/>
<xsl:apply-templates select="richcontent[@TYPE='NOTE']"/>
<xsl:if test="arrowlink/@DESTINATION != ''">
<xsl:text> (see:</xsl:text>
<xsl:for-each select="key('refid', $target)">
<xsl:value-of select="@TEXT" />
</xsl:for-each>
<xsl:text>)</xsl:text>
</xsl:if>
<xsl:apply-templates select="node"/>
</xsl:template>
</xsl:stylesheet>
@py
Copy link

py commented Nov 21, 2013

Well done - I will test it today. I agree - yours is more full-featured. I'm still rather new to xsl/xslt (having started yesterday), so may I ask you a few questions?

  • Converting html: I hadn't realized it earlier, but the html tags within the notes look like xml to xsl. It looks like you are using that fact to parse the html elements as if they were xml attributes of the note - is that what's going on? Then you're able to further differentiate, so if you find a

    attribute, add a newline, etc.

  • Can you explain the logic on the <xsl:if test="preceding-sibling::*">? Is it looking to see if there was a previous attribute (in this case html tag), and if so, it is adding a new-line?

Next would be enhancements and fixes:

  • Title/YAML metadata block in the root node: According to pandoc markdown spec [1], there is a specific format for this information, and the line breaks do need to be honored. You had mentioned in your previous post that you were trying to get the title block syntax working (e.g. "% My great mindmap"). Is there an advantage of one of these vs. the other?
  • Details: I had never actually used node details before; I had to go try it out in Freeplane. Should we treat those just like notes? I imagine most users use notes or details, but likely not both.
  • Node links: Pandoc does include syntax for internal links [2] - might be worth exploring.

This is exciting - I will use this every day.

[1] http://johnmacfarlane.net/pandoc/demo/example9/pandocs-markdown.html#yaml-metadata-block
[2] http://johnmacfarlane.net/pandoc/demo/example9/pandocs-markdown.html#internal-links

@leehach
Copy link
Author

leehach commented Nov 23, 2013

I'm pretty new to XSLT myself. I've been using XLST Quickly.

Your first two bullet points appear to be missing some text. I don't understand what you mean by "the html tags within the notes look like xml to xsl". XSLT processes tags wherever it finds them, doen't matter if their inside a note. So <i>...</i> is a tag pair, and it is replaced with *...*. The HTML elements are not "attributes" of the note, maybe they are considered children? Maybe I misunderstand the question because of the missing text in your post.

@leehach
Copy link
Author

leehach commented Nov 23, 2013

I haven't used YAML metadata myself. According to your link at [1], YAML blocks can appear anywhere in the document, so it might be easiest to just put them in the Note associated with the root node. Title blocks have to appear at the beginning of the document, and it appears that there can't even be a blank line above them, so it seems best to include them in the core of the root node (that and an empty root node looks weird). normalize-space() is destroying the newlines that I try to include to get the title block to look like:

% Title
% Author 1; Author 2
% Date

I have to figure out how to parse the root node and only the root node so as to not normalize spaces. (Maybe I should try turning it off for the whole document?)

I also never use node Details, and am not sure how I would use it in the kind of articles and long documents I am using mm2markdown for.

I'll have to take a look at [2] and see if I can adapt the internal link logic already included in mm2text to output Pandoc-friendly internal links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment