private static Replacement RowToReplacement(DataGridViewRow dataGridRow)
        {
            Replacement rep = new Replacement
                                  {
                                      Enabled = ((bool)dataGridRow.Cells["enabled"].FormattedValue),
                                      Minor = ((bool)dataGridRow.Cells["minor"].FormattedValue),
                                      IsRegex = ((bool)dataGridRow.Cells["regex"].FormattedValue),
                                      BeforeOrAfter = ((bool)dataGridRow.Cells["BeforeOrAfter"].FormattedValue)
                                  };

            if (dataGridRow.Cells["replace"].Value == null)
                dataGridRow.Cells["replace"].Value = "";

            string f = Encode(dataGridRow.Cells["find"].Value.ToString());
            string r = Encode(dataGridRow.Cells["replace"].Value.ToString());

            if (!rep.IsRegex)
                f = Regex.Escape(f);

            rep.Find = f;
            rep.Replace = r;

            if (!(bool)dataGridRow.Cells["casesensitive"].FormattedValue)
                rep.RegularExpressionOptions |= RegexOptions.IgnoreCase;

            if ((bool)dataGridRow.Cells["multi"].FormattedValue)
                rep.RegularExpressionOptions |= RegexOptions.Multiline;

            if ((bool)dataGridRow.Cells["single"].FormattedValue)
                rep.RegularExpressionOptions |= RegexOptions.Singleline;

            rep.Comment = (string)dataGridRow.Cells["comment"].FormattedValue ?? "";

            return rep;
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rep"></param>
        /// <param name="articleText"></param>
        /// <param name="articleTitle"></param>
        /// <param name="changeMade"></param>
        /// <returns></returns>
        private string PerformFindAndReplace(Replacement rep, string articleText, string articleTitle, out bool changeMade)
        {
            if (rep == null)
            {
                throw new ArgumentNullException("rep");
            }

            string findThis    = Tools.ApplyKeyWords(articleTitle, rep.Find, true);
            string replaceWith = Tools.ApplyKeyWords(articleTitle, PrepareReplacePart(rep.Replace));

            Regex           findRegex = new Regex(findThis, rep.RegularExpressionOptions);
            MatchCollection matches   = findRegex.Matches(articleText);

            changeMade = false;

            if (matches.Count > 0)
            {
                articleText = findRegex.Replace(articleText, replaceWith);

                if (matches[0].Value != matches[0].Result(replaceWith))
                {
                    changeMade = true;

                    if (!string.IsNullOrEmpty(matches[0].Result(replaceWith)))
                    {
                        if (!string.IsNullOrEmpty(_replacedSummary)) //Add comma before next replaced
                        {
                            _replacedSummary += ", ";
                        }

                        string summary = matches[0].Value + Arrow + matches[0].Result(replaceWith);

                        if (matches.Count > 1)
                        {
                            summary += " (" + matches.Count + ")";
                        }

                        _replacedSummary += summary;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(_removedSummary)) //Add comma before next removed
                        {
                            _removedSummary += ", ";
                        }

                        _removedSummary += matches[0].Value;

                        if (matches.Count > 1)
                        {
                            _removedSummary += " (" + matches.Count + ")";
                        }
                    }
                }
            }

            return(articleText);
        }
