[^\"'>\\s]+))");
imgPattern.Append("[^>]*>"); //end of tag
Regex imgRegex = new Regex(imgPattern.ToString(), RegexOptions.IgnoreCase);
//look for matches
Match imgcheck = imgRegex.Match(htmlPage);
ArrayList imagelist = new ArrayList();
//add base href for relative urls
imagelist.Add("" + url);
while(imgcheck.Success)
{
string src = imgcheck.Groups["src"].Value;
string image = returnWithTag ? "
" : src;
imagelist.Add(image);
imgcheck = imgcheck.NextMatch();
}
string[] images = new string[imagelist.Count];
imagelist.CopyTo(images);
return images;
}
///
/// Return the images links in a given URL as an array of XHTML-compliant img tags.
///
/// The URL to extract the images from.
///
public static string[] ScrapeImages(string url)
{
return ScrapeImages(url, true);
}
///
/// Scrapes a web page and parses out all the links.
///
/// The URL.
/// if set to true [make linkable].
///
public static string[] ScrapeLinks(string url, bool makeLinkable)
{
//get the content of the url
//ReadWebPage is another method in this useful methods collection
string htmlPage = ReadWebPage(url);
//set up the regex for finding the link urls
StringBuilder hrefPattern = new StringBuilder();
hrefPattern.Append("]+"); //start 'a' tag and anything that comes before 'href' tag
hrefPattern.Append("href\\s*=\\s*"); //start href property
//three possibilities for what href property --
//(1) enclosed in double quotes
//(2) enclosed in single quotes
//(3) enclosed in spaces
hrefPattern.Append("(?:\"(?[^\"]*)\"|'(?[^']*)'|(?[^\"'>\\s]+))");
hrefPattern.Append("[^>]*>.*?"); //end of 'a' tag
Regex hrefRegex = new Regex(hrefPattern.ToString(), RegexOptions.IgnoreCase);
//look for matches
Match hrefcheck = hrefRegex.Match(htmlPage);
ArrayList linklist = new ArrayList();
//add base href for relative links
linklist.Add("" + url);
while(hrefcheck.Success)
{
string href = hrefcheck.Groups["href"].Value; //link url
string link = (makeLinkable)
? "" + href + ""
: href;
linklist.Add(link);
hrefcheck = hrefcheck.NextMatch();
}
string[] links = new string[linklist.Count];
linklist.CopyTo(links);
return links;
}
///
/// Calls the Gravatar service to and returns an HTML
tag for use on your pages.
///
/// The email of the user
/// The size of the Gravatar image - 60 is standard
/// HTML image tag
public static string GetGravatar(string email, int size)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(Encoding.ASCII.GetBytes(email));
StringBuilder hash = new StringBuilder();
for(int i = 0; i < result.Length; i++)
hash.Append(result[i].ToString("x2"));
StringBuilder image = new StringBuilder();
image.Append("
");
return image.ToString();
}
///
/// Given a valid email address, returns a short javascript block that will emit a valid
/// mailto: link that can't be picked up by address harvesters. Call this method where you
/// would normally place the link in your html code.
///
/// The email address to convert to spam-free format
///
public static string CreateSpamFreeEmailLink(string emailText)
{
if(!String.IsNullOrEmpty(emailText))
{
string[] parts = emailText.Split(new char[] {'@'});
if(parts.Length == 2)
{
StringBuilder sb = new StringBuilder();
sb.Append("");
return sb.ToString();
}
}
return String.Empty;
}
///
/// A simple utility to output Lorem Ipsum text to your page.
///
/// int count, the number of either paragraphs, words, or characters you would like to display.
/// string method, 'p' for paragraphs or 'w' for words or 'c'c for characters
///
/// string with the resulting lorem ipsum text
///
public static string GenerateLoremIpsum(int count, string method)
{
Random r = new Random();
if(method.ToLower() == "p" && count > 1000)
throw new ArgumentOutOfRangeException("count", "Sorry, lorem ipsum control only allows 1000 or less paragraphs.");
string loremIpsum = LoadTextFromManifest("loremIpsum.txt");
if(null == loremIpsum)
throw new Exception("Could not load loremipsum.txt");
StringBuilder sb = new StringBuilder();
if(String.IsNullOrEmpty(method) || method.ToLower() == "p" || (method.ToLower() != "p" && method.ToLower() != "c" && method.ToLower() != "w"))
{
char[] split = {'|'};
string[] paras = loremIpsum.Split(split);
ArrayList paraList = new ArrayList();
//need a nonfixed array
foreach(string s in paras)
paraList.Add(s);
int parasLength = paras.Length;
if(count > parasLength)
{
int needmore = count - parasLength;
for(int x = 0; x < needmore; x++)
{
int pickme = r.Next(0, parasLength - 1);
paraList.Add(paras[pickme]);
}
}
for(int i = 0; i < count; i++)
{
sb.Append("");
sb.Append(paraList[i]);
sb.Append("
");
}
}
if(method.ToLower() == "c")
{
int loremLength = loremIpsum.Length;
if(count > loremLength)
{
int needmore = (count / loremLength);
for(int x = 0; x < needmore; x++)
{
string newLoremIpsum = loremIpsum;
loremIpsum += " ";
loremIpsum += newLoremIpsum;
}
}
//note: I don't catch the exception here
sb.Append(Utility.ShortenText(loremIpsum, count));
}
if(method.ToLower() == "w")
{
string[] words = loremIpsum.Split(' ');
ArrayList wordList = new ArrayList();
//need a nonfixed array
foreach(string s in words)
wordList.Add(s);
int wordLength = words.Length;
if(count > wordLength)
{
int needmore = count - wordLength;
for(int x = 0; x < needmore; x++)
{
int pickme = r.Next(0, wordLength - 1);
wordList.Add(words[pickme]);
}
}
for(int i = 0; i < count; i++)
{
sb.Append(wordList[i]);
sb.Append(" ");
}
}
return sb.ToString();
}
///
/// Loads the text from manifest.
///
/// Name of the template file.
///
private static string LoadTextFromManifest(string templateFileName)
{
string templateText = null;
Assembly asm = Assembly.GetExecutingAssembly();
using(Stream stream = asm.GetManifestResourceStream("SubSonic.Sugar." + templateFileName))
{
if(stream != null)
{
StreamReader sReader = new StreamReader(stream);
templateText = sReader.ReadToEnd();
sReader.Close();
}
}
return templateText;
}
}
}