Пример #1
0
        public IEnumerable <Block> Parse()
        {
            var           titleBuilder = new StringBuilder();
            IList <Block> blocks       = new List <Block>();

            foreach (XmlNode node in m_nodeList)
            {
                Block block = null;
                switch (node.Name)
                {
                case "chapter":
                    AddMainTitleIfApplicable(blocks, titleBuilder);
                    m_chapterNodeFound = true;
                    block = ProcessChapterNode(node);
                    if (block == null)
                    {
                        continue;
                    }
                    break;

                case "para":
                    if (!node.HasChildNodes)
                    {
                        continue;
                    }

                    var    usxPara = new UsxNode(node);
                    IStyle style   = m_stylesheet.GetStyle(usxPara.StyleTag);
                    if (style.IsChapterLabel)
                    {
                        block = ProcessChapterLabelNode(node.InnerText, usxPara);
                        if (block == null)
                        {
                            continue;
                        }

                        // The node before this was the chapter. We already added it, then found this label.
                        // Remove that block so it will be replaced with this one.
                        blocks.RemoveAt(blocks.Count - 1);
                        break;
                    }

                    if (style.IsParallelPassageReference || !style.IsPublishable)
                    {
                        continue;
                    }

                    if (style.HoldsBookNameOrAbbreviation)
                    {
                        if (style.Id.StartsWith("mt"))
                        {
                            titleBuilder.Append(node.InnerText).Append(" ");
                            if (style.Id == "mt1")
                            {
                                MainTitle = node.InnerText;
                            }
                        }
                        else if (style.Id == "h")
                        {
                            PageHeader = node.InnerText;
                        }
                        continue;
                    }
                    AddMainTitleIfApplicable(blocks, titleBuilder);

                    block = new Block(usxPara.StyleTag, m_currentChapter, m_currentStartVerse, m_currentEndVerse)
                    {
                        IsParagraphStart = true
                    };
                    if (m_currentChapter == 0)
                    {
                        block.SetStandardCharacter(m_bookId, CharacterVerseData.StandardCharacter.Intro);
                    }
                    else if (style.IsPublishable && !style.IsVerseText)
                    {
                        block.SetStandardCharacter(m_bookId, CharacterVerseData.StandardCharacter.ExtraBiblical);
                    }

                    var sb = new StringBuilder();
                    // <verse number="1" style="v" />
                    // Acakki me lok me kwena maber i kom Yecu Kricito, Wod pa Lubaŋa,
                    // <verse number="2" style="v" />
                    // <note caller="-" style="x"><char style="xo" closed="false">1.2: </char><char style="xt" closed="false">Mal 3.1</char></note>
                    // kit ma gicoyo kwede i buk pa lanebi Icaya ni,</para>
                    foreach (XmlNode childNode in usxPara.ChildNodes)
                    {
                        switch (childNode.Name)
                        {
                        case "verse":
                            if (sb.Length > 0)
                            {
                                sb.TrimStart();
                                block.BlockElements.Add(new ScriptText(sb.ToString()));
                                sb.Clear();
                            }
                            RemoveLastElementIfVerse(block);
                            var verseNumStr = childNode.Attributes.GetNamedItem("number").Value;
                            m_currentStartVerse = ScrReference.VerseToIntStart(verseNumStr);
                            m_currentEndVerse   = ScrReference.VerseToIntEnd(verseNumStr);
                            if (!block.BlockElements.Any() ||
                                (block.BlockElements.Count == 1 && block.FirstTextElementIsOnlyPunctuation))
                            {
                                block.InitialStartVerseNumber = m_currentStartVerse;
                                block.InitialEndVerseNumber   = m_currentEndVerse;
                            }

                            block.BlockElements.Add(new Verse(verseNumStr));
                            break;

                        case "char":
                            IStyle charStyle = m_stylesheet.GetStyle((new UsxNode(childNode)).StyleTag);
                            if (!charStyle.IsInlineQuotationReference && charStyle.IsPublishable)
                            {
                                sb.Append(childNode.InnerText);
                            }
                            break;

                        case "#text":
                            sb.Append(childNode.InnerText);
                            break;

                        case "#whitespace":
                            if (sb.Length > 0 && sb[sb.Length - 1] != ' ')
                            {
                                sb.Append(" ");
                            }
                            break;
                        }
                    }
                    sb.TrimStart();
                    if (sb.Length > 0)
                    {
                        block.BlockElements.Add(new ScriptText(sb.ToString()));
                        sb.Clear();
                    }
                    RemoveLastElementIfVerse(block);
                    break;
                }
                if (block != null && block.BlockElements.Count > 0)
                {
                    blocks.Add(block);
                }
            }
            return(blocks);
        }