Example #3
0
        private static Replacement RowToReplacement(DataGridViewRow dataGridRow)
        {
            Replacement rep = new Replacement();
            string      f   = "";
            string      r   = "";

            rep.Enabled = ((bool)dataGridRow.Cells["enabled"].FormattedValue);

            if (dataGridRow.Cells["replace"].Value == null)
            {
                dataGridRow.Cells["replace"].Value = "";
            }

            f = dataGridRow.Cells["find"].Value.ToString();
            r = dataGridRow.Cells["replace"].Value.ToString();

            f = Encode(f);
            r = Encode(r);

            if (!(bool)dataGridRow.Cells["regex"].FormattedValue)
            {
                f           = Regex.Escape(f);
                rep.IsRegex = false;
            }
            else
            {
                rep.IsRegex = true;
            }

            rep.Find    = f;
            rep.Replace = r;

            rep.RegularExpressionOptions = RegexOptions.None;
            if (!(bool)dataGridRow.Cells["casesensitive"].FormattedValue)
            {
                rep.RegularExpressionOptions = rep.RegularExpressionOptions | RegexOptions.IgnoreCase;
            }
            if ((bool)dataGridRow.Cells["multi"].FormattedValue)
            {
                rep.RegularExpressionOptions = rep.RegularExpressionOptions | RegexOptions.Multiline;
            }

            if ((bool)dataGridRow.Cells["single"].FormattedValue)
            {
                rep.RegularExpressionOptions = rep.RegularExpressionOptions | RegexOptions.Singleline;
            }

            if (dataGridRow.Cells["comment"].Value == null)
            {
                dataGridRow.Cells["comment"].Value = "";
            }

            rep.Comment = (string)dataGridRow.Cells["comment"].Value.ToString();

            return(rep);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="r"></param>
        public void AddNew(Replacement r)
        {
            bool caseSens   = (r.RegularExpressionOptions & RegexOptions.IgnoreCase) != RegexOptions.IgnoreCase;
            bool multiline  = (r.RegularExpressionOptions & RegexOptions.Multiline) == RegexOptions.Multiline;
            bool singleLine = (r.RegularExpressionOptions & RegexOptions.Singleline) == RegexOptions.Singleline;

            dataGridView1.Rows.Add(r.IsRegex ? Decode(r.Find) : Regex.Unescape(Decode(r.Find)), Decode(r.Replace),
                                   caseSens, r.IsRegex, multiline, singleLine, r.Minor, r.BeforeOrAfter, r.Enabled, r.Comment);

            _replacementList.Add(r);
        }
Example #5
0
        private static Replacement RowToReplacement(DataGridViewRow dataGridRow)
        {
            Replacement rep = new Replacement
            {
                Enabled       = ((bool)dataGridRow.Cells["enabled"].FormattedValue),
                Minor         = ((bool)dataGridRow.Cells["minor"].FormattedValue),
                IsRegex       = ((bool)dataGridRow.Cells["regex"].FormattedValue),
                BeforeOrAfter = ((bool)dataGridRow.Cells["BeforeOrAfter"].FormattedValue)
            };

            if (dataGridRow.Cells["replace"].Value == null)
            {
                dataGridRow.Cells["replace"].Value = "";
            }

            string f = Encode(dataGridRow.Cells["find"].Value.ToString());
            string r = Encode(dataGridRow.Cells["replace"].Value.ToString());

            // in F&R newline matching is on \n, so if not a regex ensure this isn't escaped
            if (!rep.IsRegex)
            {
                bool newlines = f.Contains("\\n");
                f = Regex.Escape(f);

                if (newlines)
                {
                    f = f.Replace(@"\\n", "\n");
                }
            }

            rep.Find    = f;
            rep.Replace = r;

            if (!(bool)dataGridRow.Cells["casesensitive"].FormattedValue)
            {
                rep.RegularExpressionOptions |= RegexOptions.IgnoreCase;
            }

            if ((bool)dataGridRow.Cells["multi"].FormattedValue)
            {
                rep.RegularExpressionOptions |= RegexOptions.Multiline;
            }

            if ((bool)dataGridRow.Cells["single"].FormattedValue)
            {
                rep.RegularExpressionOptions |= RegexOptions.Singleline;
            }

            rep.Comment = (string)dataGridRow.Cells["comment"].FormattedValue ?? "";

            return(rep);
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="r"></param>
        public void AddNew(Replacement r)
        {
            bool caseSens   = !r.RegularExpressionOptions.ToString().Contains("IgnoreCase");
            bool multiine   = r.RegularExpressionOptions.ToString().Contains("Multiline");
            bool singleLine = r.RegularExpressionOptions.ToString().Contains("Singleline");

            dataGridView1.Rows.Add(r.IsRegex ? Decode(r.Find) : Regex.Unescape(Decode(r.Find)), Decode(r.Replace),
                                   caseSens, r.IsRegex, multiine, singleLine, r.Minor, r.Enabled, r.Comment);

            if (!r.Enabled)
            {
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].DefaultCellStyle.BackColor = Color.LightGray;
            }

            _replacementList.Add(r);
        }
Example #7
0
        private static Replacement RowToReplacement(DataGridViewRow dataGridRow)
        {
            Replacement rep = new Replacement
            {
                Enabled       = ((bool)dataGridRow.Cells["enabled"].FormattedValue),
                Minor         = ((bool)dataGridRow.Cells["minor"].FormattedValue),
                IsRegex       = ((bool)dataGridRow.Cells["regex"].FormattedValue),
                BeforeOrAfter = ((bool)dataGridRow.Cells["BeforeOrAfter"].FormattedValue)
            };

            if (dataGridRow.Cells["replace"].Value == null)
            {
                dataGridRow.Cells["replace"].Value = "";
            }

            string f = Encode(dataGridRow.Cells["find"].Value.ToString());
            string r = Encode(dataGridRow.Cells["replace"].Value.ToString());

            if (!rep.IsRegex)
            {
                f = Regex.Escape(f);
            }

            rep.Find    = f;
            rep.Replace = r;

            if (!(bool)dataGridRow.Cells["casesensitive"].FormattedValue)
            {
                rep.RegularExpressionOptions |= RegexOptions.IgnoreCase;
            }

            if ((bool)dataGridRow.Cells["multi"].FormattedValue)
            {
                rep.RegularExpressionOptions |= RegexOptions.Multiline;
            }

            if ((bool)dataGridRow.Cells["single"].FormattedValue)
            {
                rep.RegularExpressionOptions |= RegexOptions.Singleline;
            }

            rep.Comment = (string)dataGridRow.Cells["comment"].FormattedValue ?? "";

            return(rep);
        }
        private static Replacement RowToReplacement(DataGridViewRow dataGridRow)
        {
            Replacement rep = new Replacement();
            string f = "";
            string r = "";

            rep.Enabled = ((bool)dataGridRow.Cells["enabled"].FormattedValue);

            if (dataGridRow.Cells["replace"].Value == null)
                dataGridRow.Cells["replace"].Value = "";

            f = dataGridRow.Cells["find"].Value.ToString();
            r = dataGridRow.Cells["replace"].Value.ToString();

            f = Encode(f);
            r = Encode(r);

            if (!(bool)dataGridRow.Cells["regex"].FormattedValue)
            {
                f = Regex.Escape(f);
                rep.IsRegex = false;
            }
            else
                rep.IsRegex = true;

            rep.Find = f;
            rep.Replace = r;

            rep.RegularExpressionOptions = RegexOptions.None;
            if (!(bool)dataGridRow.Cells["casesensitive"].FormattedValue)
                rep.RegularExpressionOptions = rep.RegularExpressionOptions | RegexOptions.IgnoreCase;
            if ((bool)dataGridRow.Cells["multi"].FormattedValue)
                rep.RegularExpressionOptions = rep.RegularExpressionOptions | RegexOptions.Multiline;

            if ((bool)dataGridRow.Cells["single"].FormattedValue)
                rep.RegularExpressionOptions = rep.RegularExpressionOptions | RegexOptions.Singleline;

            if (dataGridRow.Cells["comment"].Value == null)
                dataGridRow.Cells["comment"].Value = "";

            rep.Comment = (string)dataGridRow.Cells["comment"].Value.ToString();

            return rep;
        }
        private static Replacement RowToReplacement(DataGridViewRow dataGridRow)
        {
            Replacement rep = new Replacement
                                  {
                                      Enabled = ((bool)dataGridRow.Cells["enabled"].FormattedValue),
                                      Minor = ((bool)dataGridRow.Cells["minor"].FormattedValue),
                                      IsRegex = ((bool)dataGridRow.Cells["regex"].FormattedValue),
                                      BeforeOrAfter = ((bool)dataGridRow.Cells["BeforeOrAfter"].FormattedValue)
                                  };

            if (dataGridRow.Cells["replace"].Value == null)
                dataGridRow.Cells["replace"].Value = "";

            string f = Encode(dataGridRow.Cells["find"].Value.ToString());
            string r = Encode(dataGridRow.Cells["replace"].Value.ToString());

            // in F&R newline matching is on \n, so if not a regex ensure this isn't escaped
            if (!rep.IsRegex)
            {
                bool newlines = f.Contains("\\n");
                f = Regex.Escape(f);
                
                if(newlines)
                    f = f.Replace(@"\\n", "\n");
            }

            rep.Find = f;
            rep.Replace = r;

            if (!(bool)dataGridRow.Cells["casesensitive"].FormattedValue)
                rep.RegularExpressionOptions |= RegexOptions.IgnoreCase;

            if ((bool)dataGridRow.Cells["multi"].FormattedValue)
                rep.RegularExpressionOptions |= RegexOptions.Multiline;

            if ((bool)dataGridRow.Cells["single"].FormattedValue)
                rep.RegularExpressionOptions |= RegexOptions.Singleline;

            rep.Comment = (string)dataGridRow.Cells["comment"].FormattedValue ?? "";

            return rep;
        }
Example #10
0
        public void AddNew(Replacement R)
        {
            bool caseSens   = !R.RegularExpressionOptions.ToString().Contains("IgnoreCase");
            bool multiine   = R.RegularExpressionOptions.ToString().Contains("Multiline");
            bool singleLine = R.RegularExpressionOptions.ToString().Contains("Singleline");

            if (!R.IsRegex)
            {
                dataGridView1.Rows.Add(Regex.Unescape(Decode(R.Find)), Decode(R.Replace), caseSens, R.IsRegex, multiine, singleLine, R.Enabled, R.Comment);
            }
            else
            {
                dataGridView1.Rows.Add(Decode(R.Find), Decode(R.Replace), caseSens, R.IsRegex, multiine, singleLine, R.Enabled, R.Comment);
            }

            if (!R.Enabled)
            {
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].DefaultCellStyle.BackColor = Color.LightGray;
            }

            ReplacementList.Add(R);
        }
