/// <summary>
        /// Checks Opening Marker for Unique End Marker 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public List<LinterResult> CheckOpenMarker(Marker input, USFMDocument root)
        {
            List<int> markerPositions = new List<int>();
            List<Marker> hierarchy = root.GetHierarchyToMarker(input);
            List<Marker> siblingMarkers = new List<Marker>(hierarchy[hierarchy.Count - 2].Contents);
            siblingMarkers.Reverse();
            foreach (Marker sibling in siblingMarkers)
            {
                if (sibling.GetType() == input.GetType())
                {
                    markerPositions.Add(sibling.Position);
                }
                else if (sibling.GetType() == markerPairs[input.GetType()])
                {
                    if (markerPositions.Count > 0)
                    {
                        markerPositions.RemoveAt(markerPositions.Count - 1);
                    }
                }
            }
            List<LinterResult> results = new List<LinterResult>();
            foreach (int loneMarkerPosition in markerPositions)
            {
                results.Add(new LinterResult
                {
                    Position = loneMarkerPosition,
                    Level = LinterLevel.Error,
                    Message = $"Missing Opening marker for {input.GetType().Name}"
                });

            }
            return results;

        }
示例#2
0
        public USFMDocument ParseFromString(string input)
        {
            Regex        splitRegex = new Regex("\\\\([a-z0-9*]+)([^\\\\]*)");
            USFMDocument output     = new USFMDocument();

            foreach (Match match in splitRegex.Matches(input))
            {
                if (IgnoredTags.Contains(match.Groups[1].Value))
                {
                    continue;
                }


                ConvertToMarkerResult result = ConvertToMarker(match.Groups[1].Value, match.Groups[2].Value);

                if (result.marker is TRMarker && !output.GetTypesPathToLastMarker().Contains(typeof(TableBlock)))
                {
                    output.Insert(new TableBlock());
                }


                output.Insert(result.marker);

                if (!string.IsNullOrWhiteSpace(result.remainingText))
                {
                    output.Insert(new TextBlock(result.remainingText));
                }
            }

            return(output);
        }
        public List<LinterResult> Lint(USFMDocument input)
        {
            List<LinterResult> missingEndMarkers = new List<LinterResult>();
            markerPairs = new Dictionary<Type, Type>
            {
                {typeof(ADDEndMarker),typeof(ADDMarker)},
                {typeof(BDEndMarker),typeof(BDMarker)},
                {typeof(BDITEndMarker),typeof(BDITMarker)},
                {typeof(BKEndMarker),typeof(BKMarker)},
                {typeof(CAEndMarker),typeof(CAMarker)},
                {typeof(EMEndMarker),typeof(EMMarker)},
                {typeof(FEndMarker),typeof(FMarker)},
                {typeof(FVEndMarker),typeof(FVMarker)},
                {typeof(IOREndMarker),typeof(IORMarker)},
                {typeof(ITEndMarker),typeof(ITMarker)},
                {typeof(NDEndMarker),typeof(NDMarker)},
                {typeof(NOEndMarker),typeof(NOMarker)},
                {typeof(QACEndMarker),typeof(QACMarker)},
                {typeof(QSEndMarker),typeof(QSMarker)},
                {typeof(RQEndMarker),typeof(RQMarker)},
                {typeof(SCEndMarker),typeof(SCMarker)},
                {typeof(SUPEndMarker),typeof(SUPMarker)},
                {typeof(TLEndMarker),typeof(TLMarker)},
                {typeof(VAEndMarker),typeof(VAMarker)},
                {typeof(VPEndMarker),typeof(VPMarker)},
                {typeof(WEndMarker),typeof(WMarker)},
                {typeof(XEndMarker), typeof(XMarker)},
            };
            foreach (Marker marker in input.Contents)
            {
                missingEndMarkers.AddRange(CheckChildMarkers(marker, input));
            }
            return missingEndMarkers;

        }
        public XWPFDocument renderDoc(string usfm)
        {
            USFMDocument markerTree = parser.ParseFromString(usfm);
            XWPFDocument testDoc    = render.Render(markerTree);

            return(testDoc);
        }
        public async Task <List <LinterResult> > LintAsync(USFMDocument input)
        {
            List <LinterResult> output = new List <LinterResult>();

            output = await PerformLintParallelAsync(input);

            return(output);
        }
        public List <LinterResult> Lint(USFMDocument input)
        {
            List <LinterResult> output = new List <LinterResult>();

            foreach (var linter in linters)
            {
                output.AddRange(linter.Lint(input));
            }
            return(output);
        }
