# HG changeset patch # User jake@edge2.net # Date 1116031690 25200 # Node ID 526722d24ee5b3b860d4060e008219e083488356 # Parent db5eb6a86179ce819db03da9ef2090b32f8e3fc4 reorganize code into classes clean up html code for w3c validation diff -r db5eb6a86179 -r 526722d24ee5 hgweb.py --- a/hgweb.py Fri May 13 17:27:14 2005 -0700 +++ b/hgweb.py Fri May 13 17:48:10 2005 -0700 @@ -24,151 +24,177 @@ l.append('&#%d;' % ord(c)) return ''.join(l) -def httphdr(type = "text/html"): +def httphdr(type): print 'Content-type: %s\n' % type -def htmldoctype(): - print '' +class page: + def __init__(self, type="text/html", title="Mercurial Web", + charset="ISO-8859-1"): + print 'Content-type: %s; charset=%s\n' % (type, charset) + print '' + print '' + print '' + print '%s' % title + print '' + print '' + print '' -def htmlhead(title): - print '' - print '' - print '%s' % (title, ) - print '' - -def startpage(title): - httphdr() - htmldoctype() - htmlhead(title) - print '' - -def endpage(): - print '' - print '' - - + def endpage(self): + print '' + print '' -def ent_change(repo, nodeid, changes): - hn = hg.hex(nodeid) - i = repo.changelog.rev(nodeid) - (h1, h2) = [ hg.hex(x) for x in repo.changelog.parents(nodeid) ] - datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0]))) - print '' - print '\t' + \ - '' % (obfuscate(changes[1]), ) - print '\t\t' + \ - '' % \ - (hn, nl2br(changes[4]), ) - print '\t' % (datestr, ) - print '\t\t' - print '\t' % (hn, hn, ) - print '
author:%sdescription:' + \ - '%s
date:%s UTCfiles:' - for f in changes[3]: - print '\t\t%s  ' % f - print '\t
revision:%d:%s

' + def show_diff(self, a, b, fn): + a = a.splitlines(1) + b = b.splitlines(1) + l = difflib.unified_diff(a, b, fn, fn) + print '
'
+        for line in l:
+            line = cgi.escape(line[:-1])
+            if line.startswith('+'):
+                print '%s' % (line, )
+            elif line.startswith('-'):
+                print '%s' % (line, )
+            elif line.startswith('@'):
+                print '%s' % (line, )
+            else:
+                print line
+        print '
' -def ent_diff(a, b, fn): - a = a.splitlines(1) - b = b.splitlines(1) - l = difflib.unified_diff(a, b, fn, fn) - print '
'
-    for line in l:
-        line = cgi.escape(line[:-1])
-        if line.startswith('+'):
-            print '%s' % (line, )
-        elif line.startswith('-'):
-            print '%s' % (line, )
-        elif line.startswith('@'):
-            print '%s' % (line, )
-        else:
-            print line
-    print '
' +class errpage(page): + def __init__(self): + page.__init__(self, title="Mercurial Web Error Page") + +class change_list(page): + def __init__(self, repo, reponame): + page.__init__(self) + self.repo = repo + print '

Changes For: %s

' % reponame -def ent_checkin(repo, nodeid): - startpage("Mercurial Web") + def content(self, start='tip', end='0', boundtype='rev'): + print '' + cl = [] + for i in xrange(self.repo.changelog.count()): + n = self.repo.changelog.node(i) + cl.append((n, self.repo.changelog.read(n))) + cl.reverse() + for n, ch in cl: + print '' + + print '
' + self.change_table(n, ch) + print '
' - changes = repo.changelog.read(nodeid) - hn = hg.hex(nodeid) - i = repo.changelog.rev(nodeid) - parents = repo.changelog.parents(nodeid) - (h1, h2) = [ hg.hex(x) for x in parents ] - (i1, i2) = [ repo.changelog.rev(x) for x in parents ] - datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0]))) - mf = repo.manifest.read(changes[0]) - print '' - print '\t' % (hn, hn, ) - print '\t' - print '\t' % \ - (hg.hex(changes[0]), hg.hex(changes[0]), ) - print '\t' + \ - '' % (obfuscate(changes[1]), ) - print '\t\t' + \ - '' % \ - (hn, nl2br(changes[4]), ) - print '\t' % (datestr, ) - print '\t\t' - print '
revision:%d:' % (i, ), - print '%s
parent(s):%d:' % (i1, ) - print '%s' % (h1, h1, ), - if i2 != -1: - print '  %d:%s' % \ - (i2, h2, h2, ), - else: - print '  %d:%s' % (i2, h2, ), - print '
manifest:%d:' % \ - (repo.manifest.rev(changes[0]), ), - print '%s
author:%sdescription:' + \ - '%s
date:%s UTCfiles:' - for f in changes[3]: - print '\t\t%s' % \ - (hg.hex(mf[f]), f, f, ), - print '  ' - print '\t

' + def change_table(self, nodeid, changes): + hn = hg.hex(nodeid) + i = self.repo.changelog.rev(nodeid) + (h1, h2) = [ hg.hex(x) for x in self.repo.changelog.parents(nodeid) ] + datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0]))) + print '' + print '\t' + \ + '' % \ + (obfuscate(changes[1]), ) + print '\t\t' + \ + '' % \ + (hn, nl2br(cgi.escape(changes[4])), ) + print '\t' % (datestr, ) + print '\t\t' + print '\t' % (hn, hn, ) + print '
author:%sdescription:' + \ + '%s
date:%s UTCfiles:' + for f in changes[3]: + print '\t\t%s  ' % \ + (hn, f, cgi.escape(f), ) + print '\t
revision:%d:%s

