Example #1
0
        public string GetText(bool includeVerseNumbers)
        {
            StringBuilder bldr = new StringBuilder();

            foreach (var blockElement in BlockElements)
            {
                Verse verse = blockElement as Verse;
                if (verse != null)
                {
                    if (includeVerseNumbers)
                    {
                        bldr.Append("[");
                        bldr.Append(verse.Number);
                        bldr.Append("]\u00A0");
                    }
                }
                else
                {
                    ScriptText text = blockElement as ScriptText;
                    if (text != null)
                    {
                        bldr.Append(text.Content);
                    }
                }
            }
            return(bldr.ToString());
        }
Example #2
0
        private string GetTextFromBlockElements(bool includeVerseNumbers, bool includeAnnotations = false)
        {
            StringBuilder bldr = new StringBuilder();

            foreach (var blockElement in BlockElements)
            {
                Verse verse = blockElement as Verse;
                if (verse != null)
                {
                    if (includeVerseNumbers)
                    {
                        bldr.Append("{");
                        bldr.Append(verse.Number);
                        bldr.Append("}\u00A0");
                    }
                }
                else
                {
                    ScriptText text = blockElement as ScriptText;
                    if (text != null)
                    {
                        bldr.Append(text.Content);
                    }
                    else if (includeAnnotations)
                    {
                        ScriptAnnotation annotation = blockElement as ScriptAnnotation;
                        if (annotation != null)
                        {
                            bldr.Append(annotation.ToDisplay());
                        }
                    }
                }
            }

            return(bldr.ToString());
        }
Example #3
0
        public Block SplitBlock(Block blockToSplit, string verseToSplit, int characterOffsetToSplit)
        {
            var iBlock = m_blocks.IndexOf(blockToSplit);

            if (iBlock < 0)
            {
                throw new ArgumentException("Block not found in the list for " + BookId, "blockToSplit");
            }

            int splitId;

            if (blockToSplit.SplitId != Block.NotSplit)
            {
                splitId = blockToSplit.SplitId;
            }
            else
            {
                splitId = m_blocks.Max(b => b.SplitId) + 1;
            }

            if (verseToSplit == null && characterOffsetToSplit == 0)
            {
                SplitBeforeBlock(iBlock, splitId);
                return(blockToSplit);
            }

            var currVerse = blockToSplit.InitialVerseNumberOrBridge;

            Block newBlock = null;
            int   indexOfFirstElementToRemove = -1;

            for (int i = 0; i < blockToSplit.BlockElements.Count; i++)
            {
                var blockElement = blockToSplit.BlockElements[i];

                if (newBlock != null)
                {
                    newBlock.BlockElements.Add(blockElement);
                    continue;
                }

                Verse verse = blockElement as Verse;
                if (verse != null)
                {
                    currVerse = verse.Number;
                }
                else if (verseToSplit == currVerse)
                {
                    ScriptText text = blockElement as ScriptText;

                    if (text == null)
                    {
                        continue;
                    }

                    var content = text.Content;

                    if (blockToSplit.BlockElements.Count > i + 1)
                    {
                        indexOfFirstElementToRemove = i + 1;
                    }

                    if (characterOffsetToSplit == kSplitAtEndOfVerse)
                    {
                        characterOffsetToSplit = content.Length;
                    }

                    if (characterOffsetToSplit <= 0 || characterOffsetToSplit > content.Length)
                    {
                        throw new ArgumentOutOfRangeException("characterOffsetToSplit", characterOffsetToSplit,
                                                              "Value must be greater than 0 and less than or equal to the length (" + content.Length +
                                                              ") of the text of verse " + currVerse + ".");
                    }
                    if (characterOffsetToSplit == content.Length && indexOfFirstElementToRemove < 0)
                    {
                        SplitBeforeBlock(iBlock + 1, splitId);
                        return(m_blocks[iBlock + 1]);
                    }

                    int initialStartVerse, initialEndVerse;
                    if (characterOffsetToSplit == content.Length)
                    {
                        var firstVerseAfterSplit = ((Verse)blockToSplit.BlockElements[indexOfFirstElementToRemove]);
                        initialStartVerse = firstVerseAfterSplit.StartVerse;
                        initialEndVerse   = firstVerseAfterSplit.EndVerse;
                    }
                    else
                    {
                        var verseNumParts = verseToSplit.Split(new[] { '-' }, 2, StringSplitOptions.None);
                        initialStartVerse = int.Parse(verseNumParts[0]);
                        initialEndVerse   = verseNumParts.Length == 2 ? int.Parse(verseNumParts[1]) : 0;
                    }
                    newBlock = new Block(blockToSplit.StyleTag, blockToSplit.ChapterNumber,
                                         initialStartVerse, initialEndVerse);
                    newBlock.CharacterId = blockToSplit.CharacterId;
                    newBlock.CharacterIdOverrideForScript = blockToSplit.CharacterIdOverrideForScript;
                    newBlock.UserConfirmed = blockToSplit.UserConfirmed;
                    if (characterOffsetToSplit < content.Length)
                    {
                        newBlock.BlockElements.Add(new ScriptText(content.Substring(characterOffsetToSplit)));
                    }
                    text.Content = content.Substring(0, characterOffsetToSplit);
                    m_blocks.Insert(iBlock + 1, newBlock);
                    var chapterNumbersToIncrement = m_chapterStartBlockIndices.Keys.Where(chapterNum => chapterNum > blockToSplit.ChapterNumber).ToList();
                    foreach (var chapterNum in chapterNumbersToIncrement)
                    {
                        m_chapterStartBlockIndices[chapterNum]++;
                    }

                    m_blockCount++;
                }
            }

            if (newBlock == null)
            {
                throw new ArgumentException("Verse not found in given block.", "verseToSplit");
            }

            if (indexOfFirstElementToRemove >= 0)
            {
                while (indexOfFirstElementToRemove < blockToSplit.BlockElements.Count)
                {
                    blockToSplit.BlockElements.RemoveAt(indexOfFirstElementToRemove);
                }
            }

            blockToSplit.SplitId = newBlock.SplitId = splitId;

            return(newBlock);
        }
