def test_applystylesheets_media(): # Check that media="screen" picks up the media stylesheet with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css", media="screen") with html.body(): +html.p("gurk") css.applystylesheets(e, media="screen") assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" # Check that media="screen" doesn't pick up the print stylesheet with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css", media="screen") with html.body(): +html.p("gurk") css.applystylesheets(e, media="print") assert str(e.walknodes(html.p)[0].attrs.style) == "" # Check that @media rules are treated properly with xsc.build(): with html.html() as e: with html.head(): +html.style("@media screen { p {color: red;}}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e, media="screen") assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" with xsc.build(): with html.html() as e: with html.head(): +html.style("@media screen { p {color: red;}}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e, media="print") assert str(e.walknodes(html.p)[0].attrs.style) == ""
def index(self): def isimg(name): if name.endswith(".gif") or name.endswith(".jpg") or name.endswith(".png"): return os.path.isfile(os.path.join(self.directory, name)) return False names = [name for name in os.listdir(self.directory) if isimg(name)] names.sort() collect = xsc.Frag() i = 0 with xsc.build(): with xsc.Frag() as e: +xml.XML() +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title("All images from ", doc.z(self.directory)) +html.link(rel="stylesheet", type="text/css", href="images.css") with html.body(): with htmlspecials.plaintable(): for name in names: collect.append(html.td(htmlspecials.autoimg(src=("/images/", name)), html.br(), name, align="center")) i += 1 if i == cols: +html.tr(collect) collect = xsc.Frag() i = 0 if collect: +html.tr(collect) return e.conv().bytes()
def convert(self, converter): e = xsc.Frag( xml.XML(), "\n", html.DocTypeXHTML10transitional(), "\n", html.html( html.head( meta.contenttype(), html.title( "Python code coverage", ul4.if_("filename"), ": ", ul4.printx("filename"), ul4.else_(), " (", ul4.print_("format(timestamp, '%Y-%m-%d')"), ")", ul4.end("if"), ), meta.stylesheet(href="/coverage.css"), meta.stylesheet(href="/coverage_sortfilelist.css"), htmlspecials.javascript(src="/coverage.js"), ), html.body( html.div( html.div( html.a( htmlspecials.autoimg(src="http://www.python.org/images/python-logo.gif", alt="Python", border=0), href="http://www.python.org/", ), class_="logo", ), html.div( ul4.for_("(i, item) in enumerate(crumbs)"), html.span( html.span( ul4.if_("i"), ">", ul4.else_(), "\xbb", ul4.end("if"), class_="bullet", ), ul4.if_("item.href"), html.a(ul4.printx("item.title"), href=ul4.printx("item.href")), ul4.else_(), html.span(ul4.printx("item.title"), class_="here"), ul4.end("if"), ul4.end("for"), class_="crumb", ), class_="crumbs", ), class_="header", ), html.div( self.content, class_="content", ), onload=ul4.attr_if(ul4.printx("onload"), cond="onload"), ), ), ) return e.convert(converter)
def convert(self, converter): def namekey(node): return str(node[name][0].content) dvds = xsc.Frag(self[dvd]).sorted(key=namekey) lds = xsc.Frag(self[ld]).sorted(key=namekey) with xsc.build(): with xsc.Frag() as e: +xml.XML() +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title("Media") +meta.stylesheet(href="Media.css") with htmlspecials.plainbody(): +html.h1("Media") if lds: +html.h2(len(lds), " LDs") +html.ol(lds) if dvds: +html.h2(len(dvds), " DVDs") +html.ol(dvds) return e.convert(converter)
def test_publishelement(): node = html.html() assert node.bytes(prefixdefault=False) == b"""<html></html>""" assert node.bytes( prefixdefault=None ) == b"""<html xmlns="http://www.w3.org/1999/xhtml"></html>""" assert node.bytes( prefixdefault="h" ) == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml"></h:html>""" assert node.bytes( prefixdefault=True ) == b"""<ns:html xmlns:ns="http://www.w3.org/1999/xhtml"></ns:html>""" assert node.bytes(prefixes={html: False}) == b"""<html></html>""" assert node.bytes( prefixes={html: None} ) == b"""<html xmlns="http://www.w3.org/1999/xhtml"></html>""" assert node.bytes( prefixes={html: "h"} ) == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml"></h:html>""" assert node.bytes( prefixes={html: True} ) == b"""<ns:html xmlns:ns="http://www.w3.org/1999/xhtml"></ns:html>""" assert node.bytes(prefixdefault="h", hidexmlns=[html]) == b"""<h:html></h:html>"""
def convert(self, converter): node = html_.html(self.content, self.attrs) if converter.lang is not None: if "lang" not in node.attrs: node.attrs.lang = converter.lang if xml.Attrs.lang not in node.attrs: node.attrs[xml.Attrs.lang] = converter.lang return node.convert(converter)
def test_publishxmlattr(): node = html.html(xml.Attrs(space="preserve")) assert node.bytes(prefixdefault=False) == b"""<html xml:space="preserve"></html>""" assert node.bytes(prefixdefault=True) == b"""<ns:html xmlns:ns="http://www.w3.org/1999/xhtml" xml:space="preserve"></ns:html>""" assert node.bytes(prefixdefault=None) == b"""<html xmlns="http://www.w3.org/1999/xhtml" xml:space="preserve"></html>""" assert node.bytes(prefixes={html: "h"}) == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml" xml:space="preserve"></h:html>""" # Prefix for XML namespace can't be overwritten assert node.bytes(prefixes={html: "h", xml: "spam"}) == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml" xml:space="preserve"></h:html>"""
def test_publishglobalattr(): # FIXME: Some of those tests depend on dict iteration order node = html.html(xlink.Attrs(title="the foo bar")) assert node.bytes(prefixdefault=False) == b"""<html xmlns:ns="http://www.w3.org/1999/xlink" ns:title="the foo bar"></html>""" assert node.bytes(prefixdefault=None) == b"""<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ns="http://www.w3.org/1999/xlink" ns:title="the foo bar"></html>""" assert node.bytes(prefixdefault=True) == b"""<ns:html xmlns:ns="http://www.w3.org/1999/xhtml" xmlns:ns2="http://www.w3.org/1999/xlink" ns2:title="the foo bar"></ns:html>""" assert node.bytes(prefixdefault="h") == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns:ns="http://www.w3.org/1999/xlink" ns:title="the foo bar"></h:html>""" assert node.bytes(prefixes={html: "h", xlink: "xl"}) == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml" xmlns:xl="http://www.w3.org/1999/xlink" xl:title="the foo bar"></h:html>"""
def makenode(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css") +html.style("p {color: blue;}", type="text/css", title="blue") with html.body(): +html.p("gurk") return e
def makenode(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css") +html.style( "p {color: blue;}", type="text/css", title="blue") with html.body(): +html.p("gurk") return e
def test_applystylesheets1(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p {color: red;}", type="text/css") with html.body(): +html.p("gurk") css.applystylesheets(e) assert str(e.walknodes(html.p)[0].attrs.style) == "color: red;" assert list(e.walknodes(html.style)) == []
def test_publishelement(): node = html.html() assert node.bytes(prefixdefault=False) == b"""<html></html>""" assert node.bytes(prefixdefault=None) == b"""<html xmlns="http://www.w3.org/1999/xhtml"></html>""" assert node.bytes(prefixdefault="h") == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml"></h:html>""" assert node.bytes(prefixdefault=True) == b"""<ns:html xmlns:ns="http://www.w3.org/1999/xhtml"></ns:html>""" assert node.bytes(prefixes={html: False}) == b"""<html></html>""" assert node.bytes(prefixes={html: None}) == b"""<html xmlns="http://www.w3.org/1999/xhtml"></html>""" assert node.bytes(prefixes={html: "h"}) == b"""<h:html xmlns:h="http://www.w3.org/1999/xhtml"></h:html>""" assert node.bytes(prefixes={html: True}) == b"""<ns:html xmlns:ns="http://www.w3.org/1999/xhtml"></ns:html>""" assert node.bytes(prefixdefault="h", hidexmlns=[html]) == b"""<h:html></h:html>"""
def test_applystylesheets4(): with xsc.build(): with html.html() as e: with html.head(): +html.style("#id42 {color: red;}", type="text/css") with html.body(): +html.p("gurk", id="id42", style="color: blue;") css.applystylesheets(e) # style attribute wins assert str(e.walknodes(html.p)[0].attrs.style) == "color: blue;" assert list(e.walknodes(html.style)) == []
def test_applystylesheets5(): with xsc.build(): with html.html() as e: with html.head(): +html.style("p#id42 {color: red;}", type="text/css") with html.body(): +html.p("gurk", id="id42", style="color: blue;") css.applystylesheets(e) # stylesheet always wins (at least in CSS 2.1 and 3) assert str(e.walknodes(html.p)[0].attrs.style) == "color: blue;" assert list(e.walknodes(html.style)) == []
def xsc2txt(instream, outstream, title, width): e = parse.tree(parse.Stream(instream), parse.SGMLOP(), parse.NS(doc), parse.Node(pool=xsc.docpool())) if title is None: title = xsc.Null else: title = doc.title(title) e = html.html(html.body(doc.section(title, e))) e = e.conv() outstream.write(html.astext(e, width=width))
def test_pickle(): e = xsc.Frag( xml.XML(), html.DocTypeXHTML10transitional(), xsc.Comment("foo"), html.html(xml.Attrs(lang="de"), lang="de"), php.expression("$foo"), chars.nbsp(), abbr.xml(), ) e.append(e[3]) e2 = pickle.loads(pickle.dumps(e, 2)) assert e == e2 assert e2[3] is e2[-1]
def xsc2txt(instream, outstream, title, width): e = parse.tree(parse.Stream(instream), parse.SGMLOP(), parse.NS(doc), parse.Node(pool=xsc.docpool())) if title is None: title = xsc.Null else: title = doc.title(title) e = html.html( html.body( doc.section(title, e) ) ) e = e.conv() outstream.write(html.astext(e, width=width))
def convert(self, converter): with xsc.build(): with xsc.Frag() as e: +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title(self[title][0].content) +meta.stylesheet(href="root:python-quotes.css") +html.body( self[title], self[editor], self[description], self[quotation] ) return e.convert(converter)
def index(self): def isimg(name): if name.endswith(".gif") or name.endswith(".jpg") or name.endswith( ".png"): return os.path.isfile(os.path.join(self.directory, name)) return False names = [name for name in os.listdir(self.directory) if isimg(name)] names.sort() collect = xsc.Frag() i = 0 with xsc.build(): with xsc.Frag() as e: +xml.XML() +html.DocTypeXHTML10transitional() with html.html(): with html.head(): +meta.contenttype() +html.title("All images from ", doc.z(self.directory)) +html.link(rel="stylesheet", type="text/css", href="images.css") with html.body(): with htmlspecials.plaintable(): for name in names: collect.append( html.td( htmlspecials.autoimg(src=("/images/", name)), html.br(), name, align="center")) i += 1 if i == cols: +html.tr(collect) collect = xsc.Frag() i = 0 if collect: +html.tr(collect) return e.conv().bytes()
def test_publish_forcexmlns(): e = html.html() s = e.string(prefixes={html: "h", specials: "s"}, showxmlns=[specials]) assert f'xmlns:s="{specials.xmlns}"' in s
if __name__ == "__main__": import sys, os, os.path as op from t4.psg.drawing.engine_two.styles import lists from t4.psg.drawing.engine_two.styles.computer_modern \ import cmu_sans_serif as cmuss, style from t4.psg.util.colors import red from t4.psg.drawing.engine_two.processors import render_to_filename from ll.xist.ns import html, chars demo = html.html(html.p(u"Hallo ", html.strong("schöne "), u"Welt!"), html.p(u"DiesesWort", html.strong(u"Keine"), u"Leerzeichen."), html.p(u"Dieses", html.big(" Leerzeichen "), u"ist groß."), html.p(u"Dieses ", html.big("Leerzeichen"), u" ist klein.")) richtext = convert(demo, {"p": {"margin": (12, 0, 0, 0)}}) print richtext.__print__() render_to_filename(richtext, "xist.ps")
def test_publish_forcexmlns(): e = html.html() s = e.string(prefixes={html: "h", specials: "s"}, showxmlns=[specials]) assert 'xmlns:s="{}"'.format(specials.xmlns) in s