Click here to Skip to main content
15,881,882 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 122.8K   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.

using System;
using System.Xml;
using System.Reflection;

namespace NLiterate.Cons
{
class MainClass
{
	public static void Main(string[] args)
	{
		Console.WriteLine("-- loading file ---");

		try
		{
			Console.WriteLine("NLiterate, code examples compiler");
			Console.WriteLine("Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2004");
			Console.WriteLine("---------------------------------------------------");
			Console.WriteLine();
			Console.WriteLine("Call convention: nliterate xmldoc.xml");
			Console.WriteLine("where xmldoc.xml is the XML generated documentation file");
			Console.WriteLine();
			Console.WriteLine("In your documentation, create reference using {[id blabla]} pattern:");
			Console.WriteLine("where id must be a valid id of the code section to substiture");
			Console.WriteLine();

			XmlDocument doc = new XmlDocument();
			doc.Load(args[0]);

			// get assembly name
			XmlElement an = (XmlElement)doc.SelectSingleNode("doc/assembly/name");
			if (an==null)
				throw new ArgumentException("doc/assembly/name missing from the XML file");


			CodeSnippetCollection snippets = new CodeSnippetCollection();
			CodeSnippet.FromXml(
				an.InnerText+".dll",
				doc,
				snippets);
			
			CodeSnippetCompiler comp = new CodeSnippetCompiler();
			comp.BeginCompile+=new CodeSnippetEventHandler(comp_BeginCompile);
			comp.CompileFailure+=new CodeSnippetEventHandler(comp_CompileFailure);
			comp.CompileSuccess+=new CodeSnippetEventHandler(comp_CompileSuccess);
			foreach(CodeSnippet snippet in snippets)
			{
				comp.Process(snippet);
			}			
		}
		catch(Exception ex)
		{
			Console.WriteLine(ex.ToString());
		}
	}

	private static void comp_BeginCompile(object sender, CodeSnippetEventArgs e)
	{
		Console.WriteLine("[Compiling snippet]\n{0}",e.Snippet.ToString());
	}

	private static void comp_CompileFailure(object sender, CodeSnippetEventArgs e)
	{
		Console.WriteLine("[Compilation failed]\n{0}",e.Snippet.Results.ToString());
	}

	private static void comp_CompileSuccess(object sender, CodeSnippetEventArgs e)
	{
		Console.WriteLine("[Compilation success]");
	}
}
}

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