\n')
n = 0
# See if there's a title
if document[n].tagname == 'title':
title = cgi.escape(document[n][0][0].data)
self.w('%s\n'%title)
n += 1
if document[n].tagname == 'subtitle':
title = cgi.escape(document[n][0][0].data)
self.w('
%s
'%title)
self.section += 1
n += 1
# Now see if there's biblio information
# see if there's a field_list at the start of the document
if document[n].tagname == 'docinfo':
self.format_docinfo(document[n])
n += 1
self.w('\n')
# now for the body
l = list(document)
for entry in l[n:]:
self.formatOneTag(entry)
self.w('\n')
return self.out.getvalue()
#
# Title Elements
#
def format_title(self, node):
self.w(''%self.section)
if node.children: self.format(node)
self.w('\n'%self.section)
def format_subtitle(self, node):
raise NotImplementedError, node
#
# Bibliographic Elements
#
def format_docinfo(self, node):
''' (((%bibliographic.elements;)+, abstract?) | abstract)
bibliographic.elements:
author | authors | organization | contact | version | revision
| status | date | copyright
'''
if node.children: self.format(node)
def format_abstract(self, node):
content = urllib.quote(node[0].data)
self.w('\n'%content)
def format_author(self, node):
content = urllib.quote(node[0].data)
self.w('\n'%content)
def format_authors(self, node):
''' ((author, organization?, contact?)+)
'''
self.w('\n'%content)
def format_organization(self, node):
content = urllib.quote(node[0].data)
self.w('\n'%content)
# TODO: not in DTD
# def format_keywords(self, node):
# content = urllib.quote(node[0].data)
# self.w('\n'%content)
def format_contact(self, node):
addr = urllib.quote(node[0].data)
self.w('\n'%content)
def format_revision(self, node):
addr = urllib.quote(node[0].data)
self.w('\n'%content)
def format_status(self, node):
addr = urllib.quote(node[0].data)
self.w('\n'%content)
def format_date(self, node):
addr = urllib.quote(node[0].data)
self.w('\n'%content)
def format_copyright(self, node):
addr = urllib.quote(node[0].data)
self.w('\n'%content)
#
# Structural Elements
#
# section
#
# structure.model:
# ( ((%body.elements; | transition)+, (%structural.elements;)*)
# | (%structural.elements;)+ )
#
def format_section(self, node):
self.w(''%urllib.quote(node.attributes['name']))
self.section += 1
if node.children: self.format(node)
self.section -= 1
def format_transition(self, node):
self.w('')
#
# Body Elements
#
# paragraph | literal_block | block_quote | doctest_block| table
# | figure | image | footnote
# | bullet_list | enumerated_list | definition_list | field_list
# | option_list
# | note | tip | hint | warning | error | caution | danger | important
# | target | substitution_definition | comment | system_message
#
#
def format_paragraph(self, node):
''' %text.model;
'''
# TODO: there are situations where the
')
if node.children: self.format(node)
def format_term(self, node):
''' %text.model;
'''
self.w('')
if node.children:self.format(node)
self.w('')
def format_classifier(self, node):
''' %text.model;
'''
# TODO: handle the classifier better
self.w('')
if node.children: self.format(node)
self.w('')
def format_definition(self, node):
''' (%body.elements;)+
'''
self.w('
\n
')
# TODO: this is way suboptimal!
first = 1
for child in node.children:
if child.tagname == 'paragraph' and first:
# just format the contents of the para
self.format(child)
else:
# format the whole tag
self.formatOneTag(child)
first = 0
self.w('
\n')
# Field List
def format_field_list(self, node):
''' (field+)
'''
self.w('
\n')
def format_tgroup(self, node):
# we get the number of columns, if that's important
if node.children: self.format(node)
def format_colspec(self, node):
# we get colwidth, but don't need it
pass
def format_thead(self, node):
for row in node.children:
self.w('
')
for cell in row.children:
s = ''
attrs = cell.attributes
if attrs.has_key('morecols'):
s = s + ' colspan=%d'%(attrs['morecols']+1)
if attrs.has_key('morerows'):
s = s + ' rowspan=%d'%(attrs['morerows']+1)
self.w('
'%s)
if cell.children: self.format(cell)
self.w('
\n')
self.w('
\n')
def format_tbody(self, node):
for row in node.children:
self.w('
')
for cell in row.children:
s = ''
attrs = cell.attributes
if attrs.has_key('morecols'):
s = s + ' colspan=%d'%(attrs['morecols']+1)
if attrs.has_key('morerows'):
s = s + ' rowspan=%d'%(attrs['morerows']+1)
self.w('
'%s)
if cell.children: self.format(cell)
self.w('
\n')
self.w('
\n')
#
# Inline Elements
#
# Inline elements occur within the text contents of body elements. Some
# nesting of inline elements is allowed by these definitions, with the
# following caveats:
# - An inline element may not contain a nested element of the same type
# (e.g. may not contain another ).
# - Nested inline elements may or may not be supported by individual
# applications using this DTD.
# - The inline elements , , and do
# not support nesting.
#
# What that means is that all of these take (%text.model;) except:
# literal (#PCDATA)
# footnote_reference (#PCDATA)
#
# text.model:
# (#PCDATA | %inline.elements;)*
#
def format_emphasis(self, node):
''' (%text.model;)
'''
self.w('')
if node.children: self.format(node)
self.w('')
def format_strong(self, node):
''' (%text.model;)
'''
self.w('')
if node.children: self.format(node)
self.w('')
def format_interpreted(self, node):
''' (%text.model;)
type CDATA #IMPLIED
'''
pass #raise NotImplementedError, node
def format_literal(self, node):
''' (#PCDATA)
'''
self.w('')
for literal in node.children:
self.w(cgi.escape(literal.data))
self.w('')
def format_reference(self, node):
''' (%text.model;)
%reference.atts;
%anonymous.att;
'''
attrs = node.attributes
doc = self.document
ok = 1
print node
if attrs.has_key('refuri'):
self.w(''%attrs['refuri'])
elif doc.explicit_targets.has_key(attrs['refname']):
# an external reference has been defined
ref = doc.explicit_targets[attrs['refname']]
if ref.attributes.has_key('refuri'):
self.w(''%ref.attributes['refuri'])
else:
self.w(''%attrs['refname'])
elif doc.implicit_targets.has_key(attrs['refname']):
# internal reference
name = attrs['refname']
self.w(''%urllib.quote(name))
else:
ok = 0
self.w('target "%s" '
'undefined'%attrs['refname'])
if node.children: self.format(node)
if ok:
self.w('')
def format_footnote_reference(self, node):
''' (#PCDATA)
%reference.atts;
%auto.att;
'''
raise NotImplementedError, node
def format_substitution_reference(self, node):
''' (%text.model;)
%refname.att;
'''
raise NotImplementedError, node
def format_problematic(self, node):
''' (%text.model;)
'''
raise NotImplementedError, node
#
# Finally, #text
#
def format__text(self, node):
self.w(cgi.escape(node.data))
def main(filename, debug=0):
parser = Parser()
input = open(filename).read()
document = dps.utils.newdocument()
parser.parse(input, document)
if debug == 1:
print document.pformat()
else:
formatter = DumbHTMLFormatter()
print formatter.format_document(document)
if __name__ == '__main__':
if len(sys.argv) > 2:
main(sys.argv[1], debug=1)
else:
main(sys.argv[1])