hgweb: extract function for loading style from request context
authorAugie Fackler <augie@google.com>
Thu, 05 Oct 2017 14:29:13 -0400
changeset 34515 8afc25e7effc
parent 34514 528b21b853aa
child 34516 e79b3611223b
hgweb: extract function for loading style from request context Also make it work on Python 3. Differential Revision: https://phab.mercurial-scm.org/D970
mercurial/hgweb/hgweb_mod.py
mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgweb_mod.py	Thu Oct 05 14:27:21 2017 -0400
+++ b/mercurial/hgweb/hgweb_mod.py	Thu Oct 05 14:29:13 2017 -0400
@@ -30,6 +30,7 @@
     hg,
     hook,
     profiling,
+    pycompat,
     repoview,
     templatefilters,
     templater,
@@ -60,6 +61,17 @@
     ('bz2', ('application/x-bzip2', 'tbz2', '.tar.bz2', None)),
 ))
 
+def getstyle(req, configfn, templatepath):
+    fromreq = req.form.get('style', [None])[0]
+    if fromreq is not None:
+        fromreq = pycompat.sysbytes(fromreq)
+    styles = (
+        fromreq,
+        configfn('web', 'style'),
+        'paper',
+    )
+    return styles, templater.stylemap(styles, templatepath)
+
 def makebreadcrumb(url, prefix=''):
     '''Return a 'URL breadcrumb' list
 
@@ -170,12 +182,8 @@
         # figure out which style to use
 
         vars = {}
-        styles = (
-            req.form.get('style', [None])[0],
-            self.config('web', 'style'),
-            'paper',
-        )
-        style, mapfile = templater.stylemap(styles, self.templatepath)
+        styles, (style, mapfile) = getstyle(req, self.config,
+                                            self.templatepath)
         if style == styles[0]:
             vars['style'] = style
 
--- a/mercurial/hgweb/hgwebdir_mod.py	Thu Oct 05 14:27:21 2017 -0400
+++ b/mercurial/hgweb/hgwebdir_mod.py	Thu Oct 05 14:29:13 2017 -0400
@@ -503,12 +503,8 @@
             url += '/'
 
         vars = {}
-        styles = (
-            req.form.get('style', [None])[0],
-            config('web', 'style'),
-            'paper'
-        )
-        style, mapfile = templater.stylemap(styles, self.templatepath)
+        styles, (style, mapfile) = hgweb_mod.getstyle(req, config,
+                                                      self.templatepath)
         if style == styles[0]:
             vars['style'] = style