Example #1
0
        public Span Parse(string paragraph)
        {
            string normalisedParagraph = NormaliseParagraph(paragraph);

            List<Span> matchedSpans = new List<Span>();

            Match match = _parserExpression.Match(normalisedParagraph);

            while (match.Success)
            {
                if (match.Groups[6].Success)
                {
                    // No formatting tokens found:
                    matchedSpans.Add(new TextSpan(match.Groups[6].Value));
                }
                else
                {
                    // Formatting tokens found, with possible text before them:
                    if (match.Groups[1].Success)
                    {
                        matchedSpans.Add(new TextSpan(match.Groups[1].Value));
                    }

                    if (match.Groups[4].Success)
                    {
                        matchedSpans.Add(new TextSpan(match.Groups[4].Value));
                    }

                    if (match.Groups[2].Success)
                    {
                        // A paired marker was found, for example **bold** and friends:
                        string formatToken = match.Groups[2].Value;
                        string formatText = match.Groups[3].Value;

                        if (formatToken == "\"\"")
                        {
                            // Raw text:
                            matchedSpans.Add(new TextSpan(formatText));
                        }
                        else
                        {
                            Span childSpan = Parse(formatText);

                            matchedSpans.Add(new FormattedSpan(_tokenToSpanKind[formatToken], childSpan));
                        }
                    }
                    else
                    {
                        // A reference marker was found:
                        string referenceContents = match.Groups[5].Value;

                        Match referenceMatch = _referenceExpression.Match(referenceContents);

                        if (!referenceMatch.Success)
                        {
                            throw new AdornedTextParsingException(AdornedTextStrings.InvalidReferenceContents);
                        }

                        string referenceKindTag = referenceMatch.Groups[1].Value;
                        string referenceString = referenceMatch.Groups[2].Value;
                        string referenceText = referenceMatch.Groups[3].Value;

                        matchedSpans.Add(new ReferenceSpan(referenceKindTag, referenceString, Parse(referenceText)));
                    }
                }

                match = match.NextMatch();
            }

            if (matchedSpans.Count == 0)
            {
                return new TextSpan("");
            }
            else if (matchedSpans.Count == 1)
            {
                return matchedSpans[0];
            }
            else
            {
                SequenceSpan sequenceSpan = new SequenceSpan();
                Functional.ApplyAdd(matchedSpans, sequenceSpan.Spans);

                return sequenceSpan;
            }
        }
Example #2
0
 public virtual void VisitSequenceSpan(SequenceSpan sequenceSpan)
 {
 }