<?xml version='1.0' encoding='iso-8859-1'?>

<section><title>Creating &html;</title>

<par>You can create and output &html; like this:</par>

<prog>
from ll.xist import xsc
from ll.xist.ns import html, xml, meta

node = xsc.Frag(
	xml.XML10(),
	html.DocTypeXHTML10transitional(),
	html.html(
		html.head(
			meta.contenttype(),
			html.title("Example page")
		),
		html.body(
			html.h1("Welcome to the example page"),
			html.p(
				"This example page has a link to the ",
				html.a("Python home page", href="http://www.python.org/"),
				"."
			)
		)
	)
)

print node.conv().asBytes(encoding="us-ascii")
</prog>

</section>


<section><title>Defining new elements</title>

<par>You can define new elements and how they should be converted to &html;
(or other &xml; vocabularies) like this:</par>

<prog>
from ll.xist import xsc
from ll.xist.ns import html, xml, meta

class pypilink(xsc.Element):
	class Attrs(xsc.Element.Attrs):
		class name(xsc.TextAttr): pass

	def convert(self, converter):
		e = html.a(
			self["name"],
			href=("http://www.python.org/pypi?:action=display&amp;name=", self["name"])
		)
		return e.convert(converter)

names = ["ll-xist", "cx_Oracle", "PIL"]

node = xsc.Frag(
	xml.XML10(),
	html.DocTypeXHTML10transitional(),
	html.html(
		html.head(
			meta.contenttype(),
			html.title("PyPI links")
		),
		html.body(
			html.h1("PyPI links"),
			html.ul(html.li(pypilink(name=name)) for name in names)
		)
	)
)

print node.conv().asBytes(encoding="us-ascii")
</prog>

</section>


<section><title>Parsing &html;</title>

<par>Parsing &html; is done like this:</par>

<prog>
from ll.xist import parsers

node = parsers.parseURL("http://www.python.org/", tidy=True)
</prog>p
u
</section>


<section><title>Finding and counting nodes</title>

<par>The following example shows you how to output the &url;s of all images
inside links on Python's homepage:</par>

<tty>
<prompt>&gt;&gt;&gt; </prompt><input>from ll.xist import parsers</input>
<prompt>&gt;&gt;&gt; </prompt><input>from ll.xist.ns import html</input>
<prompt>&gt;&gt;&gt; </prompt><input>node = parsers.parseURL("http://www.python.org/", utidy=True)</input>
<prompt>&gt;&gt;&gt; </prompt><input>for img in node//html.a/html.img:</input>
<prompt>... </prompt><input>   print img["src"]</input>
<prompt>... </prompt><input></input>
http://www.python.org/images/python-logo.gif
http://www.python.org/images/trans.gif
http://www.python.org/images/trans.gif
http://www.python.org/images/success/nasa.jpg
</tty>

<par>If you want to output both the links and the image &url;s, do the
following:</par>

<tty>
<prompt>&gt;&gt;&gt; </prompt><input>from ll.xist import parsers, xfind</input>
<prompt>&gt;&gt;&gt; </prompt><input>from ll.xist.ns import html</input>
<prompt>&gt;&gt;&gt; </prompt><input>node = parsers.parseURL("http://www.python.org/", tidy=True)</input>
<prompt>&gt;&gt;&gt; </prompt><input>for link in node//html.a/xfind.contains(html.img):</input>
<prompt>... </prompt><input>   print link["href"], str((link/html.img)[0]["src"])</input>
http://www.python.org/ http://www.python.org/images/python-logo.gif
http://www.python.org/#left%2dhand%2dnavigation http://www.python.org/images/trans.gif
http://www.python.org/#content%2dbody http://www.python.org/images/trans.gif
http://www.python.org/about/success/usa http://www.python.org/images/success/nasa.jpg
</tty>

<par>If you want to count the number of links on the page you can do the
following:</par>

