Click or drag to resize

Highlighting class names

The instructions on this page are based on sample code that demonstrates how to manage the HighlightingTools, and how to use them to highlight class names in code examples included in SHFB documentation topics.

The sample is located here.

It includes what follows.

Managing the Highlighting Tools

Install the target version of SHFB

Each release of Novacta.Documentation.ShfbTools targets a specific version of SHFB. Such target version always matches the version of the tools.

Make sure that the SHFB version targeted by the tools is currently installed on your host machine.

You can download the required SHFB release here.

Manage the Highlighting Tools

Create a console application targeting the .NET Framework, version 4.6.2. In this tutorial, such application is represented by project HighlightingToolsManager.

Add to your console project a reference to the Novacta.Documentation.ShfbTools NuGet package. Remember that, at present, the tools are versioned as pre-release software. Follow these instructions to include pre-release versions when installing the tools.

Request full administration permissions for the console application: add to the project an app.manifest file as follows.

app.manifest
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
    </application>
  </compatibility>
</assembly>

In your Program class, implement method Main as follows.

HighlightingToolsManager\Program.cs
using Novacta.Documentation.ShfbTools;
using System;
using System.Collections.Generic;

namespace HighlightingToolsManager
{
    class Program
    {
        static void Main(string[] args)
        {
            // It is assumed that the version of SHFB
            // targeted by the SHFB Tools
            // is installed on the host machine.

            // To highlight specific class names,
            // execute the following steps, A to B.

            // A. Set a color to be
            // applied when highlighting.
            // See the documentation for a list of 
            // supported colors.
            HighlightingTools.SetClassNamesColor("MediumAquaMarine");

            // B. Add the class names to be highlighted.

            // B.1 In SHFB, class names are grouped by
            // an identifier referred to as a "family".
            string family = "sampleFamily";

            // B.2 List the class names.
            List<string> names = new List<string>() {
                "IntegerOperation",
                "IntegerArrayOperation"
            };

            // B.3 In SHFB, keyword families are linked to
            // languages.
            // List the languages for which you want
            // the class names highlighted.
            // See the documentation for a list of 
            // supported languages.
            List<string> languages = new List<string>() {
                "cs",
                "vbnet"
            };

            // B.4 Update your SHFB installation so that
            // your class names are recognized as a family
            // of keywords for the specified languages.
            HighlightingTools.AddClassNamesFamily(
                family,
                names,
                languages);

            // To remove a family of class names,
            // so that it will no longer be highlighted,
            // uncomment the following line.
            //HighlightingTools.RemoveClassNamesFamily(family);

            Console.ReadKey();
        }
    }
}

This example shows standard use case scenarios:

These methods have console outputs showing reports in which is signaled if the corresponding operations were successful. If failure conditions are reported, please act to remove them and try again.

As an example, the following file printout shows the source code for type IntegerOperation, as defined in project SampleClassLibrary:

SampleClassLibrary\IntegerOperation.cs
 1using System;
 2
 3namespace SampleClassLibrary
 4{
 5    /// <summary>
 6    /// Provides a method to operate on integers.
 7    /// </summary>
 8    public static class IntegerOperation
 9    {
10        /// <summary>
11        /// Applies the specified function to the given operand.
12        /// </summary>
13        /// <param name="func">The function.</param>
14        /// <param name="operand">The operand.</param>
15        /// <returns>The result of the operation.</returns>
16        /// <exception cref="ArgumentNullException">
17        /// <paramref name="func"/> is <b>null</b>.</exception>
18        /// <example>
19        /// <para>
20        /// In the following example, the applied function, say 
21        /// <latex mode='inline'>f:\mathbb{N}\rightarrow \mathbb{N},</latex> is defined as
22        /// <latex mode='display'>
23        /// \forall n \in \mathbb{N}: n \mapsto f\left(n\right)=n^2.
24        /// </latex>
25        /// A plot of such function is displayed in the following
26        /// figure.
27        /// <image>
28        /// <alt>A function.</alt>
29        /// <src>Function.png</src>
30        /// <width>100%</width>
31        /// </image>
32        /// An integer is thus squared 
33        /// executing the <see cref="Operate(Func{int, int}, int)"/> method.
34        /// In addition, input validation is also checked.
35        /// </para>
36        /// <para>
37        /// <code language="cs">
38        /// using System;
39        /// namespace SampleClassLibrary.CodeExamples
40        /// {
41        ///     public class IntegerOperationExample
42        ///     {
43        ///         public void Main()
44        ///         {
45        ///             // Define an operator that squares its operand
46        ///             Func<![CDATA[<]]>int, int> square = (int operand) => operand * operand;
47        /// 
48        ///             // Define an operand
49        ///             int integer = 2;
50        /// 
51        ///             // Operate on it
52        ///             Console.WriteLine("Squaring {0}...", integer);
53        ///             int result = IntegerOperation.Operate(square, integer);
54        ///             Console.WriteLine("...the result is {0}.", result);
55        /// 
56        ///             // Check that an operator cannot be null
57        ///             try
58        ///             {
59        ///                 IntegerOperation.Operate(null, 0);
60        ///             }
61        ///             catch (Exception e)
62        ///             {
63        ///                 Console.WriteLine();
64        ///                 Console.WriteLine("Cannot apply a null function:");
65        ///                 Console.WriteLine(e.Message);
66        ///             }
67        ///         }
68        ///     }
69        /// }
70        ///  
71        /// // Executing method Main() produces the following output:
72        /// // 
73        /// // Squaring 2...
74        /// // ...the result is 4.
75        /// // 
76        /// // Cannot apply a null function:
77        /// // Value cannot be null.
78        /// // Parameter name: func
79        /// </code>
80        /// </para>
81        /// </example>
82        public static int Operate(Func<int, int> func, int operand)
83        {
84            if (func==null)
85            {
86                throw new ArgumentNullException(nameof(func));
87            }
88            return func(operand);
89        }
90    }
91}

Note how the IntegerOperation class name at line 8 has been highlighted.