Skip to content

Commit

Permalink
Whitespace and 'no cover' in html.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Aug 21, 2017
1 parent 0afc409 commit 0bc6b17
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions src/zope/structuredtext/html.py
Expand Up @@ -15,15 +15,15 @@

try:
from html import escape
except ImportError: # pragma: NO COVER Python2
except ImportError: # pragma: no cover Python2
from cgi import escape
else: # pragma: NO COVER Py3k
else: # pragma: no cover Py3k
from functools import partial
escape = partial(escape, quote=False)

__metaclass__ = type

class HTML:
class HTML(object):

paragraph_nestable = {
'#text': '_text',
Expand All @@ -36,7 +36,7 @@ class HTML:
'StructuredTextNamedLink':'namedLink',
'StructuredTextUnderline':'underline',
'StructuredTextSGML':'sgml', # this might or might not be valid
}
}

element_types = paragraph_nestable.copy()
element_types.update({
Expand All @@ -51,14 +51,14 @@ class HTML:
'StructuredTextSection': 'section',
'StructuredTextSectionTitle': 'sectionTitle',
'StructuredTextTable':'table',
})
})

def dispatch(self, doc, level, output):
getattr(self, self.element_types[doc.getNodeName()]
)(doc, level, output)

def __call__(self, doc, level=1, header=True):
r=[]
r = []
self.header = header
self.dispatch(doc, level-1, r.append)
return ''.join(r)
Expand All @@ -67,14 +67,14 @@ def _text(self, doc, level, output):
output(doc.getNodeValue())

def document(self, doc, level, output):
children=doc.getChildNodes()
children = doc.getChildNodes()

if self.header:
output('<html>\n')
if (children and
children[0].getNodeName() == 'StructuredTextSection'):
if (children
and children[0].getNodeName() == 'StructuredTextSection'):
output('<head>\n<title>%s</title>\n</head>\n' %
children[0].getChildNodes()[0].getNodeValue())
children[0].getChildNodes()[0].getNodeValue())
output('<body>\n')

for c in children:
Expand All @@ -86,7 +86,7 @@ def document(self, doc, level, output):
output('</html>\n')

def section(self, doc, level, output):
children=doc.getChildNodes()
children = doc.getChildNodes()
for c in children:
getattr(self, self.element_types[c.getNodeName()]
)(c, level+1, output)
Expand All @@ -99,13 +99,13 @@ def sectionTitle(self, doc, level, output):
output('</h%d>\n' % (level))

def description(self, doc, level, output):
p=doc.getPreviousSibling()
p = doc.getPreviousSibling()
if p is None or p.getNodeName() is not doc.getNodeName():
output('<dl>\n')
for c in doc.getChildNodes():
getattr(self, self.element_types[c.getNodeName()]
)(c, level, output)
n=doc.getNextSibling()
n = doc.getNextSibling()
if n is None or n.getNodeName() is not doc.getNodeName():
output('</dl>\n')

Expand All @@ -124,38 +124,39 @@ def descriptionBody(self, doc, level, output):
output('</dd>\n')

def bullet(self, doc, level, output):
p=doc.getPreviousSibling()
p = doc.getPreviousSibling()
if p is None or p.getNodeName() is not doc.getNodeName():
output('\n<ul>\n')
output('<li>')
for c in doc.getChildNodes():
getattr(self, self.element_types[c.getNodeName()]
)(c, level, output)
n=doc.getNextSibling()
n = doc.getNextSibling()
output('</li>\n')
if n is None or n.getNodeName() is not doc.getNodeName():
output('\n</ul>\n')

def numbered(self, doc, level, output):
p=doc.getPreviousSibling()
p = doc.getPreviousSibling()
if p is None or p.getNodeName() is not doc.getNodeName():
output('\n<ol>\n')
output('<li>')
for c in doc.getChildNodes():
getattr(self, self.element_types[c.getNodeName()]
)(c, level, output)
n=doc.getNextSibling()
n = doc.getNextSibling()
output('</li>\n')
if n is None or n.getNodeName() is not doc.getNodeName():
output('\n</ol>\n')

def example(self, doc, level, output):
i=0
i = 0
for c in doc.getChildNodes():
if i==0:
if i == 0:
output('\n<pre>\n')
output(escape(c.getNodeValue()))
output('\n</pre>\n')
# XXX: Nothing ever changes the value of i!
else:
getattr(self, self.element_types[c.getNodeName()])(
c, level, output)
Expand Down Expand Up @@ -213,7 +214,7 @@ def underline(self, doc, level, output):
output("</u>")

def innerLink(self, doc, level, output):
output('<a href="#ref');
output('<a href="#ref')
for c in doc.getChildNodes():
getattr(self, self.element_types[c.getNodeName()]
)(c, level, output)
Expand All @@ -234,16 +235,16 @@ def namedLink(self, doc, level, output):
)(c, level, output)
output(']</a>')

def sgml(self,doc,level,output):
def sgml(self, doc, level, output):
for c in doc.getChildNodes():
getattr(self, self.element_types[c.getNodeName()]
)(c, level, output)

def xref(self, doc, level, output):
val = doc.getNodeValue()
output('<a href="#ref%s">[%s]</a>' % (val, val) )
output('<a href="#ref%s">[%s]</a>' % (val, val))

def table(self,doc,level,output):
def table(self, doc, level, output):
"""
A StructuredTextTable holds StructuredTextRow(s) which
holds StructuredTextColumn(s). A StructuredTextColumn
Expand All @@ -254,19 +255,19 @@ def table(self,doc,level,output):
for row in doc.getRows()[0]:
output("<tr>\n")
for column in row.getColumns()[0]:
if hasattr(column,"getAlign"):
if hasattr(column, "getAlign"):
str = ('<%s colspan="%s" align="%s" valign="%s">'
% (column.getType(),
column.getSpan(),
column.getAlign(),
column.getValign()))
% (column.getType(),
column.getSpan(),
column.getAlign(),
column.getValign()))
else:
str = '<td colspan="%s">' % column.getSpan()
output(str)
for c in column.getChildNodes():
getattr(self, self.element_types[c.getNodeName()]
)(c, level, output)
if hasattr(column,"getType"):
if hasattr(column, "getType"):
output("</"+column.getType()+">\n")
else:
output("</td>\n")
Expand All @@ -277,15 +278,15 @@ class HTMLWithImages(HTML):

paragraph_nestable = HTML.paragraph_nestable.copy()
paragraph_nestable.update({'StructuredTextImage': 'image'})

element_types = HTML.element_types.copy()
element_types.update({'StructuredTextImage': 'image'})

def image(self, doc, level, output):
if hasattr(doc, 'key'):
output('<a name="%s"></a>\n' % doc.key)
output('<img src="%s" alt="%s" />\n'
% (doc.href, doc.getNodeValue()))
% (doc.href, doc.getNodeValue()))
if doc.getNodeValue() and hasattr(doc, 'key'):
output('<p><b>Figure %s</b> %s</p>\n'
% (doc.key, doc.getNodeValue()))
% (doc.key, doc.getNodeValue()))

0 comments on commit 0bc6b17

Please sign in to comment.