public void SetSEOModel(Widget widget) { SEOModel result = new SEOModel(); string keywordsString = (widget.MetaKeywords == null ? string.Empty : widget.MetaKeywords).ToLower(); IEnumerable <string> keywords = keywordsString.Split(',').Select(s => s.Trim()); string content = widget.GetContentExceptTags().ToLower(); content = content == null ? string.Empty : content; content = content.Replace("-\n", "").Replace("- ", ""); result.Title = widget.MetaTitle == null ? string.Empty : widget.MetaTitle; result.Description = widget.MetaDescription == null ? string.Empty : widget.MetaDescription; result.WordCount = Regex.Matches(content, @"[\w\-_]+").Count; result.WordCountFirstWidget = -1; result.Content = content; foreach (string keyword in keywords) { if (string.IsNullOrWhiteSpace(keyword)) { continue; } SEOKeywordModel keywordModel = new SEOKeywordModel(); keywordModel.Keyword = keyword; keywordModel.Count = Regex.Matches(content, keyword).Count; keywordModel.CountFirstWidget = -1; keywordModel.IsInMetaDescription = result.Description.Contains(keyword); keywordModel.IsInTitle = result.Title.Contains(keyword); keywordModel.IsInUrl = widget.Url.Contains(keyword); result.Keywords.Add(keywordModel); } this.SEOModel = result; }
public void SetSEOModel(Site site, List <Widget> widgets) { if (widgets == null || widgets.Count < 1) { SEOModel = new SEOModel(); return; } SEOModel result = new SEOModel(); var keywordsString = (site.Keywords == null ? string.Empty : site.Keywords).ToLower(); var keywords = keywordsString.Split(',').Select(s => s.Trim()); var firstContent = widgets.First().GetContentExceptTags().ToLower(); firstContent = firstContent == null ? string.Empty : firstContent; var content = widgets.Select(w => w.GetContentExceptTags()) .Aggregate((acc, s) => acc + (s == null ? string.Empty : s)).ToLower(); content = content.Replace("-\n", "").Replace("- ", ""); firstContent = content.Replace("-\n", "").Replace("- ", ""); result.Title = site.Title == null ? string.Empty : site.Title; result.Description = site.Description == null ? string.Empty : site.Description; result.WordCount = Regex.Matches(content, @"[\w\-_]+").Count; result.WordCountFirstWidget = Regex.Matches(firstContent, @"[\w\-_]+").Count; result.Content = content; foreach (string keyword in keywords) { if (string.IsNullOrWhiteSpace(keyword)) { continue; } SEOKeywordModel keywordModel = new SEOKeywordModel(); keywordModel.Keyword = keyword; keywordModel.Count = Regex.Matches(content, keyword).Count; keywordModel.CountFirstWidget = Regex.Matches(firstContent, keyword).Count; keywordModel.IsInMetaDescription = result.Description.Contains(keyword); keywordModel.IsInTitle = result.Title.Contains(keyword); keywordModel.IsInUrl = site.Url.Contains(keyword); result.Keywords.Add(keywordModel); } this.SEOModel = result; }