2 # -*- coding: utf-8 -*-
4 # gen_kconfig_doc - confgen.py support for generating ReST markup documentation
6 # For each option in the loaded Kconfig (e.g. 'FOO'), CONFIG_FOO link target is
7 # generated, allowing options to be referenced in other documents
8 # (using :ref:`CONFIG_FOO`)
10 # Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
12 # Licensed under the Apache License, Version 2.0 (the "License");
13 # you may not use this file except in compliance with the License.
14 # You may obtain a copy of the License at
16 # http:#www.apache.org/licenses/LICENSE-2.0
18 # Unless required by applicable law or agreed to in writing, software
19 # distributed under the License is distributed on an "AS IS" BASIS,
20 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 # See the License for the specific language governing permissions and
22 # limitations under the License.
26 # Indentation to be used in the generated file
29 # Characters used when underlining section heading
30 HEADING_SYMBOLS = '#*=-^"+'
32 # Keep the heading level in sync with api-reference/kconfig.rst
33 INITIAL_HEADING_LEVEL = 3
34 MAX_HEADING_LEVEL = len(HEADING_SYMBOLS)-1
36 def write_docs(config, filename):
37 """ Note: writing .rst documentation ignores the current value
38 of any items. ie the --config option can be ignored.
39 (However at time of writing it still needs to be set to something...) """
40 with open(filename, "w") as f:
41 config.walk_menu(lambda node: write_menu_item(f, node))
44 def get_breadcrumbs(node):
45 # this is a bit wasteful as it recalculates each time, but still...
50 result = [ node.prompt[0] ] + result
52 return " > ".join(result)
54 def get_heading_level(node):
56 result = INITIAL_HEADING_LEVEL
60 if result == MAX_HEADING_LEVEL:
61 return MAX_HEADING_LEVEL
65 def format_rest_text(text, indent):
66 # Format an indented text block for use with ReST
67 text = indent + text.replace('\n', '\n' + indent)
68 # Escape some characters which are inline formatting in ReST
69 text = text.replace("*", "\\*")
70 text = text.replace("_", "\\_")
74 def write_menu_item(f, node):
76 return # Don't do anything for invisible menu items
78 if isinstance(node.parent.item, kconfiglib.Choice):
79 return # Skip choice nodes, they are handled as part of the parent (see below)
83 except AttributeError:
87 is_menu = node.item == kconfiglib.MENU or node.is_menuconfig
88 except AttributeError:
89 is_menu = False # not all MenuNodes have is_menuconfig for some reason
94 # add link target so we can use :ref:`CONFIG_FOO`
95 f.write('.. _CONFIG_%s:\n\n' % name)
97 title = node.prompt[0]
99 # if no symbol name, use the prompt as the heading
101 f.write('%s\n' % title)
102 f.write(HEADING_SYMBOLS[get_heading_level(node)] * len(title))
105 f.write('**%s**\n\n\n' % title)
108 f.write('%s%s\n\n' % (INDENT, node.prompt[0]))
109 f.write('%s:emphasis:`Found in: %s`\n\n' % (INDENT, get_breadcrumbs(node)))
113 # Help text normally contains newlines, but spaces at the beginning of
114 # each line are stripped by kconfiglib. We need to re-indent the text
115 # to produce valid ReST.
116 f.write(format_rest_text(node.help, INDENT))
117 except AttributeError:
120 if isinstance(node.item, kconfiglib.Choice):
121 f.write('%sAvailable options:\n' % INDENT)
122 choice_node = node.list
124 # Format available options as a list
125 f.write('%s- %-20s (%s)\n' % (INDENT * 2, choice_node.prompt[0], choice_node.item.name))
127 HELP_INDENT = INDENT * 2
128 fmt_help = format_rest_text(choice_node.help, ' ' + HELP_INDENT)
129 f.write('%s \n%s\n' % (HELP_INDENT, fmt_help))
130 choice_node = choice_node.next
135 if __name__ == '__main__':
136 print("Run this via 'confgen.py --output doc FILENAME'")