public void Handle(IList<string> sourceLines, DiaryMode mode, StringBuilder sink, Action<IList<string>> yield) { if (mode == DiaryMode.Html) HandleHtml(sourceLines, sink, yield); else HandlePlainText(sourceLines, sink, yield); }
public string Handle(string source, DiaryMode mode) { var fromCache = cached; var match = (fromCache != null && fromCache.Source.Equals(source)) ? fromCache.Match : regex.Match(source); var flickruser = match.Groups[1].Value; var photoid = match.Groups[2].Value; var options = match.Groups.OfType<Group>().Skip(2).Select(g => g.Value).ToArray(); var wanted = options.AnyOfOrDefault(new[] { "small", "medium", "large", "square" }) ?? "medium"; var result = source; try { XDocument sizes = FlickrApi.WithKey(apiKey).Photos.GetSizes(photoId: photoid); var image = sizes.Descendants("size").First(sz => string.Compare(sz.Attribute("label").Value, wanted, true) == 0); var jpg = image.Attribute("source").Value; var width = image.Attribute("width").Value; var height = image.Attribute("height").Value; if (mode == DiaryMode.Html) { result = string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" />", jpg, width, height); XDocument info = FlickrApi.WithKey(apiKey).Photos.GetInfo(photoId: photoid); var url = info.Descendants("url").Where(p => p.Attribute("type").Value == "photopage").First().Value; result = string.Format("<a href=\"{0}\">{1}</a>", url, result); } else { result = jpg; } } catch { } return result; }
private string ParagraphHandler(string paragraph, DiaryMode mode) { var pbuilder = new StringBuilder(); var paragraphPlugins = new Stack<IDiaryParagraphPlugin>(Plugins.OfType<IDiaryParagraphPlugin>()); var linePlugins = new Stack<IDiaryLinePlugin>(Plugins.OfType<IDiaryLinePlugin>()); var linkPlugins = new Stack<IDiaryLinkPlugin>(Plugins.OfType<IDiaryLinkPlugin>()); Action<IList<string>> prevYield = lines => { // do line plugins while (linePlugins.Count > 0) { var plugin = linePlugins.Pop(); lines = lines.SelectMany(x => plugin.Handle(x, mode)).ToArray(); } var linkRegex = new Regex(@"\[([^\]]+)\]"); var result = lines.Select(l => linkRegex.Replace(l, me => { var content = me.Groups[1].Value; var plugin = linkPlugins.FirstOrDefault(lp => lp.CanHandle(content, mode)); return plugin == null ? content : me.Result(plugin.Handle(content, mode)); })); // join final result into one string pbuilder.Append(string.Join("\r\n", result)); }; while (paragraphPlugins.Count > 0) { var plugin = paragraphPlugins.Pop(); var wrappedYield = prevYield; prevYield = l => plugin.Handle(l, mode, pbuilder, wrappedYield); } prevYield(paragraph.Split(new[] { "\r\n" }, StringSplitOptions.None)); return pbuilder.ToString(); }
private string Convert(string diary, DiaryMode mode) { if (string.IsNullOrWhiteSpace(diary)) return diary; return string.Join("\r\n", diary.Split(new[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries) .Select(x => ParagraphHandler(x, mode)) .Union(new[] { "" })); }
public void Handle(IList<string> sourceLines, DiaryMode mode, StringBuilder sink, Action<IList<string>> yield) { var marker = Guid.NewGuid().ToString("N"); var result = string.Join(marker, sourceLines); var boldReplacement = mode == DiaryMode.Html ? "<strong>$2</strong>" : "$2"; var italicsReplacement = mode == DiaryMode.Html ? "<em>$2</em>" : "$2"; result = bold.Replace(result, boldReplacement); result = italics.Replace(result, italicsReplacement); yield(result.Split(new[] { marker }, StringSplitOptions.None)); }
protected override string Handle(Match match, DiaryMode mode) { var url = match.Groups[2].Value; var alt = match.Groups[1].Value; if (mode == DiaryMode.Html) { return string.Format("<img src=\"{0}\" alt=\"{1}\" style='max-width: 600px'>", url, HttpUtility.HtmlEncode(alt)); } return string.IsNullOrEmpty(alt) ? url : string.Format("{1} ({0})", url, alt); }
protected override string Handle(Match match, DiaryMode mode) { var url = match.Groups[2].Value; var text = match.Groups[1].Value; if (mode == DiaryMode.Html) { if (string.IsNullOrWhiteSpace(text)) text = url; return string.Format("<a href=\"{0}\">{1}</a>", url, Regex.Replace(text, "^mailto:", "")); } if (string.IsNullOrWhiteSpace(text)) return Regex.Replace(url, "^mailto:", ""); return string.Format("{1} ({0})", url, Regex.Replace(text, "^mailto:", "")); }
public string Handle(string source, DiaryMode mode) { var fromCache = cached; var match = (fromCache != null && fromCache.Source.Equals(source)) ? fromCache.Match : regex.Match(source); var flickruser = match.Groups[1].Value; var setid = match.Groups[2].Value; return mode == DiaryMode.Html ? string.Format(@"<object width=""500"" height=""375""> <param name=""flashvars"" value=""offsite=true&lang=en-us&page_show_url=%2Fphotos%2F{0}%2Fsets%2F{1}%2Fshow%2F&page_show_back_url=%2Fphotos%2F{0}%2Fsets%2F{1}%2F&set_id={1}&jump_to=""></param> <param name=""movie"" value=""http://www.flickr.com/apps/slideshow/show.swf?v=71649""></param> <param name=""allowFullScreen"" value=""true""></param><embed type=""application/x-shockwave-flash"" src=""http://www.flickr.com/apps/slideshow/show.swf?v=71649"" allowFullScreen=""true"" flashvars=""offsite=true&lang=en-us&page_show_url=%2Fphotos%2F{0}%2Fsets%2F{1}%2Fshow%2F&page_show_back_url=%2Fphotos%2F{0}%2Fsets%2F{1}%2F&set_id={1}&jump_to="" width=""500"" height=""375""></embed></object>", flickruser, setid) : source; }
public bool CanHandle(string source, DiaryMode mode) { if (!Supports(mode)) return false; foreach (var regex in regexes) { var match = regex.Match(source); if (match.Success) { cached = new MatchCache(source, match); return true; } } return false; }
public string Handle(string source, DiaryMode mode) { var fromCache = cached; var match = (fromCache != null && fromCache.Source.Equals(source)) ? fromCache.Match : regex.Match(source); var videoId = match.Groups[1].Value; var result = source; try { result = string.Format(@"<iframe src=""http://player.vimeo.com/video/{0}?byline=0&portrait=0&color=ff9933"" width=""500"" height=""281"" frameborder=""0""></iframe>", videoId); } catch { } return result; }
public void Handle(IList<string> sourceLines, DiaryMode mode, StringBuilder sink, Action<IList<string>> yield) { var bqRegex = new Regex(@"^([ \t]*\><(?:[ \t]|$))", RegexOptions.Compiled); var bqTest = bqRegex.Match(sourceLines.First()); if (bqTest.Success) { // it's a blockquote! if (mode == DiaryMode.Html) { sink.Append("<div align=\"center\">\r\n"); yield(sourceLines.Select(l => bqRegex.Replace(l, "")).ToList()); sink.Append("</div>\r\n"); } else { yield(sourceLines.Select(l => " " + bqRegex.Replace(l, "")).ToList()); } } else yield(sourceLines); }
public string Handle(string source, DiaryMode mode) { var fromCache = cached; Match match = null; if (fromCache != null && fromCache.Source.Equals(source)) match = fromCache.Match; else { foreach (var regex in regexes) { var tryMatch = regex.Match(source); if (tryMatch.Success) { match = tryMatch; } } } if (match == null) return source; return Handle(match, mode); }
protected abstract bool Supports(DiaryMode mode);
protected override bool Supports(DiaryMode mode) { return mode == DiaryMode.Html; }
protected override string Handle(Match match, DiaryMode mode) { var videoId = match.Groups[1].Value; return string.Format(@"<iframe width=""500"" height=""281"" src=""http://www.youtube-nocookie.com/embed/{0}?rel=0"" frameborder=""0"" allowfullscreen></iframe>", videoId); }
protected abstract string Handle(Match match, DiaryMode mode);
protected override bool Supports(DiaryMode mode) { return true; }
public bool CanHandle(string source, DiaryMode mode) { var match = regex.Match(source); cached = new MatchCache(source, match); return match.Success; }