<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<!--
================================================================================
 mozbookmarks2wiki.xsl - Convert Mozilla Bookmarks to Wiki
 http://juliano.info/projects/MozBookmarks2Wiki/
================================================================================

  Author: Juliano Ravasi Ferraz <jferraz at users sourceforge net>
  License: GNU General Public License
  Version: 1.0 (May 12, 2005)

  This XSL stylesheet can be used to convert a Mozilla Suite or Mozilla
  Firefox bookmarks RDF file into a format suitable for submiting into a
  Wiki, like MediaWiki (wikipedia.org). It generates a list of links in the
  format:

    == Wikis ==
    * [http://openfacts.berlios.de/ OpenFacts]

    === Wikipedia ===
    * [http://en.wikipedia.org English Wikipedia]
    * [http://de.wikipedia.org Deutsch Wikipedia]

    ...and so on...

  This stylesheet should be applied on your bookmarks *RDF* file. The
  bookmarks.html in your profile folder is of no use, forget it. To retrieve
  an RDF file from your bookmarks, you should use the JavaScript provided on
  these pages:

    http://www.moztips.com/bbbook/index.php?postid=470
    http://www.moztips.com/unmork/unmork.html (save it to your disk)

  After having your bookmarks in RDF format, apply this stylesheet on it
  using an XSLT processor and you will get a text file in Wiki syntax. If
  you are using Linux and have libXSLT installed, just type this command:

    xsltproc mozbookmarks2wiki.xsl bookmarks.rdf.txt

-->
<xsl:stylesheet
		xml:lang='en'
		xmlns='http://www.w3.org/1999/xhtml'
		xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
		xmlns:NS2="http://developer.mozilla.org/rdf/vocabulary/forward-proxy#"
		xmlns:NS1="http://home.netscape.com/WEB-rdf#"
		xmlns:NC="http://home.netscape.com/NC-rdf#"
		xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		version='1.0'
	>

	<xsl:param name='linefeed'><xsl:text>
</xsl:text></xsl:param>


	<xsl:output method='text' encoding='utf-8' media-type='text/plain'/>


	<xsl:template match='/RDF:RDF'>
		<xsl:apply-templates select='NC:Folder[@RDF:about="NC:BookmarksTopRoot"]'>
			<xsl:with-param name='level' select='0'/>
		</xsl:apply-templates>
	</xsl:template>


	<xsl:template match='NC:Folder'>
		<xsl:param name='level' select='0'/>
		<xsl:variable name='this' select='@RDF:about'/>

		<xsl:if test='@NC:Name'>
			<xsl:value-of select='$linefeed'/>
			<xsl:call-template name='heading'>
				<xsl:with-param name='text' select='@NC:Name'/>
				<xsl:with-param name='level' select='$level'/>
			</xsl:call-template>
			<xsl:value-of select='$linefeed'/>
		</xsl:if>

		<xsl:apply-templates select='/RDF:RDF/RDF:Seq[@RDF:about=$this]'>
			<xsl:with-param name='level' select='$level'/>
		</xsl:apply-templates>
	</xsl:template>


	<xsl:template match='NC:Bookmark'>
		<xsl:text>* [</xsl:text>
		<xsl:value-of select='@NC:URL'/>
		<xsl:text> </xsl:text>
		<xsl:value-of select='@NC:Name'/>
		<xsl:text>]</xsl:text><xsl:value-of select='$linefeed'/>
	</xsl:template>


	<xsl:template match='RDF:Seq'>
		<xsl:param name='level' select='0'/>

		<xsl:apply-templates select='RDF:li' mode='bookmarks'>
			<xsl:with-param name='level' select='$level + 1'/>
		</xsl:apply-templates>
		<xsl:apply-templates select='RDF:li' mode='folders'>
			<xsl:with-param name='level' select='$level + 1'/>
		</xsl:apply-templates>
	</xsl:template>


	<xsl:template match='RDF:li' mode='bookmarks'>
		<xsl:param name='level' select='0'/>

		<xsl:variable name='target' select='@RDF:resource'/>
		<xsl:apply-templates select='/RDF:RDF/NC:Bookmark[@RDF:about=$target]'>
			<xsl:with-param name='level' select='$level'/>
		</xsl:apply-templates>
	</xsl:template>


	<xsl:template match='RDF:li' mode='folders'>
		<xsl:param name='level' select='0'/>

		<xsl:variable name='target' select='@RDF:resource'/>
		<xsl:apply-templates select='/RDF:RDF/NC:Folder[@RDF:about=$target]'>
			<xsl:with-param name='level' select='$level'/>
		</xsl:apply-templates>
	</xsl:template>


	<xsl:template name='heading'>
		<xsl:param name='text'/>
		<xsl:param name='level' select='0'/>

		<xsl:choose>
			<xsl:when test='$level = 0'>
				<xsl:value-of select='$text'/>
			</xsl:when>
			<xsl:when test='$level = 1'>
				<xsl:text>= </xsl:text><xsl:value-of select='$text'/><xsl:text> =</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>=</xsl:text>
				<xsl:call-template name='heading'>
					<xsl:with-param name='text' select='$text'/>
					<xsl:with-param name='level' select='$level - 1'/>
				</xsl:call-template>
				<xsl:text>=</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


<!-- vim: set sw=2 ts=2 ai noet: -->
</xsl:stylesheet>
