view make/menuconfig2html.py @ 110:01b77829d010

Lots of stuff bit-rotted in scripts to extract documentation from current kernel sources. De-bit-rot it.
author Rob Landley <rob@landley.net>
date Sat, 19 Feb 2011 21:32:24 -0600
parents 841aab0fd204
children
line wrap: on
line source

#!/usr/bin/python

# "boolean" not documented type in kconfig-language.txt line 51

import os,sys

helplen = 0

if len(sys.argv)!=4:
  sys.stderr.write("Usage: menuconfig2html.py kconfigfile outdir version\n")
  sys.exit(1)

def zapquotes(str):
  if str[0]=='"': str = str[1:str.rfind('"')]
  return str

def htmlescape(str):
  return str.strip().replace("&","&amp;").replace("<","&lt;").replace(">","&gt;")

def starthtml(out, myfn):

  out.write("<html><title>%s %s</title><body><h1>%s %s</h1>\n" % (myfn,sys.argv[3],myfn,sys.argv[3]))

def readfile(filename):
  global helplen

  myfn="%s.html" % "-".join(filename.split("/"))

  out=open("%s/%s" % (sys.argv[2], myfn),"w")
  #sys.stderr.write("Reading %s\n" % filename)
  try:
    lines = open(filename).read().split("\n")
  except IOError:
    sys.stderr.write("File %s missing\n" % filename)
    return
  starthtml(out,filename)
  config = None
  description = None
  for i in lines:
    if helplen:
      i = i.expandtabs()
      if not len(i) or i[:helplen].isspace():
        out.write("%s\n" % htmlescape(i))
        continue
      else:
        helplen = 0
        out.write("</pre></blockquote>\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"):
       if len(words)>1: description = htmlescape(zapquotes(words[1]))
    elif words[0]=="prompt":
      description = htmlescape(zapquotes(words[1]))
    elif words[0] in ("help", "---help---"):
      out.write('<a name="%s"><h2>%s</h2>\n<h3>%s</h3>\n<blockquote><pre>' % (config,config,description))
      sys.stdout.write('<li><a href="%s#%s">%s</a> %s</li>\n' % (myfn,config,config,description))
      helplen = len(i[:i.find(words[0])].expandtabs())
    elif words[0] == "comment":
      out.write("<h3>%s</h3>\n" % htmlescape(zapquotes(words[1])))
    elif words[0]=="menu":
      out.write("<hr>\n")
      if len(words)>1:
        temp = htmlescape(zapquotes(words[1]))
        out.write("<h1>Menu: %s</h1>\n" % temp)
        sys.stdout.write("<h2>Menu [%s]</h2>\n" % temp)
      sys.stdout.write("<ul>\n")
    elif words[0] == "endmenu":
      out.write("<hr>\n")
      sys.stdout.write("</ul>\n")
    elif words[0] == "source":
      fn=zapquotes(words[1])
      readfile(fn)
      out.write('<h2><a href="%s">%s</a></h2>\n' % ("%s.html" % "-".join(fn.split("/")), fn))
    elif words[0] in ("default","depends", "select", "if", "endif", "#"): pass
    #else: sys.stderr.write("unknown: %s\n" % i)
  if helplen: out.write("</pre></blockquote>\n")
  out.write("</html>\n")

starthtml(sys.stdout,sys.argv[1])
sys.stdout.write("<ul>\n")
readfile(sys.argv[1])
sys.stdout.write("</ul>\n</html>\n")