File: xhtml.py

package info (click to toggle)
pyth 0.5.6-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 312 kB
  • sloc: python: 1,609; makefile: 7
file content (52 lines) | stat: -rw-r--r-- 1,332 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-

from pyth.plugins.xhtml.reader import XHTMLReader
from pyth.plugins.xhtml.writer import XHTMLWriter
import xhtml

from cStringIO import StringIO

# A simple xhtml document with limited features.
content = StringIO(r"""
  <div>
    <p><strong>Simple document</strong></p>
    <p><i>this document has

    </i>no hypertext links yet.</p>
    <p><strong>bold text.</strong> <em>italic text.</em></p>
    <p class=important>bold text from css style
      <em> this is bold and italic</em>
    </p>
    <p class=bold> this is bold too</p>
    <p>unicode characters : 你好</p>
    <p style="font-weight: bold">bold too</p>
    <p>
      example<span style="vertical-align: super"> super </span>
      example<span style="vertical-align: sub"> sub </span>
    </p>
    a list
    <ul>
      <li>hello
      test</li>
      <li>bonjour</li>
      <li>guten tag</li>
    </ul>
    <p>
      <a href=http://www.google.com>a link
      </a> single space here.
      <br/>a br tag
    </p>
  </div>
""")

css = """
  .important {font-weight: bold}
  p.bold {font-weight: bold}
  .other {font-weight: normal; color: blue}
"""

if __name__ == '__main__':
    # Parse the document and then reconstruct it using the xhtml
    # writer.
    doc = XHTMLReader.read(content, css)
    print XHTMLWriter.write(doc).getvalue()