Thu Aug 7 05:31:41 UTC 2008 Alexandre Rossi * optional source headblob syntax highlighting using python-pygments diff -rN -u old-darcsweb/darcsweb.cgi new-darcsweb/darcsweb.cgi --- old-darcsweb/darcsweb.cgi 2015-10-10 08:33:11.000000000 +0000 +++ new-darcsweb/darcsweb.cgi 2015-10-10 08:33:11.000000000 +0000 @@ -19,6 +19,13 @@ import urllib import xml.sax from xml.sax.saxutils import escape as xml_escape +try: + import pygments + import pygments.lexers + import pygments.formatters +except ImportError: + pygments = False + time_imports = time.time() - time_begin iso_datetime = '%Y-%m-%dT%H:%M:%SZ' @@ -1262,14 +1269,32 @@ def print_blob(fname): print '
%s
' % escape(fname) - print '
' if isbinary(fname): print """ +
This is a binary file and its contents will not be displayed.
""" return + if not pygments: + print_blob_simple(fname) + return + else: + try: + print_blob_highlighted(fname) + except ValueError: + # pygments couldn't guess a lexer to highlight the code, try + # another method with sampling the file contents. + try: + print_blob_highlighted(fname, sample_code=True) + except ValueError: + # pygments really could not find any lexer for this file. + print_blob_simple(fname) + +def print_blob_simple(fname): + print '
' + f = open(realpath(fname), 'r') count = 1 for l in f: @@ -1289,6 +1314,18 @@ count += 1 print '
' +def print_blob_highlighted(fname, sample_code=False): + code = open(realpath(fname), 'r').read() + if sample_code: + lexer = pygments.lexers.guess_lexer(code[:200], + encoding=config.repoencoding[0]) + else: + lexer = pygments.lexers.guess_lexer_for_filename(fname, code[:200], + encoding=config.repoencoding[0]) + formatter = pygments.formatters.HtmlFormatter(linenos=True, + cssclass='page_body') + print pygments.highlight(code, lexer, formatter) + def print_annotate(ann, style): print '
' if isbinary(ann.fname): diff -rN -u old-darcsweb/style.css new-darcsweb/style.css --- old-darcsweb/style.css 2015-10-10 08:33:11.000000000 +0000 +++ new-darcsweb/style.css 2015-10-10 08:33:11.000000000 +0000 @@ -220,7 +220,7 @@ right:12px; } -a.linenr { +a.linenr, .linenos { color:#999999; text-decoration:none; } @@ -262,3 +262,70 @@ margin-right:5pt; } + +/* Syntax highlighting related styles. This is highly dependent on what + * python-pygments generates. + * This was generated using the following commands in a python interpreter and + * slightly modified after. + * >>> import pygments + * >>> pygments.formatters.HtmlFormatter().get_style_defs('.page_body') + */ +.page_body table { margin: 0; padding: 0;} +.page_body pre { margin : 0; padding: 0; } +.page_body .c { color: #008800; font-style: italic } /* Comment */ +.page_body .err { border: 1px solid #FF0000 } /* Error */ +.page_body .k { color: #AA22FF; font-weight: bold } /* Keyword */ +.page_body .o { color: #666666 } /* Operator */ +.page_body .cm { color: #008800; font-style: italic } /* Comment.Multiline */ +.page_body .cp { color: #008800 } /* Comment.Preproc */ +.page_body .c1 { color: #008800; font-style: italic } /* Comment.Single */ +.page_body .gd { color: #A00000 } /* Generic.Deleted */ +.page_body .ge { font-style: italic } /* Generic.Emph */ +.page_body .gr { color: #FF0000 } /* Generic.Error */ +.page_body .gh { color: #000080; font-weight: bold } /* Generic.Heading */ +.page_body .gi { color: #00A000 } /* Generic.Inserted */ +.page_body .go { color: #808080 } /* Generic.Output */ +.page_body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ +.page_body .gs { font-weight: bold } /* Generic.Strong */ +.page_body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +.page_body .gt { color: #0040D0 } /* Generic.Traceback */ +.page_body .kc { color: #AA22FF; font-weight: bold } /* Keyword.Constant */ +.page_body .kd { color: #AA22FF; font-weight: bold } /* Keyword.Declaration */ +.page_body .kp { color: #AA22FF } /* Keyword.Pseudo */ +.page_body .kr { color: #AA22FF; font-weight: bold } /* Keyword.Reserved */ +.page_body .kt { color: #AA22FF; font-weight: bold } /* Keyword.Type */ +.page_body .m { color: #666666 } /* Literal.Number */ +.page_body .s { color: #BB4444 } /* Literal.String */ +.page_body .na { color: #BB4444 } /* Name.Attribute */ +.page_body .nb { color: #AA22FF } /* Name.Builtin */ +.page_body .nc { color: #0000FF } /* Name.Class */ +.page_body .no { color: #880000 } /* Name.Constant */ +.page_body .nd { color: #AA22FF } /* Name.Decorator */ +.page_body .ni { color: #999999; font-weight: bold } /* Name.Entity */ +.page_body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ +.page_body .nf { color: #00A000 } /* Name.Function */ +.page_body .nl { color: #A0A000 } /* Name.Label */ +.page_body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ +.page_body .nt { color: #008000; font-weight: bold } /* Name.Tag */ +.page_body .nv { color: #B8860B } /* Name.Variable */ +.page_body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ +.page_body .mf { color: #666666 } /* Literal.Number.Float */ +.page_body .mh { color: #666666 } /* Literal.Number.Hex */ +.page_body .mi { color: #666666 } /* Literal.Number.Integer */ +.page_body .mo { color: #666666 } /* Literal.Number.Oct */ +.page_body .sb { color: #BB4444 } /* Literal.String.Backtick */ +.page_body .sc { color: #BB4444 } /* Literal.String.Char */ +.page_body .sd { color: #BB4444; font-style: italic } /* Literal.String.Doc */ +.page_body .s2 { color: #BB4444 } /* Literal.String.Double */ +.page_body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ +.page_body .sh { color: #BB4444 } /* Literal.String.Heredoc */ +.page_body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ +.page_body .sx { color: #008000 } /* Literal.String.Other */ +.page_body .sr { color: #BB6688 } /* Literal.String.Regex */ +.page_body .s1 { color: #BB4444 } /* Literal.String.Single */ +.page_body .ss { color: #B8860B } /* Literal.String.Symbol */ +.page_body .bp { color: #AA22FF } /* Name.Builtin.Pseudo */ +.page_body .vc { color: #B8860B } /* Name.Variable.Class */ +.page_body .vg { color: #B8860B } /* Name.Variable.Global */ +.page_body .vi { color: #B8860B } /* Name.Variable.Instance */ +.page_body .il { color: #666666 } /* Literal.Number.Integer.Long */