OmniOutliner2Wiki.py
Converts opml to wiki format. Usage: OmniOutliner2Wiki.py myfile.opml
#!/usr/bin/python
import sys
from xml.dom import minidom
def remove_whitespace_nodes(node, unlink=False):
"""Removes all of the whitespace-only text decendants of a DOM node.
When creating a DOM from an XML source, XML parsers are required to
consider several conditions when deciding whether to include
whitespace-only text nodes. This function ignores all of those
conditions and removes all whitespace-only text decendants of the
specified node. If the unlink flag is specified, the removed text
nodes are unlinked so that their storage can be reclaimed. If the
specified node is a whitespace-only text node then it is left
unmodified."""
remove_list = []
for child in node.childNodes:
if child.nodeType == minidom.Node.TEXT_NODE and \
not child.data.strip():
remove_list.append(child)
elif child.hasChildNodes():
remove_whitespace_nodes(child, unlink)
for node in remove_list:
node.parentNode.removeChild(node)
if unlink:
node.unlink()
def get_text(node):
return node.getAttribute("text")
def has_note(node):
return node.hasAttribute("_note")
def get_note(node):
return node.getAttribute("_note")
def add_children(node, doc, list_indent="*"):
for childNode in node.childNodes:
doc.append(list_indent + " " + get_text(childNode))
if has_note(childNode):
note = get_note(childNode)
note = note.replace("\n", "\n" + list_indent + ": ")
doc.append(list_indent + ": " + note)
add_children(childNode, doc, list_indent + "*")
if __name__ == "__main__":
xml_doc = minidom.parse(sys.argv[1])
doc = []
remove_whitespace_nodes(xml_doc)
for mainNode in xml_doc.childNodes[0].childNodes[1].childNodes:
doc.append("== %s ==" % get_text(mainNode))
if has_note(mainNode):
doc.append(get_note(mainNode))
# Check if all childnodes do not have
# subnodes -- handle differently in this case
if sum([len(subNode.childNodes) for subNode in mainNode.childNodes]) == 0:
for subNode in mainNode.childNodes:
doc.append("* %s" % get_text(subNode))
if has_note(subNode):
note = get_note(subNode)
note = note.replace("\n", "\n*: ")
doc.append("*: " + note)
else:
for subNode in mainNode.childNodes:
doc.append("=== %s ===" % get_text(subNode))
if has_note(subNode):
doc.append(get_note(subNode))
add_children(subNode, doc, "*")