home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 July / maximum-cd-2010-07.iso / DiscContents / wesnoth-1.8-win32.exe / data / tools / terrain2wiki.rb < prev    next >
Encoding:
Ruby Source  |  2009-09-28  |  2.5 KB  |  112 lines

  1. #!/usr/bin/ruby
  2. # A script to create the "Terrain Table" on the TerrainLettersWML wiki page.
  3. # Run this and splice the outtput into the wiki whenever you add a new 
  4. # terrain type to mainline.
  5.  
  6.  
  7. #create an array of hashes, each hash representing a [terrain] tag
  8. #removes "", _ "" and whitespaces from a value
  9. def get_value(value)
  10.     return "" if !value
  11.     re=value
  12.     if value=~/_ "(.*)"/ then
  13.         re=$1
  14.     elsif value=~/"(.*)"/ then
  15.         re=$1
  16.     end
  17.     return re.strip
  18. end
  19.  
  20. def tags_to_hashes(text)
  21.     rearray=[]
  22.     while text=~/\[terrain\]\s*\n(.*?)\n\[\/terrain\]/m
  23.         text=$'#remove the text that has already been parsed
  24.         content=$1
  25.         #create a hash from the tag's attributes
  26.         content_hash={}
  27.         content.each do |line|
  28.             line=line.chomp("\n").strip
  29.             key,value=line.split("=")
  30.             if key&&value then
  31.                 content_hash[key]=get_value(value)
  32.             end
  33.         end
  34.         rearray<<content_hash
  35.     end
  36.     return rearray
  37. end
  38.  
  39. #create a hash where each terrain string is pointing to the correspondending name(e.g. "Gg"=>"Grassland")
  40. def string_to_name_hash(terrains)
  41.     rehash={}
  42.     terrains.each do |terrain|
  43.         string=terrain["string"]
  44.         name=terrain["name"]
  45.         rehash[string]=name
  46.     end
  47.     return rehash
  48. end
  49.  
  50. def create_table_line(string,name,stats_from)
  51.     return "<tr>
  52. <td>#{string}</td>
  53. <td>#{name}</td>
  54. <td>#{stats_from}</td>
  55. </tr>"
  56. end
  57.  
  58. #create wiki text from array in that terrain tag data is stored
  59. def create_wiki(terrains)
  60.  
  61.     string_to_name=string_to_name_hash(terrains)
  62.  
  63.     table_lines=""
  64.     terrains.each do |terrain|
  65.         string=terrain["string"]
  66.         name=terrain["name"]
  67.         stats_from=""
  68.         #convert the terrain strings from the aliasof into terrain names
  69.         terrain["aliasof"]="" if !terrain["aliasof"]
  70.         terrain["aliasof"].split(",").each do |item|
  71.             stats_from+=string_to_name[item.strip].to_s+", "
  72.         end
  73.         stats_from.chomp!(", ")
  74.         table_lines+=create_table_line(string,name,stats_from)+"\n"
  75.     end
  76.  
  77. return "
  78. <table border=\"1\"><tr>
  79. <th>String</th>
  80. <th>Name</th>
  81. <th>Stats from</th>
  82. </tr>
  83. #{table_lines}
  84. </table>"
  85. end
  86.  
  87. ##input/output engine
  88. def write_file(pat,text)
  89.     file=File.new(pat,"w")
  90.     file.print(text)
  91.     file.close
  92.     return true
  93. end
  94.  
  95. $terrain_file=ARGV[1]
  96. $output_file=ARGV[0]
  97.  
  98. while !$terrain_file
  99.     print "Path of terrain.cfg: "
  100.     $terrain_file=gets.chomp("\n")
  101. end
  102.  
  103. while !$output_file
  104.     print "Where will the wiki text be saved?"
  105.     $output_file=gets.chomp("\n")
  106. end
  107.  
  108. terrain=File.new($terrain_file).read
  109. terrainarray=tags_to_hashes(terrain)
  110. output=create_wiki(terrainarray)
  111. write_file($output_file,output)
  112.