#!/usr/bin/env python3
#
# kconfig2wiki - Kconfig to MediaWiki converter for
# https://www.coreboot.org/coreboot_Options
# based on http://landley.net/kdocs/make/menuconfig2html.py
#
# SPDX-License-Identifier: GPL-2.0-only
import glob
helplen = 0
extra_chapters = 0
##
## Remove quotes from Kconfig string options
##
def zapquotes(str):
	if str[0]=='"': str = str[1:str.rfind('"')]
	return str
##
## Escape HTML special characters
##
def htmlescape(str):
	return str.strip().replace("&","&").replace("<","<").replace(">",">")
##
## Process Kconfig file
##
def readfile(filename):
	import sys
	global helplen
	source=filename.replace("src/","").replace("/Kconfig","").replace("Kconfig","toplevel")
	try:
		lines = open(filename).read().split("\n")
	except IOError:
		sys.stderr.write("File %s missing\n" % filename)
		return
	config = None
	description = None
	configtype = None
	for i in lines:
		if helplen:
			i = i.expandtabs()
			if not len(i) or i[:helplen].isspace():
				sys.stdout.write("%s
\n" % htmlescape(i))
				continue
			else:
				helplen = 0
				sys.stdout.write("\n")
		words = i.strip().split(None,1)
		if not len(words): continue
		if words[0] in ("config", "menuconfig"):
			config = words[1]
			description = ""
		elif words[0] in ("bool", "boolean", "tristate", "string", "hex", "int"):
			 configtype = htmlescape(zapquotes(words[0]))
			 if len(words)>1: description = htmlescape(zapquotes(words[1]))
		elif words[0]=="prompt":
			description = htmlescape(zapquotes(words[1]))
		elif words[0] in ("help", "---help---"):
			sys.stdout.write("
| Option\n") sys.stdout.write(" | Source\n") sys.stdout.write(" | Format\n") sys.stdout.write(" | Short Description\n") sys.stdout.write(" | Description |