highlight: Generate pygments style sheet dynamically
authorIsaac Jurado <diptongo@gmail.com>
Sat, 05 Apr 2008 21:29:02 +0200
changeset 6485 938319418d8c
parent 6484 ab8038bf5127
child 6490 b13dae8ed779
highlight: Generate pygments style sheet dynamically This patch allows a per-repository (for example, within a hgwebdir) selection of pygments_style web option. No static .css files required. Test edited by pmezard (compatibility fixes)
hgext/highlight.py
templates/static/highlight.css
tests/test-highlight
tests/test-highlight.out
--- a/hgext/highlight.py	Sat Apr 05 19:33:34 2008 +0200
+++ b/hgext/highlight.py	Sat Apr 05 21:29:02 2008 +0200
@@ -15,10 +15,7 @@
 [web]
 pygments_style = <style>
 
-The default is 'colorful'.  If this is changed the corresponding CSS
-file should be re-generated by running
-
-# pygmentize -f html -S <newstyle>
+The default is 'colorful'.
 
 -- Adam Hupp <adam@hupp.org>
 """
@@ -26,7 +23,7 @@
 from mercurial import demandimport
 demandimport.ignore.extend(['pkgutil', 'pkg_resources', '__main__',])
 
-from mercurial.hgweb import webcommands, webutil
+from mercurial.hgweb import webcommands, webutil, common
 from mercurial import util
 from mercurial.templatefilters import filters
 
@@ -35,7 +32,7 @@
 from pygments.lexers import guess_lexer, guess_lexer_for_filename, TextLexer
 from pygments.formatters import HtmlFormatter
 
-SYNTAX_CSS = ('\n<link rel="stylesheet" href="#staticurl#highlight.css" '
+SYNTAX_CSS = ('\n<link rel="stylesheet" href="{url}highlightcss" '
               'type="text/css" />')
 
 def pygmentize(field, fctx, style, tmpl):
@@ -88,7 +85,16 @@
     pygmentize('annotateline', fctx, style, tmpl)
     return web_annotate(web, req, tmpl)
 
+def generate_css(web, req, tmpl):
+    pg_style = web.config('web', 'pygments_style', 'colorful')
+    fmter = HtmlFormatter(style = pg_style)
+    req.respond(common.HTTP_OK, 'text/css')
+    return ['/* pygments_style = %s */\n\n' % pg_style, fmter.get_style_defs('')]
+
+
 # monkeypatch in the new version
 
 webcommands._filerevision = filerevision_highlight
 webcommands.annotate = annotate_highlight
+webcommands.highlightcss = generate_css
+webcommands.__all__.append('highlightcss')
--- a/templates/static/highlight.css	Sat Apr 05 19:33:34 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-.c { color: #808080 } /* Comment */
-.err { color: #F00000; background-color: #F0A0A0 } /* Error */
-.k { color: #008000; font-weight: bold } /* Keyword */
-.o { color: #303030 } /* Operator */
-.cm { color: #808080 } /* Comment.Multiline */
-.cp { color: #507090 } /* Comment.Preproc */
-.c1 { color: #808080 } /* Comment.Single */
-.cs { color: #cc0000; font-weight: bold } /* Comment.Special */
-.gd { color: #A00000 } /* Generic.Deleted */
-.ge { font-style: italic } /* Generic.Emph */
-.gr { color: #FF0000 } /* Generic.Error */
-.gh { color: #000080; font-weight: bold } /* Generic.Heading */
-.gi { color: #00A000 } /* Generic.Inserted */
-.go { color: #808080 } /* Generic.Output */
-.gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
-.gs { font-weight: bold } /* Generic.Strong */
-.gu { color: #800080; font-weight: bold } /* Generic.Subheading */
-.gt { color: #0040D0 } /* Generic.Traceback */
-.kc { color: #008000; font-weight: bold } /* Keyword.Constant */
-.kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
-.kp { color: #003080; font-weight: bold } /* Keyword.Pseudo */
-.kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
-.kt { color: #303090; font-weight: bold } /* Keyword.Type */
-.m { color: #6000E0; font-weight: bold } /* Literal.Number */
-.s { background-color: #fff0f0 } /* Literal.String */
-.na { color: #0000C0 } /* Name.Attribute */
-.nb { color: #007020 } /* Name.Builtin */
-.nc { color: #B00060; font-weight: bold } /* Name.Class */
-.no { color: #003060; font-weight: bold } /* Name.Constant */
-.nd { color: #505050; font-weight: bold } /* Name.Decorator */
-.ni { color: #800000; font-weight: bold } /* Name.Entity */
-.ne { color: #F00000; font-weight: bold } /* Name.Exception */
-.nf { color: #0060B0; font-weight: bold } /* Name.Function */
-.nl { color: #907000; font-weight: bold } /* Name.Label */
-.nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
-.nt { color: #007000 } /* Name.Tag */
-.nv { color: #906030 } /* Name.Variable */
-.ow { color: #000000; font-weight: bold } /* Operator.Word */
-.w { color: #bbbbbb } /* Text.Whitespace */
-.mf { color: #6000E0; font-weight: bold } /* Literal.Number.Float */
-.mh { color: #005080; font-weight: bold } /* Literal.Number.Hex */
-.mi { color: #0000D0; font-weight: bold } /* Literal.Number.Integer */
-.mo { color: #4000E0; font-weight: bold } /* Literal.Number.Oct */
-.sb { background-color: #fff0f0 } /* Literal.String.Backtick */
-.sc { color: #0040D0 } /* Literal.String.Char */
-.sd { color: #D04020 } /* Literal.String.Doc */
-.s2 { background-color: #fff0f0 } /* Literal.String.Double */
-.se { color: #606060; font-weight: bold; background-color: #fff0f0 } /* Literal.String.Escape */
-.sh { background-color: #fff0f0 } /* Literal.String.Heredoc */
-.si { background-color: #e0e0e0 } /* Literal.String.Interpol */
-.sx { color: #D02000; background-color: #fff0f0 } /* Literal.String.Other */
-.sr { color: #000000; background-color: #fff0ff } /* Literal.String.Regex */
-.s1 { background-color: #fff0f0 } /* Literal.String.Single */
-.ss { color: #A06000 } /* Literal.String.Symbol */
-.bp { color: #007020 } /* Name.Builtin.Pseudo */
-.vc { color: #306090 } /* Name.Variable.Class */
-.vg { color: #d07000; font-weight: bold } /* Name.Variable.Global */
-.vi { color: #3030B0 } /* Name.Variable.Instance */
-.il { color: #0000D0; font-weight: bold } /* Literal.Number.Integer.Long */
--- a/tests/test-highlight	Sat Apr 05 19:33:34 2008 +0200
+++ b/tests/test-highlight	Sat Apr 05 21:29:02 2008 +0200
@@ -5,6 +5,8 @@
 cat <<EOF >> $HGRCPATH
 [extensions]
 hgext.highlight =
+[web]
+pygments_style = friendly
 EOF
 
 hg init test
@@ -24,6 +26,28 @@
 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/annotate/tip/get-with-headers.py') \
     | sed "s/[0-9]* years ago/long ago/g"
 
+echo % hgweb highlightcss friendly
+("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss') \
+    | head -n 4
+
+echo % errors encountered
+cat errors.log
+kill `cat hg.pid`
+
+# Change the pygments style
+cat > .hg/hgrc <<EOF
+[web]
+pygments_style = fruity
+EOF
+
+echo % hg serve again
+hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
+cat hg.pid >> $DAEMON_PIDS
+
+echo % hgweb highlightcss fruity
+("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/highlightcss') \
+    | head -n 4
+
 echo % errors encountered
 cat errors.log
 
--- a/tests/test-highlight.out	Sat Apr 05 19:33:34 2008 +0200
+++ b/tests/test-highlight.out	Sat Apr 05 21:29:02 2008 +0200
@@ -10,7 +10,7 @@
 <meta name="robots" content="index, nofollow" />
 <link rel="stylesheet" href="/static/style.css" type="text/css" />
 
-<link rel="stylesheet" href="/static/highlight.css" type="text/css" />
+<link rel="stylesheet" href="/highlightcss" type="text/css" />
 <title>test:get-with-headers.py</title>
 </head>
 <body>
@@ -72,7 +72,7 @@
 <meta name="robots" content="index, nofollow" />
 <link rel="stylesheet" href="/static/style.css" type="text/css" />
 
-<link rel="stylesheet" href="/static/highlight.css" type="text/css" />
+<link rel="stylesheet" href="/highlightcss" type="text/css" />
 <title>test: get-with-headers.py annotate</title>
 </head>
 <body>
@@ -126,4 +126,16 @@
 </body>
 </html>
 
+% hgweb highlightcss friendly
+200 Script output follows
+
+/* pygments_style = friendly */
+
 % errors encountered
+% hg serve again
+% hgweb highlightcss fruity
+200 Script output follows
+
+/* pygments_style = fruity */
+
+% errors encountered