Click here to Skip to main content
15,889,116 members
Articles / Programming Languages / C#

Make NDoc compile the code examples contained in your documentation using NLiterate

Rate me:
Please Sign up or sign in to vote.
4.64/5 (19 votes)
26 Apr 20047 min read 123.4K   622   42  
An utility that merges and recompiles the examples in your documentation using NDoc.
// The MIT License
// Copyright (c) 2004 Jonathan de Halleux
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

namespace NLiterate
{
	using System;
	using System.IO;
	using System.Xml;
	using System.Diagnostics;
	using System.Text.RegularExpressions;	
	using System.Reflection;
	using NDoc.Core;
	
	/// <summary>
	/// NDoc <see cref="IDocumenter"/> that applies literate programming
	/// to comments.
	/// </summary>
	/// <remarks>
	/// </remarks>
	public class LiterateDocumenter : BaseDocumenter
	{	
		#region Private Fields
		private CodeSnippetCollection snippets;
		#endregion 
		
		/// <summary>
		/// Initializes a new instance of the class.
		/// </summary>
		public LiterateDocumenter()
		:base("Literate")
		{
			this.Clear();
		}
		
		#region IDocumenter
		/// <summary>
		/// Builds the documentation.
		/// </summary>
		public override void Build(Project project)
		{
			// step one, load snippets
			loadSnippets(project);
			// step two, compile the snippets
			compileSnippets(project);
			// step three, create the report
			compileReport(project);
			//
		}
		
		/// <summary>
		/// Resets the documenter to a clean state.
		/// </summary>
		public override void Clear()
		{
			this.Config=new LiterateDocumenterConfig();
			this.snippets=new CodeSnippetCollection();
		}

		/// <summary>
		/// Gets the development status (alpha, beta, stable) of this documenter.  
		/// </summary>
		/// <value>
		/// The development status (alpha, beta, stable) of this documenter. 
		/// </value>		
		public override DocumenterDevelopmentStatus DevelopmentStatus 
		{ 
			get
			{
				return DocumenterDevelopmentStatus.Alpha;
			}
		}
		
		/// <summary>
		/// Gets the documenter's main output file path. 
		/// </summary>
		/// <value>
		/// The Documenter's main output file path
		/// </value>
		public override string MainOutputFile
		{
			get
			{
				return ((LiterateDocumenterConfig)this.Config).OutputFile;
			}
		}
		#endregion

		#region Private implementation
		// step one, load the snippets
		private void loadSnippets(Project project)
		{
			foreach(AssemblySlashDoc ad in project.GetAssemblySlashDocs())
			{
				Assembly a  = BaseDocumenter.LoadAssembly(ad.AssemblyFilename );
				XmlDocument doc = new XmlDocument();
				doc.Load( ad.SlashDocFilename);
				
				CodeSnippet.FromXml(a.Location, doc, snippets);
			}			
		}
		
		private void compileSnippets(Project project)
		{
			CodeSnippetCompiler compiler = new CodeSnippetCompiler();
			compiler.BeginCompile+=new CodeSnippetEventHandler(compiler_BeginCompile);
			compiler.BeginRun+=new CodeSnippetEventHandler(compiler_BeginRun);
			compiler.CompileFailure+=new CodeSnippetEventHandler(compiler_CompileFailure);
			compiler.CompileSuccess+=new CodeSnippetEventHandler(compiler_CompileSuccess);
			
			foreach(CodeSnippet snippet in this.snippets)
			{
				compiler.Process(snippet);
			}			
		}
		
		
		// step three, create the report
		private void compileReport(Project project)
		{
			String path = Path.GetDirectoryName(this.MainOutputFile);
			if (path!=null && path.Length!=0)
			{
				if (!Directory.Exists(path))
					Directory.CreateDirectory(path);
			}

			using(StreamWriter writer = new StreamWriter(File.Open(this.MainOutputFile,FileMode.Create)))
			{
				foreach(CodeSnippet snippet in this.snippets)
				{
					writer.WriteLine(snippet.ToString());
				}
			}
		}

		private void compiler_BeginCompile(Object sender, CodeSnippetEventArgs e)
		{
		}

		private void compiler_BeginRun(Object sender, CodeSnippetEventArgs e)
		{

		}

		private void compiler_CompileFailure(Object sender, CodeSnippetEventArgs e)
		{

		}

		private void compiler_CompileSuccess(Object sender, CodeSnippetEventArgs e)
		{

		}
		#endregion
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions