Click here to Skip to main content
15,885,869 members
Articles / Desktop Programming / Windows Forms

Storm - the world's best IDE framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (82 votes)
4 Feb 2010LGPL311 min read 274.4K   6.5K   340  
Create fast, flexible, and extensible IDE applications easily with Storm - it takes nearly no code at all!
//
//    ___ _____ ___  ___ __  __ 
//   / __|_   _/ _ \| _ \  \/  |
//   \__ \ | || (_) |   / |\/| |
//   |___/ |_| \___/|_|_\_|  |_|
// 
//   Storm.TextEditor.dll
//   ��������������������
//     Storm.TabControl.dll was created under the LGPL 
//     license. Some of the code was created from scratch, 
//     some was not. Code not created from scratch was 
//     based on the DotNetFireball framework and evolved 
//     from that. 
//     
//     What I mostly did in this library was that I 
//     cleaned up the code, structured it, documentated 
//     it and added new features. 
//     
//     Although it might not sound like it, it was very
//     hard and took a long (pretty much a shitload)
//     time. Why? Well, this was* some of the crappiest,
//     most unstructured, undocumentated, ugly code I've
//     ever seen. It would actually have taken me less 
//     time to create it from scratch, but I figured that
//     out too late. Figuring out what the code actually
//     did and then documentating it was (mostly) a 
//     day-long process. Well, I hope you enjoy some of
//     my hard work. /rant
//     
//     Of course some/most of it is made from scratch by me.
//     *Yes, was. It isn't now. :) Some of the naming 
//     conventions might still seem a little bit off though.
//
//   What is Storm?
//   ��������������
//     Storm is a set of dynamic link libraries used in 
//     Theodor "Vestras" Storm Kristensen's application 
//     "Moonlite".
//     
//
//   Thanks:
//   �������
//     - The DotNetFireball team for creating and publishing 
//       DotNetFireball which I based some of my code on.
//
//
//   Copyright (c) Theodor "Vestras" Storm Kristensen 2009
//   �����������������������������������������������������
//


namespace Storm.TextEditor.Document.Exporters
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Diagnostics.Design;
    using System.Diagnostics.SymbolStore;
    using System.IO;
    using System.Reflection;
    using System.Resources;
    using System.Text;
    using System.Windows.Forms;

    using Storm.TextEditor;
    using Storm.TextEditor.Controls;
    using Storm.TextEditor.Controls.Core;
    using Storm.TextEditor.Controls.Core.Globalization;
    using Storm.TextEditor.Controls.Core.Timers;
    using Storm.TextEditor.Controls.IntelliMouse;
    using Storm.TextEditor.Document;
    using Storm.TextEditor.Document.Exporters;
    using Storm.TextEditor.Forms;
    using Storm.TextEditor.Interacting;
    using Storm.TextEditor.Painting;
    using Storm.TextEditor.Parsing;
    using Storm.TextEditor.Parsing.Base;
    using Storm.TextEditor.Parsing.Classes;
    using Storm.TextEditor.Parsing.Language;
    using Storm.TextEditor.Preset;
    using Storm.TextEditor.Preset.Painting;
    using Storm.TextEditor.Preset.TextDraw;
    using Storm.TextEditor.Printing;
    using Storm.TextEditor.Utilities;
    using Storm.TextEditor.Win32;

	public class HTMLExporter
    {
        #region Members
        // Basic members.
        private StringBuilder sb = null;
        #endregion

        #region Methods
        /// <summary>
		/// Exports the content of a SyntaxDocument to a HTML formatted string.
		/// </summary>
		/// <param name="doc">SyntaxDocument object to export from.</param>
		/// <param name="imagePath">File path to the images to use in the HTML string.</param>
		public string Export(SyntaxDocument doc, string imagePath)
		{ return Export(doc, Color.Transparent, imagePath, ""); }


		/// <summary>
		/// Exports the content of a SyntaxDocument to a HTML formatted string.
		/// </summary>
		/// <param name="doc">SyntaxDocument object to export from.</param>
		/// <param name="bgColor">HTML color string to use as background color.</param>
		/// <param name="imagePath">File path to the images to use in the HTML string.</param>
		/// <param name="style">HTML style string that should be applied to the output.</param>
		/// <returns></returns>
		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();
			if (bgColor.A ==0)
				Out("<table  ><tr><td nowrap><div style=\""+ style +"\">");
			else
				Out("<table  style=\"background-color:" + GetHTMLColor(bgColor) 
                    + ";" + style + "\"><tr><td nowrap><div>");
			
            foreach (Row r in doc)
			{
				i++;
				if (r.CanFold)
				{
					RenderCollapsed(r.VirtualCollapsedRow, r, i, imagePath,guid);
					Out("<div style=\"display:block;\" id=\"open" + guid + "_" 
                        + i.ToString() + "\">");

					string img="minus.gif";
					try
                    {
						if (r.Expansion_StartSegment.Parent.Parent  == null)
							img = "minusNoTopLine.gif";
                    }
					catch
					{
					}

					Out ("<img src=\"" + imagePath + img + 
                        "\" align=top onclick=\"open" + guid + "_" + i.ToString() + 
                        ".style.display='none'; closed" + guid+"_" + i.ToString() + 
                        ".style.display='block'; \">");
				}
				else
				{
					if (r.CanFoldEndPart)
						Out ("<img src=\"" + imagePath + "L.gif\"  align=top>");
					else
					{
						if (r.HasExpansionLine)
							Out ("<img src=\"" + imagePath + "I.gif\"  align=top>");
						else
							Out ("<img src=\"" + imagePath + "clear.gif\"  align=top>");
					}
				}

				foreach (Word w in r)
					Write (w.Text,w.Style);

				if (r.CanFoldEndPart)
					Out("</div>\n");
				else
					Out("<br>\n");				
			}

			Out("</div></td></tr></table>");
			return sb.ToString();
		}

		private void RenderCollapsed(Row r, Row trueRow, int i, string imagePath, string guid)
		{
			Out("<div style=\"display:none;\" id=\"closed" + guid +"_" + i.ToString() + "\">");
			string img = "plus.gif";			
			try
			{
				if (trueRow.Expansion_StartSegment.Parent.Parent  == null)
					img = "PlusNoLines.gif";
			}
			catch
			{
			}
		
			Out ("<img src=\"" + imagePath + img +"\" align=top onclick=\"open" + guid+"_"+i.ToString () +".style.display='block'; closed" + guid+"_"+i.ToString () +".style.display='none'; \">");

			foreach (Word w in r)
				Write(w.Text,w.Style);

			Out("</div>");	
		}
		
		private void Write(string text, TextStyle s)
		{
			if (s != null)
			{
				if (s.Bold == true)
					Out("<b>");
				if (s.Italic == true)
					Out("<i>");
				if (s.Transparent == true)
					Out("<span style=\"color:" + GetHTMLColor(s.ForeColor) + "\">");
				else
					Out("<span style=\"color:" + GetHTMLColor(s.ForeColor) + ";background-color:" + GetHTMLColor(s.BackColor) + ";\">");
			}

			text = text.Replace("&","&amp;");
			text = text.Replace("<","&lt;");
			text = text.Replace(">","&gt;");
			text = text.Replace(" ","&nbsp;");
			text = text.Replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;");
			Out(text);

			if (s != null)
			{
				Out("</span>");
				if (s.Italic == true)
					Out("</i>");
				if (s.Bold == true)
					Out("</b>");
			}
		}

		private 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); }
        #endregion

        /// <summary>
        /// Initializes the HTMLExporter.
        /// </summary>
        public HTMLExporter()
        {
        }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)



Comments and Discussions