Example #1
0
        public static IDictionary <Template, TextRegion> FindTemplates(string text, IEnumerable <string> templateNames, bool skipIgnored)
        {
            var items   = new Dictionary <Template, TextRegion>();
            var regex   = new Regex(@"\{\{(?:" + string.Join("|", templateNames.Select(t => "(?:" + GetArticleTitleRegex(t) + ")")) + @")[|}\s]");
            var ignored = skipIgnored ? new TextRegion[0] : GetIgnoredRegions(text).ToArray();

            for (var i = 0; ;)
            {
                var match = regex.Match(text, i);
                if (!match.Success)
                {
                    break;
                }

                if (ignored.Contains(match.Index))
                {
                    i = match.Index + match.Length;
                }
                else
                {
                    var template = Template.ParseAt(text, match.Index);
                    var region   = new TextRegion(match.Index, template.ToString().Length);
                    items.Add(template, region);
                    i = region.Offset + region.Length;
                }
            }
            return(items);
        }
Example #2
0
        public static TextRegion ExpandToWholeLine(string text, TextRegion region)
        {
            var index = 0;

            if (region.Offset > 0)
            {
                index = text.LastIndexOf('\n', region.Offset - 1) + 1;
                if (!string.IsNullOrWhiteSpace(text.Substring(index, region.Offset - index)))
                {
                    return(region);
                }
            }

            var end    = region.Offset + region.Length;
            var index2 = text.IndexOf('\n', end) + 1;

            if (index2 == 0)
            {
                index2 = text.Length;
            }

            if (!string.IsNullOrWhiteSpace(text.Substring(end, index2 - end)))
            {
                return(region);
            }

            return(new TextRegion(index, index2 - index));
        }