User:Kernigh/wikify.py
Jump to navigation
Jump to search
Forked from User:Jayt#Source wikification script.
#!/usr/bin/python #version 20120530 # 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.7EF3" # 1.3d/2.3e/3.1.0/3.4.3/etc. latestversion = "0.0.7EF3" # 3.4.3 or 0.0.07E7F3 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") longname = gamename + ' ' + uploadversion if gamename == "NetHack" and uploadversion == latestversion: wikilink = 'Source:' + srcfile srccodelink = '[[source code]]' linelink = '{{source code|' + srcfile + '|123}}' catlink = "[[Category:Source code]]" oldwarning = '' else: wikilink = 'Source:' + gamename + ' ' + uploadversion + '/' + srcfile srccodelink = ('[[' + gamename + ' ' + uploadversion + ' source code|source code]]') linelink = ('{{sourcecode|' + srcfile + '|123|version=' + gamename + ' ' + uploadversion + '}}') catlink = ("[[Category:" + gamename + ' ' + uploadversion + ' ' + "source code|" + srcfile + "]]") 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 = ''+wikiline+'' g.writelines([' <span id="line', str(i), '">', wikiline, '</span>\n']) i += 1 g.writelines([catlink, '\n'])
Changelog:
- 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)