Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Convert a list of names to HTML Wikipedia links

5.00/5 (4 votes)
8 Aug 2014CPOL3 min read 27.9K  
How to convert a list of names to the HTML needed to create a link to the corresponding Wikipedia articles

The Eagles Have Landed

If you have a list of names of famous people such as this:

  • Glenn Frey
  • Don Henley
  • Bernie Leadon
  • Randy Meisner
  • Joe Walsh
  • Timothy B. Schmit
  • Don Felder
  • Jackon Browne
  • J.D. Souther

...you may want to linkify the names to point to their respective wikipedia articles. To do so is not difficult, because wikipedia follows a standard format for the URLs for its entries - in most cases for proper names, it simply replaces the space between the first and last name with an underscore, like so: "Chris Hillman" becomes "Chris_Hillman". The link for the Byrdman of Bakersfield (poetic license) is: href="http://en.wikipedia.org/wiki/Chris_Hillman"

There's always an exception, though (there may be an exception to that rule, too, which would be nice). In this case, though, there's not an exception to the exception rule (oh, pooh!), so we have to watch out for "special cases" such as names with initials. In this case, wikipedia retains the period but places an underscore after it, such as: http://en.wikipedia.org/wiki/A._A._Milne

Still, programatically transforming names into the accepted format is pretty easy. Here's some example code; you can pass the name of any person with a wikipedia page to the Wikipidify() method to get back the appropriate HTML for the link to that wikipedia page.

The sample code uses names of current and former members of The Eagles (the Eagles from Southern California, not the Philadelphia Eagles, football fans), along with the names of some affiliated/associated people, such as "The Fifth Eagle," Jackson Browne, as well as songwriter J.D. Souther. A fictional character, Rupert Pupkin (The King of Comedy ) is also included to show what happens when a person in the list does not have a wikipedia page (the link is created fine, but clicking it takes you to a "Winchester Mystery page").

C#
private void buttonYourLip_Click(object sender, EventArgs args)
{
    List<string> eaglets = new List<string>();
    eaglets.Add("Glenn Frey");
    eaglets.Add("Don Henley");
    eaglets.Add("Bernie Leadon");
    eaglets.Add("Randy Meisner");
    eaglets.Add("Joe Walsh");
    eaglets.Add("Timothy B. Schmit");
    eaglets.Add("Don Felder");
    eaglets.Add("Jackon Browne");
    eaglets.Add("J.D. Souther");
    eaglets.Add("Rupert Pupkin");

    List<string> htmlifiedEaglets = new List<string>();

    foreach (var eaglet in eaglets)
    {
        htmlifiedEaglets.Add(Wikipidify(eaglet));
    }
    System.IO.File.WriteAllLines(@"C:\alreadyGone\eaglets.txt", htmlifiedEaglets);
}

private string Wikipidify(string eaglet)
{
    const String wikipediaTemplate = "<a href=\"http://en.wikipedia.org/wiki/{0}\"  target=\"_blank\">{1}</a>";
    string HTMLifiedEaglet = String.Empty;
    string underlinedEaglet = String.Empty;
    // make sure each "." is followed by a space
    underlinedEaglet = eaglet.Replace(".", ". ");
    // ...if there are now two spaces anywhere (such as where a period was already followed by a space), make them one 
    underlinedEaglet = underlinedEaglet.Replace("  ", " "); 
    // Now replace the single space[s] with underscore[s]
    underlinedEaglet = underlinedEaglet.Replace(" ", "_");
            
    HTMLifiedEaglet = String.Format(wikipediaTemplate, underlinedEaglet, eaglet);
    return HTMLifiedEaglet;
}
</string></string></string></string>

As you can see, the results are saved to a file. The file created for the list shown above looks like this:

<a href="http://en.wikipedia.org/wiki/Glenn_Frey" target="_blank">Glenn Frey</a> <a href="http://en.wikipedia.org/wiki/Don_Henley" target="_blank">Don Henley</a> <a href="http://en.wikipedia.org/wiki/Bernie_Leadon" target="_blank">Bernie Leadon</a> <a href="http://en.wikipedia.org/wiki/Randy_Meisner" target="_blank">Randy Meisner</a> <a href="http://en.wikipedia.org/wiki/Joe_Walsh" target="_blank">Joe Walsh</a> <a href="http://en.wikipedia.org/wiki/Timothy_B._Schmit" target="_blank">Timothy B. Schmit</a> <a href="http://en.wikipedia.org/wiki/Don_Felder" target="_blank">Don Felder</a> <a href="http://en.wikipedia.org/wiki/Jackson_Browne" target="_blank">Jackson Browne</a> <a href="http://en.wikipedia.org/wiki/J._D._Souther" target="_blank">J.D. Souther</a> <a href="http://en.wikipedia.org/wiki/Rupert_Pupkin" target="_blank">Rupert Pupkin</a>

If you don't want to take the time to add this code to a project just now, you can see the list of these generated links here

Outro

 

As you might surmise, I am a big fan of the Eagles; I bought their debut (eponymous) LP when it was still warm to the touch - in my birthplace of Fort Bragg, California. Although nobody asked me, I feel their best album - a masterpiece! - is the concept album "Desperado" (it also has the best back cover photo of all time).

Fade Out

 

I like almost everything the Eagles ever did, through all their personnel changes, with one blatant and glaring exception: "The Best of My Love" is one of the lamest songs I've ever heard, vying with the Commodore's "Three Times a Lady" for the most gag-worthy song of all time from otherwise good-great bands. If you agree with me that they sound like they've been hypnotized by Barry Manilow on that tune, go to your window, open it, lean out and shout at the tip-top of your voice: "I'm fed up, and I'm not gonna take it any more! No more plodding, saccharine love songs by people who can do so much better!!!"

Not Fade Away

 

You can purchase any of the Eagles albums from here

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)