Debian Codegraph @

cloud_sptheme/ext/index_styling.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 """cloud_sptheme.ext.index_styling - improves css styling for genindex"""
2 import logging; log = logging.getLogger(__name__)
3 import re
•4 from markupsafe import Markup as literal, escape
5 from cloud_sptheme import __version__
6
7 prefix = r"^(?P<name>.*)\("
8 suffix = r"\)$"
9 _attr_re = re.compile(prefix + r"(?P<left>)(?P<loc>.*)(?P<right> attribute)" + suffix)
10 _meth_re = re.compile(prefix + r"(?P<left>)(?P<loc>.*)(?P<right> method)" + suffix)
11 _fc_re = re.compile(prefix + r"(?P<left>class in |in module )(?P<loc>.*)(?P<right>)" + suffix)
12 _mod_re = re.compile(prefix + r"module" + suffix)
13
14 def format_index_name(name):
15 while True:
16 m = _attr_re.match(name)
17 if m:
18 name, left, loc, right = m.group("name","left", "loc", "right")
19 type = "attribute"
20 break
21 m = _meth_re.match(name)
22 if m:
23 name, left, loc, right = m.group("name","left", "loc", "right")
24 type = "method"
25 break
26 m = _fc_re.match(name)
27 if m:
28 name, left, loc, right = m.group("name","left", "loc", "right")
29 if left.startswith("class"):
30 type = "class"
31 else:
32 type = "function"
33 break
34 m = _mod_re.match(name)
35 if m:
36 name = m.group("name")
37 left = "module"
38 loc = right = ''
39 type = "module"
40 break
41 return name
42 if loc:
43 loc = literal('<span class="location">') + escape(loc) + literal("</span>")
44 cat = left + loc + right
45 return escape(name) + literal('<span class="category ' + type + '">') + escape(cat) + literal("</span>")
46
47 def mangle_index(app, pagename, templatename, ctx, event_arg):
48 if pagename != "genindex":
49 return
50 fmt = format_index_name
51 for key, entries in ctx['genindexentries']:
52 for idx, entry in enumerate(entries):
53 name, data = entry
54 entries[idx] = fmt(name), data
55 # NOTE: data is list of [links, subitems, some_key],
56 # though 'key' not added until sphinx 1.4 (1.3?)
57 subitems = data[1]
58 for idx, entry in enumerate(subitems):
59 name, links = entry
60 subitems[idx] = fmt(name), links
61
62 def setup(app):
63 app.connect('html-page-context', mangle_index)
64
65 # identifies the version of our extension
66 return {'version': __version__}