Exemplo n.º 1
0
        /// <summary>
        ///     Creates an emphasized paragraph
        /// </summary>
        /// <param name="text">Content of the emphasized paragraph</param>
        /// <param name="emphasisType">Type of the emphasis</param>
        public MvcHtmlString EmphasizedParagraph(string text, EmphasisType emphasisType)
        {
            var p = new TagBuilderExt("p");

            switch (emphasisType)
            {
            case EmphasisType.Muted:
                p.AddCssClass("muted");
                break;

            case EmphasisType.Warning:
                p.AddCssClass("text-warning");
                break;

            case EmphasisType.Error:
                p.AddCssClass("text-error");
                break;

            case EmphasisType.Info:
                p.AddCssClass("text-info");
                break;

            case EmphasisType.Success:
                p.AddCssClass("text-success");
                break;

            default:
                throw new ArgumentOutOfRangeException("emphasisType");
            }

            p.SetInnerText(text);
            return(p.ToMvcHtmlString());
        }
Exemplo n.º 2
0
        private IClassificationType GetClassificationForType(EmphasisType type)
        {
            switch (type)
            {
            case EmphasisType.Bold:
                return(_registry.GetClassificationType(FormatDefinitions.Bold.Name));

            case EmphasisType.Italic:
                return(_registry.GetClassificationType(FormatDefinitions.Italic.Name));

            case EmphasisType.Code:
                return(_registry.GetClassificationType(FormatDefinitions.Code.Name));

            case EmphasisType.Bold | EmphasisType.Italic:
                return(_registry.GetClassificationType(FormatDefinitions.BoldItalic.Name));

            case EmphasisType.Bold | EmphasisType.Code:
                return(_registry.GetClassificationType(FormatDefinitions.BoldCode.Name));

            case EmphasisType.Italic | EmphasisType.Code:
                return(_registry.GetClassificationType(FormatDefinitions.ItalicCode.Name));

            case EmphasisType.Bold | EmphasisType.Italic | EmphasisType.Code:
                return(_registry.GetClassificationType(FormatDefinitions.BoldItalicCode.Name));

            default:
                return(null);
            }
        }
Exemplo n.º 3
0
        public void RemoveCharacters(EmphasisType emphasis)
        {
            int n = emphasis == EmphasisType.Bold ? 2 : 1;

            Number -= n;
            DelimiterOps.Transform(new Ops().Delete(n));
            SetOpenerOrCloser();
        }
Exemplo n.º 4
0
        /// <summary>Initializes a new instance of the <see cref="MarkdownEmphasis"/> class.</summary>
        /// <param name="text">The emphasis text.</param>
        /// <param name="emphasisType">Type of emphasis.</param>
        public MarkdownEmphasis(string text, EmphasisType emphasisType) : this()
        {
            if (string.IsNullOrEmpty(text))
            {
                text = string.Empty;
            }

            MarkdownContentText = text;
            Emphasis            = emphasisType;

            CreateEmphasis();
        }
Exemplo n.º 5
0
        public void ReturnsSingleResultWhenMarkerIsAtEndOfText(string marker, EmphasisType type)
        {
            IEnumerable <EmphasisSpan> results;


            results = Parse($"this is {marker}marked text{marker}");

            Assert.Equal(
                new[] { new EmphasisSpan {
                            StartOffset = 8, Length = 13, Type = type
                        } },
                results
                );
        }
Exemplo n.º 6
0
        public void ReturnsCompletedSpansWhenThereAreIncompleteSpans(string marker, EmphasisType type)
        {
            IEnumerable <EmphasisSpan> results;


            results = Parse($"this {marker}comment{marker} is {marker}incomplete");

            Assert.Equal(
                new[] { new EmphasisSpan {
                            StartOffset = 5, Length = 9, Type = type
                        } },
                results
                );
        }
Exemplo n.º 7
0
        public void ReturnsSingleResultWhenMarkerCoversAllText(string marker, EmphasisType type)
        {
            IEnumerable <EmphasisSpan> results;


            results = Parse($"{marker}all covered{marker}");

            Assert.Equal(
                new[] { new EmphasisSpan {
                            StartOffset = 0, Length = 13, Type = type
                        } },
                results
                );
        }
Exemplo n.º 8
0
        public void DetectsEndMarkerBeforeBrackets(string marker, EmphasisType type, char bracket)
        {
            IEnumerable <EmphasisSpan> results;


            results = Parse($"this is {marker}marked{marker}{bracket} with brackets");

            Assert.Equal(
                new[] { new EmphasisSpan {
                            StartOffset = 8, Length = 8, Type = type
                        } },
                results
                );
        }
