Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
* Added relative url generation, removing are the need for overriding…
Browse files Browse the repository at this point in the history
… the format urls.

* Added attribute output to Format calls
* Updated views to all use Format for their urls, instead of inline generation
* Added mspec specs (although won't be executed in CI yet)
  • Loading branch information
jagregory authored and forki committed Jul 7, 2010
1 parent 9d4cb9a commit dde10f3
Show file tree
Hide file tree
Showing 22 changed files with 567 additions and 211 deletions.
3 changes: 2 additions & 1 deletion src/Docu.Console/Output/HtmlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public HtmlGenerator(IEnumerable<KeyValuePair<string, string>> templates)
engine.ViewFolder = viewFolder;
}

public string Convert(string templateName, ViewData data)
public string Convert(string templateName, ViewData data, string relativeOutputPath)
{
string template = templateName;

Expand All @@ -53,6 +53,7 @@ public string Convert(string templateName, ViewData data)
{
try
{
view.RelativeOutputPath = relativeOutputPath;
view.ViewData = data;
view.RenderView(writer);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Docu.Console/Output/IOutputGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Docu.Output
{
public interface IOutputGenerator
{
string Convert(string templateName, ViewData data);
string Convert(string templateName, ViewData data, string relativeOutputPath);
void SetTemplatePath(string path);
}
}
4 changes: 3 additions & 1 deletion src/Docu.Console/Output/PageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public void CreatePages(string templateDirectory, string destination, IList<Name

foreach (var path in paths)
{
var output = generator.Convert(path.TemplatePath, path.Data);
var outputDir = Path.GetDirectoryName(path.OutputPath);
var outputPath = path.OutputPath;

Expand All @@ -42,6 +41,9 @@ public void CreatePages(string templateDirectory, string destination, IList<Name
outputPath = Path.Combine(outputDir, outputFilename);
}

var relativePath = outputPath.Replace(destination + "\\", "");
var output = generator.Convert(path.TemplatePath, path.Data, relativePath);

if (outputDir != "" && !writer.Exists(outputDir))
writer.CreateDirectory(outputDir);

Expand Down
30 changes: 20 additions & 10 deletions src/Docu.Console/Output/Rendering/HtmlOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ namespace Docu.Output.Rendering
public class HtmlOutputFormatter : IOutputFormatter
{
private readonly IList<IOutputFormatterPart> Formatters;
private readonly IDocuTemplate view;

public HtmlOutputFormatter()
public HtmlOutputFormatter(IDocuTemplate view)
{
this.view = view;

Formatters = new List<IOutputFormatterPart>
{
new OutputFormatterPart<Summary>(FormatGeneralContainer),
Expand All @@ -28,12 +31,12 @@ public HtmlOutputFormatter()
new OutputFormatterPart<TableList>(FormatTableList),
};

NamespaceUrlFormat = "{namespace}.htm";
TypeUrlFormat = "{type.namespace}/{type}.htm";
MethodUrlFormat = "{type.namespace}/{type}.htm#{method}";
PropertyUrlFormat = "{type.namespace}/{type}.htm#{property}";
FieldUrlFormat = "{type.namespace}/{type}.htm#{field}";
EventUrlFormat = "{type.namespace}/{type}.htm#{event}";
NamespaceUrlFormat = "~/{namespace}/index.htm";
TypeUrlFormat = "~/{type.namespace}/{type}.htm";
MethodUrlFormat = "~/{type.namespace}/{type}.htm#{method}";
PropertyUrlFormat = "~/{type.namespace}/{type}.htm#{property}";
FieldUrlFormat = "~/{type.namespace}/{type}.htm#{field}";
EventUrlFormat = "~/{type.namespace}/{type}.htm#{event}";
}

public string Format(IComment comment)
Expand Down Expand Up @@ -113,14 +116,17 @@ private string FormatBulletList(BulletList comment)
return formatInlineList(comment.Items, "<ul>{0}</ul>", "<li>{0}{1}</li>", "{0}", "{0}");
}



private string FormatSee(See block)
{
return FormatReferencable(block.Reference);
}

public string FormatReferencable(IReferencable reference)
{
return FormatReferencable(reference, new KeyValuePair<string, string>[0]);
}

public string FormatReferencable(IReferencable reference, IEnumerable<KeyValuePair<string, string>> attributes)
{
string url = "";
string name = reference.PrettyName;
Expand Down Expand Up @@ -171,7 +177,11 @@ public string FormatReferencable(IReferencable reference)
if (reference.IsExternal)
return "<span title=\"" + reference.FullName + "\">" + Escape(reference.PrettyName) + "</span>";

return "<a href=\"" + url + "\">" + Escape(name) + "</a>";
var attributeHtml = "";

attributes.ForEach(x => attributeHtml += " " + x.Key + "=\"" + x.Value + "\"");

return "<a href=\"" + view.SiteResource(url) + "\"" + attributeHtml + ">" + Escape(name) + "</a>";
}

public string Escape(string value)
Expand Down
1 change: 1 addition & 0 deletions src/Docu.Console/Output/Rendering/IOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IOutputFormatter
string EventUrlFormat { get; set; }
string Format(IComment comment);
string FormatReferencable(IReferencable referencable);
string FormatReferencable(IReferencable referencable, IEnumerable<KeyValuePair<string, string>> attributes);
string Escape(string value);
}
}
69 changes: 64 additions & 5 deletions src/Docu.Console/Output/Rendering/SparkTemplateBase.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using Docu.Documentation;
using Docu.Documentation.Comments;
using Docu.Output;
using Spark;

namespace Docu.Output.Rendering
{
public interface IDocuTemplate
{
string SiteResource(string path);
}

/// <summary>
/// All public or protected methods and properties on this class are available within documentation templates
/// </summary>
public abstract class SparkTemplateBase : AbstractSparkView
public abstract class SparkTemplateBase : AbstractSparkView, IDocuTemplate
{
protected readonly IOutputFormatter Formatter = new HtmlOutputFormatter();
protected readonly IOutputFormatter Formatter;

public SparkTemplateBase()
{
Formatter = new HtmlOutputFormatter(this);
}

public string SiteResource(string path)
{
if (!path.StartsWith("~/")) return path;

var formattedPath = path.Replace("~/", "");
var outputPathDepth = (RelativeOutputPath ?? "").GetDirectoryDepth();

return outputPathDepth.Times("../") + formattedPath;
}

/// <summary>
/// All of the assemblies being documented
Expand Down Expand Up @@ -59,6 +80,11 @@ public DeclaredType Type

public ViewData ViewData { get; set; }

/// <summary>
/// The path of the file that this view represents, relative to the output directory.
/// </summary>
public string RelativeOutputPath { get; set; }

/// <summary>
/// Configures the pattern that will be used to construct links to methods referenced in the documentation of other symbols
/// </summary>
Expand Down Expand Up @@ -235,9 +261,19 @@ public string Format(IComment comment)
/// <remarks>The format of the URL in the returned hyperlink can be controlled by the methods <see cref="SetNamespaceUrlFormat"/>, <see cref="SetTypeUrlFormat"/>, <see cref="SetPropertyUrlFormat"/>, <see cref="SetMethodUrlFormat"/>, <see cref="SetFieldUrlFormat"/> and <see cref="SetEventUrlFormat"/></remarks>
/// <param name="referencable"></param>
/// <returns></returns>
public string Format(IReferencable referencable)
public string Format(IReferencable referencable, params Expression<Func<object, string>>[] attributes)
{
return Formatter.FormatReferencable(referencable);
var attributeDictionary = new Dictionary<string, string>();

attributes.ForEach(exp =>
{
var name = exp.Parameters.First().Name;
var value = ((ConstantExpression)exp.Body).Value.ToString();
attributeDictionary.Add(name, value);
});

return Formatter.FormatReferencable(referencable, attributeDictionary);
}

/// <summary>
Expand Down Expand Up @@ -290,4 +326,27 @@ public string OutputMethodParams(Method method)
return sb.ToString();
}
}

public static class StringPathExtensions
{
public static bool IsRoot(this string path)
{
return !path.Contains("\\");
}

public static int GetDirectoryDepth(this string path)
{
return path.Count(x => x == '\\');
}

public static string Times(this int times, string value)
{
var result = "";

for (var i = 0; i < times; i++)
result += value;

return result;
}
}
}
17 changes: 5 additions & 12 deletions src/Docu.Console/templates/!namespace/!type.htm.spark
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
# // overrides for paths - this should be handled better
# Formatter.NamespaceUrlFormat = "../{namespace}.htm";
# Formatter.TypeUrlFormat = "../{type.namespace}/{type}.htm";
# Formatter.MethodUrlFormat = "../{type.namespace}/{type}.htm#{method}";
# Formatter.PropertyUrlFormat = "../{type.namespace}/{type}.htm#{property}";
# Formatter.FieldUrlFormat = "../{type.namespace}/{type}.htm#{field}";
# Formatter.EventUrlFormat = "../{type.namespace}/{type}.htm#{event}";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>${h(Type.PrettyName)} - ${WriteProductName(Assemblies[0])} Documentation</title>
Expand All @@ -32,28 +25,28 @@
<if condition="Type.Events.Count > 0">
<h3 class="section">Events</h3>
<ul>
<li each="var ev in Type.Events"><a href="#${ev.Name}" class="type">${h(ev.Name)}</a></li>
<li each="var ev in Type.Events">${Format(ev)}</li>
</ul>
</if>

<if condition="Type.Methods.Count > 0">
<h3 class="section">Methods</h3>
<ul>
<li each="var method in Type.Methods"><a href="#${method.Name}" class="type">${h(method.PrettyName)}</a></li>
<li each="var method in Type.Methods">${Format(method)}</li>
</ul>
</if>

<if condition="Type.Properties.Count > 0">
<h3 class="section">Properties</h3>
<ul>
<li each="var property in Type.Properties"><a href="#${property.Name}" class="type">${h(property.Name)}</a></li>
<li each="var property in Type.Properties">${Format(property)}</li>
</ul>
</if>

<if condition="Type.Fields.Count > 0">
<h3 class="section">Fields</h3>
<ul>
<li each="var field in Type.Fields"><a href="#${field.Name}" class="type">${h(field.Name)}</a></li>
<li each="var field in Type.Fields">${Format(field)}</li>
</ul>
</if>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/Docu.Console/templates/!namespace/_namespaces.spark
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<ul>
<li each="var ns in Namespaces">
<if condition="ns == Namespace">
<a href="../${ns.Name}/index.htm" class="current">${ns.Name}</a>
<else />
<a href="../${ns.Name}/index.htm">${ns.Name}</a>
${Format(ns, class => "current")}
<else />
${Format(ns)}
</if>
</li>
</ul>
Expand Down
6 changes: 3 additions & 3 deletions src/Docu.Console/templates/!namespace/_types.spark
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<ul>
<li each="var type in Namespace.Types">
<if condition="type == Type">
<a href="../${type.Namespace.Name}/${type.Name}.htm" class="current">${h(type.PrettyName)}</a>
<else />
<a href="../${type.Namespace.Name}/${type.Name}.htm">${h(type.PrettyName)}</a>
${Format(type, class => "current")}
<else />
${Format(type)}
</if>
</li>
</ul>
Expand Down
4 changes: 2 additions & 2 deletions src/Docu.Console/templates/!namespace/index.htm.spark
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h3 class="section">Classes</h3>
<ul>
<for each="var type in Namespace.Classes">
<li><a href="${type.Name}.htm">${h(type.PrettyName)}</a></li>
<li>${Format(type)}</li>
</for>
</ul>
</if>
Expand All @@ -25,7 +25,7 @@
<h3 class="section">Interfaces</h3>
<ul>
<for each="var type in Namespace.Interfaces">
<li><a href="${type.Name}.htm">${h(type.PrettyName)}</a></li>
<li>${Format(type)}</li>
</for>
</ul>
</if>
Expand Down
2 changes: 1 addition & 1 deletion src/Docu.Console/templates/_namespaces.spark
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h2 class="fixed">Namespaces</h2>
<div class="scroll">
<ul>
<li each="var ns in Namespaces"><a href="${ns.Name}/index.htm">${ns.Name}</a></li>
<li each="var ns in Namespaces">${Format(ns)}</li>
</ul>
</div>
</div>
2 changes: 1 addition & 1 deletion src/Docu.Console/templates/_types.spark
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="scroll">
<ul>
<for each="var ns in Namespaces">
<li each="var type in ns.Types"><a href="${ns.Name}/${type.Name}.htm">${h(type.PrettyName)}</a></li>
<li each="var type in ns.Types">${Format(type)}</li>
</for>
</ul>
</div>
Expand Down
16 changes: 14 additions & 2 deletions src/Docu.Tests/Docu.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{76E3611F-A908-4A1B-8673-A221DD8C5301}</ProjectGuid>
<OutputType>Library</OutputType>
Expand Down Expand Up @@ -31,6 +31,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Machine.Specifications, Version=0.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\mspec\Machine.Specifications.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.5.0.8332, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\nunit\nunit.framework.dll</HintPath>
Expand All @@ -39,6 +43,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\rhino\Rhino.Mocks.dll</HintPath>
</Reference>
<Reference Include="Spark, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7f8549eed921a12c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\spark\Spark.dll</HintPath>
</Reference>
<Reference Include="StructureMap, Version=2.5.3.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\structuremap\StructureMap.dll</HintPath>
Expand Down Expand Up @@ -85,10 +93,14 @@
<Compile Include="Output\PatternTemplateResolverTests\single_template.cs" />
<Compile Include="Output\PatternTemplateResolverTests\type_template_in_namespace.cs" />
<Compile Include="Output\PatternTemplateResolverTests\type_template.cs" />
<Compile Include="Output\Rendering\HtmlOutputFormatter_Referencable_Specs.cs" />
<Compile Include="Output\SparkTemplateBaseSpecs.cs" />
<Compile Include="Output\TemplateTransformerTests.cs" />
<Compile Include="Parsing\IdentifierTests.cs" />
<Compile Include="ReflectionHelper.cs" />
<Compile Include="Output\Rendering\HtmlOutputFormatterTests.cs" />
<Compile Include="Output\Rendering\HtmlOutputFormatterSpecs.cs" />
<Compile Include="Utils\Mock.cs" />
<Compile Include="Utils\Stub.cs" />
<Compile Include="Utils\TestTemplateAttribute.cs" />
<Compile Include="Output\UntransformableResourceManagerTests.cs" />
<Compile Include="Output\UntransformableResourceManagerTestsBase.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Docu.Tests/Output/HtmlGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private string Convert(ViewData data)
var callStack = new StackTrace();
var caller = callStack.GetFrame(1);

return generator.Convert(caller.GetMethod().Name, data);
return generator.Convert(caller.GetMethod().Name, data, "");
}

[TestTemplate("${Assemblies[0].FullName}")]
Expand Down
Loading

0 comments on commit dde10f3

Please sign in to comment.