Esempio n. 1
0
        /// <summary>
        /// Writes an HTML file, adding the footer, header, etc if needed to the body.
        /// </summary>
        /// <param name="body">"body" tag to write into the html file</param>
        /// <param name="filePath">Path where to write the HTML file</param>
        /// <param name="UI">User interface of the application</param>
        public void ProcessAndSavePage(ChmDocumentNode node, ChmDocument document, string filePath)
        {
            HtmlNode body = node.SplittedPartBody;

            if (body == null)
            {
                throw new Exception("The node " + node + " has no body");
            }

            // Make a copy of the body and add the header and footer:
            HtmlNode clonedBody = AddFooterAndHeader(node, document);

            StreamWriter writer;

            // Determine the encoding to write the page:
            Encoding writeEncoding = OutputEncoding;

            if (writeEncoding == null)
            {
                writeEncoding = inputEncoding;
            }

            if (writeEncoding != null)
            {
                writer = new StreamWriter(filePath, false, writeEncoding);
            }
            else
            {
                // Use the default encoding.
                writer = new StreamWriter(filePath, false);
            }

            writer.WriteLine(textBeforeBody.Replace(TITLETAG, "<title>" + node.Title + "</title>"));
            string bodyText = clonedBody.OuterHtml;

            // Seems to be a bug that puts "about:blank" on links. Remove them:
            bodyText = bodyText.Replace("about:blank", "").Replace("about:", "");
            writer.WriteLine(bodyText);
            writer.WriteLine(textAfterBody);
            writer.Close();

            // Clean the files using Tidy, only if it was written with UTF-8
            if (AppSettings.UseTidyOverOutput && writeEncoding == Encoding.UTF8 && UseTidy)
            {
                TidyParser tidy = new TidyParser(ui, TidyParser.UTF8);
                tidy.DocType = TidyParser.LOOSE;
                tidy.Parse(filePath);
            }
        }
        /// <summary>
        /// Writes an HTML file, adding the footer, header, etc if needed to the body.
        /// </summary>
        /// <param name="body">"body" tag to write into the html file</param>
        /// <param name="filePath">Path where to write the HTML file</param>
        /// <param name="UI">User interface of the application</param>
        /// <param name="title">Text to put into the title tag of the page</param>
        public void ProcessAndSavePage(IHTMLElement body, string filePath, string title)
        {
            // Make a copy of the body and add the header and footer:
            IHTMLElement clonedBody = AddFooterAndHeader(body);

            StreamWriter writer;

            // Determine the encoding to write the page:
            Encoding writeEncoding = OutputEncoding;
            if (writeEncoding == null)
                writeEncoding = inputEncoding;

            if (writeEncoding != null)
                writer = new StreamWriter(filePath, false, writeEncoding);
            else
                // Use the default encoding.
                writer = new StreamWriter(filePath, false);

            writer.WriteLine( textBeforeBody.Replace(TITLETAG, "<title>" + title + "</title>") );
            string bodyText = clonedBody.outerHTML;

            // Seems to be a bug that puts "about:blank" on links. Remove them:
            // TODO: Check if this still true....
            bodyText = bodyText.Replace("about:blank", "").Replace("about:", "");
            writer.WriteLine(bodyText);
            writer.WriteLine(textAfterBody);
            writer.Close();

            // Clean the files using Tidy, only if it was written with UTF-8
            if (AppSettings.UseTidyOverOutput && writeEncoding == Encoding.UTF8 && UseTidy )
            {
                TidyParser tidy = new TidyParser(ui, TidyParser.UTF8);
                tidy.DocType = TidyParser.LOOSE;
                tidy.Parse(filePath);
            }
        }
        private string GetBodyContent(string htmlDocument, bool outputXhtml)
        {
            // Use tidy over the chapter, if it's needed:
            string goodText = "";
            if (AppSettings.UseTidyOverOutput)
                goodText = new TidyParser(UI, outputXhtml).ParseString(htmlDocument);
            else
                goodText = htmlDocument;

            // Extract the body content:
            HTMLDocumentClass docClass = new HTMLDocumentClass();
            IHTMLDocument2 iDocFirstChapter = (IHTMLDocument2)docClass;
            object[] txtHtml = { goodText };
            iDocFirstChapter.write(txtHtml);

            // return the content of the body:
            return iDocFirstChapter.body.innerHTML.Replace("about:blank", "").Replace("about:", "");
        }