-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Am 17.03.2010 15:48, schrieb Roberto Alsina:
> 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?
That depends. The special nodes like "productionlist" are leftovers from the
old Python doc system, and have a special implementation for LaTeX output, since
they were and partly still are implemented in the .sty file. For the specific
example of productionlist, the LaTeX your generic version would produce would
probably look somewhat less neat.
The other examples you give are a bit different. Graphviz doesn't actually
duplicate code in the visitor functions, and I'm not sure how it should be done
differently. What Pygments does is also very output-format specific.
But if you see anything that, after simplification, gives the same result,
don't hesitate to send me the patches.
cheers,
Georg
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (GNU/Linux)
iEYEARECAAYFAku7bQAACgkQN9GcIYhpnLA9RwCfQ0xKaZRcZzGYYdsfbvh/4m+r
/eYAniwuhdjF42YByyKfTxNZYTh9sduL
=Q2/S
-----END PGP SIGNATURE-----
--
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.