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_mapped(): def maplang(node, converter): if isinstance(node, xsc.Text): node = node.replace("lang", converter.lang) return node node = xsc.Frag( "lang", html.div( "lang", class_="lang", ) ) node2 = node.mapped(maplang, lang="en") assert node == xsc.Frag( "lang", html.div( "lang", class_="lang", ) ) assert node2 == xsc.Frag( "en", html.div( "en", class_="lang", # No replacement in attributes ) )
def test_mul(): node = xsc.Frag("a") assert 3 * node == xsc.Frag(list("aaa")) assert node * 3 == xsc.Frag(list("aaa")) node = html.div() assert 3 * node == xsc.Frag(html.div(), html.div(), html.div()) assert node * 3 == xsc.Frag(html.div(), html.div(), html.div())
def convert(self, converter): with xsc.build(): with xsc.Frag() as e: if self[term] or self[classifier]: with doc.dt(): +xsc.Frag(self[term]) if self[classifier]: +xsc.Text(" (") +xsc.Frag(self[classifier]).withsep(", ") +xsc.Text(")") +xsc.Frag(self[definition]) return e.convert(converter)
def convert_fo(self, converter): context = converter[self] context.sections[-1] += 1 context.sections.append(0) ts = xsc.Frag() cs = xsc.Frag() props = [ # size, before, after ("30pt", "30pt", "2pt"), ("22pt", "20pt", "2pt"), ("16pt", "15pt", "2pt"), ("12pt", "15pt", "2pt") ] for child in self.content: if isinstance(child, h): ts.append(child.content) else: cs.append(child) p = props[min(len(context.sections)-1, len(props)-1)] isref = str(self.attrs.role.convert(converter)) in ("class", "method", "property", "function", "module") number = None if isref: context.indentcount += 1 text_indent = context.dedent() else: if len(context.sections)>1: number = ( ".".join(str(s) for s in context.sections[:-1]), ". " ) text_indent = None tattrs = fo.block.Attrs( font_size=p[0], color=context.llblue, space_before=p[1], space_after=p[2], text_align="left", font_family=context.hdfont, keep_with_next_within_page="always", text_indent=text_indent ) e = fo.block( fo.block(number, ts, tattrs), cs, start_indent=context.indent() ) e = e.convert(converter) del context.sections[-1] if isref: context.indentcount -= 1 return e
def convert_html(self, converter): target = converter.target ts = xsc.Frag() e = xsc.Frag() for child in self: if isinstance(child, h): ts.append(child) else: e.append(child) if ts: e.append(target.div(ts, class_="example-title")) with _stack(converter[self], self): return e.convert(converter)
def test_with(): with xsc.build(): with html.ul() as e: +html.li(1) +html.li(2) assert e == html.ul(html.li(1), html.li(2)) with xsc.build(): with html.p() as e: +html.span(1) with html.b(): +html.span(2) +html.span(3) assert e == html.p(html.span(1), html.b(html.span(2)), html.span(3)) with xsc.build(): with html.p() as e: +xsc.Text(1) assert e == html.p(1) with xsc.build(): with html.p() as e: +xml.Attrs(lang="de") assert e == html.p(xml.Attrs(lang="de")) assert e.bytes() == b'<p xml:lang="de"></p>' with xsc.build(): with xsc.Frag() as e: +xsc.Text(1) assert e == xsc.Frag(1) # Test add() with xsc.build(): with html.p() as e: xsc.add(1) assert e == html.p(1) with xsc.build(): with html.p() as e: xsc.add(class_="foo") assert e == html.p(class_="foo") with xsc.build(): with html.p() as e: xsc.add({"class": "foo"}) assert e == html.p(class_="foo") with xsc.build(): with html.p() as e: xsc.add(xml.Attrs(lang="en")) assert e == html.p(xml.Attrs(lang="en"))
def convert(self, converter): e = html.li(html.span(self[name], class_="name")) durations = xsc.Frag(self[duration]) rcs = xsc.Frag(self[rc]) if len(durations) or len(rcs): e.append(" (") if len(durations): e.append(durations[0]) if len(rcs): e.append("; ") if len(rcs): e.append("RC ", rcs.withsep(", ")) e.append(")") e.append(self[purchase]) return e.convert(converter)
def convert_fo(self, converter): # FIXME: handle title e = xsc.Frag() for child in self.content: if not isinstance(child, h): e.append(child) return e.convert(converter)
def convert(self, converter): context = converter[self] e = self.content converter.push(target=sys.modules[__name__]) # our own module e = e.convert(converter) converter.pop() converter.push(target=fo) e = e.convert(converter) converter.pop() e = xsc.Frag( xml.XML(), "\n", fo.root( fo.layout_master_set( fo.simple_page_master( fo.region_body( region_name="xsl-region-body", margin_bottom="3cm" ), fo.region_after( region_name="xsl-region-after", extent="2cm" ), master_name="default", page_height="29.7cm", page_width="21cm", margin_top="1cm", margin_bottom="1cm", margin_left="2.5cm", margin_right="1cm" ) ), fo.page_sequence( fo.static_content( fo.block( fo.page_number(), border_before_width="0.1pt", border_before_color="#000", border_before_style="solid", padding_before="4pt", text_align="center" ), flow_name="xsl-region-after" ), fo.flow( e, flow_name="xsl-region-body" ), master_reference="default" ), font_family=context.font, font_size="10pt", text_align="justify", line_height="normal", language="en", orphans=2, widows=3 ) ) return e
def test_expr(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): +detox.expr("arg") assert makeoutput(e, "gurk", "hurz") == "hurz"
def convert(self, converter): e = xsc.Frag( detox.while_(self.attrs.loop), self.content, detox.end("while") ) return e.convert(converter)
def test_text(): with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("foo") +detox.end("def") assert makeoutput(e, "gurk") == "foo"
def convert(self, converter): e = xsc.Frag( detox.def_(self.attrs.func), self.content, detox.end("def") ) return e.convert(converter)
def convert(self, converter): e = xsc.Frag( detox.for_(self.attrs.loop), self.content, detox.end("for") ) return e.convert(converter)
def sqlservercode(): return xsc.Frag( toxic.args("@search varchar(100)"), toxic.vars("declare @i integer;"), htmlspecials.plaintable( toxic.code(""" set @i = 1; declare @row_name varchar(100); declare person_cursor cursor for select name from person where name like @search open person_cursor while 1 = 1 begin fetch next from person_cursor into @row_name; if (@@fetch_status != 0) break """), html.tr(html.th(toxic.expr("@i"), align="right"), html.td(toxic.expr("schema.xmlescape(@row_name)"))), toxic.code(""" set @i = @i+1; end close person_cursor deallocate person_cursor """)))
def convert_html(self, converter): target = converter.target context = converter[self] context.sections[-1] += 1 level = len(context.sections) context.sections.append(0) # for numbering the subsections ts = xsc.Frag() cs = html.div(class_="content") for child in self: if isinstance(child, h): ts.append(child) else: cs.append(child) e = target.div(class_=("section level", level), id=self.attrs.id) if "role" in self.attrs: e.attrs.class_.append(" ", self.attrs.role) #if "id" in self.attrs: # e.append(target.a(name=self.attrs.id, id=self.attrs.id)) hclass = getattr(target, f"h{level}", target.h6) for t in ts: e.append(hclass(t.content)) e.append(cs) with _stack(context, self): # make sure to call the inner convert() before popping the number off of the stack e = e.convert(converter) del context.sections[-1] return e
def test_fraglen(): check_lenstr(xsc.Frag(), 0, "") check_lenstr(xsc.Frag(1), 1, "1") check_lenstr(xsc.Frag(1, 2, 3), 3, "123") check_lenstr(xsc.Frag(None), 0, "") check_lenstr(xsc.Frag(None, None, None), 0, "") check_lenstr(xsc.Frag(1, None, 2, None, 3, None, 4), 4, "1234") check_lenstr(xsc.Frag(1, (2, 3)), 3, "123") check_lenstr(xsc.Frag(1, (None, None)), 1, "1")
def test_getitem(): for cls in (xsc.Frag, html.div): e = cls(list(range(6))) # int assert e[2] == xsc.Text(2) assert e[-1] == xsc.Text(5) # slice assert e[:] == cls(list(range(6))) assert e[:2] == cls(0, 1) assert e[-2:] == cls(4, 5) assert e[::2] == cls(0, 2, 4) assert e[1::2] == cls(1, 3, 5) assert e[::-1] == cls(list(range(5, -1, -1))) # selector e = cls((html.dt(i), html.dd(2 * i)) for i in range(3)) assert xsc.Frag(e[html.dt]) == xsc.Frag(html.dt(0), html.dt(1), html.dt(2)) assert xsc.Frag(e[html.dt[1]]) == xsc.Frag(html.dt(1)) assert e[e[0]][0] is e[ 0] # selector for a single node (returns an iterator nevertheless) def isgt1(p): return int(str(p[-1])) > 1 assert xsc.Frag(e[html.dt & isgt1]) == xsc.Frag(html.dt(2)) assert xsc.Frag(e[e / html.dt]) == xsc.Frag(html.dt(0), html.dt(1), html.dt(2)) assert xsc.Frag(e[e.__class__ / html.dt]) == xsc.Frag( html.dt(0), html.dt(1), html.dt(2)) for attr in ("class_", xml.Attrs.lang): e = cls("foo", html.div("bar", {attr: "gurk"}), "baz") i = e[xsc.Text] assert str(next(i)) == "foo" assert str(next(i)) == "baz" with pytest.raises(StopIteration): next(i) # list for attr in ("class", xml.Attrs.lang): node = cls(html.div("foo", html.div("bar", {attr: "gurk"}), "baz")) assert node[[]] is node assert str(node[[0, 1]]) == "bar" assert str(node[[0, 1, attr]]) == "gurk"
def test_scopecheck(): with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("hurz") +detox.end() assert makeoutput(e, "gurk") == "hurz" with xsc.build(): with xsc.Frag() as e: +detox.def_("gurk()") +xsc.Text("hurz") +detox.end("for") with pytest.raises(SyntaxError): makeoutput(e, "gurk")
def test_for(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): with forblock(loop="i in range(arg)"): +detox.expr("str(i)") assert makeoutput(e, "gurk", 3) == "012"
def test_textexpr(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk()"): +detox.code("""s = '"a" < "b" & "b" > "a"'""") +detox.textexpr("s") assert makeoutput(e, "gurk") == '"a" < "b" & "b" > "a"'
def convert(self, converter): e = xsc.Frag() if "name" in self.attrs: e.append(html.meta(name="author", content=self.attrs["name"])) if "lang" in self.attrs: e[-1].attrs["lang"] = self["lang"] if "email" in self.attrs: e.append(html.link(rel="made", href=("mailto:", self["email"]))) return e.convert(converter)
def test_oracle_clobproc(): e = xsc.Frag(toxic.proc(), toxic.args("c_out out clob"), oraclecode()) tmpl = e.conv().string(encoding="ascii") sql = toxicc.compile(tmpl) print(sql) assert "dbms_lob.createtemporary" in sql assert "procedure write" in sql assert "c_out out clob" in sql
def test_while(): with xsc.build(): with xsc.Frag() as e: with defblock(func="gurk(arg)"): +detox.code("i = 0") with whileblock(loop="i < arg"): +detox.expr("str(i)") +detox.code("i += 1") assert makeoutput(e, "gurk", 3) == "012"
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_text(): s = "test" node = xsc.Text(s) hash(node) assert len(node), 4 assert node[1] == xsc.Text("e") assert 3 * node == xsc.Text(3 * s) assert node * 3 == xsc.Text(s * 3) assert node[1:3] == xsc.Text("es") assert node.capitalize() == xsc.Text("Test") assert node.center(8) == xsc.Text(" test ") assert node.count("t") == 2 assert node.endswith("st") is True assert node.index("s") == 2 assert node.isalpha() is True assert node.isalnum() is True assert node.isdecimal() is False assert node.isdigit() is False assert node.islower() is True assert node.isnumeric() is False assert node.isspace() is False assert node.istitle() is False assert node.isupper() is False assert node.join(xsc.Frag(list("abc"))) == xsc.Frag( "a", "test", "b", "test", "c") assert node.ljust(6) == xsc.Text("test ") assert node.ljust(6, ".") == xsc.Text("test..") assert node.lower() == xsc.Text("test") assert xsc.Text(" test").lstrip() == xsc.Text("test") assert node.replace("s", "x") == xsc.Text("text") assert node.rjust(6) == xsc.Text(" test") assert node.rjust(6, ".") == xsc.Text("..test") assert xsc.Text("test ").rstrip() == xsc.Text("test") assert node.rfind("s") == 2 assert node.rindex("s") == 2 assert node.split("e") == xsc.Frag("t", "st") assert xsc.Text("a\nb\n").splitlines() == xsc.Frag("a", "b") assert node.startswith("te") is True assert xsc.Text(" test ").strip() == xsc.Text("test") assert node.swapcase() == xsc.Text("TEST") assert node.title() == xsc.Text("Test") assert node.upper() == xsc.Text("TEST")
def test_sqlserver_clobproc(): e = xsc.Frag( toxic.proc(), toxic.args("@c_out varchar(max) output"), sqlservercode(), ) tmpl = e.conv().string(encoding="ascii") sql = toxicc.compile(tmpl, mode="sqlserver") assert "@c_out varchar(max) output" in sql assert "set @c_out = @c_out +" in sql
def test_sqlserver_varcharfunc(): e = xsc.Frag( toxic.type("varchar(20000)"), sqlservercode(), ) tmpl = e.conv().string(encoding="ascii") sql = toxicc.compile(tmpl, mode="sqlserver") assert "declare @c_out varchar(20000)" in sql assert "returns varchar(20000)\n" in sql assert "set @c_out = @c_out +" in sql
def test_pretty(): tests = [ (html.p("apple", "tree"), b"<p>appletree</p>"), (html.p("apple", html.br(), "tree"), b"<p>apple<br />tree</p>"), (html.p(php.php("apple")), b"<p>\n\t<?php apple?>\n</p>"), (html.p(php.php("apple"), "tree"), b"<p><?php apple?>tree</p>"), (html.div(2 * html.p("apple", "tree"), html.br()), b"<div>\n\t<p>appletree</p>\n\t<p>appletree</p>\n\t<br />\n</div>"), (html.div(php.php("apple"), html.p("apple", "tree"), html.div( html.p("apple"), html.p("tree"), ), html.br()), b"<div>\n\t<?php apple?>\n\t<p>appletree</p>\n\t<div>\n\t\t<p>apple</p>\n\t\t<p>tree</p>\n\t</div>\n\t<br />\n</div>" ), (html.ul( ul4.for_("name in names"), html.li(ul4.printx("name"), ), ul4.end("for"), ), b"<ul>\n\t<?for name in names?>\n\t\t<li>\n\t\t\t<?printx name?>\n\t\t</li>\n\t<?end for?>\n</ul>" ), (xsc.Frag( ul4.if_("n == 0"), html.span("zero"), ul4.elif_("n == 1"), html.span("one"), ul4.else_(), html.span("many"), ul4.end("if"), ), b"<?if n == 0?>\n\t<span>zero</span>\n<?elif n == 1?>\n\t<span>one</span>\n<?else ?>\n\t<span>many</span>\n<?end if?>" ), (xsc.Frag( ul4.def_("spam"), ul4.printx("eggs"), ul4.end("def"), ), b"<?def spam?>\n\t<?printx eggs?>\n<?end def?>"), ] for (got, exp) in tests: got.pretty().bytes() == exp