Exemplo n.º 1
0
        public virtual XDocument Generate()
        {
            var mainDocument = new HTMLDocument(Compatibility);

            GenerateHead();
            GenerateBody();
            var encoding = new UTF8Encoding();

            foreach (var file in _styles)
            {
                IHTMLItem styleElement;
                if (EmbedStyles)
                {
                    var styleElementEntry = new Style(Compatibility);
                    styleElement = styleElementEntry;
                    styleElementEntry.Type.Value = CSSFile.MediaType.GetAsSerializableString();
                    try
                    {
                        using (var outStream = new MemoryStream())
                        {
                            file.Write(outStream);
                            styleElementEntry.InternalTextItem.Text = encoding.GetString(outStream.ToArray());
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                }
                else
                {
                    var cssStyleSheet = new Link(Compatibility);
                    styleElement = cssStyleSheet;
                    cssStyleSheet.Relation.Value = "stylesheet";
                    cssStyleSheet.Type.Value     = file.GetMediaType().GetAsSerializableString();
                    cssStyleSheet.HRef.Value     = file.PathInEPUB.GetRelativePath(FileEPubInternalPath, FlatStructure);
                }
                HeadElement.Add(styleElement);
            }

            mainDocument.RootHTML.Add(HeadElement);

            mainDocument.RootHTML.Add(BodyElement);

            if (!mainDocument.RootHTML.IsValid())
            {
                throw new Exception("Document content is not valid");
            }


            var titleElm = new Title(Compatibility);

            titleElm.InternalTextItem.Text = InternalPageTitle;
            HeadElement.Add(titleElm);


            _generatedCodeXDocument = mainDocument.Generate();
            Durty = false;
            return(_generatedCodeXDocument);
        }