#-*- coding: utf-8 -*- import os import csv import xml.dom import optparse class XHTML(object): def __init__(self): impl = xml.dom.getDOMImplementation() self.doc = impl.createDocument('', 'html', '') self.root = self.doc.documentElement def add(self, *children): for child in children: self.root.appendChild(child) def text(self, parent, line): first = True for l in line.split('\n'): #if l.strip() == '': # continue if first is False: br = self.br() parent.appendChild(br) else: first = False txt = self.doc.createTextNode(l) parent.appendChild(txt) def set_text_attr(self, parent, text='', children=[], **attrs): for key, value in attrs.iteritems(): if key[0] == '_': key = key[1:] parent.setAttribute(key, str(value)) for elm in children: parent.appendChild(elm) if text != '': self.text(parent, text) def tag(self, name, *args, **kwds): elm = self.doc.createElement(name) self.set_text_attr(elm, *args, **kwds) return elm def singletag(self, name, *args, **kwds): tags = self.doc.getElementsByTagName(name) length = len(tags) assert length in (0, 1), length elm = None if length == 0: elm = self.tag(name, *args, **kwds) self.add(elm) else: elm = tags[0] self.set_text_attr(elm, *args, **kwds) return elm # mark up def strong(self, *args, **kwds): return self.tag('strong', *args, **kwds) def font(self, *args, **kwds): return self.tag('font', *args, **kwds) def b(self, *args, **kwds): return self.tag('b', *args, **kwds) def i(self, *args, **kwds): return self.tag('i', *args, **kwds) def em(self, *args, **kwds): return self.tag('em', *args, **kwds) def u(self, *args, **kwds): return self.tag('u', *args, **kwds) def s(self, *args, **kwds): return self.tag('s', *args, **kwds) def tt(self, *args, **kwds): return self.tag('tt', *args, **kwds) def blink(self, *args, **kwds): return self.tag('blink', *args, **kwds) def marquee(self, *args, **kwds): return self.tag('marquee', *args, **kwds) ## layout def br(self, *args, **kwds): return self.tag('br', *args, **kwds) def p(self, *args, **kwds): return self.tag('p', *args, **kwds) def div(self, *args, **kwds): return self.tag('div', *args, **kwds) def ruby(self, *args, **kwds): return self.tag('ruby', *args, **kwds) def blockquote(self, *args, **kwds): return self.tag('blockquote', *args, **kwds) def pre(self, *args, **kwds): return self.tag('pre', *args, **kwds) def pre(self, *args, **kwds): return self.tag('pre', *args, **kwds) def bdo(self, *args, **kwds): return self.tag('pre', *args, **kwds) ## link def a(self, *args, **kwds): return self.tag('a', *args, **kwds) def img(self, *args, **kwds): return self.tag('img', *args, **kwds) ## table def table(self, *args, **kwds): return self.tag('table', *args, **kwds) def caption(self, *args, **kwds): return self.tag('caption', *args, **kwds) def th(self, *args, **kwds): return self.tag('th', *args, **kwds) def td(self, *args, **kwds): return self.tag('td', *args, **kwds) def td(self, *args, **kwds): return self.tag('td', *args, **kwds) ## headline def h1(self, *args, **kwds): return self.tag('h1', *args, **kwds) def h2(self, *args, **kwds): return self.tag('h2', *args, **kwds) def h3(self, *args, **kwds): return self.tag('h3', *args, **kwds) def h4(self, *args, **kwds): return self.tag('h4', *args, **kwds) ## list def ul(self, *args, **kwds): return self.tag('ul', *args, **kwds) def ol(self, *args, **kwds): return self.tag('ol', *args, **kwds) def li(self, *args, **kwds): return self.tag('li', *args, **kwds) ## definitial list def dl(self, *args, **kwds): return self.tag('dl', *args, **kwds) def dt(self, *args, **kwds): return self.tag('dt', *args, **kwds) def dd(self, *args, **kwds): return self.tag('dd', *args, **kwds) ## frame def frameset(self, *args, **kwds): return self.tag('frameset', *args, **kwds) def iframe(self, *args, **kwds): return self.tag('iframe', *args, **kwds) ## elements def head(self, *args, **kwds): return self.singletag('head', *args, **kwds) def title(self, *args, **kwds): return self.singletag('title', *args, **kwds) def body(self, *args, **kwds): return self.singletag('body', *args, **kwds)
2011年11月4日金曜日
xhtmlを生成するクラス
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