Last active
August 5, 2023 03:08
-
-
Save domdavis/9988867 to your computer and use it in GitHub Desktop.
OPML to Markdown converter, based on https://gist.github.com/alecperkins/5671192 and fixed up to produce a nested list in Markdown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Based on https://gist.github.com/alecperkins/5671192 | |
$ pip install opml | |
$ python opml2md.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') as f: | |
outline = opml.from_string(f.read()) | |
blocks = [] | |
def _extractBlocks(indent, node): | |
for child in node: | |
if indent == 0: | |
text = "# " + child.text + "\n" | |
else: | |
depth = 4 * (indent - 1) | |
text = (" " * depth) + "* " + child.text | |
blocks.append(text) | |
if len(child) > 0: | |
depth = indent + 1 | |
_extractBlocks(depth, child) | |
_extractBlocks(0, outline) | |
output_content = '\n'.join(blocks) | |
with codecs.open(OUTPUT, 'w', 'utf-8') as f: | |
f.write(output_content) | |
print '->', OUTPUT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment