Skip to content
Browse files

Wikipedia addin improvements.

 - Better regex
 - Better handling of multiple links
 - Set UserAgent (wikipedia doesn't seem to like mono)
 - Use HtmlSanitizer.ToPlainText()
 - Fix code indentation
 - Make preview box slightly prettier (add icon and padding)
  • Loading branch information...
1 parent 7ce188b commit 49bed2976e3699e7ff78b82c703707ba449b8811 @codebutler committed Mar 31, 2009
View
4 Addins.mds
@@ -6,13 +6,15 @@
<Entry build="True" name="Twitter" configuration="Debug" />
<Entry build="True" name="Flickr" configuration="Debug" />
<Entry build="True" name="YouTube" configuration="Debug" />
+ <Entry build="True" name="Wikipedia" configuration="Debug" />
</Configuration>
<Configuration name="Release" ctype="CombineConfiguration">
<Entry build="True" name="CodeSnippets" configuration="Release" />
<Entry build="True" name="Banshee" configuration="Release" />
<Entry build="True" name="Twitter" configuration="Release" />
<Entry build="True" name="Flickr" configuration="Release" />
<Entry build="True" name="YouTube" configuration="Release" />
+ <Entry build="True" name="Wikipedia" configuration="Release" />
</Configuration>
</Configurations>
<StartMode startupentry="CodeSnippets" single="True">
@@ -21,12 +23,14 @@
<Execute type="None" entry="Twitter" />
<Execute type="None" entry="Flickr" />
<Execute type="None" entry="YouTube" />
+ <Execute type="None" entry="Wikipedia" />
</StartMode>
<Entries>
<Entry filename="addins/CodeSnippets/CodeSnippets.mdp" />
<Entry filename="addins/Banshee/Banshee.mdp" />
<Entry filename="addins/Twitter/Twitter.mdp" />
<Entry filename="addins/Flickr/Flickr.mdp" />
<Entry filename="addins/YouTube/YouTube.mdp" />
+ <Entry filename="addins/Wikipedia/Wikipedia.mdp" />
</Entries>
</Combine>
View
4 addins/Wikipedia/Makefile.am
@@ -4,7 +4,9 @@ FILES = \
AssemblyInfo.cs \
WikipediaMessageDisplayFormatter.cs
-RESOURCES = Wikipedia.addin.xml
+RESOURCES = \
+ logo-16.png \
+ Wikipedia.addin.xml
REFERENCES = \
System \
View
5 addins/Wikipedia/Wikipedia.addin.xml
@@ -9,6 +9,11 @@
<Addin id="UI" version="0.1" />
</Dependencies>
+
+ <Extension path="/Synapse/UI/ResourceProviders">
+ <ResourceProvider id="wikipedia"/>
+ </Extension>
+
<Extension path="/Synapse/UI/Chat/MessageDisplayFormatters">
<MessageDisplayFormatter class="Synapse.Addins.Wikipedia.WikipediaMessageDisplayFormatter" />
</Extension>
View
3 addins/Wikipedia/Wikipedia.mdp
@@ -16,8 +16,9 @@
</Configurations>
<Contents>
<File name="WikipediaMessageDisplayFormatter.cs" subtype="Code" buildaction="Compile" />
- <File name="Wikipedia.addin.xml" subtype="Code" buildaction="Nothing" />
+ <File name="Wikipedia.addin.xml" subtype="Code" buildaction="EmbedAsResource" />
<File name="AssemblyInfo.cs" subtype="Code" buildaction="Compile" />
+ <File name="logo-16.png" subtype="Code" buildaction="EmbedAsResource" />
</Contents>
<References>
<ProjectReference type="Project" localcopy="False" refto="Synapse.UI" />
View
48 addins/Wikipedia/WikipediaMessageDisplayFormatter.cs
@@ -10,10 +10,10 @@ namespace Synapse.Addins.Wikipedia
{
public class WikipediaMessageDisplayFormatter : IMessageDisplayFormatter
{
- const string WIKIPEDIA_PAGE_LINK_PATTERN = @"<a href=""http://(www\.)?([a-zA-Z]{2})\.wikipedia\.org/wiki/(.*?)"""; // Pattern to see if a string contains a wikipedia-url
+ const string WIKIPEDIA_PAGE_LINK_PATTERN = @"(<a\s.*?href=""(http://(www\.)?([a-zA-Z]{2})\.wikipedia.(com|org)/wiki/(.*?))"".*?>.*?</a>)";
- const string STYLE_BEGIN = "<p style=\"background-color:white; color:black; border-width:1px; border-style:solid;\">"; // Style to format the wikipediaPreview.
- const string STYLE_END = "</p>";
+ const string STYLE_BEGIN = "<p style=\"min-height: 20px; margin-top: 0px; padding: 2px 2px 2px 22px; background: url(resource:/wikipedia/logo-16.png) 2px 2px no-repeat white; color: black; border: 1px solid #666;\">"; // Style to format the wikipediaPreview.
+ const string STYLE_END = "</p>";
public bool SupportsMessage(string bodyHtml, Message message)
{
@@ -22,15 +22,10 @@ public bool SupportsMessage(string bodyHtml, Message message)
public string FormatMessage(string bodyHtml, Message message)
{
- string chatMessage = bodyHtml;
- MatchCollection matchCollection = Regex.Matches(bodyHtml, WIKIPEDIA_PAGE_LINK_PATTERN); // matchCollection contains (ia) the Wikipedia-Locations and the names of the articles
-
- foreach(Match match in matchCollection) // Maybe more than one article was posted in a message
- {
- string linkUrl = String.Format("http://{0}.wikipedia.org/wiki/{1}", match.Groups[2], match.Groups[3]); // Groups[2] = Location , Groups[3] = Article
- chatMessage += BuildHtmlPreview(linkUrl); // Add the WikipediaPreview to the chatMessage
- }
- return chatMessage; // This will be displayed in the ChatWindow
+ return Regex.Replace(bodyHtml, WIKIPEDIA_PAGE_LINK_PATTERN, delegate (Match match) {
+ string linkUrl = match.Groups[2].Value;
+ return match.Value + BuildHtmlPreview(linkUrl);
+ }, RegexOptions.Singleline);
}
public bool StopAfter {
@@ -44,6 +39,7 @@ public string FormatMessage(string bodyHtml, Message message)
try { // Connect to Wikipedia and read the Article
Uri url = new Uri(linkUrl);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
+ request.UserAgent = "Mozilla/5.0"; // Seriously wikipedia?
request.Timeout = 2000;
WebResponse response = request.GetResponse();
string sourceCode = null;
@@ -55,25 +51,23 @@ public string FormatMessage(string bodyHtml, Message message)
if (regex.IsMatch(sourceCode)) {
Match match = regex.Match(sourceCode);
string article = match.Groups[1].ToString(); // Get the first passage from the article
+ article = Synapse.Core.HtmlSanitizer.ToPlainText(article); // Remove HTML
+ article = Regex.Replace(article, @"\[\d*?\]", ""); // Remove source-tags
- article = Regex.Replace(article, "(<.*?>)", delegate(Match tagmatch) { // Remove html-tags (except for <b> and </b>)
- string tag = tagmatch.Groups[1].ToString();
- if(tag.CompareTo("<b>") == 0 || tag.CompareTo("</b>") == 0)
- return tag;
- else
- return "";
- });
- article = Regex.Replace(article, @"\[\d*?\]", ""); // Remove source-tags
-
- if(article.Length > 160) // More than 160chars is too long.
- {
- article = String.Format("{0}[...]", article.Remove(160));
- }
- return String.Format("{0}{1}{2}", STYLE_BEGIN, article, STYLE_END); // Format the message with html/css and return.
+ if(article.Length > 160) // More than 160chars is too long.
+ {
+ article = String.Format("{0}...", article.Remove(160));
+ }
+
+ // FIXME:
+ // article = article.Replace(articleName, "<b>" + articleName + "</b>");
+
+ return String.Format("{0}{1}{2}", STYLE_BEGIN, article, STYLE_END); // Format the message with html/css and return.
}
}
- } catch {
+ } catch (Exception ex) {
Console.Error.WriteLine("WikipediaMessageDisplayFormatter.BuildHtmlPreview(string linkUrl) has an error (Maybe The Article doesn't exist?)");
+ Console.Error.WriteLine(ex);
}
return String.Empty; // No Preview
}
View
BIN addins/Wikipedia/logo-16.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 49bed29

Please sign in to comment.
Something went wrong with that request. Please try again.