' + +class checkin(page): + def __init__(self, repo, nodestr): + page.__init__(self) + self.repo = repo + self.node = hg.bin(nodestr) + self.nodestr = nodestr + print '

Checkin: %s

' % nodestr - (c, a, d) = repo.diffrevs(parents[0], nodeid) - change = repo.changelog.read(parents[0]) - mf2 = repo.manifest.read(change[0]) - for f in c: - ent_diff(repo.file(f).read(mf2[f]), repo.file(f).read(mf[f]), f) - for f in a: - ent_diff('', repo.file(f).read(mf[f]), f) - for f in d: - ent_diff(repo.file(f).read(mf2[f]), '', f) - - endpage() - + def content(self): + changes = self.repo.changelog.read(self.node) + i = self.repo.changelog.rev(self.node) + parents = self.repo.changelog.parents(self.node) + (h1, h2) = [ hg.hex(x) for x in parents ] + (i1, i2) = [ self.repo.changelog.rev(x) for x in parents ] + datestr = time.asctime(time.gmtime(float(changes[2].split(' ')[0]))) + mf = self.repo.manifest.read(changes[0]) + print '' + print '\t' % \ + (self.nodestr, self.nodestr, ) + print '\t' + print '\t' % \ + (hg.hex(changes[0]), hg.hex(changes[0]), ) + print '\t' + \ + '' % \ + (obfuscate(changes[1]), ) + print '\t\t' + \ + '' % \ + (self.nodestr, nl2br(cgi.escape(changes[4])), ) + print '\t' % (datestr, ) + print '\t\t' + print '
revision:%d:' % (i, ), + print '%s
parent(s):%d:' % (i1, ) + print '%s' % (h1, h1, ), + if i2 != -1: + print '  %d:%s' % \ + (i2, h2, h2, ), + else: + print '  %d:%s' % (i2, h2, ), + print '
manifest:%d:' % \ + (self.repo.manifest.rev(changes[0]), ), + print '%s
author:%sdescription:' + \ + '%s
date:%s UTCfiles:' + for f in changes[3]: + print '\t\t%s' % \ + (hg.hex(mf[f]), f, cgi.escape(f), ), + print '  ' + print '\t

' -def ent_file(repo, nodeid, fn): - print '
%s (%s)
' % (fn, hg.hex(nodeid), ) - print '
'
-    print cgi.escape(repo.file(fn).read(nodeid))
-    print '
' + (c, a, d) = self.repo.diffrevs(parents[0], self.node) + change = self.repo.changelog.read(parents[0]) + mf2 = self.repo.manifest.read(change[0]) + for f in c: + self.show_diff(self.repo.file(f).read(mf2[f]), \ + self.repo.file(f).read(mf[f]), f) + for f in a: + self.show_diff('', self.repo.file(f).read(mf[f]), f) + for f in d: + self.show_diff(self.repo.file(f).read(mf2[f]), '', f) -def change_page(): - startpage("Mercurial Web") - print '' - cl = [] - for i in xrange(repo.changelog.count()): - n = repo.changelog.node(i) - cl.append((n, repo.changelog.read(n))) - cl.reverse() - for n, ch in cl: - print '' +class filepage(page): + def __init__(self, repo, fn, node=None, cs=None): + page.__init__(self) + self.repo = repo + self.fn = fn + if cs: + chng = self.repo.changelog.read(hg.bin(cs)) + mf = self.repo.manifest.read(chng[0]) + self.node = mf[self.fn] + self.nodestr = hg.hex(self.node) + else: + self.nodestr = node + self.node = hg.bin(node) + print '
%s (%s)
' % \ + (cgi.escape(self.fn), self.nodestr, ) + print 'history
' % self.fn - print '
' - ent_change(repo, n, ch) - print '
' - endpage() + def content(self): + print '
'
+        print cgi.escape(self.repo.file(self.fn).read(self.node))
+        print '
' args = cgi.parse() @@ -176,24 +202,31 @@ repo = hg.repository(ui, repo_path) if not args.has_key('cmd'): - change_page() + page = change_list(repo, 'Mercurial') + page.content() + page.endpage() elif args['cmd'][0] == 'chkin': if not args.has_key('nd'): + page = errpage() print '
No Node!
' else: - ent_checkin(repo, hg.bin(args['nd'][0])) + page = checkin(repo, args['nd'][0]) + page.content() + page.endpage() elif args['cmd'][0] == 'file': - startpage("Mercurial Web") - - if not args.has_key('nd'): - print '
No Node!
' - elif not args.has_key('fn'): - print '
No Filename!
' + if not (args.has_key('nd') and args.has_key('fn')) and \ + not (args.has_key('cs') and args.has_key('fn')): + page = errpage() + print '
Invalid Args!
' else: - ent_file(repo, hg.bin(args['nd'][0]), args['fn'][0]) - endpage() + if args.has_key('nd'): + page = filepage(repo, args['fn'][0], node=args['nd'][0]) + else: + page = filepage(repo, args['fn'][0], cs=args['cs'][0]) + page.content() + page.endpage() elif args['cmd'][0] == 'branches': httphdr("text/plain") @@ -225,6 +258,7 @@ sys.stdout.write(z.flush()) else: - startpage("Mercurial Web Error") - print '
unknown command: ', args['cmd'][0], '
' - endpage() + page = errpage() + print '
unknown command: %s
' % \ + cgi.escape(args['cmd'][0]) + page.endpage()