Skip to content

Instantly share code, notes, and snippets.

@mrtouch93
Created November 20, 2022 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrtouch93/f2526af9c55284cbbd5a0d22a2cf2a08 to your computer and use it in GitHub Desktop.
Save mrtouch93/f2526af9c55284cbbd5a0d22a2cf2a08 to your computer and use it in GitHub Desktop.
OPML to MD file conversion
# Based on https://gist.github.com/domdavis/9988867
# $ pip install opml
# $ python2.7 opml2md.py some_outline.opml
# -> some_outline.md
#used for https://github.com/mrtouch93/awesome-security-feed
import codecs
import opml
import sys
INPUT = sys.argv[1]
OUTPUT = '.'.join(INPUT.split('.')[:-1] + ['md'])
with open(INPUT, 'r') as f:
outline = opml.from_string(f.read())
blocks = []
header = "# Awesome Security Feeds\nA semi-curated list of Security Feeds\n\nYou can import the opml file to a service like Feedly, Tiny Tiny RSS or similar.\n\nBelow a list of all the sites, feel free to suggest others!\n\n## Contents"
blocks.append(header)
content = ""
for grandchild in outline: #- [Platforms](#platforms)
content = "- ["+str(grandchild.text)+"](#"+str(grandchild.text.replace(" ","_").replace("&","and"))+")"
blocks.append(content)
# * [The Talk Show With John Gruber](htmlUrl) [RSS](xmlUrl)
def substring_after(s, delim):
return s.partition(delim)[2]
def substring_before(s, delim):
return s.partition(delim)[0]
def strip_end(text, suffix):
if not text.endswith(suffix):
return text
return text[:len(text)-len(suffix)]
def _extractBlocks(indent, node):
xmlURL = ""
textStr = ""
for grandchild in node:
title = "## "+str(grandchild.text.replace(" ","_").replace("&","and"))
blocks.append(title)
#print("# "+str(grandchild.text))
for child in grandchild:
if indent == 0:
# alternative output without the FontAwesome SVG stuff
text = "* [" + child.text + "](" + child.htmlUrl + ") \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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment