User:Rjwilmsi/Test2

From Wikipedia, the free encyclopedia

//

/*  This script replaces hyphens with endashes in specified template/infobox parameter values

    It is a custom module script for AutoWikiBrowser.

    It has two parts:
    (1) a list of templates and their parameter(s) to process
    (2) a code part to execute the logic on each entry in the list.

    The list part of the module may be maintained to add new templates or remove false positives.

    The original author, [[User:Rjwilmsi]], does not have specific knowledge of all the templates in the list,
    if you use this custom module you are responsible for your edits and should take care that the
    changes made by the module are indeed correct. There may be false positives.

    This script is licensed under the GPL v2, or a later version if you choose. */

// Part 1: list of templates and parameters; each one in list will have all hyphens converted to endashes
public static List<string> TemplatesAndValues = new List<string>();
public void Load()
{
// format: 'template,parameter' i.e. CSV. Code logic will handle first letter case sensitivity and underscores vs spaces.
TemplatesAndValues.Add(@"Infobox college coach,CurrentRecord");
TemplatesAndValues.Add(@"Infobox college coach,OverallRecord");
TemplatesAndValues.Add(@"Infobox college coach,BowlRecord");
TemplatesAndValues.Add(@"Infobox college coach,TournamentRecord");
TemplatesAndValues.Add(@"college coach infobox,CoachYears");
// add more template & parameter combinations below here...

// and above here
}
// Part 2: code to execute hyphen conversion logic on each entry in list.
// Maintaining the list of templates and parameters should not require you to change anything below this line.
 public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
int replacements = 0;
string ArticleTextBefore = ArticleText;

if(TemplatesAndValues.Count ==0)
	Load();

List<string[]> TemplatesAndValuesSplit = new List<string[]>();

// split templates and parameters list at comma
foreach(string s in TemplatesAndValues)
{
  TemplatesAndValuesSplit.Add(s.Split(','));
}

// loop through each template & parameter combination
foreach(string[] tav in TemplatesAndValuesSplit)
{
  string templateName = tav[0];
  string parameterName = tav[1];

  // loop through each match for the current template
  foreach(Match m in Tools.NestedTemplateRegex(templateName).Matches(ArticleText))
  {
    string template = m.Value;
    
    string currentValue = Tools.GetTemplateParameterValue(template, parameterName);
    string newValue = Regex.Replace(currentValue, @"([^\!\-])\-", "$1–"); // Unicode endash on right

    // update parameter value if hyphen replaced with endash
    if(!newValue.Equals(currentValue))
    {
      // technical note: if logic is later changed such that ArticleText length changes due to hyphen replacement this method may not work correctly
	  ArticleText = ArticleText.Substring(0, m.Index) + Tools.UpdateTemplateParameterValue(template, parameterName, newValue) + ArticleText.Substring(m.Index+m.Length);
      replacements++;
    }
  }
}

Summary = "Template hyphen fixes: replaced " + replacements + " hyphens with endashes";

Skip = ArticleText.Equals(ArticleTextBefore);

return ArticleText;
}
//