This project is read-only.

How do it do Export to HTML ?

Topics: Developer Forum, Project Management Forum, User Forum
Jul 4, 2011 at 2:19 PM
Edited Jul 4, 2011 at 2:37 PM

How do it convert all the Text in Document to HTML code / Export to HTML  I liked the control so much if it can provide this that it will be savior for me . :)

if its not possible to convert it to HTML then RTF of the current code will be ok for me .

Thanks In Advance!

Jul 7, 2011 at 11:54 PM

Export to HTML functionality is included in the Trunk but not in the current release. It hasn't been fully tested yet, but it should work. If you're not comfortable doing your own build from Trunk I've pasted the complete Scintilla.ExportHtml method(s) below which you should be able to add to your own code with little modification.

Cheers,
Jacob

/// <summary>
/// Exports a HTML representation of the current document.
/// </summary>
/// <returns>A <see cref="String"/> containing the contents of the document formatted as HTML.</returns>
/// <remarks>Only ASCII documents are supported. Other encoding types have undefined behavior.</remarks>
public string ExportHtml()
{
    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
        ExportHtml(sw, "Untitled", false);

    return sb.ToString();
}


/// <summary>
/// Exports a HTML representation of the current document.
/// </summary>
/// <param name="writer">The <see cref="TextWriter"/>with which to write. </param>
/// <param name="title">The title of the HTML document.</param>
/// <param name="allStyles"><c>true</c> to output all styles including those not
/// used in the document; otherwise, <c>false</c>.</param>
/// <remarks>Only ASCII documents are supported. Other encoding types have undefined behavior.</remarks>
public void ExportHtml(TextWriter writer, string title, bool allStyles)
{
    // Make sure the document is current
    // Lexing.Colorize();

    // Get the styles used
    int length = NativeInterface.GetLength();
    bool[] stylesUsed = new bool[(int)StylesCommon.Max + 1];
    if (allStyles)
    {
        for (int i = 0; i < stylesUsed.Length; i++)
            stylesUsed[i] = true;
    }
    else
    {
        // Record all the styles used
        for (int i = 0; i < length; i++)
            stylesUsed[Styles.GetStyleAt(i) & (int)StylesCommon.Max] = true;
    }

    // The tab width
    int tabWidth = Indentation.TabWidth;

    // Start writing
    writer.WriteLine(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"" ""http://www.w3.org/TR/html4/loose.dtd"">");
    writer.WriteLine("<html>");
    writer.WriteLine("<head>");
    writer.WriteLine("<title>{0}</title>", title);
    writer.WriteLine(@"<style type=""text/css"">");
    writer.WriteLine();

    // Write the body style
    writer.WriteLine("body {");
    writer.WriteLine("background-color: {0};", Utilities.ColorToHtml(Styles.Default.BackColor));
    if (LineWrap.Mode == WrapMode.None)
        writer.WriteLine("white-space: nowrap;");
    writer.WriteLine("}");
    writer.WriteLine();

    // Write the styles
    for (int i = 0; i < stylesUsed.Length; i++)
    {
        if (!stylesUsed[i])
            continue;

        Style s = Styles[i];
        writer.WriteLine("span.s{0} {{", i);
        writer.WriteLine("font-family: \"" + s.FontName + "\";");
        writer.WriteLine("font-size: {0}pt;", s.Size);
        if (s.Italic)
            writer.WriteLine("font-style: italic;");
        if (s.Bold)
            writer.WriteLine("font-weight: bold;");
        if (!s.ForeColor.IsEmpty && s.ForeColor != Color.Transparent)
            writer.WriteLine("color: {0};", Utilities.ColorToHtml(s.ForeColor));
        if (!s.BackColor.IsEmpty && s.BackColor != Color.Transparent)
            writer.WriteLine("background-color: {0};", Utilities.ColorToHtml(s.BackColor));

        writer.WriteLine("}");
        writer.WriteLine();
    }

    writer.WriteLine("</style>");
    writer.WriteLine("</head>");
    writer.WriteLine("<body>");

    // Write the document
    // TODO There's more to be done here to support codepages/UTF-8
    char lc;
    char c = '\0';
    int lastStyle = -1;
    for (int i = 0; i < length; i++)
    {
        lc = c;
        c = NativeInterface.GetCharAt(i);
        int style = Styles.GetStyleAt(i);
        if(style != lastStyle)
        {
            if(lastStyle != -1)
                writer.Write("</span>");

            writer.Write(@"<span class=""s{0}"">", style);
            lastStyle = style;
        }

        switch (c)
        {
            case '\0':
                continue;

            case ' ':
                if (lc == ' ')
                    writer.Write("&nbsp;");
                else
                    writer.Write(c);
                continue;

            case '\t':
                for (int t = 0; t < tabWidth; t++)
                    writer.Write("&nbsp; ");
                continue;

            case '\r':
            case '\n':
                if (c == '\r' && i < length - 1 && NativeInterface.GetCharAt(i + 1) == '\n')
                    i++;

                if (lastStyle != -1)
                    writer.Write("</span>");

                writer.WriteLine("<br />");
                lastStyle = -1;
                continue;

            case '<':
                writer.Write("&lt;");
                continue;

            case '>':
                writer.Write("&gt;");
                continue;

            case '&':
                writer.Write("&amp;");
                continue;

            default:
                writer.Write(c);
                continue;
        }
    }

    if (lastStyle != -1)
        writer.Write("</span>");

    writer.WriteLine();
    writer.WriteLine("</body>");
    writer.WriteLine("</html>");
}
 

Jul 8, 2011 at 5:19 AM

Thanks a lot man  i was able to convert it to HTML using your code :-) 

Thanks a lot ..i was waiting to do this HTML conversion from many days ! .

currently I have used thrid party DLL file to convert the HTML that generated using ScintillaNet to HTML with Embbeded Style HTML if possible in future version include that functionality too about generating embedded styled HTML 

 


 

Jun 1, 2013 at 5:00 AM
Edited Jun 1, 2013 at 5:20 AM
There are several major issues with the ExportHtml method. A large one being that the writer is never closed. This causes major problems such as much of the document never being written.

Some changes that would really help are as follows:

1) Convert the content to ASCII and store it temporarily. Use the temporary content for writing. This solves the encoding issue mentioned in the remarks. It can be done using the Encoding.Convert method.

You could also set the encoding of the writer to that of the document.

2) Surround the content in a div tag like this
writer.WriteLine("<div style=\"float: left; white-space: pre; line-height: {0}px;\">", this.Font.SizeInPoints);
.
// Write the document
.
writer.WriteLine("</div>");
Then just write the spaces directly instead of using non-breaking space.

3) Close the writer
writer.Close();
Because of these problems I would recommend writing your own function using the code above and fixes.