User:RoyCurtis/WikiTemplateTransmute.cs

From ActiveWiki
Revision as of 04:02, 16 November 2012 by RoyCurtis (talk | contribs) (updated)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is a utility I have created to transmute old-style infoboxes to new; it is used by AutoWikiBrowser when processing an article.

C# code

Uses .NET Framework 4.5
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace WikiTemplateTransmute
{
    class Program
    {
        static string filePath;
        static string article;

        static void WARN(string msg, params string[] parts)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(msg, parts);
            Console.ForegroundColor = ConsoleColor.White;
        }

        static void ERROR(string msg, params string[] parts)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(msg, parts);
            Console.ForegroundColor = ConsoleColor.White;
        }

        static void Main(string[] args)
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.White;
                filePath = args[0];
                if (!File.Exists(filePath))
                {
                    ERROR("Fed non-existant file path: {0}", filePath);
                    return;
                }

                Console.WriteLine("Reading article file {0}", filePath);
                article = File.ReadAllText(filePath);

                // Replace Citizen infoboxes
                article = Regex.Replace(article,
@"\{\{Citizenbox
\|(?<name>.*?)
\|(?<uni>.*?)
\|(?<nicks>.*?)
\|(?<status>.*?)
\|(?<date>.*?)
\|(?<citnum>.*?)
\|(?<location>.*?)
\|(?<avatar>.*?)
\|(?<world>.*?)
\|(?<build>.*?)
\|(?<www>.*?)\}\}",
@"{{Citizen
|imagearea=${name}
|universe=${uni}
|aliases=${nicks}
|footer=${status}
|since=${date}
|citnums=${citnum}
|location=${location}
|avatars=${avatar}
|worlds=${world}
|builds=${build}
|www=${www}
}}", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Singleline);

                // Replace single-line Citizen infoboxes
                article = Regex.Replace(article,
@"\{\{Citizenbox\|(?<name>.*?)\|(?<uni>.*?)\|(?<nicks>.*?)\|(?<status>.*?)\|(?<date>.*?)\|(?<citnum>.*?)\|(?<location>.*?)\|(?<avatar>.*?)\|(?<world>.*?)\|(?<build>.*?)\|(?<www>.*?)\}\}",
@"{{Citizen
|imagearea=${name}
|universe=${uni}
|aliases=${nicks}
|footer=${status}
|since=${date}
|citnums=${citnum}
|location=${location}
|avatars=${avatar}
|worlds=${world}
|builds=${build}
|www=${www}
}}", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Singleline);

                // Replace Building infoboxes
                article = Regex.Replace(article,
@"\{\{Building
\|(?<name>.*?)
\|(?<tagline>.*?)
\|(?<location>.*?)
\|(?<size>.*?)
\|(?<builders>.*?)
\|(?<purpose>.*?)
\|(?<category>.*?)
\|(?<image>.*?)\}\}",
@"{{Building
|image=${image}
|tagline=${tagline} ${purpose}
|location=${location}
|size=${size}
|builders=${builders}
}}", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Singleline);

                // Replace Community infoboxes
                article = Regex.Replace(article, @"Approx. ?([0-9])", "~$1", RegexOptions.IgnoreCase);
                article = Regex.Replace(article,
@"\{\{Community
\|(?<name>.*?)
\|(?<world>.*?)
\|(?<location>.*?)
\|(?<population>.*?)
\|(?<founded>.*?)
\|(?<leaders>.*?)
\|(?<www>.*?)(
\|(?<image>.*?))?\}\}",
@"{{Community
|origin=${world}
|location=${location}
|users=${population}
|founded=${founded}
|leaders=${leaders}
|www=${www}
|image=${image}
}}", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Singleline);

                // Replace World infoboxes
                article = Regex.Replace(article,@"\{\{TouristAccess\}\}", "{{yes}}", RegexOptions.IgnoreCase);
                article = Regex.Replace(article, @"\{\{Active\}\}", "{{yes}}", RegexOptions.IgnoreCase);
                article = Regex.Replace(article, @"\{\{Expire\}\}", "{{no}}", RegexOptions.IgnoreCase);
                article = Regex.Replace(article, @"\{\{NoTouristAccess\}\}", "{{no}}", RegexOptions.IgnoreCase);
                article = Regex.Replace(article,
@"\{\{World
\|(?<name>.*?)
\|(?<universe>.*?)
\|(?<owner>.*?)
\|(?<caretakers>.*?)
\|(?<size>.*?)
\|(?<founded>.*?)
\|(?<type>.*?)
\|(?<www>.*?)
\|(?<image>.*?)\n?(\|?
(\|?(?<active>.*?))?
(\|(?<tourist>.*?)(\}\})?\n?)?)?\}\}",
@"{{World
|image=${image}
|universe=${universe}
|owners=* ${owner}
* ${caretakers}
|founded=${founded}
|size=${size}
|users=
|rating=
|running=${active}
|tourist=${tourist}
|www=${www}
}}", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Singleline);

                Console.WriteLine("Writin article file {0}", filePath);
                File.WriteAllText(filePath, article);
            }
            catch (Exception e)
            {
                ERROR("Exception hit: {0}", e.Message);
                Console.WriteLine(e.StackTrace);
                Console.ReadKey();
            }
        }
    }
}