using System;
using System.Text;
using System.Collections.Generic;
public static class ClipboardFusionHelper
{
private static readonly string[] ExceptionsArray =
{
//articles
"a",
"an",
"the",
//prepositions (incomplete)
"on",
"in",
"at",
"for",
"ago",
"to",
"from",
"by",
"of",
"with",
//coordinating conjunctions
"and",
"but",
"or",
"nor",
"for",
"yet",
"so"
};
private static readonly HashSet<string> Exceptions = new HashSet<string>(ExceptionsArray);
public static string ProcessText(string text)
{
//get the highlighted text
text = BFS.Clipboard.CopyText();
//convert the text to lower case
string lower = text.ToLowerInvariant();
//if the copied text is lower case, convert it to title case
if(lower.Equals(text, StringComparison.Ordinal))
{
BFS.Clipboard.PasteText(ToTitleCaseSmart(text));
return null;
}
//convert to text to title case
string title = ToTitleCaseSmart(text);
//if the copied text is title case, convert it to upper case
if(title.Equals(text, StringComparison.Ordinal))
{
BFS.Clipboard.PasteText(text.ToUpperInvariant());
return null;
}
//otherwise, paste the lower case text
BFS.Clipboard.PasteText(lower);
return null;
}
private static string ToTitleCaseSmart(string text)
{
Queue<string> words = new Queue<string>(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(text.ToLowerInvariant()).Split(new char[] { ' ' }));
//Always leave the first word capitalized, regardless of what it is
StringBuilder builder = new StringBuilder(words.Dequeue());
while(words.Count > 0)
{
string word = words.Dequeue();
string lower = word.ToLower();
builder.Append(" ");
if(Exceptions.Contains(lower))
builder.Append(lower);
else
builder.Append(word);
}
return builder.ToString();
}
}