summaryrefslogtreecommitdiff
path: root/doc/sphinx/_exts/httpdomain/autohttp/flaskqref.py
blob: c28bb15355f91694974469dc5a5662e3d03f1596 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
    sphinxcontrib.autohttp.flaskqref
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The sphinx.ext.autodoc-style HTTP API quick reference 
    builder (from Flask)
    for sphinxcontrib.httpdomain.

    :copyright: Copyright 2011 by Hong Minhee
    :license: BSD, see LICENSE for details.

"""

from docutils import nodes
from docutils.statemachine import ViewList

from sphinxcontrib import httpdomain
from sphinx.util.nodes import nested_parse_with_titles

from .flask import AutoflaskBase


class QuickReferenceFlaskDirective(AutoflaskBase):


    header = [ '',
              '.. list-table::',
              '    :widths: 20 45 35',
              '    :header-rows: 1',
              '',
              '    * - Resource',
              '      - Operation',
              '      - Description'
            ]

    def run(self):
        node = nodes.section()
        node.document = self.state.document
        result = ViewList()
        for line in QuickReferenceFlaskDirective.header:
            result.append(line, '<qrefflask>') 
        table={}
        table_sorted_names=[]
        
        for table_row in self.make_rst(qref=True):
            name = table_row['name']
            if table.get(name) is None:
                table[name]=[]
            table[name].append(table_row) 
            if name not in table_sorted_names:
                table_sorted_names.append(name)

        table_sorted_names.sort()

        for name in table_sorted_names:
            # Keep table display clean by not repeating duplicate
            # resource names and descriptions
            display_name = name
            previous_description=None
            for row in table[name]:
                result.append('    * - %s' % display_name, '<qrefflask>')
                display_name =""
                result.append(row['operation'], '<qrefflask>')
                description = row['description']
                if previous_description is not None and previous_description == description:
                    description =""
                else:
                    previous_description = description

                result.append('      - %s' % description,  '<qrefflask>')

        result.append('', '<qrefflask>')
        nested_parse_with_titles(self.state, result, node)
        return node.children
        
def setup(app):
    if 'http' not in app.domains:
        httpdomain.setup(app)
    app.add_directive('qrefflask', QuickReferenceFlaskDirective)