#!/usr/bin/python # converts vim outliner files to xHTML # Copyright (C) 2008 Steve Pomeroy # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. import sys, re from xml.dom.minidom import getDOMImplementation import xml.dom.ext as domext class vo: dom = getDOMImplementation() tab_re = re.compile(r'(\t*)(.+)') def __init__(self, fname): self.tree = self.parse(fname) def html(self): d = self.dom.createDocument('http://www.w3.org/1999/xhtml', "html", None) head = d.createElement('head') body = d.createElement('body') d.documentElement.appendChild(head) d.documentElement.appendChild(body) h1 = None for b_item in self.tree: if type(b_item) == list: body.appendChild(self.html_list(d, b_item)) else: if not h1: h1 = d.createElement('h1') h1.appendChild(d.createTextNode(b_item)) body.appendChild(h1) title = d.createElement('title') title.appendChild(d.createTextNode(b_item)) head.appendChild(title) else: h2 = d.createElement('h2') h2.appendChild(d.createTextNode(b_item)) body.appendChild(h2) domext.PrettyPrint(d) def html_list(self, d, l): if len(l) == 1 and type(l[0]) == str: p = d.createElement('p') p.appendChild(d.createTextNode(l[0])) elem = p else: ul = d.createElement('ul') prev = None for item in l: li = d.createElement('li') if type(item) == list: if len(item) == 1 and type(item[0]) == str and item[0][0:4] == 'http' and prev: a = d.createElement('a') a.setAttribute('href', item[0]) prev_c = prev.firstChild prev.removeChild(prev_c) a.appendChild(prev_c) prev.appendChild(a) li = prev else: prev.appendChild(self.html_list(d, item)) li = prev else: li.appendChild(d.createTextNode(item)) ul.appendChild(li) prev = li elem = ul return elem def parse(self, fname): s = [[]] f = open(fname) prev_i = None working = 0 ret = None for line in f: m = self.tab_re.match(line) if not m: continue i = len(m.group(1)) txt = m.group(2) if prev_i == None or i == prev_i: s[working].append(txt) elif i > prev_i: s.append([txt]) working += 1 elif i < prev_i: for times in range(i, prev_i): last = s.pop() working -= 1 s[working].append(last) s[working].append(txt) prev_i = i for times in range(0, len(s)-1): last = s.pop() working -= 1 s[working].append(last) return s[0] v = vo(sys.argv[1]) v.html()