Пример #1
0
        private static Ranges <int> FindTabs(Regex rx, string s, ref Ranges <int> TokenRange)
        {
            Ranges <int>    ranges = new Ranges <int>();
            MatchCollection mc     = rx.Matches(s);

            foreach (Match m in mc)
            {
                ranges.add(new Range <int>()
                {
                    Minimum = m.Index,
                    Maximum = m.Index
                });

                TokenRange.add(new Range <int>()
                {
                    Minimum = m.Index,
                    Maximum = m.Index + m.Length - 1
                });
            }
            return(ranges);
        }
Пример #2
0
        private static Paragraph GenerateRuns(Paragraph p, string Buffer)
        {
            // Calculate positions of all tokens and use this to set
            // run styles when iterating through the string

            // in the same calculation note down location of tokens
            // so they can be ignored when loading strings into the buffer

            Regex pBold, pItalic, pUnderline;

            if (!CustomMode)
            {
                pBold      = Patterns[rExKey.DblAsterisk];
                pItalic    = Patterns[rExKey.Asterisk];
                pUnderline = Patterns[rExKey.Underscore];
            }
            else
            {
                pBold      = Patterns[rExKey.DblAsterisk];
                pItalic    = Patterns[rExKey.Grave];
                pUnderline = Patterns[rExKey.Underscore];
            }


            Ranges <int> Tokens    = new Ranges <int>();
            Ranges <int> Bold      = FindMatches(pBold, Buffer, ref Tokens);
            Ranges <int> Italic    = FindMatches(pItalic, Buffer, ref Tokens);
            Ranges <int> Underline = FindMatches(pUnderline, Buffer, ref Tokens);

            Ranges <int> Tabs = FindTabs(new Regex(@"(\\t|[\\ ]{4})"), Buffer, ref Tokens);

            if ((Bold.Count() + Italic.Count() + Underline.Count() + Tabs.Count()) == 0)
            {
                Run run = new Run();
                run.Append(new Text(Buffer)
                {
                    Space = SpaceProcessingModeValues.Preserve
                });
                p.Append(run);
            }
            else
            {
                int CurrentPosition = 0;
                Run run;

                // This needs optimizing so it builds a string buffer before adding the run itself

                while (CurrentPosition < Buffer.Length)
                {
                    if (!Tokens.ContainsValue(CurrentPosition) || Tabs.ContainsValue(CurrentPosition))
                    {
                        run = new Run();

                        if (Tabs.ContainsValue(CurrentPosition))
                        {
                            run.Append(new TabChar());
                        }
                        else
                        {
                            RunProperties rPr = new RunProperties();
                            if (Bold.ContainsValue(CurrentPosition))
                            {
                                rPr.Append(new Bold()
                                {
                                    Val = new OnOffValue(true)
                                });
                            }
                            if (Italic.ContainsValue(CurrentPosition))
                            {
                                rPr.Append(new Italic());
                            }
                            if (Underline.ContainsValue(CurrentPosition))
                            {
                                rPr.Append(new Underline()
                                {
                                    Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single
                                });
                            }
                            run.Append(rPr);

                            string TextBuffer = Buffer.Substring(CurrentPosition, 1);
                            run.Append(new Text(TextBuffer)
                            {
                                Space = SpaceProcessingModeValues.Preserve
                            });
                        }
                        p.Append(run);
                    }
                    CurrentPosition++;
                }
            }
            return(p);
        }
        public void FindMatches(string md, ref Ranges <int> Tokens)
        {
            if (pattern == Pattern.DblAsterisk || pattern == Pattern.Asterisk || pattern == Pattern.Grave || pattern == Pattern.Underscore || pattern == Pattern.BulletList)
            {
                MatchCollection mc  = regex.Matches(md);
                int             num = 0;

                foreach (Match m in mc)
                {
                    num++;

                    int sToken = m.Groups[1].Index;
                    int match  = m.Groups[2].Index;
                    int eToken = m.Groups[3].Index;
                    int endStr = m.Groups[3].Index + m.Groups[3].Length;
                    //int bToken = m.Groups[4].Index;
                    //int endStr1 = m.Groups[4].Index + m.Groups[4].Length;


                    Tokens.add(new Range <int>()
                    {
                        Minimum = sToken,
                        Maximum = match - 1
                    });

                    matches.add(new Range <int>()
                    {
                        Minimum = match,
                        Maximum = eToken - 1
                    });

                    Tokens.add(new Range <int>()
                    {
                        Minimum = m.Index,
                        Maximum = m.Index
                    });
                    Tokens.add(new Range <int>()
                    {
                        Minimum = m.Index,
                        Maximum = m.Index + m.Index - 1
                    });
                }
            }
            else if (pattern == Pattern.Tab)
            {
                MatchCollection mc = regex.Matches(md);

                foreach (Match m in mc)
                {
                    matches.add(new Range <int>()
                    {
                        Minimum = m.Index,
                        Maximum = m.Index
                    });

                    Tokens.add(new Range <int>()
                    {
                        Minimum = m.Index,
                        Maximum = m.Index + m.Length - 1
                    });
                }
            }
            else if (pattern == Pattern.Hyperlink_Text || pattern == Pattern.Hyperlink)
            {
            }
            else
            {
                throw new System.InvalidOperationException("FindMatches not implemented for this pattern");
            }
        }