Example #4
0
        internal Block SplitBlock(string verseToSplit, int characterOffsetToSplit)
        {
            var currVerse = InitialVerseNumberOrBridge;

            Block newBlock = null;
            int   indexOfFirstElementToRemove = -1;

            for (int i = 0; i < BlockElements.Count; i++)
            {
                var blockElement = BlockElements[i];

                if (newBlock != null)
                {
                    newBlock.BlockElements.Add(blockElement);
                    continue;
                }

                Verse verse = blockElement as Verse;
                if (verse != null)
                {
                    currVerse = verse.Number;
                }
                else if (verseToSplit == currVerse)
                {
                    ScriptText text = blockElement as ScriptText;

                    string content;
                    if (text == null)
                    {
                        if (BlockElements.Count > i + 1 && BlockElements[i + 1] is Verse)
                        {
                            content = string.Empty;
                            characterOffsetToSplit      = 0;
                            indexOfFirstElementToRemove = i + 1;
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        content = text.Content;

                        if (BlockElements.Count > i + 1)
                        {
                            if (!(BlockElements[i + 1] is Verse) &&
                                (characterOffsetToSplit == BookScript.kSplitAtEndOfVerse || characterOffsetToSplit > content.Length))
                            {
                                // Some kind of annotation. We can skip this. If we're splitting at
                                continue;
                            }
                            indexOfFirstElementToRemove = i + 1;
                        }

                        if (characterOffsetToSplit == BookScript.kSplitAtEndOfVerse)
                        {
                            characterOffsetToSplit = content.Length;
                        }

                        if (characterOffsetToSplit <= 0 || characterOffsetToSplit > content.Length)
                        {
                            throw new ArgumentOutOfRangeException("characterOffsetToSplit", characterOffsetToSplit,
                                                                  @"Value must be greater than 0 and less than or equal to the length (" + content.Length +
                                                                  @") of the text of verse " + currVerse + @".");
                        }
                        if (characterOffsetToSplit == content.Length && indexOfFirstElementToRemove < 0)
                        {
                            return(null);
                        }
                    }

                    int initialStartVerse, initialEndVerse;
                    if (characterOffsetToSplit == content.Length)
                    {
                        var firstVerseAfterSplit = (Verse)BlockElements[indexOfFirstElementToRemove];
                        initialStartVerse = firstVerseAfterSplit.StartVerse;
                        initialEndVerse   = firstVerseAfterSplit.EndVerse;
                    }
                    else
                    {
                        var verseNumParts = verseToSplit.Split(new[] { '-' }, 2, StringSplitOptions.None);
                        initialStartVerse = int.Parse(verseNumParts[0]);
                        initialEndVerse   = verseNumParts.Length == 2 ? int.Parse(verseNumParts[1]) : 0;
                    }
                    newBlock = new Block(StyleTag, ChapterNumber, initialStartVerse, initialEndVerse)
                    {
                        CharacterId = CharacterId,
                        CharacterIdOverrideForScript = CharacterIdOverrideForScript,
                        Delivery      = Delivery,
                        UserConfirmed = UserConfirmed
                    };
                    if (characterOffsetToSplit < content.Length)
                    {
                        newBlock.BlockElements.Add(new ScriptText(content.Substring(characterOffsetToSplit)));
                    }
                    if (text != null)
                    {
                        text.Content = content.Substring(0, characterOffsetToSplit);
                    }
                }
            }

            if (newBlock == null)
            {
                throw new ArgumentException(String.Format("Verse {0} not found in given block: {1}", verseToSplit, GetText(true)), "verseToSplit");
            }

            if (indexOfFirstElementToRemove >= 0)
            {
                while (indexOfFirstElementToRemove < BlockElements.Count)
                {
                    BlockElements.RemoveAt(indexOfFirstElementToRemove);
                }
            }
            return(newBlock);
        }
Example #5
0
        public string GetTextAsHtml(bool showVerseNumbers, bool rightToLeftScript, string verseToInsertExtra = null, int offsetToInsertExtra = -1, string extra = null)
        {
            StringBuilder bldr = new StringBuilder();

            var currVerse = InitialVerseNumberOrBridge;

            foreach (var blockElement in BlockElements)
            {
                Verse verse = blockElement as Verse;
                if (verse != null)
                {
                    if (showVerseNumbers)
                    {
                        bldr.Append("<sup>");
                        if (rightToLeftScript)
                        {
                            bldr.Append("&rlm;");
                        }
                        bldr.Append(verse.Number);
                        bldr.Append("&#160;");
                        if (rightToLeftScript)
                        {
                            bldr.Append("&rlm;");
                        }
                        bldr.Append("</sup>");
                    }
                    currVerse = verse.Number;
                }
                else
                {
                    ScriptText text = blockElement as ScriptText;
                    if (text != null)
                    {
                        var encodedContent = HttpUtility.HtmlEncode(text.Content);
                        if (verseToInsertExtra == currVerse)
                        {
                            if (offsetToInsertExtra == BookScript.kSplitAtEndOfVerse)
                            {
                                offsetToInsertExtra = encodedContent.Length;
                            }
                            if (offsetToInsertExtra < 0 || offsetToInsertExtra > encodedContent.Length)
                            {
                                throw new ArgumentOutOfRangeException("offsetToInsertExtra", offsetToInsertExtra,
                                                                      "Value must be greater than or equal to 0 and less than or equal to the length (" + encodedContent.Length +
                                                                      ") of the encoded content of verse " + currVerse);
                            }
                            if (extra == null)
                            {
                                throw new ArgumentNullException("extra");
                            }
                            encodedContent = HttpUtility.HtmlEncode(text.Content.Insert(offsetToInsertExtra, kAwooga)).Replace(kAwooga, extra);
                        }
                        var content = String.Format("<div id=\"{0}\" class=\"scripttext\">{1}</div>", currVerse,
                                                    encodedContent);
                        bldr.Append(content);
                    }
                }
            }

            return(bldr.ToString());
        }