/** * this methods parse the input text line by line and allocate related units and their contents */ protected RetCode Parse() { if (m_FileInPath.Length == 0) { return(RetCode.ERR_WRONGPATH); } if (!File.Exists(m_FileInPath)) { return(RetCode.ERR_FILENOTEXIST); } string[] lines = File.ReadAllLines(m_FileInPath); if (lines.Length == 0) { return(RetCode.ERR_FILEEMPTY); } m_ParsedWork = new MetaDoc(); int curLevel = 1; bool bInsideContent = false; bool bParsingIntro = false; int unitContentCount = 1; Unit curUnit = null; List <Unit> unitList = new List <Unit>(); string line = ""; // line currently read string prevLine = ""; string introText = ""; try { for (int iLine = 0; iLine < lines.Length; iLine++) { prevLine = line; line = lines[iLine]; // skip comments if (line.StartsWith(m_Tags.Comment)) { continue; } if (line.Length == 0 && !bInsideContent) // keep empty line inside units only { continue; } if (!bInsideContent) { // here parse the document's general attribute tags if (line.IndexOf(m_Tags.Header) >= 0) { m_ParsedWork.SetHeader(StripTag(line, m_Tags.Header)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Title) >= 0) { m_ParsedWork.SetTitle(StripTag(line, m_Tags.Title)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Subtitle) >= 0) { m_ParsedWork.SetSubTitle(StripTag(line, m_Tags.Subtitle)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Author) >= 0) { m_ParsedWork.SetAuthor(StripTag(line, m_Tags.Author)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Place) >= 0) { m_ParsedWork.SetPlace(StripTag(line, m_Tags.Place)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.PublishDate) >= 0) { m_ParsedWork.SetShowPublishDate(true); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Revision) >= 0) { m_ParsedWork.SetShowRevision(true); bParsingIntro = false; } else if (line.IndexOf(m_Tags.RebuildDate) >= 0) { m_ParsedWork.SetShowRebuildDate(true); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Abstract) >= 0) { m_ParsedWork.SetAbstract(StripTag(line, m_Tags.Abstract)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Summary) >= 0) { m_ParsedWork.SetSummaryEnabled(false); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Intro) >= 0) { introText += line; bParsingIntro = true; } else if (line.IndexOf(m_Tags.Category) >= 0) { m_ParsedWork.SetCategory(StripTag(line, m_Tags.Category)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.NoGlobal) >= 0) { m_ParsedWork.SetShowInGlobalIndex(false); bParsingIntro = false; } else if (line.IndexOf(m_Tags.NoProcess) >= 0) { m_ParsedWork.SetNoProcess(true); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Dedication) >= 0) { m_ParsedWork.SetDedication(StripTag(line, m_Tags.Dedication)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Creation) >= 0) { m_ParsedWork.SetCreationDate(StripTag(line, m_Tags.Creation)); bParsingIntro = false; } else if (line.IndexOf(m_Tags.ShowUnitNumber) >= 0) { m_ParsedWork.SetShowUnitNumber(true); bParsingIntro = false; } else if (line.IndexOf(m_Tags.Start) >= 0) // now start! { bParsingIntro = false; m_ParsedWork.SetIntro(StripTag(introText, m_Tags.Intro)); bInsideContent = true; } else { if (bParsingIntro) { introText += ("\n" + line); } } continue; } //// from here start parsing the text // normalize the line stripping the indentation level curLevel = GetIndentationLevel(line); line = StripIndentation(line); if (curUnit == null && line.Length == 0) { continue; // skip starting empty lines } if (line.StartsWith(m_Tags.UnitStart)) // new unit is found { // store previous unit if (curUnit != null) { unitList.Add(curUnit); } // create a new unit curUnit = new Unit(unitList.Count + 1); curUnit.SetLevel(curLevel); curUnit.SetTitleVisible(!line.Contains(m_Tags.UnitNoTitle)); curUnit.SetVisibleInSummary(!line.Contains(m_Tags.UnitNoSummary)); line = line.Replace(m_Tags.UnitNoTitle, ""); line = line.Replace(m_Tags.UnitNoSummary, ""); line = line.Replace(m_Tags.UnitStart, ""); curUnit.SetTitle(line); unitContentCount = 0; continue; } //// parse contents if (curUnit == null) { throw new Exception("No unit defined in the body text! Every content must be included inside a unit"); } if (prevLine.Length == 0 && line.Length == 0) { continue; // avoid multiple empty lines } Content predictedContent = TestLineContentType(line, curUnit.GetLastContent()); if (predictedContent == null) { continue; } ParseCode parseRet = ParseCode.NO_PARSE; if (predictedContent.GetType() == typeof(Quote)) { parseRet = LineParseQuote(ref line, ref unitContentCount, ref curUnit); } else if (predictedContent.GetType() == typeof(MetaImage)) { parseRet = LineParseImage(ref line, ref unitContentCount, ref curUnit); } else if (predictedContent.GetType() == typeof(ItemList)) { parseRet = LineParseItemList(ref line, ref unitContentCount, ref curUnit); } else if (predictedContent.GetType() == typeof(Paragraph)) { parseRet = LineParseParagraph(ref line, ref unitContentCount, ref curUnit); } else { throw new Exception("Not handled content detection"); } if (parseRet == ParseCode.ERROR) { Globals.m_Logger.Error(string.Format("Content parser error parsing line nr. {0}", iLine)); } else if (parseRet == ParseCode.NO_PARSE) { Globals.m_Logger.Warn(string.Format("Content parser warning: nothing to parse at line {0}", iLine)); } } // handle last unit if (curUnit != null) { unitList.Add(curUnit); } } catch (Exception ex) { Globals.m_Logger.Error(ex.ToString()); return(RetCode.ERR_EXCEPTION); } // ok, parsed. Now we organize the units' gerarchy Unit root = BuildUnitTree(unitList); if (root == null) { return(RetCode.ERR_INVALIDTREE); } m_ParsedWork.SetUnit(root); m_ParsedWork.SetCurrentHash(Utils.GetHashSha256(m_FileInPath)); return(RetCode.NO_ERR); }