public Delimiter(Ops delimiterOps, int delimiterOpsIndex, char delimiterType, int number, char?preceding, char?following, Delimiter previous)
        {
            DelimiterOps      = delimiterOps;
            DelimiterOpsIndex = delimiterOpsIndex;
            DelimiterChar     = delimiterType == '_' ? DelimiterType.Underscore : DelimiterType.Asterisk;
            Number            = number;
            Previous          = previous;
            Next = null;

            precedingWhitespace  = preceding is null ? true : whitespaceRegex.IsMatch(((string)preceding.ToString()));
            followingWhitespace  = following is null ? true : whitespaceRegex.IsMatch(((string)following.ToString()));
            precedingPunctuation = preceding is null ? false : punctuationRegex.IsMatch(((string)preceding.ToString()));
            followingPunctuation = following is null ? false : punctuationRegex.IsMatch(((string)following.ToString()));

            leftFlanking  = false;
            rightFlanking = false;

            if (!followingWhitespace && (
                    !followingPunctuation || (followingPunctuation && (precedingPunctuation || precedingWhitespace))
                    ))
            {
                leftFlanking = true;
            }

            if (!precedingWhitespace && (
                    !precedingPunctuation || (precedingPunctuation && (followingPunctuation || followingWhitespace))
                    ))
            {
                rightFlanking = true;
            }

            SetOpenerOrCloser();
        }
Exemple #2
0
        private static Ops TextToDelta(string text)
        {
            var       opsList        = new List <Ops>();
            Delimiter delimiterStack = null;

            int  i                             = -1;
            int  opStart                       = 0;
            int  delimiterStart                = 0;
            bool cancelled                     = false;
            char?preceding                     = null;
            int  consecutiveDelimiters         = 0;
            int  previousConsecutiveDelimiters = 0;
            char delimiterType                 = '*';

            foreach (char x in text)
            {
                i += 1;
                previousConsecutiveDelimiters = consecutiveDelimiters;

                if (!cancelled && (
                        (consecutiveDelimiters == 0 && (x == '*' || x == '_')) ||
                        (consecutiveDelimiters > 0 && x == delimiterType)
                        ))
                {
                    consecutiveDelimiters += 1;
                    delimiterType          = x;
                }
                else
                {
                    consecutiveDelimiters = 0;
                }

                if (consecutiveDelimiters == 1)
                {
                    delimiterStart = i;
                }

                if (consecutiveDelimiters == 0 && previousConsecutiveDelimiters != 0)
                {
                    string textToInsert = text.Substring(opStart, delimiterStart - opStart);
                    if (textToInsert != "")
                    {
                        var textOps = new Ops().Insert(textToInsert);
                        opsList.Add(textOps);
                    }

                    var delimiterOps = new Ops().Insert(
                        text.Substring(delimiterStart, previousConsecutiveDelimiters)
                        );
                    opsList.Add(delimiterOps);
                    delimiterStack = (new Delimiter(
                                          delimiterOps,
                                          opsList.Count - 1,
                                          delimiterType,
                                          previousConsecutiveDelimiters,
                                          preceding,
                                          x,
                                          delimiterStack
                                          ));
                    if (delimiterStack.Previous is not null)
                    {
                        delimiterStack.Previous.Next = delimiterStack;
                    }
                    opStart = delimiterStart + previousConsecutiveDelimiters;
                }

                if (consecutiveDelimiters == 0)
                {
                    preceding = x;
                }

                if (cancelled)
                {
                    cancelled = false;
                }
                else if (x == '\\')
                {
                    cancelled = true;
                }
            }

            if (consecutiveDelimiters == 0)
            {
                string textToInsert = text.Substring(opStart, text.Length - opStart);
                if (textToInsert != "")
                {
                    var textOps = new Ops().Insert(textToInsert);
                    opsList.Add(textOps);
                }
            }
            else
            {
                string textToInsert = text.Substring(opStart, delimiterStart - opStart);
                if (textToInsert != "")
                {
                    var textOps = new Ops().Insert(textToInsert);
                    opsList.Add(textOps);
                }

                var delimiterOps = new Ops().Insert(
                    text.Substring(delimiterStart, consecutiveDelimiters)
                    );
                opsList.Add(delimiterOps);
                delimiterStack = new Delimiter(
                    delimiterOps,
                    opsList.Count - 1,
                    delimiterType,
                    consecutiveDelimiters,
                    preceding,
                    null,
                    delimiterStack
                    );
                if (delimiterStack.Previous is not null)
                {
                    delimiterStack.Previous.Next = delimiterStack;
                }
            }

            var openersBottom = new Dictionary <ValueTuple <EmphasisType, DelimiterType>, Delimiter>();

            foreach (var emphasis in new List <EmphasisType>()
            {
                EmphasisType.Italic, EmphasisType.Bold
            })
            {
                foreach (var delimiter in new List <DelimiterType>()
                {
                    DelimiterType.Asterisk, DelimiterType.Underscore
                })
                {
                    openersBottom.Add((emphasis, delimiter), null);
                }
            }

            var currentDelimiter = delimiterStack;

            while (currentDelimiter?.Previous is not null)
            {
                currentDelimiter = currentDelimiter.Previous;
            }

            while (currentDelimiter is not null)
            {
                if (!currentDelimiter.OpenerOrCloser[EmphasisType.Italic].HasFlag(OpenerOrCloserType.Closer) &&
                    !currentDelimiter.OpenerOrCloser[EmphasisType.Bold].HasFlag(OpenerOrCloserType.Closer))
                {
                    currentDelimiter = currentDelimiter.Next;
                    continue;
                }

                var possibleEmphasis = new List <EmphasisType>();
                if (currentDelimiter.OpenerOrCloser[EmphasisType.Bold].HasFlag(OpenerOrCloserType.Closer))
                {
                    possibleEmphasis.Add(EmphasisType.Bold);
                }
                if (currentDelimiter.OpenerOrCloser[EmphasisType.Italic].HasFlag(OpenerOrCloserType.Closer))
                {
                    possibleEmphasis.Add(EmphasisType.Italic);
                }

                var currentOpenerBottoms = possibleEmphasis.Select(
                    emphasis => openersBottom[(emphasis, currentDelimiter.DelimiterChar)]