Example #11
0
        public void FindAndReplaceRemove()
        {
            WikiFunctions.Parse.Replacement r = new WikiFunctions.Parse.Replacement("foo", "", true, true, true, true, RegexOptions.None, "");

            WikiFunctions.Parse.FindandReplace fr = new FindandReplace();

            bool changemade = false;

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the was", "Test", out changemade), "the was");
            Assert.IsFalse(changemade);
            Assert.AreEqual(null, fr.ReplacedSummary, "No match: no edit summary");
            Assert.AreEqual(null, fr.RemovedSummary, "No match: no edit summary");

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foo was", "Test", out changemade), "the  was");
            Assert.IsTrue(changemade);
            Assert.AreEqual("foo", fr.RemovedSummary, "One match: removed a");

            fr = new FindandReplace();
            changemade = false;

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foo was or foo was", "Test", out changemade), "the  was or  was");
            Assert.AreEqual("foo (2)", fr.RemovedSummary, "Match count shown");

            r = new WikiFunctions.Parse.Replacement("foot?", "", true, true, true, true, RegexOptions.None, "");
            fr = new FindandReplace();
            changemade = false;

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foot was or foo was", "Test", out changemade), "the  was or  was");
            Assert.IsTrue(changemade);
            Assert.AreEqual("foot (2)", fr.RemovedSummary, "Different matches, match text of first used");
        }
