Edgewall Software

Changes between Version 31 and Version 32 of GenshiFaq


Ignore:
Timestamp:
Jan 18, 2007, 8:23:44 PM (17 years ago)
Author:
cmlenz
Comment:

Added Q/A about whitespace stripping

Legend:

Unmodified
Added
Removed
Modified
  • GenshiFaq

    v31 v32  
    105105 * If you are generating the snippets in question yourself, you may want to use the [wiki:ApiDocs/genshi.builder genshi.builder] to [wiki:Documentation/builder.html generate markup streams programmatically]. Just as the results of the `XML` and `HTML` functions discussed above, the stream produced using `genshi.builder` will not be escaped in the template output.
    106106
     107=== What is Genshi doing with the whitespace in my markup template? ===
     108
     109When you serialize to XML/HTML, Genshi by default attempts to remove excessive whitespace: basically, it strips all trailing spaces and remove any empty lines. This is done to help keep down the size of generated content to be transmitted over the web. Genshi will not do any reformatting (such as indenting tags), as that would be too expensive and often of no practical use.
     110
     111If you're rendering XHTML or HTML output, Genshi tries not to mess with certain tags that are known to be sensitive to whitespace (such as `<textarea>` and `<pre>`). However, in general it is not trivial to determine whether whitespace is significant or not in markup, so you ''may'' see unexpected results in some cases. If that is a problem, the simplest solution is to disable stripping of whitespace entirely (a side effect is that serialization will be sped up):
     112
     113{{{
     114#!python
     115stream.render('html', strip_whitespace=False)
     116}}}
     117
     118If you'd like to keep the whitespace removal in general, but need to disable it inside certain elements, you can use the [http://www.w3.org/TR/REC-xml/#sec-white-space xml:space] attribute:
     119
     120{{{
     121#!xml
     122  … whitespace is stripped here …
     123
     124  <div xml:space="preserve">
     125
     126     … whitespace is kept intact inside this tag …
     127
     128  </div>
     129
     130  … whitespace is stripped here, again …
     131}}}
     132
     133You'll need to do that when:
     134 1. you're rendering to XML
     135 2. you're rendering to XHTML/HTML, and have set the element to display as `whitespace: nowrap` via CSS
     136 3. you have `<pre>` or `<textarea>` tags that are embedded as opaque `Markup` instances, in which case Genshi never actually sees the tags (see #78)
     137
    107138=== Can I add custom directives? ===
    108139