1#!/usr/bin/env python3 2# 3# kconfig2wiki - Kconfig to MediaWiki converter for 4# https://www.coreboot.org/coreboot_Options 5# based on http://landley.net/kdocs/make/menuconfig2html.py 6# 7# SPDX-License-Identifier: GPL-2.0-only 8 9import glob 10 11helplen = 0 12extra_chapters = 0 13 14## 15## Remove quotes from Kconfig string options 16## 17def zapquotes(str): 18 if str[0]=='"': str = str[1:str.rfind('"')] 19 return str 20 21## 22## Escape HTML special characters 23## 24def htmlescape(str): 25 return str.strip().replace("&","&").replace("<","<").replace(">",">") 26 27## 28## Process Kconfig file 29## 30def readfile(filename): 31 import sys 32 global helplen 33 34 source=filename.replace("src/","").replace("/Kconfig","").replace("Kconfig","toplevel") 35 36 try: 37 lines = open(filename).read().split("\n") 38 except IOError: 39 sys.stderr.write("File %s missing\n" % filename) 40 return 41 config = None 42 description = None 43 configtype = None 44 for i in lines: 45 if helplen: 46 i = i.expandtabs() 47 if not len(i) or i[:helplen].isspace(): 48 sys.stdout.write("%s<br />\n" % htmlescape(i)) 49 continue 50 else: 51 helplen = 0 52 sys.stdout.write("</td></tr>\n") 53 54 words = i.strip().split(None,1) 55 if not len(words): continue 56 57 if words[0] in ("config", "menuconfig"): 58 config = words[1] 59 description = "" 60 elif words[0] in ("bool", "boolean", "tristate", "string", "hex", "int"): 61 configtype = htmlescape(zapquotes(words[0])) 62 if len(words)>1: description = htmlescape(zapquotes(words[1])) 63 elif words[0]=="prompt": 64 description = htmlescape(zapquotes(words[1])) 65 elif words[0] in ("help", "---help---"): 66 sys.stdout.write("<tr bgcolor=\"#eeeeee\">\n") 67 sys.stdout.write("<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>\n" % (config,source,configtype,description) ) 68 helplen = len(i[:i.find(words[0])].expandtabs()) 69 elif words[0] == "comment": 70 sys.stdout.write("<tr bgcolor=\"#eeeeee\">\n") 71 sys.stdout.write("<td></td><td>%s</td><td>(comment)</td><td></td><td>%s</td></tr>\n" % (source, htmlescape(zapquotes(words[1])))) 72 elif words[0]=="menu": 73 if len(words)>1: 74 temp = htmlescape(zapquotes(words[1])) 75 sys.stdout.write("<tr bgcolor=\"#6699dd\">\n") 76 sys.stdout.write("<td colspan=5>Menu: %s</td></tr>\n" % temp) 77 elif words[0] == "endmenu": 78 sys.stdout.write("\n") 79 elif words[0] == "source": 80 fn=zapquotes(words[1]) 81 for name in glob.glob(fn): 82 readfile(name) 83 elif words[0] in ("default","depends", "select", "if", "endif", "#"): pass 84 #else: sys.stderr.write("unknown: %s\n" % i) 85 if helplen: sys.stdout.write("</td></tr>\n") 86 87def main(): 88 import sys, time 89 90 if len(sys.argv)!=3: 91 sys.stderr.write("Usage: kconfig2wiki kconfigfile version\n") 92 sys.exit(1) 93 94 sys.stdout.write("This is an automatically generated list of '''coreboot compile-time options'''.\n") 95 sys.stdout.write("\nLast update: %s\n" % sys.argv[2]) 96 sys.stdout.write("<table border=\"0\" style=\"font-size: smaller\">\n"); 97 sys.stdout.write("<tr bgcolor=\"#6699dd\">\n") 98 sys.stdout.write("<td align=\"left\">Option</td>\n") 99 sys.stdout.write("<td align=\"left\">Source</td>\n") 100 sys.stdout.write("<td align=\"left\">Format</td>\n") 101 sys.stdout.write("<td align=\"left\">Short Description</td>\n") 102 sys.stdout.write("<td align=\"left\">Description</td></tr>\n") 103 readfile(sys.argv[1]) 104 sys.stdout.write("</table>\n") 105 106if __name__ == "__main__": 107 main() 108