Example #12
0
        public void FindAndReplaceProperties()
        {
            WikiFunctions.Parse.Replacement r = new WikiFunctions.Parse.Replacement("foo", "bar", true, true, true, true, RegexOptions.None, "");
            WikiFunctions.Parse.Replacement r2 = new WikiFunctions.Parse.Replacement("foo2", "bar2", true, true, true, false, RegexOptions.None, "");
            WikiFunctions.Parse.Replacement r2Disabled = new WikiFunctions.Parse.Replacement("foo2", "bar2", true, false, true, false, RegexOptions.None, "");
            WikiFunctions.Parse.FindandReplace fr = new FindandReplace();

            fr.AddNew(r);
            Assert.IsTrue(fr.HasAfterProcessingReplacements);
            Assert.IsTrue(fr.HasProcessingReplacements(true));
            Assert.IsFalse(fr.HasProcessingReplacements(false));

            fr.AddNew(r2);
            Assert.IsTrue(fr.HasAfterProcessingReplacements);
            Assert.IsTrue(fr.HasProcessingReplacements(true));
            Assert.IsTrue(fr.HasProcessingReplacements(false));

            fr.Clear();
            fr.AddNew(r2);
            Assert.IsFalse(fr.HasAfterProcessingReplacements);
            Assert.IsFalse(fr.HasProcessingReplacements(true));
            Assert.IsTrue(fr.HasProcessingReplacements(false));

            // all false when no enabled rules
            fr.Clear();
            fr.AddNew(r2Disabled);
            Assert.IsFalse(fr.HasAfterProcessingReplacements);
            Assert.IsFalse(fr.HasProcessingReplacements(true));
            Assert.IsFalse(fr.HasProcessingReplacements(false));

            fr.Clear();
            List<WikiFunctions.Parse.Replacement> l = new List<Replacement>();
            l.Add(r);
            l.Add(r2);
            fr.AddNew(l);
            Assert.IsTrue(fr.HasReplacements);
            Assert.AreEqual(fr.GetList(), l);
        }
