/// <summary> /// Convert FB2 "notes" and "comments" and other "additional" sections /// </summary> /// <param name="linkSectionItem">item to convert</param> /// <param name="linkSectionConverterParams"></param> /// <returns>XHTML representation</returns> public IHTMLItem Convert(SectionItem linkSectionItem,LinkSectionConverterParamsV2 linkSectionConverterParams) { if (linkSectionItem == null) { throw new ArgumentNullException("linkSectionItem"); } var content = new Div(HTMLElementType.XHTML11); var a = new Anchor(HTMLElementType.XHTML11); var titleConverter = new TitleConverterV2(); foreach (var item in titleConverter.Convert(linkSectionItem.Title,new TitleConverterParamsV2{Settings = linkSectionConverterParams.Settings,TitleLevel = 2}).SubElements()) { content.Add(item); } string newId = linkSectionConverterParams.Settings.ReferencesManager.EnsureGoodId(linkSectionItem.ID); a.Add(new SimpleHTML5Text(HTMLElementType.XHTML11) { Text = newId }); a.HRef.Value = string.Format("{0}_back", newId); if (((string)a.HRef.Value).StartsWith("_back") == false) { SetClassType(a, ElementStylesV2.NoteAnchor); linkSectionConverterParams.Settings.ReferencesManager.AddBackReference((string)a.HRef.Value, a); //content.Add(a); } SetClassType(content, ElementStylesV2.NoteSection); return content; }
private List<FB2NotesPageSectionFile> AddSection(SectionItem section, BaseXHTMLFileV3 navParent) { List<FB2NotesPageSectionFile> documents = new List<FB2NotesPageSectionFile>(); string docTitle = string.Empty; if (section.Title != null) { docTitle = section.Title.ToString(); } Logger.Log.DebugFormat("Adding notes section : {0}", docTitle); FB2NotesPageSectionFile sectionDocument = null; bool firstDocumentOfSplit = true; var converterSettings = new ConverterOptionsV3 { CapitalDrop = false, Images = _images, MaxSize = _v3Settings.HTMLFileMaxSize, ReferencesManager = _referencesManager, }; var sectionConverter = new SectionConverterV3 { LinkSection = true, RecursionLevel = GetRecursionLevel(navParent), Settings = converterSettings }; foreach (var subitem in sectionConverter.Convert(section)) { sectionDocument = new FB2NotesPageSectionFile { PageTitle = docTitle, FileEPubInternalPath = EPubInternalPath.GetDefaultLocation(DefaultLocations.DefaultTextFolder), GuideRole = (navParent == null) ? GuideTypeEnum.Text : navParent.GuideRole, Content = subitem, NavigationParent = navParent, FileName = BuildSectionFileName() }; if (!firstDocumentOfSplit || ((navParent != null) && navParent.NotPartOfNavigation)) { sectionDocument.NotPartOfNavigation = true; } firstDocumentOfSplit = false; documents.Add(sectionDocument); } Logger.Log.Debug("Adding sub-sections"); foreach (var subSection in section.SubSections) { documents.AddRange(AddSection(subSection, sectionDocument)); } return documents; }
/// <summary> /// Converts FB2 section element /// </summary> /// <param name="sectionItem">item to convert</param> /// <returns>XHTML representation</returns> public List<IHTMLItem> Convert(SectionItem sectionItem) { if (sectionItem == null) { throw new ArgumentNullException("sectionItem"); } var resList = new List<IHTMLItem>(); Logger.Log.Debug("Converting section"); var content = new Div(HTMLElementType.HTML5); ulong documentSize = 0; _checker.MaxSizeLimit = Settings.MaxSize; SetClassType(content, string.Format(ElementStylesV3.SectionItemFormat, RecursionLevel)); content.GlobalAttributes.ID.Value = Settings.ReferencesManager.AddIdUsed(sectionItem.ID, content); if (sectionItem.Lang != null) { content.GlobalAttributes.Language.Value = sectionItem.Lang; } // Load Title if (sectionItem.Title != null) { IHTMLItem titleItem; if (!LinkSection) { var titleConverter = new TitleConverterV3(); titleItem = titleConverter.Convert(sectionItem.Title, new TitleConverterParamsV3 { Settings = Settings, TitleLevel = RecursionLevel + 1 }); } else { var linkSectionConverter = new LinkSectionConverterV3(); titleItem = linkSectionConverter.Convert(sectionItem, new LinkSectionConverterParamsV3 { Settings = Settings }); } if (titleItem != null) { documentSize = SplitBlockHTMLItem(titleItem,content,resList,documentSize); } } // Load epigraphs documentSize = (from epigraph in sectionItem.Epigraphs let epigraphConverter = new EpigraphConverterV3() select epigraphConverter.Convert(epigraph, new EpigraphConverterParamsV3 {Settings = Settings, Level = RecursionLevel + 1})).Aggregate(documentSize, (current, epigraphItem) => SplitBlockHTMLItem(epigraphItem, content, resList, current)); // Load section image if (Settings.Images.HasRealImages()) { documentSize = sectionItem.SectionImages.Aggregate(documentSize, (current, sectionImage) => ConvertSectionImage(sectionImage, content, resList, current)); } // Load annotations if (sectionItem.Annotation != null) { var annotationConverter = new AnnotationConverterV3(); IHTMLItem annotationItem = annotationConverter.Convert(sectionItem.Annotation, new AnnotationConverterParamsV3 { Level = RecursionLevel + 1, Settings = Settings }); documentSize = SplitBlockHTMLItem(annotationItem, content, resList, documentSize); } // Parse all elements only if section has no sub section if (sectionItem.SubSections.Count == 0) { bool startSection = true; sectionItem.Content.Aggregate(documentSize, (current, item) => ConvertSimpleSubItem(item, sectionItem, content, resList, ref startSection, current)); } resList.Add(content); return resList; }
private ulong ConvertSimpleSubItem(IFb2TextItem item, SectionItem sectionItem, Div content, List<IHTMLItem> resList, ref bool startSection, ulong documentSize) { ulong docSize = documentSize; IHTMLItem newItem = null; var subtitleItem = item as SubTitleItem; if (subtitleItem != null) { var subtitleConverter = new SubtitleConverterV3(); newItem = subtitleConverter.Convert(subtitleItem, new SubtitleConverterParamsV3 { Settings = Settings }); } else if (item is ParagraphItem) { var paragraphConverter = new ParagraphConverterV3(); newItem = paragraphConverter.Convert((ParagraphItem) item, new ParagraphConverterParamsV3 { ResultType = ParagraphConvTargetEnumV3.Paragraph, StartSection = startSection, Settings = Settings }); startSection = false; } else if (item is PoemItem) { var poemConverter = new PoemConverterV3(); newItem = poemConverter.Convert((PoemItem) item, new PoemConverterParamsV3 { Settings = Settings, Level = RecursionLevel + 1 }); } else if (item is CiteItem) { var citationConverter = new CitationConverterV3(); newItem = citationConverter.Convert((CiteItem) item, new CitationConverterParamsV3 { Level = RecursionLevel + 1, Settings = Settings }); } else if (item is EmptyLineItem) { var emptyLineConverter = new EmptyLineConverterV3(); newItem = emptyLineConverter.Convert(); } else if (item is TableItem) { var tableConverter = new TableConverterV3(); newItem = tableConverter.Convert((TableItem) item, new TableConverterParamsV3 { Settings = Settings }); } else if ((item is ImageItem) && Settings.Images.HasRealImages()) { var fb2Img = item as ImageItem; // if it's not section image and it's used if ((sectionItem.SectionImages.Find(x => x == fb2Img) == null) && (fb2Img.HRef != null)) { if (Settings.Images.IsImageIdReal(fb2Img.HRef)) { var enclosing = new Div(HTMLElementType.HTML5); // we use the enclosing so the user can style center it var imageConverter = new ImageConverterV3(); enclosing.Add(imageConverter.Convert(fb2Img, new ImageConverterParamsV3 { Settings = Settings })); SetClassType(enclosing, ElementStylesV3.NormalImage); newItem = enclosing; } } } if (newItem != null) { docSize = SplitBlockHTMLItem(newItem, content, resList, docSize); } return docSize; }
private void AddSection(EPubFileV2 epubFile, SectionItem section, BaseXHTMLFileV2 navParent, bool fbeNotesSection) { string docTitle = string.Empty; if (section.Title != null) { docTitle = section.Title.ToString(); } Logger.Log.DebugFormat("Adding section : {0}", docTitle); BaseXHTMLFileV2 sectionDocument = null; bool firstDocumentOfSplit = true; var converterSettings = new ConverterOptionsV2 { CapitalDrop = !fbeNotesSection && _commonSettings.CapitalDrop, Images = _images, MaxSize = _maxSize, ReferencesManager = _referencesManager, }; var sectionConverter = new SectionConverterV2 { LinkSection = fbeNotesSection, RecursionLevel = GetRecursionLevel(navParent), Settings = converterSettings }; foreach (var subitem in sectionConverter.Convert(section)) { sectionDocument = new BaseXHTMLFileV2 { PageTitle = docTitle, FileEPubInternalPath = EPubInternalPath.GetDefaultLocation(DefaultLocations.DefaultTextFolder), GuideRole = (navParent == null) ? GuideTypeEnum.Text : navParent.GuideRole, Type = (navParent == null) ? SectionTypeEnum.Text : navParent.Type, Content = subitem, NavigationParent = navParent, FileName = string.Format("section{0}.xhtml", ++_sectionCounter) }; if (!firstDocumentOfSplit || (sectionDocument.Type == SectionTypeEnum.Links)) { sectionDocument.NotPartOfNavigation = true; } firstDocumentOfSplit = false; epubFile.AddXHTMLFile(sectionDocument); } Logger.Log.Debug("Adding sub-sections"); foreach (var subSection in section.SubSections) { AddSection(epubFile, subSection, sectionDocument, fbeNotesSection); } }
internal void Load(XElement xSection) { ClearAll(); if (xSection == null) { throw new ArgumentNullException("xSection"); } if (xSection.Name.LocalName != Fb2TextSectionElementName) { throw new ArgumentException("Element of wrong type passed", "xSection"); } XElement xTitle = xSection.Element(xSection.Name.Namespace + TitleItem.Fb2TitleElementName); if (xTitle != null) { Title = new TitleItem(); try { Title.Load(xTitle); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section title : {0}.", ex.Message)); } } IEnumerable<XElement> xEpigraphs = xSection.Elements(xSection.Name.Namespace + EpigraphItem.Fb2EpigraphElementName); foreach (var xEpigraph in xEpigraphs) { EpigraphItem epigraph = new EpigraphItem(); try { epigraph.Load(xEpigraph); _epigraphs.Add(epigraph); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section epigraph : {0}.", ex.Message)); } } XElement xAnnotation = xSection.Element(xSection.Name.Namespace + AnnotationItem.Fb2AnnotationItemName); if (xAnnotation != null) { Annotation = new AnnotationItem(); try { Annotation.Load(xAnnotation); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section annotation : {0}.", ex.Message)); } } IEnumerable<XElement> xElements = xSection.Elements(); foreach (var xElement in xElements) { switch (xElement.Name.LocalName) { case ParagraphItem.Fb2ParagraphElementName: ParagraphItem paragraph = new ParagraphItem(); try { paragraph.Load(xElement); _content.Add(paragraph); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section paragraph : {0}.", ex.Message)); } break; case PoemItem.Fb2PoemElementName: PoemItem poem = new PoemItem(); try { poem.Load(xElement); _content.Add(poem); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section poem : {0}.", ex.Message)); } break; case ImageItem.Fb2ImageElementName: ImageItem image = new ImageItem(); try { image.Load(xElement); AddImage(image); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section image : {0}.", ex.Message)); } break; case SubTitleItem.Fb2SubtitleElementName: SubTitleItem subtitle = new SubTitleItem(); try { subtitle.Load(xElement); _content.Add(subtitle); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section subtitle : {0}.", ex.Message)); } break; case CiteItem.Fb2CiteElementName: CiteItem cite = new CiteItem(); try { cite.Load(xElement); _content.Add(cite); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section citation : {0}.", ex.Message)); } break; case EmptyLineItem.Fb2EmptyLineElementName: EmptyLineItem eline = new EmptyLineItem(); _content.Add(eline); break; case TableItem.Fb2TableElementName: TableItem table = new TableItem(); try { table.Load(xElement); _content.Add(table); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load section emptly line : {0}.", ex.Message)); } break; case Fb2TextSectionElementName: // internal <section> read recurive SectionItem section = new SectionItem(); try { section.Load(xElement); AddSection(section); } catch (Exception ex) { Debug.WriteLine(string.Format("Failed to load sub-section : {0}.", ex.Message)); } break; case AnnotationItem.Fb2AnnotationItemName: // already processed break; case TitleItem.Fb2TitleElementName: // already processed break; case EpigraphItem.Fb2EpigraphElementName: // already processed break; default: if (!string.IsNullOrEmpty(xElement.Value)) { ParagraphItem tempParagraph = new ParagraphItem(); try { SimpleText text = new SimpleText {Text = xElement.Value}; tempParagraph.ParagraphData.Add(text); _content.Add(tempParagraph); } catch { // ignored } } Debug.WriteLine("AnnotationItem:Load - invalid element <{0}> encountered in title .", xElement.Name.LocalName); break; } } ID = null; XAttribute xID = xSection.Attribute("id"); if ((xID != null)) { ID = xID.Value; } Lang = null; XAttribute xLang = xSection.Attribute(XNamespace.Xml + "lang"); if ((xLang != null)) { Lang = xLang.Value; } }
private void AddSection(SectionItem section) { _content.Add(section); _subSections.Add(section); }
private static void AppendSectionItem(SectionItem item, int itemDepth, List<AbstractTextPart> appendTo) { List<AbstractTextPart> titleText = new List<AbstractTextPart>(); if (item.Title != null) { foreach (IFb2TextItem iTextItem in item.Title.TitleData) AppendIFb2TextItem(iTextItem, itemDepth + 1, titleText); if (titleText.Count != 0) { string headingText = titleText.Select(part => part.CreateTextVisual().Text).Aggregate((left, right) => left + ' ' + right); appendTo.Add(new Heading(itemDepth, headingText)); } } foreach (IFb2TextItem textItem in item.SubSections) { SectionItem sectionItem = textItem as SectionItem; if (sectionItem != null) { AppendSectionItem(sectionItem, itemDepth + 1, appendTo); continue; } throw new NotImplementedException(); } foreach (IFb2TextItem content in item.Content) { if (content is SectionItem) { AppendSectionItem(content as SectionItem, itemDepth + 1, appendTo); continue; } if (content is ParagraphItem) { ParagraphItem paragraph = content as ParagraphItem; foreach (StyleType styleType in paragraph.ParagraphData) { AppendStyleType(styleType, appendTo); } continue; } if (content is EmptyLineItem) { appendTo.Add(SimpleText.EmptyParagraph); continue; } } }
internal void Load(XElement xSection) { ClearAll(); if (xSection == null) { throw new ArgumentNullException("xSection"); } if (xSection.Name.LocalName != Fb2TextSectionElementName) { throw new ArgumentException("Element of wrong type passed", "xSection"); } XElement xTitle = xSection.Element(xSection.Name.Namespace + TitleItem.Fb2TitleElementName); if (xTitle != null) { Title = new TitleItem(); try { Title.Load(xTitle); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section title : {0}.", ex.Message)); } } IEnumerable <XElement> xEpigraphs = xSection.Elements(xSection.Name.Namespace + EpigraphItem.Fb2EpigraphElementName); foreach (var xEpigraph in xEpigraphs) { EpigraphItem epigraph = new EpigraphItem(); try { epigraph.Load(xEpigraph); _epigraphs.Add(epigraph); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section epigraph : {0}.", ex.Message)); } } XElement xAnnotation = xSection.Element(xSection.Name.Namespace + AnnotationItem.Fb2AnnotationItemName); if (xAnnotation != null) { Annotation = new AnnotationItem(); try { Annotation.Load(xAnnotation); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section annotation : {0}.", ex.Message)); } } IEnumerable <XElement> xElements = xSection.Elements(); foreach (var xElement in xElements) { switch (xElement.Name.LocalName) { case ParagraphItem.Fb2ParagraphElementName: ParagraphItem paragraph = new ParagraphItem(); try { paragraph.Load(xElement); _content.Add(paragraph); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section paragraph : {0}.", ex.Message)); } break; case PoemItem.Fb2PoemElementName: PoemItem poem = new PoemItem(); try { poem.Load(xElement); _content.Add(poem); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section poem : {0}.", ex.Message)); } break; case ImageItem.Fb2ImageElementName: ImageItem image = new ImageItem(); try { image.Load(xElement); AddImage(image); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section image : {0}.", ex.Message)); } break; case SubTitleItem.Fb2SubtitleElementName: SubTitleItem subtitle = new SubTitleItem(); try { subtitle.Load(xElement); _content.Add(subtitle); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section subtitle : {0}.", ex.Message)); } break; case CiteItem.Fb2CiteElementName: CiteItem cite = new CiteItem(); try { cite.Load(xElement); _content.Add(cite); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section citation : {0}.", ex.Message)); } break; case EmptyLineItem.Fb2EmptyLineElementName: EmptyLineItem eline = new EmptyLineItem(); _content.Add(eline); break; case TableItem.Fb2TableElementName: TableItem table = new TableItem(); try { table.Load(xElement); _content.Add(table); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load section emptly line : {0}.", ex.Message)); } break; case Fb2TextSectionElementName: // internal <section> read recurive SectionItem section = new SectionItem(); try { section.Load(xElement); AddSection(section); } catch (Exception ex) { Debug.Fail(string.Format("Failed to load sub-section : {0}.", ex.Message)); } break; case AnnotationItem.Fb2AnnotationItemName: // already processed break; case TitleItem.Fb2TitleElementName: // already processed break; case EpigraphItem.Fb2EpigraphElementName: // already processed break; default: if (!string.IsNullOrEmpty(xElement.Value)) { ParagraphItem tempParagraph = new ParagraphItem(); try { SimpleText text = new SimpleText { Text = xElement.Value }; tempParagraph.ParagraphData.Add(text); _content.Add(tempParagraph); } catch { // ignored } } Debug.Print("AnnotationItem:Load - invalid element <{0}> encountered in title .", xElement.Name.LocalName); break; } } ID = null; XAttribute xID = xSection.Attribute("id"); if ((xID != null)) { ID = xID.Value; } Lang = null; XAttribute xLang = xSection.Attribute(XNamespace.Xml + "lang"); if ((xLang != null)) { Lang = xLang.Value; } }
internal void Load(XElement xBody) { if (xBody == null) { throw new ArgumentNullException("xBody"); } if (xBody.Name.LocalName != Fb2BodyItemName) { throw new ArgumentException("Element of wrong type passed", "xBody"); } ImageName = null; XElement xImage = xBody.Element(NameSpace + Fb2ImageElementName); if ((xImage != null)) { ImageName = new ImageItem(); try { ImageName.Load(xImage); } catch (Exception ex) { Debug.WriteLine(string.Format("Error reading body image: {0}", ex.Message)); } } XElement xTitle = xBody.Element(NameSpace + TitleItem.Fb2TitleElementName); if ((xTitle != null) && (xTitle.Value != null)) { try { title.Load(xTitle); } catch (Exception ex) { Debug.WriteLine(string.Format("Error reading body title: {0}", ex.Message)); } } // Load epigraph elements IEnumerable <XElement> xEpigraphs = xBody.Elements(NameSpace + EpigraphItem.Fb2EpigraphElementName); epigraphs.Clear(); foreach (var xEpigraph in xEpigraphs) { EpigraphItem item = new EpigraphItem(); try { item.Load(xEpigraph); } catch (Exception ex) { Debug.WriteLine(string.Format("Error reading body epigraph: {0}", ex.Message)); continue; } epigraphs.Add(item); } // Load body elements (first is main text) IEnumerable <XElement> xSections = xBody.Elements(NameSpace + SectionItem.Fb2TextSectionElementName); sections.Clear(); foreach (var section in xSections) { SectionItem item = new SectionItem(); try { item.Load(section); } catch (Exception ex) { Debug.WriteLine(string.Format("Error reading body sections: {0}", ex.Message)); continue; } sections.Add(item); } Name = string.Empty; XAttribute xName = xBody.Attribute(Fb2NameAttributeName); if ((xName != null) && (xName.Value != null)) { Name = xName.Value; } Lang = null; XAttribute xLang = xBody.Attribute(XNamespace.Xml + "lang"); if ((xLang != null) && (xLang.Value != null)) { Lang = xLang.Value; } }
internal void Load(XElement xBody) { if (xBody == null) { throw new ArgumentNullException("xBody"); } if (xBody.Name.LocalName != Fb2BodyItemName) { throw new ArgumentException("Element of wrong type passed", "xBody"); } ImageName = null; XElement xImage = xBody.Element(NameSpace + Fb2ImageElementName); if ((xImage != null)) { ImageName = new ImageItem(); try { ImageName.Load(xImage); } catch (Exception ex) { Debug.Fail(string.Format("Error reading body image: {0}", ex.Message)); } } XElement xTitle = xBody.Element(NameSpace + TitleItem.Fb2TitleElementName); if ((xTitle != null) && (xTitle.Value != null)) { try { title.Load(xTitle); } catch (Exception ex) { Debug.Fail(string.Format("Error reading body title: {0}", ex.Message)); } } // Load epigraph elements IEnumerable<XElement> xEpigraphs = xBody.Elements(NameSpace + EpigraphItem.Fb2EpigraphElementName); epigraphs.Clear(); foreach (var xEpigraph in xEpigraphs) { EpigraphItem item = new EpigraphItem(); try { item.Load(xEpigraph); } catch (Exception ex) { Debug.Fail(string.Format("Error reading body epigraph: {0}", ex.Message)); continue; } epigraphs.Add(item); } // Load body elements (first is main text) IEnumerable<XElement> xSections = xBody.Elements(NameSpace + SectionItem.Fb2TextSectionElementName); sections.Clear(); foreach (var section in xSections) { SectionItem item = new SectionItem(); try { item.Load(section); } catch (Exception ex) { Debug.Fail(string.Format("Error reading body sections: {0}", ex.Message)); continue; } sections.Add(item); } Name = string.Empty; XAttribute xName = xBody.Attribute(Fb2NameAttributeName); if ((xName != null) && (xName.Value != null)) { Name = xName.Value; } Lang = null; XAttribute xLang = xBody.Attribute(XNamespace.Xml + "lang"); if ((xLang != null) && (xLang.Value != null)) { Lang = xLang.Value; } }