Sunday, March 1, 2015

Export parts of Firefox bookmarks to HTML or Trac

After searching around on the web for a while, i decided to write my own little conversion tool to export only parts of my firefox bookmarks, e.g. subfolders, to a HTML-File or in TRAC format.
Heres how i did it:
I added the OPML Addon to my firefox: https://addons.mozilla.org/de/firefox/addon/2625
I exported my bookmarks to an OPML-File, which is just XML.
Next i create a little XSL-Stylesheet that converts the OPML to the desired format.
To convert OPML to TRAC links the XSLT looks like this (OPML2TRAC.xsl):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-16" media-type="text/plain" omit-xml-declaration="yes" indent="no"/>
<xsl:variable name="exportRootFolderName">PlaceTheNameOfYourSubfolderHere</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="//outline[@text=$exportRootFolderName]">
<xsl:call-template name="outlineConvert"></xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="outlineConvert">
<xsl:for-each select="outline[@url != ]">
<xsl:sort select="@text" />[<xsl:value-of select="./@url" disable-output-escaping="yes"/><xsl:text> </xsl:text><xsl:value-of select="./@text"/>]<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


To convert OPML to HTML links the XSLT looks like this (OPML2HTML.xsl):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-16" media-type="text/html" omit-xml-declaration="yes" indent="no"/>
<xsl:variable name="exportRootFolderName">PlaceTheNameOfYourSubfolderHere</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="//outline[@text=$exportRootFolderName]">
<xsl:call-template name="outlineConvert"></xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="outlineConvert">
<html>
<body>
<h2>My Bookmarks</h2>
<table border="1">
<xsl:for-each select="outline[@url != ]">
<xsl:sort select="@text" />
<tr>
<td>
<a>
<xsl:attribute name="href">
<xsl:value-of select="./@url"/>
</xsl:attribute>
<xsl:value-of select="./@text"/>
</a>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Replace the "PlaceTheNameOfYourSubfolderHere" value with the name of the Subfolder of your firefox bookmarks.

In the head of the OPML-File insert the reference to the XSL-File:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="opml2trac.xsl"?>
<opml version="1.0">
... Rest of the OPML file ...

Open the OPML in Firefox or Internet Explorer now. You should then see the converted result.

You can also use a XSLT-Processor, e.g. MSXSL to do the conversion.
for details click below

No comments:

Post a Comment