public void GetEnvironmentsTest() { string text = @"\begin{equation*} \begin{env1}text\end{env1} \begin{env2} text %\begin{envCommented} \begin{env3} text \end{env3} text \end{env2} \|f\|_{p(\cdot),w} \le r_{p,q}^w \|f\|_{q(\cdot),w}, \end{equation*}"; var actual = Utils.GetEnvironments(text).ToArray(); var expected = new TextProcessor.Latex.Environment[] { new TextProcessor.Latex.Environment(19, @"\begin{env1}", 35, @"\end{env1}", @"env1"), new TextProcessor.Latex.Environment(102, @"\begin{env3}", 134, @"\end{env3}", @"env3"), new TextProcessor.Latex.Environment(47, @"\begin{env2}", 156, @"\end{env2}", @"env2"), new TextProcessor.Latex.Environment(0, @"\begin{equation*}", 228, @"\end{equation*}", @"equation*"), }; Assert.AreEqual(expected, actual); }
/// <summary> /// Reorders biblio items in appearing order and removes orphan biblio items /// </summary> /// <param name="sources"></param> /// <returns>biblioIndex is index in <paramref name="sources" /> of source from which /// bibliography env was taken. This source is changed and new version of it contains in modBiblio /// </returns> public static (int biblioIndex, string modBiblio) ArrangeCites(params string[] sources) { // find thebibliography env Environment bibenv = null; List <Bibitem> bibitems = null; string sourceWithBibenv = null; var citeKeys = new List <string>(); var biblioIndex = -1; for (var i = 0; i < sources.Length; i++) { var source = sources[i]; citeKeys.AddRange(Utils.GetCites(source).SelectMany(c => c.Keys)); if (source.Contains("thebibliography")) { biblioIndex = i; bibenv = Utils.FindEnv(source, "thebibliography"); bibitems = Utils.GetBibitems(source, bibenv); sourceWithBibenv = source; } } if (biblioIndex == -1) { throw new Exception("thebibliography environment not found"); } var newBibitemList = new List <Bibitem>(); Console.WriteLine("Bib keys in order of appearance:"); foreach (var citeKey in citeKeys) { if (newBibitemList.Any(b => b.Key == citeKey)) { continue; } var item = bibitems.Find(b => b.Key == citeKey); if (item == null) { Console.WriteLine($"Warning: not bibitem for cite key {citeKey}"); } else { Console.WriteLine(citeKey); newBibitemList.Add(item); } } // Replace bibitems in thebibliography environment var startpos = bibitems.First().Block.StartPos; var endpos = bibitems.Last().Block.EndPos; var sb = new StringBuilder(sources[biblioIndex]); sb.Remove(startpos, endpos - startpos + 1) .Insert(startpos, String.Join("\r\n", newBibitemList)); return(biblioIndex, sb.ToString()); }