public string BoldTitle(string articleText, string articleTitle, out bool noChange)
{
noChange = true;
List<string> alltemplates = GetAllTemplates(articleText);
// don't apply in exception cases: (<section> used to transclude text in other articles so self link is valid use case)
if (TemplateExists(alltemplates, NoBoldTitle) || articleText.Contains("<section"))
return articleText;
HideText Hider2 = new HideText(), Hider3 = new HideText(true, true, true);
// 1) clean up bolded self links first, provided no noinclude use in article
string afterSelfLinks = BoldedSelfLinks(articleTitle, articleText);
if (!afterSelfLinks.Equals(articleText) && !WikiRegexes.IncludeonlyNoinclude.IsMatch(articleText))
articleText = afterSelfLinks;
// 2) Clean up self wikilinks
string articleTextAtStart = articleText, zerothSection = Tools.GetZerothSection(articleText);
string restOfArticle = articleText.Substring(zerothSection.Length);
string zerothSectionHidden, zerothSectionHiddenOriginal;
// first check for any self links and no bold title, if found just convert first link to bold and return
// https://en.wikipedia.org/wiki/Wikipedia_talk:AutoWikiBrowser/Bugs/Archive_11#Includes_and_selflinks
// don't apply if bold in lead section already or some noinclude transclusion business
if (!SelfLinks(zerothSection, articleTitle).Equals(zerothSection) && !WikiRegexes.IncludeonlyNoinclude.IsMatch(articleText))
{
// There's a limitation here in that we can't hide image descriptions that may be above lead sentence without hiding the self links we are looking to correct
zerothSectionHidden = Hider2.HideMore(zerothSection, false, false, false);
zerothSectionHiddenOriginal = zerothSectionHidden;
zerothSectionHidden = SelfLinks(zerothSectionHidden, articleTitle);
zerothSection = Hider2.AddBackMore(zerothSectionHidden);
if (!zerothSectionHiddenOriginal.Equals(zerothSectionHidden))
{
noChange = false;
return (zerothSection + restOfArticle);
}
}
// Performance check: if article title not in zeroth section have nothing further to do
if (zerothSection.IndexOf(BracketedAtEndOfLine.Replace(articleTitle, ""), StringComparison.OrdinalIgnoreCase) < 0)
return articleTextAtStart;
// 3) '''Emboldens''' the first occurrence of the article title
// ignore date articles (date in American or international format), nihongo title
if (WikiRegexes.Dates2.IsMatch(articleTitle) || WikiRegexes.Dates.IsMatch(articleTitle)
|| TemplateExists(alltemplates, NihongoTitle))
return articleTextAtStart;
string escTitle = Regex.Escape(articleTitle), escTitleNoBrackets = Regex.Escape(BracketedAtEndOfLine.Replace(articleTitle, ""));
Regex boldTitleAlready1 = new Regex(@"'''\s*(" + escTitle + "|" + Tools.TurnFirstToLower(escTitle) + @")\s*'''");
Regex boldTitleAlready2 = new Regex(@"'''\s*(" + escTitleNoBrackets + "|" + Tools.TurnFirstToLower(escTitleNoBrackets) + @")\s*'''");
// if title in bold already exists in article, or paragraph starts with something in bold, don't change anything
// ignore any bold in infoboxes
if (BoldTitleAlready4.IsMatch(Tools.ReplaceWithSpaces(zerothSection, WikiRegexes.InfoBox.Matches(zerothSection))) || DfnTag.IsMatch(zerothSection))
return articleTextAtStart;
string articleTextNoInfobox = Tools.ReplaceWithSpaces(articleText, WikiRegexes.InfoBox.Matches(articleText));
if (boldTitleAlready1.IsMatch(articleTextNoInfobox) || boldTitleAlready2.IsMatch(articleTextNoInfobox)
|| BoldTitleAlready3.IsMatch(articleTextNoInfobox))
return articleTextAtStart;
// so no self links to remove, check for the need to add bold
string articleTextNoTemplates = WikiRegexes.NestedTemplates.Replace(articleText, "");
// first quick check: ignore articles with some bold in first 5% of article, ignoring infoboxes, dablinks etc.
int fivepc = articleTextNoTemplates.Length / 20;
if (articleTextNoTemplates.Substring(0, fivepc).Contains("'''"))
return articleTextAtStart;
Regex regexBoldNoBrackets = new Regex(@"([^\[]|^)(" + escTitleNoBrackets + "|" + Tools.TurnFirstToLower(escTitleNoBrackets) + ")([ ,.:;])");
zerothSectionHidden = Hider3.HideMore(zerothSection);
zerothSectionHiddenOriginal = zerothSectionHidden;
// first try title with brackets removed
zerothSectionHidden = regexBoldNoBrackets.Replace(zerothSectionHidden, "$1'''$2'''$3", 1);
zerothSection = Hider3.AddBackMore(zerothSectionHidden);
articleText = zerothSection + restOfArticle;
// check that the bold added is the first bit in bold in the main body of the article
if (!zerothSectionHiddenOriginal.Equals(zerothSectionHidden) && AddedBoldIsValid(articleText, escTitleNoBrackets))
{
noChange = false;
return articleText;
}
return articleTextAtStart;
}