File: helpers.py

package info (click to toggle)
sphinxcontrib-datatemplates 0.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 580 kB
  • sloc: python: 828; makefile: 172; xml: 38; javascript: 1
file content (60 lines) | stat: -rw-r--r-- 1,907 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def make_list_table(headers, data, title='', columns=None):
    """Build a list-table directive.

    :param headers: List of header values.
    :param data: Iterable of row data, yielding lists or tuples with rows.
    :param title: Optional text to show as the table title.
    :param columns: Optional widths for the columns.
    """
    results = []
    add = results.append
    add('.. list-table:: %s' % title)
    add('   :header-rows: 1')
    if columns:
        add('   :widths: %s' % (','.join(str(c) for c in columns)))
    add('')
    add('   - * %s' % headers[0])
    for h in headers[1:]:
        add('     * %s' % h)
    for row in data:
        add('   - * %s' % row[0])
        for r in row[1:]:
            add('     * %s' % r)
    add('')
    return '\n'.join(results)


def make_list_table_from_mappings(headers, data, title, columns=None):
    """Build a list-table directive.

    :param headers: List of tuples containing header title and key value.
    :param data: Iterable of row data, yielding mappings with rows.
    :param title: Optional text to show as the table title.
    :param columns: Optional widths for the columns.
    """
    header_names = [h[0] for h in headers]
    header_keys = [h[1] for h in headers]
    row_data = ([d.get(k) for k in header_keys] for d in data)
    return make_list_table(header_names, row_data, title, columns)


def escape_rst(s):
    """Escape string for inclusion in RST documents.

    See
    https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#escaping-mechanism

    :param s: String for escaping
    """
    return "".join(c if c.isspace() else "\\" + c for c in s)


def escape_rst_url(s):
    """Escape string for inclusion in URLs in RST documents.

    See
    https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#escaping-mechanism

    :param s: String for escaping
    """
    return "\\".join(s)