[PATCH] highlight: Generate pygments style sheet dynamically

Isaac Jurado diptongo at gmail.com
Sat Apr 5 14:32:07 CDT 2008


# HG changeset patch
# User Isaac Jurado <diptongo at gmail.com>
# Date 1207423742 -7200
# Node ID 27cbb32e100b12ba825aa94c8fda341339e23952
# Parent  9c426da6b03b2f201e23107af5138d6b4a387428
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.

diff -r 9c426da6b03b -r 27cbb32e100b hgext/highlight.py
--- a/hgext/highlight.py	Fri Apr 04 16:39:44 2008 +0200
+++ b/hgext/highlight.py	Sat Apr 05 21:29:02 2008 +0200
@@ -15,10 +15,7 @@ There is a single configuration option:
 [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 at hupp.org>
 """
@@ -26,7 +23,7 @@ from mercurial import demandimport
 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,
 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 @@ def annotate_highlight(web, req, tmpl):
     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')
diff -r 9c426da6b03b -r 27cbb32e100b templates/static/highlight.css
--- a/templates/static/highlight.css	Fri Apr 04 16:39:44 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 */
diff -r 9c426da6b03b -r 27cbb32e100b tests/test-highlight
--- a/tests/test-highlight	Fri Apr 04 16:39:44 2008 +0200
+++ b/tests/test-highlight	Sat Apr 05 21:29:02 2008 +0200
@@ -5,6 +5,8 @@ cat <<EOF >> $HGRCPATH
 cat <<EOF >> $HGRCPATH
 [extensions]
 hgext.highlight =
+[web]
+pygments_style = friendly
 EOF
 
 hg init test
@@ -14,7 +16,6 @@ hg ci -Ama
 
 echo % hg serve
 hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
-cat hg.pid >> $DAEMON_PIDS
 
 echo % hgweb filerevision
 ("$TESTDIR/get-with-headers.py" localhost:$HGPORT '/file/tip/get-with-headers.py') \
@@ -24,6 +25,20 @@ echo % hgweb fileannotate
 ("$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') \
+    | sed '5,$ d'
+
+kill -TERM `cat hg.pid`
+perl -pi -e 's/friendly/fruity/' $HGRCPATH
+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') \
+    | sed '5,$ d'
+
 echo % errors encountered
 cat errors.log
 
diff -r 9c426da6b03b -r 27cbb32e100b tests/test-highlight.out
--- a/tests/test-highlight.out	Fri Apr 04 16:39:44 2008 +0200
+++ b/tests/test-highlight.out	Sat Apr 05 21:29:02 2008 +0200
@@ -10,7 +10,7 @@ 200 Script output follows
 <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 @@ 200 Script output follows
 <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,15 @@ 200 Script output follows
 </body>
 </html>
 
+% hgweb highlightcss friendly
+200 Script output follows
+
+/* pygments_style = friendly */
+
+% hg serve again
+% hgweb highlightcss fruity
+200 Script output follows
+
+/* pygments_style = fruity */
+
 % errors encountered


More information about the Mercurial-devel mailing list