// * // * Copyright (C) 2008 Roger Alsing : http://www.RogerAlsing.com // * // * This library is free software; you can redistribute it and/or modify it // * under the terms of the GNU Lesser General Public License 2.1 or later, as // * published by the Free Software Foundation. See the included license.txt // * or http://www.gnu.org/copyleft/lesser.html for details. // * // * using System; using System.Drawing; using System.Globalization; using System.Text; namespace Alsing.SourceCode.SyntaxDocumentExporters { /// /// Html exporter class /// public class CollapsingHTMLExporter { private StringBuilder sb; /// /// Exports the content of a SyntaxDocument to a HTML formatted string /// /// SyntaxDocument object to export from /// File path tho the images to use in the HTML string /// public string Export(SyntaxDocument doc, string ImagePath) { return Export(doc, Color.Transparent, ImagePath, ""); } /// /// Exports the content of a SyntaxDocument to a HTML formatted string /// /// SyntaxDocument object to export from /// HTML color string to use as background color /// File path tho the images to use in the HTML string /// HTML style string that should be applied to the output /// public string Export(SyntaxDocument doc, Color BGColor, string ImagePath, string Style) { sb = new StringBuilder(); doc.ParseAll(true); int i = 0; string guid = DateTime.Now.Ticks.ToString (CultureInfo.InvariantCulture); //style=\"font-family:courier new;font-size:13px;\" if (BGColor.A == 0) Out("
"); else Out("
"); foreach (Row r in doc) { i++; if (r.CanFold) { RenderCollapsed(r.VirtualCollapsedRow, r, i, ImagePath, guid); Out("
"); string img = "minus.gif"; try { if (r.expansion_StartSpan.Parent.Parent == null) img = "minusNoTopLine.gif"; } catch {} Out(""); } else { if (r.CanFoldEndPart) { Out(""); } else { if (r.HasExpansionLine) { Out(""); } else { Out(""); } } } foreach (Word w in r) { write(w.Text, w.Style); } if (r.CanFoldEndPart) Out("
\n"); else Out("
\n"); } Out("
"); return sb.ToString(); } private void RenderCollapsed(Row r, Row TrueRow, int i, string ImagePath, string guid) { Out("
"); string img = "plus.gif"; try { if (TrueRow.expansion_StartSpan.Parent.Parent == null) img = "PlusNoLines.gif"; } catch {} Out(""); foreach (Word w in r) { write(w.Text, w.Style); } Out("
"); } private void write(string text, TextStyle s) { if (s != null) { if (s.Bold) Out(""); if (s.Italic) Out(""); if (s.Transparent) Out(""); else Out(""); } text = text.Replace("&", "&"); text = text.Replace("<", "<"); text = text.Replace(">", ">"); text = text.Replace(" ", " "); text = text.Replace("\t", "    "); Out(text); if (s != null) { Out(""); if (s.Italic) Out(""); if (s.Bold) Out(""); } } private static string GetHTMLColor(Color c) { return string.Format("#{0}{1}{2}", c.R.ToString("x2"), c.G.ToString("x2"), c.B.ToString("x2")); } private void Out(string text) { sb.Append(text); } } }