#!/usr/bin/env python
# encoding: utf8
#

#
# This must be run from the Wesnoth root directory.
#
# Currently something strange is generated when using utf8, this needs a fix.
# I tried this solution, but it does not really work either (though the
# resulting output should be plain utf8 and the webbrowser is able to display
# things correctly, the wiki does not like it...
# Change text.encode to utf16 and do this to have the pasteable entry in the
# terminal (or redirect the output of the iconv line into a file and paste the
# content of it):
# ./data/tools/about_cfg_to_wiki > wiki_utf16
# iconv --from-code=utf16 --to-code=utf8 wiki_utf16
# rm wiki_utf16
#

import sys, re, glob

import wesnoth.wmldata as wmldata

def output(text):
    sys.stdout.write(text.encode("utf8"))

if __name__ == "__main__":
    import optparse, subprocess
    try: import psyco
    except ImportError: pass
    else: psyco.full()

    optionparser = optparse.OptionParser()
    options, args = optionparser.parse_args()

    files = ["data/core/about.cfg"]
    files.extend(glob.glob("data/campaigns/*/_main.cfg"))

    # Parse WML.
    class Section: pass
    class Entry: pass
    chapters = []
    for arg in files:
        sections = []
        wml = wmldata.read_file(arg)
        if not wml.get_first("about"):
            wml = wml.get_first("campaign")
        if not wml.get_first("about"):
            sys.stderr.write("No about section found in %s\n" % arg)
        for about in wml.get_all("about"):
            section = Section()
            section.title = about.get_text_val("title")
            section.lines = []
            for entry in about.get_all("entry"):
                name = entry.get_text_val("name")
                comment = entry.get_text_val("comment", "")
                wikiuser = entry.get_text_val("wikiuser", "")
                email = entry.get_text_val("email", "")
                # Mask email names from spammers
                email = email.replace("@", "&#x40;").replace(".", "&#x2E;")
                # Interpret our local conventions for obfuscsting in SVN files
                email = email.replace("_AT_", "&#x40;").replace("_DOT_", "&#x2E;")
                section.lines.append((name, comment, wikiuser, email))
            if section.title: sections.append(section)
        chapters.append((arg, sections))

    # Output.
    output("""
{| style="float:right"
|
__TOC__
|}
__NOEDITSECTION__
In July 2003, '''David White''' released the first version of Wesnoth. Since
then, many people have joined the project, contributing in very different ways.

To make any changes to this list, please modify about.cfg in SVN or ask any
developer to do it for you.

""".lstrip())
    for path, sections in chapters:
        if path == "data/core/about.cfg":
            output("== Contributors ==\n")
        else:
            slash1 = path.rfind("/")
            slash2 = path.rfind("/", 0, slash1)
            beautified = path[slash2 + 1:slash1]
            beautified = beautified.replace("_", " ")
            beautified = beautified[0].upper() + beautified[1:]
            output("== " + beautified + " ==\n")
        for section in sections:
            output("=== %s ===\n" % section.title)
            for name, comment, wikiuser, email in section.lines:
                if name == "*":
                    output("<hr>\n")
                    continue
                if comment: comment = " - " + comment
                if wikiuser:
                    # If a wiki user is given, the nickname is turned into a
                    # wiki link, or else the whole name.
                    if "(" in name:
                        name = re.sub(r"\((.*)\)", "([[User:%s|\\1]])" % wikiuser, name)
                    else:
                        # The whole name is turned into a link, but also an
                        # email is given - in this case add an extra link.
                        if email:
                            name += " ([[User:%s|%s]])" % (wikiuser, wikiuser)
                        else:
                            name = "[[User:%s|%s]]" % (wikiuser, name)
                if email:
                    if "(" in name:
                        name, nick = name.split("(", 1)
                        name = name.strip()
                        name = "[mailto:" + email + " " + name + "]"
                        name += " (" + nick
                    else:
                        name = "[mailto:" + email + " " + name + "]"
                output("* %s%s\n" % (name, comment))

    output("""[[Category:Generated]]""")
