hgkw/keyword.py
changeset 367 7bc6c0e12464
parent 365 e99ace93ded0
child 368 82e2b3807073
--- a/hgkw/keyword.py	Tue Feb 05 08:04:50 2008 +0100
+++ b/hgkw/keyword.py	Tue Feb 05 12:22:48 2008 +0100
@@ -83,8 +83,9 @@
 from mercurial import commands, cmdutil, context, filelog, localrepo
 from mercurial import patch, revlog, templater, templatefilters, util
 from mercurial.node import *
+from mercurial.hgweb import webcommands
 from mercurial.i18n import _
-import re, shutil, tempfile, time
+import mimetypes, re, shutil, tempfile, time
 
 commands.optionalrepo += ' kwdemo'
 
@@ -492,8 +493,32 @@
             kwshrunk = kwt.shrink(''.join(self.lines))
             self.lines = kwshrunk.splitlines(True)
 
+    def kwweb_rawfile(web, req, tmpl):
+        '''Monkeypatch webcommands.rawfile so it expands keywords.'''
+        path = web.cleanpath(req.form.get('file', [''])[0])
+        if not path:
+            content = web.manifest(tmpl, web.changectx(req), path)
+            req.respond(webcommands.HTTP_OK, web.ctype)
+            return content
+        try:
+            fctx = web.filectx(req)
+        except revlog.LookupError:
+            content = web.manifest(tmpl, web.changectx(req), path)
+            req.respond(webcommands.HTTP_OK, web.ctype)
+            return content
+        path = fctx.path()
+        text = fctx.data()
+        if kwt.matcher(path):
+            text = kwt.expand(path, text, None)
+        mt = mimetypes.guess_type(path)[0]
+        if mt is None or util.binary(text):
+            mt = mt or 'application/octet-stream'
+        req.respond(webcommands.HTTP_OK, mt, path, len(text))
+        return [text]
+
     repo.__class__ = kwrepo
     patch.patchfile.__init__ = kwpatchfile_init
+    webcommands.rawfile = kwweb_rawfile
 
 
 cmdtable = {