示例#7
0
        private string GetEncoding(USFMDocument input)
        {
            var encodingSearch = input.GetChildMarkers <IDEMarker>();

            if (encodingSearch.Count > 0)
            {
                return(encodingSearch[0].Encoding);
            }
            return(null);
        }
 /// <summary>
 /// Render a USFMDocument to a CSV
 /// </summary>
 /// <param name="document">The document to render</param>
 /// <param name="stream">A stream to write the resulting CSV to</param>
 public void Render(USFMDocument document, Stream stream)
 {
     using (var writer = new StreamWriter(stream))
     {
         using (var csv = new CsvWriter(writer, CultureInfo.CurrentCulture))
         {
             WriteHeader(csv);
             WriteContent(document, csv);
         }
     }
 }
示例#9
0
        public string Render(USFMDocument input)
        {
            UnrenderableTags = new List <string>();
            var           encoding = GetEncoding(input);
            StringBuilder output   = new StringBuilder();

            output.AppendLine("<html>");

            if (InsertedHead == null)
            {
                output.AppendLine("<head>");
                if (!string.IsNullOrEmpty(encoding))
                {
                    output.Append($"<meta charset=\"{encoding}\">");
                }
                output.AppendLine("<link rel=\"stylesheet\" href=\"style.css\">");

                output.AppendLine("</head>");
            }
            else
            {
                output.AppendLine(InsertedHead);
            }

            output.AppendLine("<body>");

            output.AppendLine(FrontMatterHTML);

            // HTML tags can only have one class, when render to docx

            foreach (string class_name in ConfigurationHTML.divClasses)
            {
                output.AppendLine($"<div class=\"{class_name}\">");
            }



            foreach (Marker marker in input.Contents)
            {
                output.Append(RenderMarker(marker));
            }



            foreach (string class_name in ConfigurationHTML.divClasses)
            {
                output.AppendLine($"</div>");
            }
            output.AppendLine(InsertedFooter);

            output.AppendLine("</body>");
            output.AppendLine("</html>");
            return(output.ToString());
        }
        public void TestHeaderParse()
        {
            Assert.AreEqual("Genesis", ((HMarker)parser.ParseFromString("\\h Genesis").Contents[0]).HeaderText);
            Assert.AreEqual("", ((HMarker)parser.ParseFromString("\\h").Contents[0]).HeaderText);
            Assert.AreEqual("1 John", ((HMarker)parser.ParseFromString("\\h 1 John").Contents[0]).HeaderText);
            Assert.AreEqual("", ((HMarker)parser.ParseFromString("\\h   ").Contents[0]).HeaderText);

            USFMDocument doc = parser.ParseFromString("\\sp Job");
            SPMarker     sp  = (SPMarker)doc.Contents[0];

            Assert.AreEqual("Job", sp.Speaker);
        }
        public XWPFDocument Render(USFMDocument input)
        {
            UnrenderableMarkers = new List <string>();
            CrossRefMarkers     = new Dictionary <string, Marker>();
            newDoc = new XWPFDocument();

            if (FrontMatter != null)
            {
                RenderFrontMatter(FrontMatter);
            }

            if (configDocx.renderTableOfContents)
            {
                DocumentStylesBuilder.BuildStylesForTOC(newDoc);
                RenderTOC();
            }

            newDoc.CreateFootnotes();

            setStartPageNumber();

            newDoc.ColumnCount = configDocx.columnCount;

            foreach (Marker marker in input.Contents)
            {
                RenderMarker(marker, new StyleConfig());
            }

            // Add section header for final book
            if (previousBookHeader != null)
            {
                createBookHeaders(previousBookHeader);
            }

            // Make final document section continuous so that it doesn't
            // create an extra page at the end.  Final section is unique:
            // it's a direct child of the document, not a child of the last
            // paragraph.
            CT_SectPr finalSection = new CT_SectPr();

            finalSection.type           = new CT_SectType();
            finalSection.type.val       = ST_SectionMark.continuous;
            newDoc.Document.body.sectPr = finalSection;
            finalSection.cols.num       = configDocx.columnCount.ToString();
            CT_PageNumber pageNum = new CT_PageNumber
            {
                fmt = ST_NumberFormat.@decimal
            };

            finalSection.pgNumType = pageNum;

            return(newDoc);
        }
