User:Kernigh/wikify.py

From NetHackWiki
Jump to navigation Jump to search

Forked from User:Jayt#Source wikification script.

Script

#!/usr/bin/python
#version 20120605
# usage: ./wikify.py sourcefile.c ...
# the first line of output will be the article title, for use with
# an uploadbot
#config:
outputdir="out"                   # where to put the wikified file
gamename = "SLASH'EM"             # NetHack or Hack or SLASH'EM
uploadversion = "0.0.7E7F3"       # 1.3d/2.3e/3.1.0/3.4.3/etc.
latestversion = "0.0.7E7F3"       # 3.4.3 or 0.0.7E7F3
license = "NGPL"                  # NGPL or CWI
##

import sys, re
from os import path

for srcpath in sys.argv[1:]:
    srcfile = path.basename(srcpath)
    f = open(srcpath)
    g = open(path.join(outputdir, srcfile), "w")

    if gamename == "NetHack" and uploadversion == latestversion:
        wikilink = 'Source:' + srcfile
        srccodelink = '[[source code]]'
        linelink = '{{tl|sourcecode|2=' + srcfile + '{{!}}123}}'
        catlink = "[[Category:Source code]]"
    else:
        wikilink = ('Source:' + gamename + ' ' + uploadversion +
                    '/' + srcfile)
        srccodelink = ('[[' + gamename + ' ' + uploadversion +
                       ' source code|source code]]')
        linelink = ('{{tl|sourcecode|2=' + srcfile +
                    '{{!}}123{{!}}version=' +
                    gamename + ' ' + uploadversion + '}}')
        catlink = ("[[Category:" + gamename + ' ' + uploadversion +
                   ' ' + "source code|" + srcfile + "]]")

    if uploadversion == latestversion:
        oldwarning = ''
    else:
        oldwarning = ("'''Warning!''' This is the source code from an "
                      "old release. For the latest release, see [[" +
                      gamename + " source code]].\n\n")

    if gamename == "Hack" or gamename == "NetHack":
        slashwarning = ''
    else:
        slashwarning = ("The latest source code for vanilla NetHack "
                        "is at [[Source code]].\n\n")

    g.writelines([wikilink, '\n',
                  "__MIXEDSYNTAXHIGHLIGHT__\n",
                  "Below is the full text to '''", srcfile,
                  "''' from the ", srccodelink, " of [[",
                  gamename, ' ', uploadversion,
                  "]]. To link to a particular line, write ", linelink,
                  ", for example.\n\n",
                  oldwarning, slashwarning,
                  "{{" + license + "}}\n"])

    i = 1
    for line in f.readlines():
        wikiline = (str(i) + '.').ljust(6) + line.rstrip("\n")
        if re.search("{{",line):
            wikiline = '<nowiki>'+wikiline+'</nowiki>'
        g.writelines([' <span id="line', str(i), '">',
                      wikiline, '</span>\n'])
        i += 1

    g.writelines([catlink, '\n'])

Changelog

  • Use {{tl}} to show {{sourcecode}}. --Kernigh (talk) 16:24, 5 June 2012 (UTC)
  • Use 'Source:' namespace and {{sourcecode}}. Add __MIXEDSYNTAXHIGHLIGHT__ to output. Handle multiple source files in one run. Make minor improvements. --Kernigh (talk) 01:20, 30 May 2012 (UTC)