Hello, I have spent a while now trying to implement production list support
for rst2pdf when used with sphinx, and I think I have noticed a pattern in the
code that makes these things harder than they need to be.
Here's sphinx's html writer's implementation of production lists:
def visit_productionlist(self, node):
self.body.append(self.starttag(node, 'pre'))
names = []
for production in node:
names.append(production['tokenname'])
maxlen = max(len(name) for name in names)
for production in node:
if production['tokenname']:
lastname = production['tokenname'].ljust(maxlen)
self.body.append(self.starttag(production, 'strong', ''))
self.body.append(lastname + '</strong> ::= ')
else:
self.body.append('%s ' % (' '*len(lastname)))
production.walkabout(self)
self.body.append('\n')
self.body.append('</pre>\n')
raise nodes.SkipNode
Here's what I did for rst2pdf:
def visit_productionlist(self, node):
replacement=nodes.literal_block(classes=["code"])
names = []
for production in node:
names.append(production['tokenname'])
maxlen = max(len(name) for name in names)
for production in node:
if production['tokenname']:
lastname = production['tokenname'].ljust(maxlen)
n=nodes.strong()
n+=nodes.Text(lastname)
replacement+=n
replacement+=nodes.Text(' ::= ')
else:
replacement+=nodes.Text('%s ' % (' '*len(lastname)))
production.walkabout(self)
replacement.children.extend(production.children)
replacement+=nodes.Text('\n')
node.parent.replace(node,replacement)
The main difference is that my version is not PDF-specific at all. It's just
docutils node manipulation!
In fact, replacing my version into sphinx's HTML writer would probably give
the exact same output as it currently has (haven't tried!).
The same things happen with the graphviz extension, with the pygments
extension and so on. All of them are implementable in a non-writer-specific
way, they all are implemented so they have to be done once for HTML and once
for LaTeX (and once more for rst2pdf by me).
Am I missing something that makes Sphinx's approach better?
--
You received this message because you are subscribed to the Google Groups
"sphinx-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sphinx-dev?hl=en.