tweaks to pygment highlighting, integrated with docs
diff --git a/doc/build/content/defs.txt b/doc/build/content/defs.txt
index 4767303..aa97480 100644
--- a/doc/build/content/defs.txt
+++ b/doc/build/content/defs.txt
@@ -129,6 +129,7 @@
     
 This produces the output:
 
+    {html}
     <table>
         <tr><td>
             I am the table body.
@@ -149,6 +150,7 @@
     
 Produces:
     
+    {html}
     hi
     hi
     hi
@@ -158,7 +160,7 @@
     <%def name="conditional(expr)">
         % if expr:
             ${body()}
-        %
+        % endif
     </%def>
 
     <%call expr="conditional(4==4)">
@@ -167,6 +169,7 @@
 
 Produces:
 
+    {html}
     im the result
 
 But that's not all.  The `body()` function also can handle arguments, which will augment the local namespace of the body callable:
@@ -189,6 +192,7 @@
     
 Produces:
 
+    {html}
     <table>
         <tr>
             <td>Body data: 1</td><td>Body data: 2</td><td>Body data: 3</td>
diff --git a/doc/build/content/usage.txt b/doc/build/content/usage.txt
index e4624e8..062ae2d 100644
--- a/doc/build/content/usage.txt
+++ b/doc/build/content/usage.txt
@@ -5,6 +5,7 @@
 
 The most basic way to create a template and render it is through the `Template` class:
 
+    {python}
     from mako.template import Template
     
     mytemplate = Template("hello world!")
@@ -14,6 +15,7 @@
 
 The code inside the `render_body()` function has access to a namespace of variables.  You can specify these variables by sending them as additional keyword arguments to the `render()` method:
 
+    {python}
     from mako.template import Template
     
     mytemplate = Template("hello, ${name}!")
@@ -21,6 +23,7 @@
     
 The `template.render()` method calls upon Mako to create a `Context` object, which stores all the variable names accessible to the template and also defines a buffer used to capture output.  You can create this `Context` yourself and have the template render with it, using the `render_context` method:
 
+    {python}
     from mako.template import Template
     from mako.runtime import Context
     from StringIO import StringIO
@@ -35,6 +38,7 @@
 
 A `Template` can also load its template source code from a file, using the `filename` keyword argument:
 
+    {python}
     from mako.template import Template
     
     mytemplate = Template(filename='/docs/mytmpl.txt')
@@ -42,6 +46,7 @@
     
 For improved performance, a `Template` which is loaded from a file can also cache the source code to its generated module on the filesystem as a regular Python module file (i.e. a .py file).  To do this, just add the `module_directory` argument to the template:
 
+    {python}
     from mako.template import Template
 
     mytemplate = Template(filename='/docs/mytmpl.txt', module_directory='/tmp/mako_modules')
@@ -53,6 +58,7 @@
 
 All of the examples thus far have dealt with the usage of a single `Template` object.  If the code within those templates tries to locate another template resource, it will need some way to find them, using simple URI strings.  For this need, the resolution of other templates from within a template is accomplished by the `TemplateLookup` class.   This class is constructed given a list of directories in which to search for templates, as well as keyword arguments that will be passed to the `Template` objects it creates.
 
+    {python}
     from mako.template import Template
     from mako.lookup import TemplateLookup
     
@@ -63,6 +69,7 @@
 
 Usually, an application will store most or all of its templates as text files on the filesystem.  So far, all of our examples have been a little bit contrived in order to illustrate the basic concepts.  But a real application would get most or all of its templates directly from the `TemplateLookup`, using the aptly named `get_template` method, which accepts the URI of the desired template:
 
+    {python}
     from mako.template import Template
     from mako.lookup import TemplateLookup
     
@@ -80,6 +87,7 @@
 
 The `TemplateLookup` also serves the important need of caching a fixed set of templates in memory at a given time, so that successive uri lookups do not result in full template compilations and/or module reloads on each request.  By default, the `TemplateLookup` size is unbounded.  You can specify a fixed size using the `collection_size` argument:
 
+    {python}
     mylookup = TemplateLookup(directories=['/docs'], 
                     module_directory='/tmp/mako_modules', collection_size=500)
     
@@ -93,6 +101,7 @@
 
 Both `Template` and `TemplateLookup` accept an `output_encoding` parameter which can be used to encode the output in any Python supported codec:
 
+    {python}
     from mako.template import Template
     from mako.lookup import TemplateLookup
     
@@ -103,14 +112,17 @@
     
 Additionally, the `render_unicode()` method exists which will return the template output as a Python `unicode` object:
 
+    {python}
     print mytemplate.render_unicode()
     
 The above method disgards the output encoding keyword argument; you can encode yourself by saying:
 
+    {python}
     print mytemplate.render_unicode().encode('utf-8')
     
 The above methods deal only with the encoding of the template output.  To indicate an encoding for the template's source file, place a Python-style "magic encoding comment" as the very first line in the template file:
 
+    {python}
     # -*- coding: utf-8 -*-
 
     <template text>