Example #13
0
        public void FindAndReplaceNewLines()
        {
            // regex
            WikiFunctions.Parse.Replacement r = new WikiFunctions.Parse.Replacement("foo\n", "bar ", true, true, true, true, RegexOptions.None, "");
            WikiFunctions.Parse.FindandReplace fr = new FindandReplace();
            bool changemade = false;

            Assert.AreEqual("the bar was", fr.PerformFindAndReplace(r, "the foo\nwas", "Test", out changemade));
            Assert.IsTrue(changemade);

            // not regex
            r = new WikiFunctions.Parse.Replacement("foo\n", "bar ", false, true, true, true, RegexOptions.None, "");
            Assert.AreEqual("the bar was", fr.PerformFindAndReplace(r, "the foo\nwas", "Test", out changemade));
            Assert.IsTrue(changemade);
        }
Example #14
0
        public void FindAndReplace()
        {
            WikiFunctions.Parse.Replacement r = new WikiFunctions.Parse.Replacement("foo", "bar", true, true, true, true, RegexOptions.None, "");

            WikiFunctions.Parse.FindandReplace fr = new FindandReplace();

            bool changemade = false;

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the was", "Test", out changemade), "the was");
            Assert.IsFalse(changemade);
            Assert.AreEqual(null, fr.ReplacedSummary, "No match: no edit summary");

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foo was", "Test", out changemade), "the bar was");
            Assert.IsTrue(changemade);
            Assert.AreEqual("foo" + WikiFunctions.Parse.FindandReplace.Arrow + "bar", fr.ReplacedSummary, "One match: a to b");

            fr = new FindandReplace();
            changemade = false;

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foo was or foo was", "Test", out changemade), "the bar was or bar was");
            Assert.AreEqual("foo" + WikiFunctions.Parse.FindandReplace.Arrow + "bar (2)", fr.ReplacedSummary, "Match count shown");

            fr = new FindandReplace();
            changemade = false;

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foo was or foo was foo", "Test", out changemade), "the bar was or bar was bar");
            Assert.AreEqual("foo" + WikiFunctions.Parse.FindandReplace.Arrow + "bar (3)", fr.ReplacedSummary, "Match count shown, 3");

            r = new WikiFunctions.Parse.Replacement("foot?", "bar", true, true, true, true, RegexOptions.None, "");
            fr = new FindandReplace();
            changemade = false;

            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foot was or foo was", "Test", out changemade), "the bar was or bar was");
            Assert.IsTrue(changemade);
            Assert.AreEqual("foot" + WikiFunctions.Parse.FindandReplace.Arrow + "bar (2)", fr.ReplacedSummary, "Different matches, match text of first used");

            r = new WikiFunctions.Parse.Replacement("fooo?", "foo", true, true, true, true, RegexOptions.None, "");
            fr = new FindandReplace();
            changemade = false;
            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foo was a fooo it", "Test", out changemade), "the foo was a foo it");
            Assert.IsTrue(changemade);
            Assert.AreEqual("fooo" + WikiFunctions.Parse.FindandReplace.Arrow + "foo", fr.ReplacedSummary, "No-change match ignored");

            fr = new FindandReplace();
            changemade = false;
            Assert.AreEqual(fr.PerformFindAndReplace(r, "the foo was", "Test", out changemade), "the foo was");
            Assert.IsFalse(changemade, "only match is no-change on replace, so no change made");
            Assert.AreEqual(null, fr.ReplacedSummary, "Only match is No-change match, no edit summary");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="r"></param>
        public void AddNew(Replacement r)
        {
            bool caseSens = !r.RegularExpressionOptions.ToString().Contains("IgnoreCase");
            bool multiine = r.RegularExpressionOptions.ToString().Contains("Multiline");
            bool singleLine = r.RegularExpressionOptions.ToString().Contains("Singleline");

            dataGridView1.Rows.Add(r.IsRegex ? Decode(r.Find) : Regex.Unescape(Decode(r.Find)), Decode(r.Replace),
                                   caseSens, r.IsRegex, multiine, singleLine, r.Minor, r.Enabled, r.Comment);

            if (!r.Enabled)
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].DefaultCellStyle.BackColor = Color.LightGray;

            _replacementList.Add(r);
        }
        public void AddNew(Replacement R)
        {
            bool caseSens = !R.RegularExpressionOptions.ToString().Contains("IgnoreCase");
            bool multiine = R.RegularExpressionOptions.ToString().Contains("Multiline");
            bool singleLine = R.RegularExpressionOptions.ToString().Contains("Singleline");

            if(!R.IsRegex)
                dataGridView1.Rows.Add(Regex.Unescape(Decode(R.Find)), Decode(R.Replace), caseSens, R.IsRegex, multiine, singleLine, R.Enabled, R.Comment);
            else
                dataGridView1.Rows.Add(Decode(R.Find), Decode(R.Replace), caseSens, R.IsRegex, multiine, singleLine, R.Enabled, R.Comment);

            if (!R.Enabled)
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].DefaultCellStyle.BackColor = Color.LightGray;

            ReplacementList.Add(R);
        }
        /// <summary>
        /// Executes a single find & replace rule against the article text. First applies keywords to the find and replace portions of the rule, then executes the rule.
        /// Edit summary is generated from the first match of the rule that changes the article text on replacement. Count of changes is replacements affecting article, not total matches
        /// i.e. no-change replacements are not counted in the edit summary
        /// </summary>
        /// <param name="rep">F&amp;R rule to execute</param>
        /// <param name="articleText">The article text</param>
        /// <param name="articleTitle">The article title</param>
        /// <param name="changeMade">Whether the F&amp;R rule caused changes to the article text</param>
        /// <returns>The updated article text</returns>
        public string PerformFindAndReplace(Replacement rep, string articleText, string articleTitle, out bool changeMade)
        {
            if (rep == null) throw new ArgumentNullException("rep");

            string findThis = Tools.ApplyKeyWords(articleTitle, rep.Find, true), replaceWith = Tools.ApplyKeyWords(articleTitle, PrepareReplacePart(rep.Replace));

            Regex findRegex = new Regex(findThis, rep.RegularExpressionOptions);

            int Repcount = 0, Remcount = 0;

            // use first replace that changes article text to generate edit summary
            string res = findRegex.Replace(articleText, m =>  {
                                               string mres = m.Result(replaceWith);

                                               if (!m.Value.Equals(mres))
                                               {
                                                   if (!string.IsNullOrEmpty(mres))
                                                   {
                                                       if(Repcount == 0)
                                                       {
                                                           if (!string.IsNullOrEmpty(ReplacedSummary)) //Add comma before next replaced
                                                               ReplacedSummary +=  ", ";

                                                           ReplacedSummary += m.Value + Arrow + mres;
                                                       }
                                                       Repcount++;
                                                   }
                                                   else
                                                   {
                                                       if(Remcount == 0)
                                                       {
                                                           if (!string.IsNullOrEmpty(RemovedSummary)) //Add comma before next removed
                                                               RemovedSummary += ", ";

                                                           RemovedSummary += m.Value;
                                                       }
                                                       Remcount++;
                                                   }
                                               }
                                               
                                               return mres;
                                           } );

            // update summaries with count of changes
            if(Repcount > 1)
                ReplacedSummary += " (" + Repcount + ")";

            if(Remcount > 1)
                RemovedSummary += " (" + Remcount + ")";

            changeMade = (Repcount + Remcount > 0);

            return res;
        }
