Ejemplo n.º 1
0
    def test_XHTML(self):
        """Construct an XHTML page. Verify visually."""
        htd = XHTML.new_document(dtds.XHTML)
        htd.title = "This is the title."
        htd.add_header(1, 'Main document & "stuff"')
        htd.new_para("This is a test. This is text.")
        htd.add_unordered_list(["List line one.", "list line two."])
        BR = htd.get_new_element("Br")
        A = htd.get_new_element("A", href="somelink.html")
        A.add_text("some link")
        p = htd.get_para()
        p.append(A)
        p.add_text(" This is ")
        b = p.bold("bold")
        p.add_text(" text. using ")
        stb = htd.get_new_element("B")
        stb.add_text("bold tags")
        p.text(stb)
        p.add_text(" Dynamic Date: ")
        p.append(XHTML.DynamicNode(thedate))
        rp = str(p)
        htd.append(POM.ASIS(rp))
        # table methods
        t = htd.add_table(border=1)
        t.summary = "This is a test table."
        t.caption("table caption")
        h = t.set_heading(2, "heading col 2")
        h.set_attribute("class", "headerclass")
        t.set_heading(1, "heading col 1")
        t.set_cell(1, 1, "row 1, col 1")
        t.set_cell(1, 2, "row 2, col 1")
        t.set_cell(2, 1, "row 1, col 2")
        t.set_cell(2, 2, "row 2, col 2")
        # sections
        div = htd.get_section("section1")
        div.add_header(1, "Div heading.")
        div.new_para("First div para.")
        htd.append(div)
        div2 = div.get_section("section2")
        div2.new_para("Second div para")
        div.append(div2)

        dl = div.add_definition_list()
        dl.add_definitions({
            "def1": "The definition of 1",
            "def2": "The definition of 2"
        })

        # using the nodemaker object
        NM = htd.nodemaker
        ul = NM(
            "Ul",
            None,
            NM("Li", None, "line 1"),
            NM("Li", None, "line 2"),
            NM("Li", None, "Date: ",
               NM("code", None, thedate)),  # another way to add dynamic node
        )
        htd.append(ul)
        htd.append(NM("JS", None, 'var a = new Array(8);'))
        # using the creator object.
        creator = htd.creator
        parts = creator([("Just", "just/"), "How will this turn out?",
                         ["It is hard to tell.", "Well, not too hard."]])

        htd.add_comment(
            "the name attribute is required for all but submit & reset")
        htd.append(parts)
        f = htd.add_form(action="http://localhost:4001/cgi-bin/testing.py",
                         method="post")

        f.add_textarea("mytextarea", """Default text in the textarea.""")
        f.append(BR)
        f.add_input(type="text", name="mytext", value="mytext text")
        f.append(BR)
        f.add_input(type="button",
                    name="button1",
                    src="button.png",
                    value="Button")
        f.append(BR)
        f.add_input(type="submit",
                    name="submit1",
                    src="submit.png",
                    value="Ok")
        f.append(BR)
        f.add_radiobuttons("radlist", ["one", "two", "three", "four"],
                           vertical=False)
        f.append(BR)
        f.add_checkboxes("checks", ["one", "two", "three", "four"],
                         vertical=True)
        f.append(BR)
        f.add_fileinput(name="myfile", default="/etc/hosts")
        f.append(BR)
        f.add_textinput(name="mytext", label="Enter text")
        f.append(BR)
        f.yes_no("What's it gonna be?")
        f.add_select([
            "one", "two", ("three", True), "four", {
                "agroup": ["group1", "group2"]
            }
        ],
                     name="myselect")
        f.append(BR)

        f.add_select(
            {
                "Group1":
                Enums("g1one", "g1two", "g1three") + [("g1four", True)],
                "Group2": Enums("g2one", "g2two", "g2three"),
                "Group3": Enums("g3one", "g3two", "g3three"),
            },
            name="useenums")
        f.append(BR)

        f.add_select(["mone", "mtwo", ("mthree", True), ("mfour", True)],
                     name="multiselect",
                     multiple=True)
        f.append(BR)

        set = f.add_fieldset("afieldset")
        set.add_textinput(name="settext", label="Enter set text")
        set.add_textinput(name="settext2",
                          label="Enter set text 2",
                          default="Default text.")
        set.append(BR)
        tbl = htd.new_table([1, 2, 3, 4, 5], [NULL, NULL, NULL],
                            ["col1", "col2", "col3"],
                            width="100%",
                            summary="autogenerated")

        gentable = table.GenericTable(["heading1", "heading2", "heading3"],
                                      title="Sample generic table")
        gentable.append([1, 2, 3])
        gentable.append([4, 5, 6])
        tbl2 = htd.new_table_from_GenericTable(gentable)
        # object
        subdoc = XHTML.new_document(dtds.XHTML)
        parts = subdoc.creator(
            ("Add a document object.", ["Some data.", "some more data.."]))
        subdoc.append(parts)
        sdfo = open("/tmp/subdoc.html", "w")
        subdoc.emit(sdfo)
        sdfo.close()
        htd.add_object(data="subdoc.html",
                       type=subdoc.MIMETYPE,
                       width="400px",
                       height="600px")
        htd.emit(sys.stdout)
        print "-----"
        fo = open(XHTMLFILENAME, "w")
        bw = POM.BeautifulWriter(fo, XHTML.INLINE)
        htd.emit(bw)
        fo.close()
        print "----- Form values:"
        print f.fetch_form_values()
        print "----- Form elements:"
        felems = f.fetch_form_elements()
        for name, elemlist in felems.items():
            print repr(name), ": ", repr(elemlist)
            print
        # visually verify the page.
        webbrowser.open("file://%s" % (XHTMLFILENAME, ))
Ejemplo n.º 2
0
try:
    from PIL import Image
except ImportError:
    Image = None

from pycopia.urlparse import quote_plus
from pycopia.textutils import identifier
from pycopia.aid import partial, Enums, Enum

from pycopia import dtds
from pycopia.XML import POM, XMLVisitorContinue, ValidationError, XMLPathError

get_class = dtds.get_class

NBSP = POM.ASIS(b" ")
TRUE = True
FALSE = False

MIME_XHTML = b"application/xhtml+xml"
MIME_HTML = b"text/html"

# tags defined to be inline - use for BeautifulWriter and other type checks
INLINE_SPECIAL = ["span", "bdo", "object", "img", "map"]
# (br is ommitted on purpose - looks better)
INLINE_FONTSTYLE = ["tt", "i", "b", "big", "small"]
INLINE_PHRASE = [
    "em", "strong", "dfn", "code", "samp", "kbd", "cite", "var", "abbr",
    "acronym", "q", "sub", "sup"
]
INLINE_FORM = ["input", "select", "textarea", "label", "button"]