-
Notifications
You must be signed in to change notification settings - Fork 0
/
opml2otl
51 lines (47 loc) · 1.3 KB
/
opml2otl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/ruby
# Convert OTML (http://www.opml.org/spec) outline files to TVO .otl
# files
#
# Ned Konz <[email protected]>
# $Id: opml2otl 83 2002-12-23 05:38:47Z ned $
#
# Requires:
# Ruby (http://www.ruby-lang.org)
# REXML (http://www.germane-software.com/~ser)
#
# Usage:
# opml2otl file.opml [file2.opml [...]]
# Writes files with .otl extension replacing the original extension
#
# NOTE: requires that opml files end in native line-termination
# characters.
#
require 'rexml/document'
# print the <head> node
def doHead(h, io)
io.print("head\n")
h.elements.each { |el| io.print("\t", el.name, ": ", el.text, "\n") }
end
# print the <body> node
def doBody(b, io)
b.elements.each('outline') { |el| doOutline(el, io) }
end
# print an <outline> node, recursively
def doOutline(o, io, level = 0)
indent = "\t" * level
io.print(indent, o.attributes['text'], "\n")
o.attributes.each do |name, value|
next if name == 'text'
io.print(indent, "| ", name, ": ", value, "\n")
end
o.elements.each('outline') { |el| doOutline(el, io, level+1) }
end
ARGV.each do | arg |
File.open(arg) do |io|
File.open(arg.sub(/(\.[^.]+)?$/, '.otl'), 'w') do |out|
xml = REXML::Document.new(io)
doHead(xml.root.elements['head'], out)
doBody(xml.root.elements['body'], out)
end
end
end