示例#12
0
        private void RenderHtml(string fileName)
        {
            // Does not parse through section headers yet
            var parser = new USFMParser(new List <string> {
                "s5"
            });

            HTMLConfig configHTML = BuildHTMLConfig();
            //Configure Settings -- Spacing ? 1, Column# ? 1, TextDirection ? L2R
            var renderer = new HtmlRenderer(configHTML);

            // Added ULB License and Page Number
            renderer.FrontMatterHTML = GetLicenseInfo();
            renderer.InsertedFooter  = GetFooterInfo();

            var usfm = new USFMDocument();

            var progress     = fileDataGrid.RowCount - 1;
            var progressStep = 0;

            foreach (DataGridViewRow row in fileDataGrid.Rows)
            {
                var cell = row.Cells[0];
                if (cell.Value == null)
                {
                    continue;
                }
                var filename = cell.Value.ToString();

                var text = File.ReadAllText(filename);
                usfm.Insert(parser.ParseFromString(text));



                progressStep++;
                LoadingBar.Value = (int)(progressStep / (float)progress * 100);
            }

            var html = renderer.Render(usfm);

            File.WriteAllText(fileName, html);

            var dirname = Path.GetDirectoryName(fileName);

            filePathConversion = dirname;
            var cssFilename = Path.Combine(dirname, "style.css");

            if (!File.Exists(cssFilename))
            {
                File.Copy("style.css", cssFilename);
            }
        }
        /// <summary>
        /// Iterates through all children markers
        /// </summary>
        /// <param name="input"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public List<LinterResult> CheckChildMarkers(Marker input, USFMDocument root)
        {
            List<LinterResult> results = new List<LinterResult>();

            foreach (Marker marker in input.Contents)
            {
                if (markerPairs.ContainsKey(marker.GetType()))
                {
                    results.AddRange(CheckOpenMarker(marker, root));
                }
                results.AddRange(CheckChildMarkers(marker, root));
            }
            return results;
        }
        public void TestIdentificationMarkers()
        {
            Assert.AreEqual("Genesis", ((IDMarker)parser.ParseFromString("\\id Genesis").Contents[0]).TextIdentifier);
            Assert.AreEqual("UTF-8", ((IDEMarker)parser.ParseFromString("\\ide UTF-8").Contents[0]).Encoding);
            Assert.AreEqual("2", ((STSMarker)parser.ParseFromString("\\sts 2").Contents[0]).StatusText);

            Assert.AreEqual("3.0", ((USFMMarker)parser.ParseFromString("\\usfm 3.0").Contents[0]).Version);

            USFMDocument doc = parser.ParseFromString("\\rem Remark");

            Assert.IsInstanceOfType(doc.Contents[0], typeof(REMMarker));
            REMMarker rem = (REMMarker)doc.Contents[0];

            Assert.AreEqual("Remark", rem.Comment);
        }
        public List <LinterResult> Lint(USFMDocument input)
        {
            List <LinterResult> output = new List <LinterResult>();
            LinterResult        tmp;

            foreach (var verse in input.GetChildMarkers <VMarker>())
            {
                tmp = ValidateVerse(verse);
                if (tmp != null)
                {
                    output.Add(tmp);
                }
            }

            return(output);
        }
示例#16
0
        /// <summary>
        /// Checks if Table Cells/Headers are contained within a Table Row
        /// </summary>
        /// <param name="input"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        private LinterResult ValidateTable(Marker input, USFMDocument root)
        {
            List <Marker> hierarchy    = root.GetHierarchyToMarker(input);
            Marker        parentMarker = hierarchy[hierarchy.Count - 2];

            if (!(parentMarker is TRMarker))
            {
                return(new LinterResult
                {
                    Position = input.Position,
                    Level = LinterLevel.Warning,
                    Message = $"Missing Table Row container"
                });
            }
            return(null);
        }
