OILS / doctools / html_lib.py View on Github | oilshell.org

43 lines, 14 significant
1#!/usr/bin/env python2
2"""
3html_lib.py
4
5Shared between HTML processors.
6
7TODO: Write a "pull parser" API!
8"""
9from __future__ import print_function
10
11import cgi
12import re
13
14
15def AttrsToString(attrs):
16 if not attrs:
17 return ''
18
19 # Important: there's a leading space here.
20 # TODO: Change href="$help:command" to href="help.html#command"
21 return ''.join(' %s="%s"' % (k, cgi.escape(v)) for (k, v) in attrs)
22
23
24def PrettyHref(s, preserve_anchor_case=False):
25 """
26 Turn arbitrary heading text into a clickable href with no special characters.
27
28 This is modelled after what github does. It makes everything lower case.
29 """
30 # Split by whitespace or hyphen
31 words = re.split(r'[\s\-]+', s)
32
33 # Keep only alphanumeric
34 keep = [''.join(re.findall(r'\w+', w)) for w in words]
35
36 # Join with - and lowercase. And then remove empty words, unlike Github.
37 # This is SIMILAR to what Github does, but there's no need to be 100%
38 # compatible.
39
40 pretty = '-'.join(p for p in keep if p)
41 if not preserve_anchor_case:
42 pretty = pretty.lower()
43 return pretty