Created
August 2, 2013 23:59
-
-
Save robstenzinger/6144383 to your computer and use it in GitHub Desktop.
A utility that helps me with taking an OPML file, often a first draft of most any document or blog post I write, then transform it into 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
""" | |
opml2markdown.py -- A command line tool to transform OPML files into markdown. | |
Written by Rob Stenzinger | |
License: MIT | |
http://opensource.org/licenses/MIT | |
Usage: | |
python opml2markdown.py PATH-TO-OMPL-FILE OPTIONAL-HEIRARCHY-MAPPED-MARKDOWN | |
By default, script will infer level 1 in OPML is level 1 heading, level 2, and level 3 their | |
given heading level as well: | |
** Example ** | |
* python opml2markdown.py my.opml "#" "*" " " " " > my.md | |
If you pass-in parameters as a set of quoted strings after the opml file path, you can set your own mappings, | |
including removing all heirchy: | |
** Example ** | |
* python opml2markdown.py my.opml " " > my.md | |
Inspired by github user: Alec Perkins (alecperkins) gist: | |
https://gist.github.com/alecperkins/5671192 | |
** Dependencies ** | |
Python OPML library "opml". | |
""" | |
import opml | |
import os | |
import sys | |
levelConfigCustom = {} | |
levelConfigDefault = { | |
"l1pre":"# ", | |
"l1end":" #\n\n", | |
"l2pre":"## ", | |
"l2end":" ##\n", | |
"l3pre":"### ", | |
"l3end":" ###\n", | |
"l4pre":"", | |
"l4end":"\n", | |
"l5pre":"* ", | |
"l5end":"\n", | |
"l6pre":" * ", | |
"l6end":"\n", | |
"l7pre":" * ", | |
"l7end":"\n", | |
"l8pre":" * ", | |
"l8end":"\n", | |
"l9pre":"", | |
"l9end":"\n", | |
"l10pre":"", | |
"l10end":"\n", | |
} | |
def updateLevelConfig(levelNumber, value): | |
levelPrefix = "" | |
levelSuffix = "" | |
if len(value) > 0: | |
if str.find(value, "#") > -1: | |
levelPrefix = "\n" + value + " " | |
levelSuffix = " " + value + "\n" | |
elif str.find(value, "*") > -1: | |
levelPrefix = "" + value + " " | |
# levelSuffix = "\n" | |
levelConfigCustom["l" + str(levelNumber) + "pre"] = levelPrefix | |
levelConfigCustom["l" + str(levelNumber) + "end"] = levelSuffix | |
def parseOPMLSection(outline, levelNumber, levelConfig): | |
if levelNumber <= 0: | |
levelNumber = 1 | |
levelPrefix = "" | |
levelSuffix = "\n" | |
try: | |
levelPrefix = levelConfig["l" + str(levelNumber) + "pre"] | |
levelSuffix = levelConfig["l" + str(levelNumber) + "end"] | |
except: | |
pass | |
for l in outline: | |
try: | |
# current line may have only text, may have additional levels or may be empty | |
print(levelPrefix + l.text + levelSuffix) | |
if len(l._outlines) > 0: | |
parseOPMLSection(l, levelNumber + 1, levelConfig) | |
except: | |
pass | |
def main(): | |
filePath = sys.argv[1] | |
customConfig = False | |
levelNumber = 0 | |
for i in range(2,len(sys.argv)): | |
value = sys.argv[i] | |
if len(value) > 0: | |
customConfig = True | |
levelNumber = levelNumber + 1 | |
updateLevelConfig(levelNumber, value) | |
if len(filePath) > 0: | |
outline = opml.parse(os.path.expanduser(filePath)) | |
if customConfig == False: | |
parseOPMLSection(outline, 1, levelConfigDefault) | |
else: | |
parseOPMLSection(outline, 1, levelConfigCustom) | |
else: | |
print("opml2markdown requires a file path of an OPML file as a command line argument.") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment