Beispiel #1
0
        public IDfmFencesBlockPathQueryOption ParsePathQueryString(string queryOrFragment, bool noCache = false)
        {
            if (string.IsNullOrEmpty(queryOrFragment))
            {
                return(null);
            }

            return(_pathQueryOptionCreaters.ParseQueryOrFragment(DfmFencesBlockPathQueryOptionParameters.Create(queryOrFragment), noCache));
        }
        public IDfmFencesBlockPathQueryOption ParseQueryOrFragment(DfmFencesBlockPathQueryOptionParameters parameters, bool noCache)
        {
            foreach (var creater in _pathQueryOptionCreaters)
            {
                var option = creater.ParseQueryOrFragment(parameters, noCache);
                if (option != null)
                {
                    return(option);
                }
            }

            throw new NotSupportedException($"Unable to parse DfmFencesBlockPathQueryOptionParameters");
        }
        public static DfmFencesBlockPathQueryOptionParameters Create(string queryAndFragment)
        {
            var result = new DfmFencesBlockPathQueryOptionParameters();

            if (string.IsNullOrEmpty(queryAndFragment))
            {
                return(result);
            }

            var queryOption = queryAndFragment.Remove(1);
            var queryString = queryAndFragment.Substring(1);
            int startLine, endLine;

            if (queryOption == "#")
            {
                // check if line number representation
                var match = _dfmFencesSharpQueryStringRegex.Match(queryString);
                if (match.Success && int.TryParse(match.Groups["start"].Value, out startLine) && int.TryParse(match.Groups["end"].Value, out endLine))
                {
                    result.LinePairs.Add(new Tuple <int?, int?>(startLine, endLine));
                }
                else
                {
                    result.TagName = queryString;
                }
            }
            else if (queryOption == "?")
            {
                var collection = HttpUtility.ParseQueryString(queryString);
                result.TagName        = collection[TagNameQueryStringKey];
                result.HighlightLines = collection[HighlightLinesQueryStringKey];
                var start = int.TryParse(collection[StartLineQueryStringKey], out startLine) ? startLine : (int?)null;
                var end   = int.TryParse(collection[EndLineQueryStringKey], out endLine) ? endLine: (int?)null;
                var range = collection[RangeQueryStringKey];
                if (collection[DedentQueryStringKey] != null)
                {
                    if (int.TryParse(collection[DedentQueryStringKey], out int dedentTemp))
                    {
                        result.DedentLength = dedentTemp;
                    }
                    else
                    {
                        Logger.LogWarning($"Illegal dedent `{collection[DedentQueryStringKey]}` in query parameter `dedent`. Auto-dedent will be applied.");
                    }
                }
                if (range != null)
                {
                    var regions = range.Split(RegionSeparatorInRangeQueryString);
                    if (regions != null)
                    {
                        foreach (var region in regions)
                        {
                            var match = _dfmFencesRangeQueryStringRegex.Match(region);
                            if (match.Success)
                            {
                                // consider region as `{startlinenumber}-{endlinenumber}`, in which {endlinenumber} is optional
                                result.LinePairs.Add(new Tuple <int?, int?>(
                                                         int.TryParse(match.Groups["start"].Value, out startLine) ? startLine : (int?)null,
                                                         int.TryParse(match.Groups["end"].Value, out endLine) ? endLine : (int?)null
                                                         ));
                            }
                            else
                            {
                                // consider region as a sigine line number
                                var tempLine = int.TryParse(region, out int line) ? line : (int?)null;
                                result.LinePairs.Add(new Tuple <int?, int?>(tempLine, tempLine));
                            }
                        }
                    }
                }
                else if (start != null || end != null)
                {
                    result.LinePairs.Add(new Tuple <int?, int?>(start, end));
                }
            }

            return(result);
        }