Skip to content

Instantly share code, notes, and snippets.

@alecperkins
Created May 29, 2013 15:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alecperkins/5671192 to your computer and use it in GitHub Desktop.
Save alecperkins/5671192 to your computer and use it in GitHub Desktop.
Convert an OPML outline to Markdown
"""
$ pip install opml
$ python opml_to_markdown.py some_outline.opml
-> some_outline.md
"""
import codecs
import opml
import sys
INPUT = sys.argv[1]
OUTPUT = '.'.join(INPUT.split('.')[:-1] + ['md'])
with codecs.open(INPUT, 'r', 'utf-8') as f:
outline = opml.from_string(f.read())
blocks = []
def _extractBlocks(node):
for child in node:
blocks.append(child.text)
if len(child) > 0:
_extractBlocks(child)
_extractBlocks(outline)
output_content = '\n\n'.join(blocks)
with codecs.open(OUTPUT, 'w', 'utf-8') as f:
f.write(output_content)
print '->', OUTPUT
@domdavis
Copy link

domdavis commented Apr 5, 2014

I found this didn't work on my Mac (didn't like line 14 setting utf-8, despite the file being UTF-8). With the OPML files I was using this was also just output the files as plain text. I've updated the code to produce a nested markdown list here: https://gist.github.com/domdavis/9988867

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