Exemplo n.º 9
0
        public void DetectsEndMarkerBeforePunctuation(string marker, EmphasisType type, char punctuation)
        {
            IEnumerable <EmphasisSpan> results;


            results = Parse($"this is {marker}marked{marker}{punctuation} and has punctuation");

            Assert.Equal(
                new[] { new EmphasisSpan {
                            StartOffset = 8, Length = 8, Type = type
                        } },
                results
                );
        }
Exemplo n.º 10
0
        private bool CanBeStartMarker(string text, int charIndex, EmphasisType markerType, EmphasisType spanType)
        {
            // Markers cannot start at the end of the text, so there must
            // be a following character, and it cannot be whitespace.
            if ((charIndex < (text.Length - 1)) && (!char.IsWhiteSpace(text[charIndex + 1])))
            {
                char previousChar;


                // Markers can start at the start of the text.
                if (charIndex == 0)
                {
                    return(true);
                }

                previousChar = text[charIndex - 1];

                // Markers can start after whitespace or an opening bracket.
                if (char.IsWhiteSpace(text[charIndex - 1]) || OpeningBrackets.Contains(previousChar))
                {
                    return(true);
                }

                // Or markers can start after another start marker.
                if (Markers.TryGetValue(previousChar, out EmphasisType previousType))
                {
                    // The previous character would be a start marker if
                    // it's a different type to this marker, and that
                    // previous marker type is in the current span type.
                    if (previousType != markerType)
                    {
                        if ((spanType & previousType) == previousType)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 11
0
        private void RemoveEmphasisFromLastSpans(EmphasisType type, int unclosedMarkerStartIndex, List <EmphasisSpan> spans)
        {
            for (int i = spans.Count - 1; i >= 0; i--)
            {
                EmphasisSpan temp;


                // We can stop when we reach a span that ended before the unclosed
                // marker started because it won't be affected by the unclosed marker.
                if ((spans[i].StartOffset + spans[i].Length) < unclosedMarkerStartIndex)
                {
                    break;
                }

                // This span included the unclosed marker, so
                // we need to remove the type from this span.
                temp       = spans[i];
                temp.Type &= (~type);
                spans[i]   = temp;
            }
        }
Exemplo n.º 12
0
        private bool CanBeEndMarker(string text, int charIndex, EmphasisType markerType, EmphasisType spanType)
        {
            // Markers can only end after a non-whitespace character.
            if ((charIndex > 0) && (!char.IsWhiteSpace(text[charIndex - 1])))
            {
                char nextChar;


                // Markers can end at the end of the text.
                if (charIndex == (text.Length - 1))
                {
                    return(true);
                }

                nextChar = text[charIndex + 1];

                // Markers can end before whitespace, punctuation, or a closing bracket.
                if (char.IsWhiteSpace(nextChar) || char.IsPunctuation(nextChar) || ClosingBrackets.Contains(nextChar))
                {
                    return(true);
                }

                // Or a marker can end before another end marker.
                if (Markers.TryGetValue(nextChar, out EmphasisType nextType))
                {
                    // The next character will be an end marker if
                    // it's a different type to this marker, and that
                    // next marker type is in the current span type.
                    if (nextType != markerType)
                    {
                        if ((spanType & nextType) == nextType)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 13
0
        /// <summary>Creates the emphasis using the specified text.</summary>
        /// <param name="text">The text.</param>
        /// <param name="emphasis">The emphasis type to use.</param>
        /// <returns>The <see cref="string"/>.</returns>
        public static string CreateEmphasis(string text, EmphasisType emphasis)
        {
            if (string.IsNullOrEmpty(text))
            {
                text = string.Empty;
            }

            // Sets the emphasis.
            string outputText;

            switch (emphasis)
            {
            case EmphasisType.Bold:
            {
                outputText = Bold + text + Bold;
                break;
            }

            case EmphasisType.Italic:
            {
                outputText = Italic + text + Italic;
                break;
            }

            case EmphasisType.Strikethrough:
            {
                outputText = Strikethrough + text + Strikethrough;
                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException(nameof(emphasis), emphasis, null);
            }
            }

            return(outputText);
        }
Exemplo n.º 14
0
        public void ReturnsMultipleResultsWhenTextContainsMultipleMarkedSpans(string marker, EmphasisType type)
        {
            IEnumerable <EmphasisSpan> results;


            results = Parse($"this {marker}is{marker} marked {marker}and{marker} so is this");

            Assert.Equal(
                new[] {
                new  EmphasisSpan {
                    StartOffset = 5, Length = 4, Type = type
                },
                new  EmphasisSpan {
                    StartOffset = 17, Length = 5, Type = type
                }
            },
                results
                );
        }