nakedmud-mod/
nakedmud-mod/html/tutorials/
nakedmud-mod/html/tutorials/building_extras/
nakedmud-mod/html/tutorials/c/
nakedmud-mod/html/tutorials/reference/
nakedmud-mod/html/tutorials/scripting/
nakedmud-mod/html/tutorials/scripting_extras/
nakedmud-mod/lib/
nakedmud-mod/lib/help/A/
nakedmud-mod/lib/help/B/
nakedmud-mod/lib/help/C/
nakedmud-mod/lib/help/D/
nakedmud-mod/lib/help/G/
nakedmud-mod/lib/help/H/
nakedmud-mod/lib/help/J/
nakedmud-mod/lib/help/L/
nakedmud-mod/lib/help/M/
nakedmud-mod/lib/help/O/
nakedmud-mod/lib/help/P/
nakedmud-mod/lib/help/R/
nakedmud-mod/lib/help/S/
nakedmud-mod/lib/help/W/
nakedmud-mod/lib/logs/
nakedmud-mod/lib/misc/
nakedmud-mod/lib/players/
nakedmud-mod/lib/pymodules/polc/
nakedmud-mod/lib/txt/
nakedmud-mod/lib/world/
nakedmud-mod/lib/world/zones/examples/
nakedmud-mod/lib/world/zones/examples/mproto/
nakedmud-mod/lib/world/zones/examples/oproto/
nakedmud-mod/lib/world/zones/examples/reset/
nakedmud-mod/lib/world/zones/examples/rproto/
nakedmud-mod/lib/world/zones/examples/trigger/
nakedmud-mod/lib/world/zones/limbo/
nakedmud-mod/lib/world/zones/limbo/room/
nakedmud-mod/lib/world/zones/limbo/rproto/
nakedmud-mod/src/alias/
nakedmud-mod/src/dyn_vars/
nakedmud-mod/src/editor/
nakedmud-mod/src/example_module/
nakedmud-mod/src/help2/
nakedmud-mod/src/set_val/
nakedmud-mod/src/socials/
nakedmud-mod/src/time/
"""
doc.py

This module allows documentation for Python classes and modules to be viewed
in-game via the 'doc' command. 
"""
import pydoc, os, mudsys, display



################################################################################
# local variables
################################################################################

# where do we store documentation?
HTML_DOC_DIR = "../html/pydocs"

shortcuts = { "ch"      : "char",
              "sock"    : "mudsock",
              }

# just a list of all our builtin modules
builtins = [
    "char",
    "room",
    "obj",
    "exit",
    "account",
    "mudsock",
    "mud",
    "mudsys",
    "hooks",
    "event",
    "auxiliary",
    "storage",
    "olc",
    ]

# append all of our builtins to suggested reading list
suggested_reading = [mod for mod in builtins]

def register_module_doc(modname, package = None, root = "pymodules"):
    """Add a new module name to suggested_reading. If modname is a package,
       recursively add its packages as well
    """
    fname = root + "/" + modname.replace(".", "/")
    suggested_reading.append(modname)
    if os.path.isdir(fname):
        for file in os.listdir(fname):
            if (file.endswith(".py") or not "." in file) and not file[0] in "._":
                module = modname + "." + file.split(".", 1)[0]
                register_module_doc(module, root)
 
# now, append all of our Python packages and modules
for fname in os.listdir("pymodules/"):
    # look for modules and packages
    if (fname.endswith(".py") or not "." in fname) and not fname[0] in "._":
        modname = fname.split(".", 1)[0]
        register_module_doc(modname)



################################################################################
# player commands
################################################################################
def cmd_htmldoc(ch, cmd, arg):
    """Creates html documentation for all registered modules. html files will
       be saved to html/pydocs/
    """
    try:
        os.makedirs(HTML_DOC_DIR)
    except: pass
    doc = pydoc.HTMLDoc()
    for modname in suggested_reading:
        todoc = pydoc.locate(modname)
        if todoc != None:
            fname = HTML_DOC_DIR + "/" + modname + ".html"
            fl    = file(fname, "w+")
            fl.write(doc.page(modname, doc.document(todoc)))
            fl.close()

    builtin_index = doc.multicolumn([doc.modulelink(pydoc.locate(modname)) for modname in builtins], lambda x: x)
    
    # build our index page. That includes things in pymodules/ and builtins
    index_contents ="".join([doc.section("<big><strong>builtins</big></strong>",
                                         'white', '#ee77aa', builtin_index),
                             doc.index("../lib/pymodules/")])

    # go over all of our builtins and add them to the index
    index = file(HTML_DOC_DIR + "/index.html", "w+")
    index.write(doc.page("index", index_contents))
    index.close()
    
    ch.send("html documentation generated for all known modules.")

def cmd_doc(ch, cmd, arg):
    """Return Python documentation for the specified module, class, function,
       etc... for example:
       
       > doc char.Char

       Will return all available documentation for the Char class.
    """
    if arg == "":
        ch.page("\r\n".join(display.pagedlist({ "Topics" : suggested_reading },
                                              header = "Suggested doc readings include:")))
    else:
        # just because sometimes I forget periods
        arg = arg.replace(" ", ".")

        # are we looking for a shortcut value?
        if arg in shortcuts:
            arg = shortcuts[arg]

        # try to find what we're documenting
        todoc = pydoc.locate(arg)
        if todoc == None:
            ch.send("Could not find Python documentation on: '%s'" % arg)
        else:
            doc = pydoc.TextDoc()
            ch.page(doc.document(todoc).replace("{", "{{"))



################################################################################
# initialization
################################################################################
mudsys.add_cmd("doc",     None, cmd_doc,     "wizard", False)
mudsys.add_cmd("htmldoc", None, cmd_htmldoc, "admin",  False)