flaskqref.py (2471B)
1 """ 2 sphinxcontrib.autohttp.flaskqref 3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 5 The sphinx.ext.autodoc-style HTTP API quick reference 6 builder (from Flask) 7 for sphinxcontrib.httpdomain. 8 9 :copyright: Copyright 2011 by Hong Minhee 10 :license: BSD, see LICENSE for details. 11 12 """ 13 14 from docutils import nodes 15 from docutils.statemachine import ViewList 16 17 from sphinxcontrib import httpdomain 18 from sphinx.util.nodes import nested_parse_with_titles 19 20 from .flask import AutoflaskBase 21 22 23 class QuickReferenceFlaskDirective(AutoflaskBase): 24 25 26 header = [ '', 27 '.. list-table::', 28 ' :widths: 20 45 35', 29 ' :header-rows: 1', 30 '', 31 ' * - Resource', 32 ' - Operation', 33 ' - Description' 34 ] 35 36 def run(self): 37 node = nodes.section() 38 node.document = self.state.document 39 result = ViewList() 40 for line in QuickReferenceFlaskDirective.header: 41 result.append(line, '<qrefflask>') 42 table={} 43 table_sorted_names=[] 44 45 for table_row in self.make_rst(qref=True): 46 name = table_row['name'] 47 if table.get(name) is None: 48 table[name]=[] 49 table[name].append(table_row) 50 if name not in table_sorted_names: 51 table_sorted_names.append(name) 52 53 table_sorted_names.sort() 54 55 for name in table_sorted_names: 56 # Keep table display clean by not repeating duplicate 57 # resource names and descriptions 58 display_name = name 59 previous_description=None 60 for row in table[name]: 61 result.append(' * - %s' % display_name, '<qrefflask>') 62 display_name ="" 63 result.append(row['operation'], '<qrefflask>') 64 description = row['description'] 65 if previous_description is not None and previous_description == description: 66 description ="" 67 else: 68 previous_description = description 69 70 result.append(' - %s' % description, '<qrefflask>') 71 72 result.append('', '<qrefflask>') 73 nested_parse_with_titles(self.state, result, node) 74 return node.children 75 76 def setup(app): 77 if 'http' not in app.domains: 78 httpdomain.setup(app) 79 app.add_directive('qrefflask', QuickReferenceFlaskDirective) 80