예제 #1
0
        private static void PrepareTextItem(IFb2TextItem textItem, FB2File file, List <ILine> lines, string bodyName, int headerLevel)
        {
            switch (textItem)
            {
            case CiteItem citeItem:
                var cite = new Epigraph();

                PrepareTextItems(citeItem.CiteData, file, cite.Texts, bodyName, headerLevel);
                PrepareTextItems(citeItem.TextAuthors, file, cite.Authors, bodyName, headerLevel);

                lines.Add(cite);
                return;

            case PoemItem poemItem: {
                AddTitle(poemItem.Title, lines, poemItem.ID, headerLevel);
                PrepareTextItems(poemItem.Epigraphs, file, lines, bodyName, headerLevel + 1);
                PrepareTextItems(poemItem.Content, file, lines, bodyName, headerLevel + 1);
                return;
            }

            case SectionItem sectionItem: {
                if (bodyName == "notes")
                {
                    var note = new NoteLine {
                        Id = sectionItem.ID
                    };

                    AddTitle(sectionItem.Title, note.Titles, sectionItem.ID, headerLevel);
                    PrepareTextItems(sectionItem.Content, file, note.Texts, bodyName, headerLevel);

                    lines.Add(note);
                }
                else
                {
                    AddTitle(sectionItem.Title, lines, sectionItem.ID, headerLevel);
                    PrepareTextItems(sectionItem.Epigraphs, file, lines, bodyName, headerLevel + 1);
                    PrepareTextItems(sectionItem.Content, file, lines, bodyName, headerLevel + 1);
                }

                return;
            }

            case StanzaItem stanzaItem: {
                AddTitle(stanzaItem.Title, lines, string.Empty, headerLevel);
                PrepareTextItems(stanzaItem.Lines, file, lines, bodyName, headerLevel + 1);
                return;
            }

            case SimpleText text: {
                lines.Add(text.ToHtml().ToTextLine());
                break;
            }

            case EmptyLineItem empty: {
                lines.Add(empty.ToString().ToTextLine());
                break;
            }

            case TitleItem title: {
                AddTitle(title, lines, string.Empty, headerLevel + 1);
                break;
            }

            case SubTitleItem _:
            case ParagraphItem _: {
                var sb = new StringBuilder();
                foreach (var paragraphData in ((ParagraphItem)textItem).ParagraphData)
                {
                    sb.Append(paragraphData.ToHtml());
                }

                lines.Add(sb.ToString().ToTextLine());

                return;
            }

            case ImageItem imageItem: {
                var key = imageItem.HRef.Replace("#", string.Empty);

                if (file.Images.TryGetValue(key, out var image))
                {
                    lines.Add(image.ToImageLine(imageItem.ID));
                }

                return;
            }

            case DateItem item:
                lines.Add(item.DateValue.ToString().ToTextLine());
                return;

            case EpigraphItem epigraphItem: {
                var epigraph = new Epigraph();

                PrepareTextItems(epigraphItem.EpigraphData, file, epigraph.Texts, bodyName, headerLevel);
                PrepareTextItems(epigraphItem.TextAuthors, file, epigraph.Authors, bodyName, headerLevel);

                lines.Add(epigraph);
                return;
            }

            default:
                throw new Exception(textItem.GetType().ToString());
            }
        }
예제 #2
0
        //record book strings to dictionary for read
        public virtual void PrepareTextItem(IFb2TextItem textItem)
        {
            if (textItem is CiteItem)
            {
                PrepareTextItems(((CiteItem)textItem).CiteData);
                return;
            }

            if (textItem is PoemItem)
            {
                var item = (PoemItem)textItem;
                AddTitle(item.Title);
                PrepareTextItems(item.Content);
                return;
            }

            if (textItem is SectionItem)
            {
                var item = (SectionItem)textItem;
                AddTitle(item.Title);
                PrepareTextItems(item.Content);
                return;
            }

            if (textItem is StanzaItem)
            {
                var item = (StanzaItem)textItem;
                AddTitle(item.Title);
                PrepareTextItems(item.Lines);
                return;
            }

            if (textItem is ParagraphItem ||
                textItem is EmptyLineItem ||
                textItem is TitleItem ||
                textItem is SimpleText)
            {
                BookText.Add(textItem.ToString());
                return;
            }

            if (textItem is ImageItem)
            {
                var item = (ImageItem)textItem;
                var key  = item.HRef.Replace("#", string.Empty);

                if (Book.Images.ContainsKey(key))
                {
                    var data = Book.Images[key].BinaryData;
                    Images = data;
                }
                return;
            }

            if (textItem is DateItem)
            {
                BookText.Add(((DateItem)textItem).DateValue.ToString());
                return;
            }

            if (textItem is EpigraphItem)
            {
                var item = (EpigraphItem)textItem;
                PrepareTextItems(item.EpigraphData);
                return;
            }

            throw new Exception(textItem.GetType().ToString());
        }