#!/usr/bin/env python # vim: tabstop=4: shiftwidth=4: expandtab: softtabstop=4: autoindent: """ Copyright (C) 2007 - 2009 by Mark de Wever Part of the Battle for Wesnoth Project http://www.wesnoth.org/ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. See the COPYING file for more details. The wiki grabber is a tool to convert wiki comment formatting[1] into a text page which can be used in the wiki. [1] http://wesnoth.org/wiki/Wiki_grabber """ from __future__ import with_statement # For python < 2.6 import operator import os import sys import re try: import argparse except ImportError: print 'Please install argparse by running "easy_install argparse"' sys.exit(1) if __name__ == "__main__": # setup and parse command line arguments # The defaults are set to the values of the older hardcoded implementation. parser = argparse.ArgumentParser(description='The wiki grabber is a tool' + ' to convert wiki comment formatting into a text page which can' + ' be used in the wiki. For more details, see' + ' http://wesnoth.org/wiki/Wiki_grabber') parser.add_argument('-s', '--src-dir', default='../src/gui', dest='src_dir', help="the location of wesnoth's source code") parser.add_argument('-o', '--output', default='/tmp/', dest='output_dir', help='the output directory') args = parser.parse_args() # contains all output generated: # - key filename # - value node list # # every node is a list with 2 items # - first the sorting order # - second the actual data file_map = {} # contains all macros: # - key macro name # - value macro contents macro_map = {} output_directory = args.output_dir src_directory = args.src_dir if not os.path.isdir(output_directory): raise IOError("'%s' isn't a directory." % output_directory) if not os.path.isdir(src_directory): raise IOError("'%s' isn't a directory." % src_directory) # current file being processed current_file = "" # current block being processed current_block = "" re_record_start = '^\s*' # There must be whitespace between the field separator (&). # However if there is an empty field eg '...foo & & bar...' # The first field separator eats the whitespace before the second # which would \s+ cause to fail. # The solution is to make the whitespace optional and assert the # character before the ampersand is whitespace. re_field_separator = '\s*(?:(?<=\s))&\s+' # Same issue as with the re_field_separator but then with '...foo & $' re_record_end = '\s*(?:(?<=\s))\$$' re_variable = '([a-zA-Z]\w*)' re_string = '(.+?)' def is_empty(res, data): """ This checks whether or not a table is empty and writes to stderr if it is. It returns True if the table is empty, False otherwise. """ if not res: sys.stderr.write("Empty container:\n%s\n" % data) return True return False def reindent(data): """Converts the raw input to an easier to use format. Lines starting with 8 spaces are concatenated with the previous line. The start of line ' *' are removed and if another space exists it's also removed.""" # concatenate data = re.sub(r'\n \*(?: ){8,}', " ", data) # strip data = re.sub(" \*(?: |)", "", data) #annotation data = re.sub(r'@(?:begin|end|allow|remove)\{(?:parent|tag|link|global|type|key)\}(?:\{.*\})', "", data) return data def get_value(data, key): """ Extracts data from a key value pair, a key must start at the start of a line. """ res = re.compile("^" + key + " *= *(.*)$", re.M).search(data) if res != None: res = res.group(1) return res def process_header(data): """Processes the header.""" page = get_value(data, "@page") order = get_value(data, "@order") if order == None: order = 10000 return [page, order] def debug_dump(data, res): """Show the data the regex retrieved from a match. data is the raw data the regex tried to match. res is the result of the regex.findall. """ sys.stderr.write("data : %s" % data) for i, val in res: for j, sub_val in val: sys.stderr.write("Line %s match %s: %s\n" % (i, j, sub_val)) def format(data): """Formats the data for the wiki. Replaces the following: - An end of line and its surrounding whitespace to a single space. - @$ -> $ - @& -> & - @* -> \n* needed in a list. - @- -> \n needed to add text after a list. """ data = re.sub(r'\s*\n\s*', ' ', data) data = re.sub(r'@\$', '$', data) data = re.sub(r'@&', '&', data) data = re.sub(r'@\*', "\n*", data) data = re.sub(r'@\-', "\n", data) return data def create_config_table(data): """Creates a table for data in a config table. A config table is a table with info about WML configuration key value pairs. """ # matches a line like # x1 & f_unsigned & 0 & The x coordinate of the # startpoint. $ # x1 & f_unsigned & & The x coordinate of the # startpoint. $ regex = re.compile("([A-Za-z]\w*) +& +([A-Za-z]\w*) +& +([^&]*?) *& +(.*) +\$") res = regex.findall(data) regex = re_record_start regex += re_variable # 0 variable id regex += re_field_separator regex += re_variable # 1 variable type regex += re_field_separator regex += '(.*?)' # 2 optional default value, if omitted mandatory field regex += re_field_separator regex += re_string # 3 description regex += re_record_end regex = re.compile(regex, re.DOTALL | re.MULTILINE) res = regex.findall(data) if is_empty(res, data): return "Empty table." result = '{| border="1"' result += "\n!key\n!type\n!default\n!description\n" for i in res: result += "|-\n" result += "| %s\n" % i[0] result += "| [[GUIVariable#%s|%s]]\n" % (i[1], i[1]) if not i[2]: result += "| mandatory\n" else: result += "| %s\n" % i[2] result += "| %s\n" % format(i[3]) result += "|}" return result def create_formula_table(data): """Creates a table for data in a formula table. A formula table is a table with info about which function parameters are available for processing in WML formulas. """ regex = re_record_start regex += re_variable # 0 variable id regex += re_field_separator regex += re_variable # 1 variable type regex += re_field_separator regex += re_string # 2 description regex += re_record_end regex = re.compile(regex, re.DOTALL | re.MULTILINE) res = regex.findall(data) if is_empty(res, data): return "Empty table." result = '{| border="1"' result += "\n!Variable\n!type\n!description\n" for i in res: result += "|-\n" result += "| %s\n" % i[0] result += "| %s\n" % i[1] result += "| %s\n" % format(i[2]) result += "|}" return result def create_variable_types_table(data): """Creates a table for the variable types.""" regex = re_record_start regex += re_variable # 0 variable type regex += re_field_separator regex += re_string # 1 description regex += re_record_end regex = re.compile(regex, re.DOTALL | re.MULTILINE) res = regex.findall(data) if is_empty(res, data): return "Empty table." result = '{| border="1"' result += "\n!Variable\n!description\n" for i in res: result += "|-\n" result += '| %s\n' % (i[0], i[0]) result += "| %s\n" % format(i[1]) result += "|}" return result def create_widget_overview_table(data): """Creates a table for all available widgets.""" regex = re_record_start regex += re_variable # widget type regex += re_field_separator regex += re_string # description regex += re_record_end regex = re.compile(regex, re.DOTALL | re.MULTILINE) res = regex.findall(data) if is_empty(res, data): return "Empty table." result = '{| border="1"' result += "\n!Section\n!Description\n" for i in res: result += "|-\n" result += '| ' % i[0].lower() result += re.sub(r'_', ' ', i[0]) result += "" result += " ([[GUIWidgetDefinitionWML#%s|definition]]" % i[0] result += ", [[GUIWidgetInstanceWML#%s|instantiation]])\n" % i[0] result += "| %s\n" % format(i[1]) result += "|}" return result def create_window_overview_table(data): """Creates a table for all available windows.""" regex = re_record_start regex += re_variable # 0 window id regex += re_field_separator regex += re_string # 1 description regex += re_record_end regex = re.compile(regex, re.DOTALL | re.MULTILINE) res = regex.findall(data) if is_empty(res, data): return "Empty table." result = '{| border="1"' result += "\n!Section\n!Description\n" for i in res: result += "|-\n" result += "| " + re.sub(r'_', ' ', i[0]) result += " ([[GUIWindowDefinitionWML#%s|definition]])\n" % i[0] result += "| %s\n" % format(i[1]) result += "|}" return result def create_dialog_widgets_table(data): """Creates a table for the widgets in a dialog.""" regex = re_record_start regex += '(-*)' # 0 indention markers regex += '((?:[a-zA-Z_]?)\w*)' # 1 optional id may start with an underscore regex += re_field_separator regex += '(.*?)' # 2 optional retval regex += re_field_separator regex += re_variable # 3 type regex += re_field_separator regex += '([om])' # 4 mandatory flag regex += re_field_separator regex += re_string # 5 description regex += re_record_end regex = re.compile(regex, re.DOTALL | re.MULTILINE) res = regex.findall(data) if is_empty(res, data): return "Empty table." result = '{| border="1"' result += "\n!ID (return value)\n!Type\n!Mandatory\n!Description\n" for i in res: result += "|-\n| " + " " * len(i[0]) * 8 if not i[1]: result += "''free to choose''" else: result += i[1] if not i[2]: result += "\n" else: result += " (%s)\n" % i[2] result += "| [[GUIToolkitWML#%s|%s]]\n" % (i[3], i[3]) if i[4] == 'm': result += "| yes\n" else: result += "| no\n" result += "| %s\n" % format(i[5]) result += "|}" return result def validate_table(table): """Validates a table. At the moments tests for whitespace around separators.""" regex = '((?