@@ -119,5 +131,6 @@
 
 For the picky, the regular expression used is derived from that of [pep-0263](http://www.python.org/dev/peps/pep-0263/):
     
+    {python}
     #.*coding[:=]\s*([-\w.]+).*\n
 
diff --git a/doc/build/read_markdown.py b/doc/build/read_markdown.py
index 2c0f01f..097e071 100644
--- a/doc/build/read_markdown.py
+++ b/doc/build/read_markdown.py
@@ -131,7 +131,20 @@
     parents = get_parent_map(tree)
 
     for precode in tree.findall('.//pre/code'):
+        reg = re.compile(r'\{(python|mako|html)(?: title="(.*?)"){0,1}\}(.*)', re.S)
+        m = reg.match(precode[0].text.lstrip())
+        if m:
+            code = m.group(1)
+            title = m.group(2)
+            precode[0].text = m.group(3)
+        else:
+            code = title = None
+            
         tag = et.Element("MAKO:formatting.code")
+        if code:
+            tag.attrib["syntaxtype"] = repr(code)
+        if title:
+            tag.attrib["title"] = repr(title)
         tag.text = precode.text
         [tag.append(x) for x in precode]
         pre = parents[precode]
@@ -144,7 +157,7 @@
     parents = get_parent_map(tree)
     for code in tree.findall('.//code'):
         tag = et.Element('%text')
-        tag.attrib["filter"] = "h"
+        #tag.attrib["filter"] = "h"
         tag.text = code.text
         code.append(tag)
         code.text = ""
diff --git a/doc/build/templates/autohandler b/doc/build/templates/autohandler
index 7d62d82..7d22d4c 100644
--- a/doc/build/templates/autohandler
+++ b/doc/build/templates/autohandler
@@ -1,11 +1,10 @@
 <html>
 <head>
 	<title>${self.title()}</title>
-    <link href="style.css" rel="stylesheet" type="text/css"></link>
-    <link href="syntaxhighlight.css" rel="stylesheet" type="text/css"></link>
+	${self.style()}
+<%def name="style()">
+</%def>
 
-    <link href="docs.css" rel="stylesheet" type="text/css"></link>
-    <script src="scripts.js"></script>
 </head>
 <body>
 ${next.body()}
diff --git a/doc/build/templates/base.html b/doc/build/templates/base.html
index ac12cca..2a9ce1e 100644
--- a/doc/build/templates/base.html
+++ b/doc/build/templates/base.html
@@ -6,6 +6,7 @@
 
 <%def name="style()">
     <link rel="stylesheet" href="docs.css"></link>
+    <link rel="stylesheet" href="highlight.css"></link>
     ${parent.style()}
 </%def>
 
diff --git a/doc/build/templates/formatting.html b/doc/build/templates/formatting.html
index e3627fd..f187fa0 100644
--- a/doc/build/templates/formatting.html
+++ b/doc/build/templates/formatting.html
@@ -1,7 +1,6 @@
 # formatting.myt - Provides section formatting elements, syntax-highlighted code blocks, and other special filters.
 <%!
     import string, re
-    #import highlight
     from mako import filters
     
     def plainfilter(f):
@@ -53,9 +52,20 @@
     <span class="codeline">${ caller.body() }</span>
 </%def>
 
-<%def name="code(title=None, syntaxtype='python', html_escape=False, use_sliders=False)">
+<%def name="code(title=None, syntaxtype='mako', html_escape=False, use_sliders=False)">
+    <%!
+        import pygments
+        from pygments.formatters import HtmlFormatter
+        from pygments.lexers import PythonLexer, HtmlLexer
+        from mako.ext.pygmentplugin import MakoLexer
+        lexers = {'mako':MakoLexer(), 'python':PythonLexer(), 'html':HtmlLexer()}
+    %>
     <%
-        content = "<pre>" + capture(caller.body) + "</pre>"
+        lexer = lexers.get(syntaxtype, None)
+        if lexer is not None:
+            content = pygments.highlight(capture(caller.body) , lexer, HtmlFormatter())
+        else:
+            content = "<pre>" + capture(caller.body) + "</pre>"
     %>
 
     <div class="${ use_sliders and "sliding_code" or "code" }">
diff --git a/lib/mako/ext/pygmentplugin.py b/lib/mako/ext/pygmentplugin.py
index 0f2899c..20ff558 100644
--- a/lib/mako/ext/pygmentplugin.py
+++ b/lib/mako/ext/pygmentplugin.py
@@ -10,7 +10,7 @@
 from pygments.lexer import Lexer, DelegatingLexer, RegexLexer, bygroups, \
      include, using, this
 from pygments.token import Error, Punctuation, \
-     Text, Comment, Operator, Keyword, Name, String, Number, Other
+     Text, Comment, Operator, Keyword, Name, String, Number, Other, Literal
 from pygments.util import html_doctype_matches, looks_like_xml
 
 class MakoLexer(RegexLexer):
@@ -20,8 +20,8 @@
 
     tokens = {
         'root': [
-            (r'(\s*)(\%)(\s*endfor|endwhile|endif)(\n|\Z)',
-             bygroups(Text, Comment.Preproc, Name.Tag, Other)),
+            (r'(\s*)(\%)(\s*end(?:\w+))(\n|\Z)',
+             bygroups(Text, Comment.Preproc, Keyword, Other)),
             (r'(\s*)(\%)([^\n]*)(\n|\Z)',
              bygroups(Text, Comment.Preproc, using(PythonLexer), Other)),
             (r'(<%)(def|call)', bygroups(Comment.Preproc, Name.Builtin), 'tag'),
@@ -51,9 +51,9 @@
             include('tag'),
         ],
         'tag': [
-            (r'((?:name|expr)\s*=)\s*(")(.*?)(")',
-             bygroups(Name.Attribute, String, using(PythonLexer), String)),
-            (r'[a-zA-Z0-9_:-]+\s*=', Name.Attribute, 'attr'),
+            (r'((?:\w+)\s*=)\s*(".*?")',
+             bygroups(Name.Attribute, String)),
+            #(r'[a-zA-Z0-9_:-]+\s*=', Name.Attribute, 'attr'),
             (r'/?\s*>', Comment.Preproc, '#pop'),
             (r'\s+', Text),
         ],