Example #18
0
 /// <summary>
 /// Loads a single replacement entry into the find and replace data grid
 /// </summary>
 /// <param name="r"></param>
 public void AddNew(Replacement r)
 {
     AddNew(r, true);
 }
Example #19
0
 public Replacement(Replacement repl)
     : this(
         repl.Find, repl.Replace, repl.IsRegex, repl.Enabled, repl.Minor,
         repl.BeforeOrAfter, repl.RegularExpressionOptions, repl.Comment)
 {
 }
Example #20
0
        /// <summary>
        /// Executes a single find & replace rule against the article text. First applies keywords to the find and replace portions of the rule, then executes the rule.
        /// Edit summary is generated from the first match of the rule that changes the article text on replacement. Count of changes is replacements affecting article, not total matches
        /// i.e. no-change replacements are not counted in the edit summary
        /// </summary>
        /// <param name="rep">F&amp;R rule to execute</param>
        /// <param name="articleText">The article text</param>
        /// <param name="articleTitle">The article title</param>
        /// <param name="changeMade">Whether the F&amp;R rule caused changes to the article text</param>
        /// <returns>The updated article text</returns>
        public string PerformFindAndReplace(Replacement rep, string articleText, string articleTitle, out bool changeMade)
        {
            if (rep == null)
            {
                throw new ArgumentNullException("rep");
            }

            string findThis = Tools.ApplyKeyWords(articleTitle, rep.Find, true), replaceWith = Tools.ApplyKeyWords(articleTitle, PrepareReplacePart(rep.Replace));

            string comma = @", ";

            if (Variables.LangCode.Equals("ar") || Variables.LangCode.Equals("arz") || Variables.LangCode.Equals("fa"))
            {
                comma = @"، ";
            }

            Regex findRegex = new Regex(findThis, rep.RegularExpressionOptions);

            int Repcount = 0, Remcount = 0;

            // use first replace that changes article text to generate edit summary
            string res = findRegex.Replace(articleText, m => {
                string mres = m.Result(replaceWith);

                if (!m.Value.Equals(mres))
                {
                    if (!string.IsNullOrEmpty(mres))
                    {
                        if (Repcount == 0)
                        {
                            if (!string.IsNullOrEmpty(ReplacedSummary))                                //Add comma before next replaced
                            {
                                ReplacedSummary += comma;
                            }

                            ReplacedSummary += m.Value + Arrow + mres;
                        }
                        Repcount++;
                    }
                    else
                    {
                        if (Remcount == 0)
                        {
                            if (!string.IsNullOrEmpty(RemovedSummary))                                //Add comma before next removed
                            {
                                RemovedSummary += comma;
                            }

                            RemovedSummary += m.Value;
                        }
                        Remcount++;
                    }
                }

                return(mres);
            });

            // update summaries with count of changes
            if (Repcount > 1)
            {
                ReplacedSummary += " (" + Repcount + ")";
            }

            if (Remcount > 1)
            {
                RemovedSummary += " (" + Remcount + ")";
            }

            changeMade = (Repcount + Remcount > 0);

            return(res);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="r"></param>
        public void AddNew(Replacement r)
        {
            bool caseSens = (r.RegularExpressionOptions & RegexOptions.IgnoreCase) != RegexOptions.IgnoreCase;
            bool multiline = (r.RegularExpressionOptions & RegexOptions.Multiline) == RegexOptions.Multiline;
            bool singleLine = (r.RegularExpressionOptions & RegexOptions.Singleline) == RegexOptions.Singleline;

            dataGridView1.Rows.Add(r.IsRegex ? Decode(r.Find) : Regex.Unescape(Decode(r.Find)), Decode(r.Replace),
                                   caseSens, r.IsRegex, multiline, singleLine, r.Minor, r.BeforeOrAfter, r.Enabled, r.Comment);

            _replacementList.Add(r);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rep"></param>
        /// <param name="articleText"></param>
        /// <param name="articleTitle"></param>
        /// <param name="changeMade"></param>
        /// <returns></returns>
        private string PerformFindAndReplace(Replacement rep, string articleText, string articleTitle, out bool changeMade)
        {
            if (rep == null) throw new ArgumentNullException("rep");

            string findThis = Tools.ApplyKeyWords(articleTitle, rep.Find);
            string replaceWith = Tools.ApplyKeyWords(articleTitle, PrepareReplacePart(rep.Replace));

            Regex findRegex = new Regex(findThis, rep.RegularExpressionOptions);
            MatchCollection matches = findRegex.Matches(articleText);

            changeMade = false;

            if (matches.Count > 0)
            {
                articleText = findRegex.Replace(articleText, replaceWith);

                if (matches[0].Value != matches[0].Result(replaceWith))
                {
                    changeMade = true;

                    if (!string.IsNullOrEmpty(matches[0].Result(replaceWith)))
                    {
                        if (!string.IsNullOrEmpty(_replacedSummary)) //Add comma before next replaced
                            _replacedSummary +=  ", ";

                        string summary = matches[0].Value + Arrow + matches[0].Result(replaceWith);

                        if (matches.Count > 1)
                            summary += " (" + matches.Count + ")";

                        _replacedSummary += summary;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(_removedSummary)) //Add comma before next removed
                            _removedSummary += ", ";

                        _removedSummary += matches[0].Value;

                        if (matches.Count > 1)
                            _removedSummary += " (" + matches.Count + ")";
                    }
                }
            }

            return articleText;
        }
 public Replacement(Replacement repl)
     : this(
         repl.Find, repl.Replace, repl.IsRegex, repl.Enabled, repl.Minor,
         repl.BeforeOrAfter, repl.RegularExpressionOptions, repl.Comment)
 {
 }
Example #24
0
 /// <summary>
 /// Loads a single replacement entry into the find and replace data grid
 /// </summary>
 /// <param name="r"></param>
 public void AddNew(Replacement r)
 {
     AddNew(r, true);
 }