using RMuseum.Models.Divan; using System; using System.Collections.Generic; namespace RMuseum.Services.Implementation { /// /// IDivanService implementation /// public partial class DivanService : IDivanService { private List _extractVersesFromPoemHtmlText(int poemId, string poemtext) { List verses = new List(); //this spagetti code has been imported from my internal utilities: while (poemtext.IndexOf("", ahrefStart) + 1, poemtext.IndexOf("") - (poemtext.IndexOf(">", ahrefStart) + 1)); poemtext = part1 + part2 + poemtext.Substring(poemtext.IndexOf("") + 4, poemtext.Length - (poemtext.IndexOf("") + 4)); } while (poemtext.IndexOf("", acroStart) + 1, poemtext.IndexOf("") - (poemtext.IndexOf(">", acroStart) + 1)); poemtext = part1 + part2 + poemtext.Substring(poemtext.IndexOf("") + 10, poemtext.Length - (poemtext.IndexOf("") + 10)); } catch { part2 = poemtext.Substring(poemtext.IndexOf(">", acroStart) + 1, poemtext.IndexOf("") - (poemtext.IndexOf(">", acroStart) + 1)); poemtext = part1 + part2 + poemtext.Substring(poemtext.IndexOf("") + 10, poemtext.Length - (poemtext.IndexOf("") + 10)); } } while (poemtext.IndexOf("") != -1) { string part1 = poemtext.Substring(0, poemtext.IndexOf("")); try { poemtext = part1 + poemtext.Substring(poemtext.IndexOf("") + 6, poemtext.Length - (poemtext.IndexOf("") + 6)); poemtext = poemtext.Replace(" ", " "); } catch { throw new Exception($"poemtext.IndexOf(\"\": {poemtext}"); } } while (poemtext.IndexOf("id=\"bn") != -1) { int idxbn1 = poemtext.IndexOf(" id=\"bn"); int idxbn2 = poemtext.IndexOf("\"", idxbn1 + " id=\"bn".Length); poemtext = poemtext.Substring(0, idxbn1) + poemtext.Substring(idxbn2+1); } poemtext = poemtext.Replace("Adaptation du milieu", "یییییییییییییییییییی"); poemtext = poemtext.Replace("Empirique", "ببببببببب"); poemtext = poemtext.Replace(" >", ">"); poemtext = poemtext.Replace("
", "
").Replace("
", "
").Replace("
", "
").Replace("
", "
").Replace("
", "
").Replace("

", "

").Replace("

", "

").Replace("

", "").Replace(" ", "").Replace("
", "").Replace("
", "
"); poemtext = poemtext.Replace("", "").Replace("", ""); poemtext = poemtext.Replace("", "").Replace("", "").Replace("", "").Replace("", ""); poemtext = poemtext.Replace("", "").Replace("", "").Replace("", "").Replace("", ""); poemtext = poemtext.Replace("


", "").Replace("
", ""); if (poemtext.IndexOf("\r\n") == 0) poemtext = poemtext.Substring(2); poemtext = poemtext.Replace("\r", "").Replace("\n", ""); poemtext = poemtext.Replace("
", "").Replace("

", ""); poemtext = poemtext.Replace("
", "a"); poemtext = poemtext.Replace("
", "b"); poemtext = poemtext.Replace("
", "m"); poemtext = poemtext.Replace("
", "n"); poemtext = poemtext.Replace("

", "s"); poemtext = poemtext.Replace("

", "s"); poemtext = poemtext.Replace("

", "l"); poemtext = poemtext.Replace("

", "l"); poemtext = poemtext.Replace("

", "c"); poemtext = poemtext.Replace("

", "c"); poemtext = poemtext.Replace("

", "p"); poemtext = poemtext.Replace("bmp", "b"); poemtext = poemtext.Replace("np", "n"); poemtext = poemtext.Replace("ap", "a"); poemtext = poemtext.Replace("\"", "").Replace("'", ""); if (poemtext.IndexOfAny(new char[] { '<', '>' }) != -1) throw new Exception($"Invalid Characteres: {poemtext}"); if (poemtext.IndexOf("mp") != -1) throw new Exception($"مصرع اول بدون مصرع دوم: {poemtext}"); if (poemtext.Length > 0) { int idx = poemtext.IndexOfAny(new char[] { 'a', 'b', 'm', 'n', 'p', 's', 'l' , 'c' }); bool preWasBand = false; while (idx != -1) { DivanVerse verse = new DivanVerse(); verse.PoemId = poemId; verse.VOrder = verses.Count + 1; switch (poemtext[idx]) { case 'p': if (preWasBand) verse.VersePosition = VersePosition.CenteredVerse2; else verse.VersePosition = VersePosition.Paragraph; preWasBand = false; break; case 'b': verse.VersePosition = VersePosition.Right; preWasBand = false; break; case 'n': verse.VersePosition = VersePosition.Left; preWasBand = false; break; case 'a': verse.VersePosition = VersePosition.CenteredVerse1; preWasBand = true; break; case 's': verse.VersePosition = VersePosition.Paragraph; preWasBand = false; break; case 'l': verse.VersePosition = VersePosition.Single; preWasBand = false; break; case 'c': verse.VersePosition = VersePosition.Comment; preWasBand = false; break; } int nextIdx = poemtext.IndexOfAny(new char[] { 'a', 'b', 'm', 'n', 'p', 's', 'l' , 'c' }, idx + 1); if (nextIdx == -1) { verse.Text = poemtext.Substring(idx + 1).Replace("یییییییییییییییییییی", "Adaptation du milieu").Replace("ببببببببب", "Empirique"); } else { verse.Text = poemtext.Substring(idx + 1, nextIdx - idx - 1).Replace("یییییییییییییییییییی", "Adaptation du milieu").Replace("ببببببببب", "Empirique"); } verse.Text = verse.Text.Trim(); verses.Add(verse); idx = nextIdx; } } return verses; } } }