WikiCrawler/MediaWiki/WikiUtils.cs
/// <summary>
/// Add the specified category to the text.
/// </summary>
Add the specified category to the text.
Add the specified category to the text .
public static string AddCategory(string name, string text)
		{
			if (!name.StartsWith("Category:") && !name.StartsWith("category:"))
			{
				name = "Category:" + name;
			}
			if (!HasCategory(name, text))
			{
				int c = text.Length - 1;
				int lastCatStart = text.LastIndexOf("[[Category:", StringComparison.OrdinalIgnoreCase);
				if (lastCatStart >= 0)
				{
					c = text.IndexOf("]]", lastCatStart) + 1;
				}
				else
				{
					for (; c >= 0; c--)
					{
						if (!char.IsWhiteSpace(text[c]))
						{
							if (c > 0 && text[c] == ']' && text[c - 1] == ']')
							{
								int linkstart = text.Substring(0, c).LastIndexOf("[[");
								if (linkstart >= 0)
								{
									string content = text.Substring(linkstart + 2, c - 2 - (linkstart + 2));
									string[] contentNsSplit = content.Split(':');
									if (contentNsSplit.Length >= 2
										&& string.Compare(contentNsSplit[0], "category", true) != 0
										&& string.Compare(contentNsSplit[0], "template", true) != 0)
									{
										c = linkstart;
									}
									else
									{
										break;
									}
								}
								else
								{
									break;
								}
							}
							else if (c > 1 && text[c] == '>' && text[c - 1] == '-' && text[c - 2] == '-')
							{
								c = text.Substring(0, c).LastIndexOf("<!--");
							}
							else
							{
								break;
							}
						}
					}
				}
				return text.Substring(0, c + 1)
					+ "\n[[" + name + "]]"
					+ text.Substring(c + 1, text.Length - (c + 1));
			}
			else
			{
				return text;
			}
		}
public static string AddCategory ( string name , string text ) { if ( ! name . StartsWith ( " " ) && ! name . StartsWith ( " " ) ) { name = " " + name ; } if ( ! HasCategory ( name , text ) ) { int c = text . Length - 1 ; int lastCatStart = text . LastIndexOf ( " " , StringComparison . OrdinalIgnoreCase ) ; if ( lastCatStart >= 0 ) { c = text . IndexOf ( " " , lastCatStart ) + 1 ; } else { for ( ; c >= 0 ; c -- ) { if ( ! char . IsWhiteSpace ( text [ c ] ) ) { if ( c > 0 && text [ c ] == ' ' && text [ c - 1 ] == ' ' ) { int linkstart = text . Substring ( 0 , c ) . LastIndexOf ( " " ) ; if ( linkstart >= 0 ) { string content = text . Substring ( linkstart + 2 , c - 2 - ( linkstart + 2 ) ) ; string [ ] contentNsSplit = content . Split ( ' ' ) ; if ( contentNsSplit . Length >= 2 && string . Compare ( contentNsSplit [ 0 ] , " " , true ) != 0 && string . Compare ( contentNsSplit [ 0 ] , " " , true ) != 0 ) { c = linkstart ; } else { break ; } } else { break ; } } else if ( c > 1 && text [ c ] == ' ' && text [ c - 1 ] == ' ' && text [ c - 2 ] == ' ' ) { c = text . Substring ( 0 , c ) . LastIndexOf ( " " ) ; } else { break ; } } } } return text . Substring ( 0 , c + 1 ) + " \n " + name + " " + text . Substring ( c + 1 , text . Length - ( c + 1 ) ) ; } else { return text ; } }
Add the specified category to the text.
Add the specified category to the text .
// if there are existing categories, add after the last one // backtrack past any interwiki links, and comments // interwiki, keep going // not an interwiki, break // backtrack through comment
name
string
text
string