Пример #2
0
        public IEnumerable <Block> Parse()
        {
            var           titleBuilder = new StringBuilder();
            IList <Block> blocks       = new List <Block>();

            foreach (XmlNode node in m_nodeList)
            {
                Block block = null;
                switch (node.Name)
                {
                case "chapter":
                    AddMainTitleIfApplicable(blocks, titleBuilder);
                    block = ProcessChapterNode(node);
                    if (block == null)
                    {
                        continue;
                    }
                    break;

                case "para":
                    if (!node.HasChildNodes)
                    {
                        continue;
                    }

                    var    usxPara = new UsxNode(node);
                    IStyle style   = m_stylesheet.GetStyle(usxPara.StyleTag);
                    if (style.IsChapterLabel)
                    {
                        block = ProcessChapterLabelNode(node.InnerText, usxPara, blocks);
                        break;
                    }

                    if (style.IsParallelPassageReference || !style.IsPublishable)
                    {
                        continue;
                    }

                    if (style.HoldsBookNameOrAbbreviation)
                    {
                        if (style.Id.StartsWith("mt"))
                        {
                            titleBuilder.Append(node.InnerText).Append(" ");
                            if (style.Id == "mt1")
                            {
                                MainTitle = node.InnerText;
                            }
                        }
                        else if (style.Id == "h")
                        {
                            PageHeader = node.InnerText;
                        }
                        continue;
                    }
                    AddMainTitleIfApplicable(blocks, titleBuilder);

                    block = new Block(usxPara.StyleTag, m_currentChapter, m_currentStartVerse, m_currentEndVerse)
                    {
                        IsParagraphStart = true
                    };
                    if (m_currentChapter == 0)
                    {
                        block.SetStandardCharacter(m_bookId, CharacterVerseData.StandardCharacter.Intro);
                    }
                    else if (style.IsPublishable && !style.IsVerseText)
                    {
                        block.SetStandardCharacter(m_bookId, CharacterVerseData.StandardCharacter.ExtraBiblical);
                    }

                    var sb = new StringBuilder();
                    // <verse number="1" style="v" />
                    // Acakki me lok me kwena maber i kom Yecu Kricito, Wod pa Lubaŋa,
                    // <verse number="2" style="v" />
                    // <note caller="-" style="x"><char style="xo" closed="false">1.2: </char><char style="xt" closed="false">Mal 3.1</char></note>
                    // kit ma gicoyo kwede i buk pa lanebi Icaya ni,</para>
                    foreach (XmlNode childNode in usxPara.ChildNodes)
                    {
                        switch (childNode.Name)
                        {
                        case "verse":
                            var verseNumAttr = childNode.Attributes.GetNamedItem("number");
                            if (verseNumAttr == null)
                            {
                                continue;                                                 // presumably this is an end-verse element
                            }
                            if (sb.Length > 0)
                            {
                                sb.TrimStart();
                                block.BlockElements.Add(new ScriptText(sb.ToString()));
                                sb.Clear();
                            }
                            var verseNumStr = verseNumAttr.Value;
                            if (!HandleVerseBridgeInSeparateVerseFields(block, ref verseNumStr))
                            {
                                RemoveEmptyTrailingVerse(block);
                                m_currentStartVerse = BCVRef.VerseToIntStart(verseNumStr);
                                m_currentEndVerse   = BCVRef.VerseToIntEnd(verseNumStr);
                            }
                            if (!block.BlockElements.Any() ||
                                (block.BlockElements.Count == 1 && block.StartsWithScriptTextElementContainingOnlyPunctuation))
                            {
                                block.InitialStartVerseNumber = m_currentStartVerse;
                                block.InitialEndVerseNumber   = m_currentEndVerse;
                            }

                            block.BlockElements.Add(new Verse(verseNumStr));
                            break;

                        case "char":
                            var    charTag   = (new UsxNode(childNode)).StyleTag;
                            IStyle charStyle = m_stylesheet.GetStyle(charTag);
                            if (!charStyle.IsInlineQuotationReference && charStyle.IsPublishable)
                            {
                                // Starting with USFM 3.0, char styles can have attributes separated by |
                                var tokens = childNode.InnerText.Split('|');
                                if (tokens.Any())
                                {
                                    if (ControlCharacterVerseData.TryGetCharacterForCharStyle(charTag, out var character) && block.StyleTag != charTag)
                                    {
                                        FinalizeCharacterStyleBlock(sb, ref block, blocks, charTag);
                                        block.CharacterId = character;
                                        //ControlCharacterVerseData.Singleton.GetCharacters(m_bookNum, block.ChapterNumber,
                                        //block.InitialStartVerseNumber, block.InitialEndVerseNumber, block.LastVerseNum, m_versification, true)
                                        //.FirstOrDefault(cv => cv.Character == character)?.Character ?? CharacterVerseData.kNeedsReview;
                                    }
                                    sb.Append(tokens[0]);
                                }
                            }

                            break;

                        case "#text":
                            var textToAppend = childNode.InnerText;
                            if (ControlCharacterVerseData.IsCharStyleThatMapsToSpecificCharacter(block.StyleTag) && textToAppend.Any(IsLetter))
                            {
                                if (sb.Length > 0 && sb[sb.Length - 1] != ' ' && textToAppend.StartsWith(" "))
                                {
                                    // Not terribly important (in fact, we don't even need the trailing space either), but if blocks have
                                    // leading spaces, it might look funny in some places in the display.
                                    sb.Append(" ");
                                    textToAppend = textToAppend.TrimStart();
                                }
                                FinalizeCharacterStyleBlock(sb, ref block, blocks, usxPara.StyleTag);
                            }
                            sb.Append(textToAppend);
                            break;

                        case "#whitespace":
                            if (sb.Length > 0 && sb[sb.Length - 1] != ' ')
                            {
                                sb.Append(" ");
                            }
                            break;
                        }
                    }
                    FlushStringBuilderToBlockElement(sb, block);
                    if (RemoveEmptyTrailingVerse(block))
                    {
                        var lastVerse = block.LastVerse;
                        m_currentStartVerse = lastVerse.StartVerse;
                        m_currentEndVerse   = lastVerse.EndVerse;
                    }
                    break;
                }
                if (block != null && block.BlockElements.Count > 0)
                {
                    blocks.Add(block);
                }
            }
            return(blocks);
        }