OPML to Markdown and back
By R. S. Doiel 2016-05-28
Overview
I wrote a Go language package to sort OPML outlines. I wrote this because my preferred feed reader supports manual sorting but not automatic alpha sorting by the outline element’s text attribute.
Observations
Out of the box the OPML 2 Spec provides attributes indicating inclusion of other OPML files, scripts, basic metadata (create, modified, authorship), and even directory structures.
Fargo allows user defined attributes to be applied to the outline element in OPML. This could be used in support some of the Scrivener features I miss such as describing how to render a project to various formats such as rtf, pdf, ePub, web pages or even Final Draft fdx files.
I write allot of Markdown formatted text. Markdown is simple to index, search and convert into useful formats. Markdown is not good at expressing more complex structures such as metadata. Website generators that use markdown often require a preamble or front matter in the markdown to provide any metadata. This leaves your document head cluttered and less human readable.
Another approach is to include a parallel document with the metadata. It occurred to me that an OPML file could easily hold that metadata. It can even hold Markdown content. The trouble with OPML is that it is not quick to edit by hand.
Is there a round trip semantic mapping between OPML and Markdown?
Germination of an idea
Entering a web link in Fargo the link is URL encoded and saved in the text attribute of the outline element.
The source view of a web links in Fargo’s outline element looks like
<outline text="> href="http://example.org"<My example.org>/a<" />
That outline element might render in Markdown as
+ [My element.org](http://example.org)
The steps to create the Markdown view are simple
- URL decode the text attribute
- Convert HTML to Markdown
Making a round trip could be done by
- Convert Markdown into HTML
- For each li element covert to an outline element URL encoding the inner HTML of the li
So far so good. What about something more complex?
Here’s an outline element example from http://hosting.opml.org/dave/spec/directory.opml
<outline text="Scripting News sites" created="Sun, 16 Oct 2005 05:56:10 GMT" type="link" url="http://hosting.opml.org/dave/mySites.opml"/>
To me that should look like
+ [Scripting News Sites](http://hosting.opml.org/dave/mySites.opml)
What about the created attribute? Could we render this case as an additional set of anchors using data uri?
This suggest a rule like
- if the text attribute contains HTML markup
- URL decode into HTML
- Convert HTML to Markdown
- else render attributes as additional anchors using data URI
This might work as follows.
<outline text="Scripting News sites"
created="Sun, 16 Oct 2005 05:56:10 GMT"
type="link"
url="http://hosting.opml.org/dave/mySites.opml"/>
Would become
+ [Scripting News Sites](http://hosting.opml.org/dave/mySites.opml) [type](data:text/plain;link) [created](data:text/date;Sun, 16 Oct 2005 05:56:10 GMT)
In HTML this would look like
<li><a href="http://histing.opml.org/dave/mySites.opml">Scripting News Sites</a>
<a href="data:text/plain;link">type</a>
<a href="data:text/date;Sun, 16 Oct 2005 05:56:10 GMT">created</a></li>
Markdown to OPML
Coming back to OPML from Markdown then becomes
- Convert Markdown to HTML
- For each li element inspect anchors,
- if anchors contain data URI then map outline element
- else URL encode and embed in outline text attribute
Is this viable? Does it have any advantages?