示例#17
0
        public List <LinterResult> Lint(USFMDocument input)
        {
            List <LinterResult> output = new List <LinterResult>();

            foreach (var marker in input.GetChildMarkers <UnknownMarker>())
            {
                output.Add(new LinterResult()
                {
                    Level    = LinterLevel.Warning,
                    Position = marker.Position,
                    Message  = $"The marker {marker.ParsedIdentifier} is unknown"
                }
                           );
            }
            return(output);
        }
        public void TestIgnoredTags()
        {
            parser = new USFMToolsSharp.USFMParser(new List <string> {
                "bd", "bd*"
            });
            USFMDocument doc = parser.ParseFromString("\\v 1 In the beginning \\bd God \\bd*");

            Assert.AreEqual(1, doc.Contents.Count);
            VMarker vm = (VMarker)doc.Contents[0];

            Assert.AreEqual(1, vm.Contents.Count);
            TextBlock tb = (TextBlock)vm.Contents[0];

            Assert.AreEqual(0, tb.Contents.Count);
            Assert.AreEqual("In the beginning ", tb.Text);
        }
        private async Task <List <LinterResult> > PerformLintParallelAsync(USFMDocument root)
        {
            List <LinterResult> output = new List <LinterResult>();
            List <Task <List <LinterResult> > > tasks = new List <Task <List <LinterResult> > >();

            foreach (var linter in linters)
            {
                tasks.Add(Task.Run(() => linter.Lint(root)));
            }
            var results = await Task.WhenAll(tasks);

            foreach (var item in results)
            {
                output.AddRange(item);
            }
            return(output);
        }
        public void TestPoetryParse()
        {
            USFMDocument doc = parser.ParseFromString("\\q Quote");

            Assert.IsInstanceOfType(doc.Contents[0], typeof(QMarker));
            Assert.AreEqual("Quote", ((TextBlock)doc.Contents[0].Contents[0]).Text);
            Assert.AreEqual(1, ((QMarker)parser.ParseFromString("\\q Quote").Contents[0]).Depth);
            Assert.AreEqual(1, ((QMarker)parser.ParseFromString("\\q1 Quote").Contents[0]).Depth);
            Assert.AreEqual(2, ((QMarker)parser.ParseFromString("\\q2 Quote").Contents[0]).Depth);
            Assert.AreEqual(3, ((QMarker)parser.ParseFromString("\\q3 Quote").Contents[0]).Depth);

            doc = parser.ParseFromString("\\qr God's love never fails.");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(QRMarker));
            Assert.AreEqual("God's love never fails.", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\qc Amen! Amen!");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(QCMarker));
            Assert.AreEqual("Amen! Amen!", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\qd For the director of music.");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(QDMarker));
            Assert.AreEqual("For the director of music.", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\qac P\\qac*");
            Assert.AreEqual(2, doc.Contents.Count);
            QACMarker    qac    = (QACMarker)doc.Contents[0];
            QACEndMarker qacEnd = (QACEndMarker)doc.Contents[1];

            Assert.AreEqual("P", qac.AcrosticLetter);

            doc = parser.ParseFromString("\\qm God is on your side.");
            Assert.AreEqual(1, doc.Contents.Count);
            Assert.IsInstanceOfType(doc.Contents[0], typeof(QMMarker));
            Assert.AreEqual("God is on your side.", ((TextBlock)doc.Contents[0].Contents[0]).Text);
            Assert.AreEqual(1, ((QMMarker)parser.ParseFromString("\\qm God is on your side.").Contents[0]).Depth);
            Assert.AreEqual(1, ((QMMarker)parser.ParseFromString("\\qm1 God is on your side.").Contents[0]).Depth);
            Assert.AreEqual(2, ((QMMarker)parser.ParseFromString("\\qm2 God is on your side.").Contents[0]).Depth);
            Assert.AreEqual(3, ((QMMarker)parser.ParseFromString("\\qm3 God is on your side.").Contents[0]).Depth);

            doc = parser.ParseFromString("\\qa Aleph");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(QAMarker));
            QAMarker qa = (QAMarker)doc.Contents[0];

            Assert.AreEqual("Aleph", qa.Heading);
        }
        private void RenderFrontMatter(USFMDocument frontMatter)
        {
            // reset default format before rendering front matters
            DocxConfig currentConfig = configDocx;

            configDocx = new DocxConfig();

            foreach (var marker in frontMatter.Contents)
            {
                RenderMarker(marker, new StyleConfig());
            }

            // revert to user config format
            configDocx = currentConfig;

            CT_P pHeader = CreateFrontHeader();

            newDoc.Document.body.Items.Add(pHeader);
            newDoc.CreateParagraph().CreateRun().AddBreak(BreakType.PAGE);
        }
        /// <summary>
        /// Write the contents of the usfm document to a CSV
        /// </summary>
        /// <param name="document">The document whose contents to write</param>
        /// <param name="csv">The CSV writer to write the content to</param>
        private static void WriteContent(USFMDocument document, CsvWriter csv)
        {
            var book     = document.GetChildMarkers <TOC3Marker>().First().BookAbbreviation;
            var chapters = document.GetChildMarkers <CMarker>();

            foreach (var chapter in chapters)
            {
                var verses = chapter.GetChildMarkers <VMarker>();
                foreach (var verse in verses)
                {
                    var textBlocks = verse.GetChildMarkers <TextBlock>()
                                     .Where(m => !document.GetHierarchyToMarker(m).Any(i => i is FMarker));
                    var text = string.Join(string.Empty, textBlocks.Select(b => b.Text));
                    csv.WriteField(book);
                    csv.WriteField(chapter.Number);
                    csv.WriteField(verse.VerseNumber);
                    csv.WriteField(text.TrimEnd());
                    csv.NextRecord();
                }
            }
        }
示例#23
0
        private async void BtnCheck_Click(object sender, EventArgs e)
        {
            Msg_Linter.ResetText();
            BtnCheck.Enabled    = false;
            BtnAddFiles.Enabled = false;
            var parser = new USFMParser(new List <string> {
                "s5"
            });
            var usfm = new USFMDocument();
            DataGridViewSelectedCellCollection SelectedFiles = fileDataGrid.SelectedCells;
            USFMLinter lint = new USFMLinter();

            if (fileDataGrid.Rows.Count > 1)
            {
                foreach (DataGridViewCell SelectFile in SelectedFiles)
                {
                    if (SelectFile.OwningRow.Index != fileDataGrid.RowCount - 1)
                    {
                        var cell = SelectFile.OwningRow.Cells[0];
                        if (cell.Value == null)
                        {
                            continue;
                        }
                        var filename = cell.Value.ToString();

                        var text = File.ReadAllText(filename);
                        usfm.Insert(parser.ParseFromString(text));
                        List <LinterResult> linterResults = await lint.LintAsync(usfm);
                        await DisplayErrors(linterResults, filename);

                        usfm = new USFMDocument();
                    }
                }
            }


            BtnCheck.Enabled    = true;
            BtnAddFiles.Enabled = true;
        }
示例#24
0
        private void RenderDocx(string fileName)
        {
            // Does not parse through section headers yet
            var parser = new USFMParser(new List <string> {
                "s5"
            });

            var renderer = new DocxRenderer(BuildDocxConfig());

            var usfm = new USFMDocument();

            var progress     = fileDataGrid.RowCount - 1;
            var progressStep = 0;

            foreach (DataGridViewRow row in fileDataGrid.Rows)
            {
                var cell = row.Cells[0];
                if (cell.Value == null)
                {
                    continue;
                }
                var filename = cell.Value.ToString();

                var text = File.ReadAllText(filename);
                usfm.Insert(parser.ParseFromString(text));



                progressStep++;
                LoadingBar.Value = (int)(progressStep / (float)progress * 100);
            }

            var output = renderer.Render(usfm);

            using (Stream outputStream = File.Create(fileName))
            {
                output.Write(outputStream);
            }
        }
示例#25
0
        public List <LinterResult> Lint(USFMDocument input)
        {
            List <LinterResult> output = new List <LinterResult>();

            foreach (Marker marker in input.GetChildMarkers <TCMarker>())
            {
                LinterResult tmp = ValidateTable(marker, input);
                if (tmp != null)
                {
                    output.Add(tmp);
                }
            }
            foreach (Marker marker in input.GetChildMarkers <TCRMarker>())
            {
                LinterResult tmp = ValidateTable(marker, input);
                if (tmp != null)
                {
                    output.Add(tmp);
                }
            }
            foreach (Marker marker in input.GetChildMarkers <THMarker>())
            {
                LinterResult tmp = ValidateTable(marker, input);
                if (tmp != null)
                {
                    output.Add(tmp);
                }
            }
            foreach (Marker marker in input.GetChildMarkers <THRMarker>())
            {
                LinterResult tmp = ValidateTable(marker, input);
                if (tmp != null)
                {
                    output.Add(tmp);
                }
            }
            return(output);
        }
示例#26
0
        /// <summary>
        /// Parses a string into a USFMDocument
        /// </summary>
        /// <param name="input">A USFM string</param>
        /// <returns>A USFMDocument representing the input</returns>
        public USFMDocument ParseFromString(string input)
        {
            USFMDocument output  = new USFMDocument();
            var          markers = TokenizeFromString(input);

            foreach (Marker marker in markers)
            {
                if (marker is TRMarker && !output.GetTypesPathToLastMarker().Contains(typeof(TableBlock)))
                {
                    output.Insert(new TableBlock());
                }

                var markerIndex = markers.IndexOf(marker);

                if (marker is QMarker && markerIndex != markers.Count - 1 && markers[markerIndex + 1] is VMarker)
                {
                    ((QMarker)marker).IsPoetryBlock = true;
                }

                output.Insert(marker);
            }

            return(output);
        }
        public void Run()
        {
            // Does not parse through section headers yet
            var parser = new USFMParser(new List <string> {
                "s5"
            });

            configHTML = BuildConfig();
            //Configure Settings -- Spacing ? 1, Column# ? 1, TextDirection ? L2R
            var renderer = new HtmlRenderer(configHTML);

            // Added ULB License and Page Number
            renderer.FrontMatterHTML = GetLicenseInfo();
            renderer.InsertedFooter  = GetFooterInfo();

            var usfm = new USFMDocument();

            if (folderName.Equals(string.Empty))
            {
                return;
            }

            var dirinfo = new DirectoryInfo(folderName);

            if (!dirinfo.Exists)
            {
                Console.WriteLine("The input path doesn't exist. Make sure you spelled it correctly");
                return;
            }

            if (!Directory.Exists(projectOutput))
            {
                Console.WriteLine("The output path doesn't exist. Make sure you spelled it correctly");
                return;
            }

            var allFiles = dirinfo.GetFiles("*.usfm", SearchOption.AllDirectories)
                           .Union(dirinfo.GetFiles("*.txt", SearchOption.AllDirectories)).ToArray();

            Array.Sort(allFiles, delegate(FileInfo a, FileInfo b)
            {
                return(a.FullName.CompareTo(b.FullName));
            });
            var progress     = allFiles.Length;
            var progressStep = 0;

            foreach (FileInfo fileInfo in allFiles)
            {
                var text = File.ReadAllText(fileInfo.FullName);
                usfm.Insert(parser.ParseFromString(text));
                progressStep++;
                Console.Write("\r[{0}%] ", (int)(progressStep / (float)progress * 100));
            }

            var html         = renderer.Render(usfm);
            var htmlFilename = Path.Combine(projectOutput, "out.html");

            File.WriteAllText(htmlFilename, html);

            var dirname     = Path.GetDirectoryName(htmlFilename);
            var cssFilename = Path.Combine(dirname, "style.css");

            if (!File.Exists(cssFilename))
            {
                File.Copy("style.css", cssFilename);
            }

            Console.WriteLine("Your project was successfully converted!");
        }
        public void TestIntroductionMarkers()
        {
            Assert.AreEqual("Title", ((IMTMarker)parser.ParseFromString("\\imt Title").Contents[0]).IntroTitle);
            Assert.AreEqual(1, ((IMTMarker)parser.ParseFromString("\\imt").Contents[0]).Weight);
            Assert.AreEqual(1, ((IMTMarker)parser.ParseFromString("\\imt1").Contents[0]).Weight);
            Assert.AreEqual(2, ((IMTMarker)parser.ParseFromString("\\imt2").Contents[0]).Weight);
            Assert.AreEqual(3, ((IMTMarker)parser.ParseFromString("\\imt3").Contents[0]).Weight);

            Assert.AreEqual("Heading", ((ISMarker)parser.ParseFromString("\\is Heading").Contents[0]).Heading);
            Assert.AreEqual(1, ((ISMarker)parser.ParseFromString("\\is").Contents[0]).Weight);
            Assert.AreEqual(1, ((ISMarker)parser.ParseFromString("\\is1").Contents[0]).Weight);
            Assert.AreEqual(2, ((ISMarker)parser.ParseFromString("\\is2").Contents[0]).Weight);
            Assert.AreEqual(3, ((ISMarker)parser.ParseFromString("\\is3").Contents[0]).Weight);

            Assert.AreEqual(1, ((IQMarker)parser.ParseFromString("\\iq").Contents[0]).Depth);
            Assert.AreEqual(1, ((IQMarker)parser.ParseFromString("\\iq1").Contents[0]).Depth);
            Assert.AreEqual(2, ((IQMarker)parser.ParseFromString("\\iq2").Contents[0]).Depth);
            Assert.AreEqual(3, ((IQMarker)parser.ParseFromString("\\iq3").Contents[0]).Depth);

            Assert.IsNotNull(((IBMarker)parser.ParseFromString("\\ib").Contents[0]));

            Assert.AreEqual("Title", ((IOTMarker)parser.ParseFromString("\\iot Title").Contents[0]).Title);

            Assert.AreEqual(1, ((IOMarker)parser.ParseFromString("\\io").Contents[0]).Depth);
            Assert.AreEqual(1, ((IOMarker)parser.ParseFromString("\\io1").Contents[0]).Depth);
            Assert.AreEqual(2, ((IOMarker)parser.ParseFromString("\\io2").Contents[0]).Depth);
            Assert.AreEqual(3, ((IOMarker)parser.ParseFromString("\\io3").Contents[0]).Depth);

            USFMDocument doc = parser.ParseFromString("\\ior (1.1-3)\\ior*");

            Assert.AreEqual(2, doc.Contents.Count);
            Assert.AreEqual("(1.1-3)", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            Assert.AreEqual("Text", ((TextBlock)parser.ParseFromString("\\ili Text").Contents[0].Contents[0]).Text);
            Assert.AreEqual(1, ((ILIMarker)parser.ParseFromString("\\ili").Contents[0]).Depth);
            Assert.AreEqual(1, ((ILIMarker)parser.ParseFromString("\\ili1").Contents[0]).Depth);
            Assert.AreEqual(2, ((ILIMarker)parser.ParseFromString("\\ili2").Contents[0]).Depth);
            Assert.AreEqual(3, ((ILIMarker)parser.ParseFromString("\\ili3").Contents[0]).Depth);

            doc = parser.ParseFromString("\\ip Text");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(IPMarker));
            Assert.AreEqual("Text", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\ipi Text");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(IPIMarker));
            Assert.AreEqual("Text", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\im Text");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(IMMarker));
            Assert.AreEqual("Text", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\is Heading");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(ISMarker));
            Assert.AreEqual("Heading", ((ISMarker)doc.Contents[0]).Heading);

            doc = parser.ParseFromString("\\iq Quote");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(IQMarker));
            Assert.AreEqual("Quote", ((TextBlock)doc.Contents[0].Contents[0]).Text);
            Assert.AreEqual(1, ((IQMarker)parser.ParseFromString("\\iq Quote").Contents[0]).Depth);
            Assert.AreEqual(1, ((IQMarker)parser.ParseFromString("\\iq1 Quote").Contents[0]).Depth);
            Assert.AreEqual(2, ((IQMarker)parser.ParseFromString("\\iq2 Quote").Contents[0]).Depth);
            Assert.AreEqual(3, ((IQMarker)parser.ParseFromString("\\iq3 Quote").Contents[0]).Depth);

            doc = parser.ParseFromString("\\imi Text");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(IMIMarker));
            Assert.AreEqual("Text", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\ipq Text");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(IPQMarker));
            Assert.AreEqual("Text", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\imq Text");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(IMQMarker));
            Assert.AreEqual("Text", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\ipr Text");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(IPRMarker));
            Assert.AreEqual("Text", ((TextBlock)doc.Contents[0].Contents[0]).Text);
        }
示例#29
0
        public string Render(USFMDocument input)
        {
            UnrenderableTags = new List <string>();
            var           encoding = GetEncoding(input);
            StringBuilder output   = new StringBuilder();

            if (!ConfigurationHTML.partialHTML)
            {
                output.AppendLine("<html>");

                if (InsertedHead == null)
                {
                    output.AppendLine("<head>");
                    if (!string.IsNullOrEmpty(encoding))
                    {
                        output.Append($"<meta charset=\"{encoding}\">");
                    }
                    output.AppendLine("<link rel=\"stylesheet\" href=\"style.css\">");

                    output.AppendLine("</head>");
                }
                else
                {
                    output.AppendLine(InsertedHead);
                }

                output.AppendLine("<body>");

                output.AppendLine(FrontMatterHTML);
            }

            // HTML tags can only have one class, when render to docx
            foreach (string class_name in ConfigurationHTML.divClasses)
            {
                output.AppendLine($"<div class=\"{class_name}\">");
            }

            // Will add a table with two columns where the second column will be blank
            if (ConfigurationHTML.blankColumn)
            {
                output.AppendLine("<table class=\"blank_col\"><tr><td>");
            }

            foreach (Marker marker in input.Contents)
            {
                output.Append(RenderMarker(marker));
            }

            if (ConfigurationHTML.blankColumn)
            {
                output.AppendLine("</td><td></td></tr></table>");
            }

            foreach (string class_name in ConfigurationHTML.divClasses)
            {
                output.AppendLine($"</div>");
            }

            if (!ConfigurationHTML.partialHTML)
            {
                output.AppendLine(InsertedFooter);
                output.AppendLine("</body>");
                output.AppendLine("</html>");
            }
            return(output.ToString());
        }
        public void TestChapterParse()
        {
            Assert.AreEqual(1, ((CMarker)parser.ParseFromString("\\c 1").Contents[0]).Number);
            Assert.AreEqual(1000, ((CMarker)parser.ParseFromString("\\c 1000").Contents[0]).Number);
            Assert.AreEqual(0, ((CMarker)parser.ParseFromString("\\c 0").Contents[0]).Number);

            // Chapter Labels
            Assert.AreEqual("Chapter One", ((CLMarker)parser.ParseFromString("\\c 1 \\cl Chapter One").Contents[0].Contents[0]).Label);
            Assert.AreEqual("Chapter One", ((CLMarker)parser.ParseFromString("\\cl Chapter One \\c 1").Contents[0]).Label);
            Assert.AreEqual("Chapter Two", ((CLMarker)parser.ParseFromString("\\c 1 \\cl Chapter One \\c 2 \\cl Chapter Two").Contents[1].Contents[0]).Label);

            USFMDocument doc = parser.ParseFromString("\\cp Q");

            Assert.IsInstanceOfType(doc.Contents[0], typeof(CPMarker));
            Assert.AreEqual("Q", ((CPMarker)doc.Contents[0]).PublishedChapterMarker);

            doc = parser.ParseFromString("\\ca 53 \\ca*");
            Assert.AreEqual(2, doc.Contents.Count);
            CAMarker    caBegin = (CAMarker)doc.Contents[0];
            CAEndMarker caEnd   = (CAEndMarker)doc.Contents[1];

            Assert.AreEqual("53", caBegin.AltChapterNumber);

            doc = parser.ParseFromString("\\va 22 \\va*");
            Assert.AreEqual(2, doc.Contents.Count);
            VAMarker    vaBegin = (VAMarker)doc.Contents[0];
            VAEndMarker vaEnd   = (VAEndMarker)doc.Contents[1];

            Assert.AreEqual("22", vaBegin.AltVerseNumber);

            doc = parser.ParseFromString("\\p In the beginning God created the heavens and the earth.");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(PMarker));
            Assert.AreEqual("In the beginning God created the heavens and the earth.", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\pc In the beginning God created the heavens and the earth.");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(PCMarker));
            Assert.AreEqual("In the beginning God created the heavens and the earth.", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\p \\v 1 In the beginning God created the heavens and the earth.");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(PMarker));
            PMarker pm = (PMarker)doc.Contents[0];
            VMarker vm = (VMarker)pm.Contents[0];

            Assert.AreEqual("In the beginning God created the heavens and the earth.", ((TextBlock)vm.Contents[0]).Text);

            doc = parser.ParseFromString("\\mi");
            Assert.AreEqual(1, doc.Contents.Count);
            Assert.IsInstanceOfType(doc.Contents[0], typeof(MIMarker));

            doc = parser.ParseFromString("\\d A Psalm of David");
            Assert.AreEqual("A Psalm of David", ((DMarker)doc.Contents[0]).Description);

            doc = parser.ParseFromString("\\nb");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(NBMarker));

            doc = parser.ParseFromString("\\fq the Son of God");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(FQMarker));
            Assert.AreEqual("the Son of God", ((TextBlock)doc.Contents[0].Contents[0]).Text);

            doc = parser.ParseFromString("\\pi The one who scattered");
            Assert.IsInstanceOfType(doc.Contents[0], typeof(PIMarker));
            Assert.AreEqual(1, doc.Contents.Count);
            Assert.AreEqual("The one who scattered", ((TextBlock)doc.Contents[0].Contents[0]).Text);
            Assert.AreEqual(1, ((PIMarker)parser.ParseFromString("\\pi").Contents[0]).Depth);
            Assert.AreEqual(1, ((PIMarker)parser.ParseFromString("\\pi1").Contents[0]).Depth);
            Assert.AreEqual(2, ((PIMarker)parser.ParseFromString("\\pi2").Contents[0]).Depth);
            Assert.AreEqual(3, ((PIMarker)parser.ParseFromString("\\pi3").Contents[0]).Depth);

            doc = parser.ParseFromString("\\m \\v 37 David himself called him 'Lord';");
            Assert.AreEqual(1, doc.Contents.Count);
            MMarker mm = (MMarker)doc.Contents[0];

            Assert.AreEqual(1, mm.Contents.Count);
            vm = (VMarker)mm.Contents[0];
            Assert.AreEqual("David himself called him 'Lord';", ((TextBlock)vm.Contents[0]).Text);

            doc = parser.ParseFromString("\\b");
            Assert.AreEqual(1, doc.Contents.Count);
            Assert.IsInstanceOfType(doc.Contents[0], typeof(BMarker));
        }