<tty>
<prompt>&gt;&gt;&gt; </prompt><input>from ll import misc</input>
<prompt>&gt;&gt;&gt; </prompt><input>from ll.xist import parsers</input>
<prompt>&gt;&gt;&gt; </prompt><input>from ll.xist.ns import html</input>
<prompt>&gt;&gt;&gt; </prompt><input>node = parsers.parseURL("http://www.python.org/", tidy=True)</input>
<prompt>&gt;&gt;&gt; </prompt><input>misc.count(node//html.a)</input>
90
</tty>

</section>


<section><title>Replacing text</title>

<par>This example demonstrates how to make a copy of an &xml; tree with some
text replacements:</par>

<prog>
from ll.xist import xsc, parsers

def p2p(node, converter):
	if isinstance(node, xsc.Text):
		node = node.replace("Python", "Parrot")
		node = node.replace("python", "parrot")
	return node

node = parsers.parseURL("http://www.python.org/", tidy=True)

node = node.mapped(p2p)
node.write(open("parrot_index.html", "wb"))
</prog>

</section>


<section><title>Converting &html; to &xist; code</title>

<par>The class <pyref module="ll.xist.presenters" class="CodePresenter"><class>ll.xist.presenters.CodePresenter</class></pyref>
makes it possible to output an &xist; tree as usable Python source code:</par>

<tty>
<prompt>&gt;&gt;&gt;</prompt> <input>from ll.xist import parsers, presenters</input>
<prompt>&gt;&gt;&gt;</prompt> <input>e = parsers.parseURL("http://www.python.org/", tidy=True)</input>
<prompt>&gt;&gt;&gt;</prompt> <input>print e.asrepr(presenters.CodePresenter())</input>
ll.xist.xsc.Frag(
	ll.xist.ns.html.html(
		xsc.Comment(' THIS PAGE IS AUTOMATICALLY GENERATED.  DO NOT EDIT. '),
		xsc.Comment(' Thu Jan 27 12:33:38 2005 '),
		xsc.Comment(' USING HT2HTML 2.0 '),
		xsc.Comment(' SEE http://ht2html.sf.net '),
		xsc.Comment(' User-specified headers:\nTitle: Python Programming Language\n\n'),
		ll.xist.ns.html.head(
			ll.xist.ns.html.title(
				'Python Programming Language'
			),
			ll.xist.ns.html.meta(
				content='text/html; charset=iso-8859-1',
				http_equiv='Content-Type'
			),
			ll.xist.ns.html.meta(
				content='HT2HTML/2.0',
				name='generator'
			),
			ll.xist.ns.html.meta(
				content='Python programming language object oriented web free\n source',
				name='keywords'
			),
			<rep>[... Many lines deleted ...]</rep>
			xsc.Comment(' end of page table '),
			marginheight=0,
			alink='#ff0000',
			text='#000000',
			bgcolor='#ffffff',
			link='#0000bb',
			marginwidth=0,
			vlink='#551a8b'
		)
	)
)
</tty>
</section>


<section><title>Using converter contexts to pass information between elements</title>

<par>Converter contexts can be used to pass information between elements.
The following example will generate &html; <lit>&lt;h1&gt;</lit>, ..., <lit>&lt;h6&gt;</lit>
elements according to the nesting depth of a <lit>&lt;section&gt;</lit> element.</par>

<prog>
from ll.xist import xsc

class section(xsc.Element):
	class Attrs(xsc.Element.Attrs):
		class title(xsc.TextAttr): pass

	class Context(xsc.Element.Context):
		def __init__(self):
			xsc.Element.Context.__init__(self)
			self.level = 1

	def convert(self, converter):
		context = converter[self]
		elementname = "h%d" % min(context.level, 6)
		node = xsc.Frag(
			converter.target.element(elementname)(self["title"]),
			self.content
		)
		context.level += 1
		node = node.convert(converter)
		context.level -= 1
		return node

document = section(title="Python Tutorial")(
	section(title="Using the Python Interpreter")(
		section(title="Invoking the Interpreter")(
			section(title="Argument Passing"),
			section(title="Interactive Mode")
		),
		section(title="The Interpreter and Its Environment")(
			section(title="Error Handling"),
			section(title="Executable Python Scripts"),
			section(title="Source Code Encoding"),
			section(title="The Interactive Startup File")
		)
	)
)

print document.conv().asBytes()
</prog>

<par>The output of this script will be:</par>

<tty>
&lt;h1&gt;Python Tutorial&lt;/h1&gt;
&lt;h2&gt;Using the Python Interpreter&lt;/h2&gt;
&lt;h3&gt;Invoking the Interpreter&lt;/h3&gt;
&lt;h4&gt;Argument Passing&lt;/h4&gt;
&lt;h4&gt;Interactive Mode&lt;/h4&gt;
&lt;h3&gt;The Interpreter and Its Environment&lt;/h3&gt;
&lt;h4&gt;Error Handling&lt;/h4&gt;
&lt;h4&gt;Executable Python Scripts&lt;/h4&gt;
&lt;h4&gt;Source Code Encoding&lt;/h4&gt;
&lt;h4&gt;The Interactive Startup File&lt;/h4&gt;
</tty>

</section>
