| 1 | # Script for viewing text IRC logs in a pretty HTML format |
|---|
| 2 | # Also browses the log directory. |
|---|
| 3 | # Files that don't end in .log will not be recognised. |
|---|
| 4 | # Log files must be written in a format pygments understands. |
|---|
| 5 | |
|---|
| 6 | # Requirements: pygments, jinja, web.py |
|---|
| 7 | |
|---|
| 8 | # Put or symlink log directory structure under 'logs' in the same |
|---|
| 9 | # directory as this file. |
|---|
| 10 | |
|---|
| 11 | # Copyright 2010 Adrianna Pinska |
|---|
| 12 | # Released under terms of the MIT/X/Expat Licence. |
|---|
| 13 | |
|---|
| 14 | import web |
|---|
| 15 | import os |
|---|
| 16 | |
|---|
| 17 | from pygments import highlight |
|---|
| 18 | from pygments.lexers import IrcLogsLexer |
|---|
| 19 | from pygments.formatters import HtmlFormatter |
|---|
| 20 | |
|---|
| 21 | from jinja import from_string |
|---|
| 22 | |
|---|
| 23 | urls = ( |
|---|
| 24 | '/style.css', 'style', |
|---|
| 25 | '/(.*\.log)', 'logfile', |
|---|
| 26 | '/(.*[^/])', 'redirect', |
|---|
| 27 | '/', 'redirect_log_root', |
|---|
| 28 | '/(.*)', 'directory', |
|---|
| 29 | ) |
|---|
| 30 | |
|---|
| 31 | lexer = IrcLogsLexer() |
|---|
| 32 | formatter = HtmlFormatter() |
|---|
| 33 | |
|---|
| 34 | html = """ |
|---|
| 35 | <html> |
|---|
| 36 | <head> |
|---|
| 37 | <title>{{ title }}</title> |
|---|
| 38 | {% for item in head %} |
|---|
| 39 | {{ item }} |
|---|
| 40 | {% endfor %} |
|---|
| 41 | </head> |
|---|
| 42 | <body> |
|---|
| 43 | {% for item in contents %} |
|---|
| 44 | {{ item }} |
|---|
| 45 | {% endfor %} |
|---|
| 46 | </body> |
|---|
| 47 | </html> |
|---|
| 48 | """ |
|---|
| 49 | |
|---|
| 50 | def escape_hash(s): |
|---|
| 51 | return s.replace('#', '%23') |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | class style: |
|---|
| 55 | def GET(self): |
|---|
| 56 | return formatter.get_style_defs('.highlight') |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | class logfile: |
|---|
| 60 | def GET(self, path): |
|---|
| 61 | f = open(os.path.join(os.path.dirname(__file__), path), "r") |
|---|
| 62 | text = f.read() |
|---|
| 63 | f.close() |
|---|
| 64 | |
|---|
| 65 | title = '<h1>%s</h1>' % path |
|---|
| 66 | logs = highlight(text, lexer, formatter) |
|---|
| 67 | css = '<link rel="stylesheet" type="text/css" href="/%s/style.css"/>' % os.path.dirname(__file__).split('/')[-1] |
|---|
| 68 | end_anchor = '<a name="end"></a>' |
|---|
| 69 | goto_end = '<p><a href="#end">Go to newest...</a></p>' |
|---|
| 70 | logs_directory = '<p><a href="./"><-- parent directory ---</a></p>' |
|---|
| 71 | |
|---|
| 72 | return from_string(html).render(title=path, head=[css], contents=[title, goto_end, logs, end_anchor, logs_directory]) |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | class redirect: |
|---|
| 76 | def GET(self, path): |
|---|
| 77 | web.seeother("/%s/" % escape_hash(path)) |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | class redirect_log_root: |
|---|
| 81 | def GET(self): |
|---|
| 82 | web.seeother("/logs/") |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | class directory: |
|---|
| 86 | def GET(self, path): |
|---|
| 87 | if not os.path.isdir(os.path.join(os.path.dirname(__file__), path)): |
|---|
| 88 | raise web.notfound() |
|---|
| 89 | ls = os.listdir(os.path.join(os.path.dirname(__file__), path)) |
|---|
| 90 | |
|---|
| 91 | title = '<h1>%s</h1>' % path |
|---|
| 92 | links = "<br/>\n".join(['<a href="%s">%s</a>' % (escape_hash(p), p) for p in ls]) |
|---|
| 93 | up_directory = '<p><a href="../"><-- parent directory ---</a></p>' |
|---|
| 94 | |
|---|
| 95 | return from_string(html).render(title=path, contents=[title, links, up_directory]) |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | application = web.application(urls, globals()).wsgifunc() |
|---|