Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List all headings and build a hierarchy of the headings inside a markdown file #63

Merged
merged 6 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/DocNet/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ internal bool Load(string configFile)
/// Generates the search data, which is the json file called 'search_index.json' with search data of all pages as well as the docnet_search.htm file in the output.
/// The search index is written to the root of the output folder.
/// </summary>
internal void GenerateSearchData()
/// <param name="navigationContext">The navigation context.</param>
internal void GenerateSearchData(NavigationContext navigationContext)
{
GenerateSearchPage();
GenerateSearchDataIndex();
GenerateSearchPage(navigationContext);
GenerateSearchDataIndex(navigationContext);
}

internal void CopyThemeToDestination()
Expand Down Expand Up @@ -128,10 +129,10 @@ internal void ClearDestinationFolder()
/// <summary>
/// Generates the index of the search data. this is a json file with per page which has markdown a couple of data elements.
/// </summary>
private void GenerateSearchDataIndex()
private void GenerateSearchDataIndex(NavigationContext navigationContext)
{
var collectedSearchEntries = new List<SearchIndexEntry>();
this.Pages.CollectSearchIndexEntries(collectedSearchEntries, new NavigatedPath(), this.PathSpecification);
this.Pages.CollectSearchIndexEntries(collectedSearchEntries, new NavigatedPath(), navigationContext);
JObject searchIndex = new JObject(new JProperty("docs",
new JArray(
collectedSearchEntries.Select(e=>new JObject(
Expand All @@ -145,7 +146,7 @@ private void GenerateSearchDataIndex()
}


private void GenerateSearchPage()
private void GenerateSearchPage(NavigationContext navigationContext)
{
var activePath = new NavigatedPath();
activePath.Push(this.Pages);
Expand All @@ -164,7 +165,7 @@ private void GenerateSearchPage()
searchSimpleElement.ExtraScriptProducerFunc = e=> @"
<script>var base_url = '.';</script>
<script data-main=""js/search.js"" src=""js/require.js""></script>";
searchSimpleElement.GenerateOutput(this, activePath, this.PathSpecification);
searchSimpleElement.GenerateOutput(this, activePath, navigationContext);
activePath.Pop();
}

Expand Down Expand Up @@ -216,6 +217,14 @@ public bool ConvertLocalLinks
}
}

public int MaxLevelInToC
{
get
{
return _configData.MaxLevelInToC ?? 2;
}
}

public string ThemeName
{
get
Expand Down
1 change: 1 addition & 0 deletions src/DocNet/Docnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Engine.cs" />
<Compile Include="INavigationElement.cs" />
<Compile Include="NavigatedPath.cs" />
<Compile Include="NavigationContext.cs" />
<Compile Include="NavigationElement.cs" />
<Compile Include="NavigationLevel.cs" />
<Compile Include="PathSpecification.cs" />
Expand Down
15 changes: 11 additions & 4 deletions src/DocNet/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ public int DoWork()
{
return 1;
}
GeneratePages();

var navigationContext = new NavigationContext
{
MaxLevel = _loadedConfig.MaxLevelInToC,
PathSpecification = _loadedConfig.PathSpecification
};

GeneratePages(navigationContext);
return 0;
}

Expand Down Expand Up @@ -86,7 +93,7 @@ public Config LoadConfig()
/// Generates the pages from the md files in the source, using the page template loaded and the loaded config.
/// </summary>
/// <returns>true if everything went ok, false otherwise</returns>
private void GeneratePages()
private void GeneratePages(NavigationContext navigationContext)
{
if(_input.ClearDestinationFolder)
{
Expand All @@ -98,9 +105,9 @@ private void GeneratePages()
Console.WriteLine("Copying source folders to copy.");
_loadedConfig.CopySourceFoldersToCopy();
Console.WriteLine("Generating pages in '{0}'", _loadedConfig.Destination);
_loadedConfig.Pages.GenerateOutput(_loadedConfig, new NavigatedPath(), _loadedConfig.PathSpecification);
_loadedConfig.Pages.GenerateOutput(_loadedConfig, new NavigatedPath(), navigationContext);
Console.WriteLine("Generating search index");
_loadedConfig.GenerateSearchData();
_loadedConfig.GenerateSearchData(navigationContext);
Console.WriteLine("Done!");
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/DocNet/INavigationElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ public interface INavigationElement
/// </summary>
/// <param name="activeConfig">The active configuration to use for the output.</param>
/// <param name="activePath">The active path navigated through the ToC to reach this element.</param>
/// <param name="pathSpecification">The path specification.</param>
void GenerateOutput(Config activeConfig, NavigatedPath activePath, PathSpecification pathSpecification);
/// <param name="navigationContext">The navigation context.</param>
void GenerateOutput(Config activeConfig, NavigatedPath activePath, NavigationContext navigationContext);

/// <summary>
/// Generates the ToC fragment for this element, which can either be a simple line or a full expanded menu.
/// </summary>
/// <param name="navigatedPath">The navigated path to the current element, which doesn't necessarily have to be this element.</param>
/// <param name="relativePathToRoot">The relative path back to the URL root, e.g. ../.., so it can be used for links to elements in this path.</param>
/// <param name="pathSpecification">The path specification.</param>
/// <param name="navigationContext">The navigation context.</param>
/// <returns></returns>
string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, PathSpecification pathSpecification);
string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, NavigationContext navigationContext);
/// <summary>
/// Collects the search index entries. These are created from simple navigation elements found in this container, which aren't index element.
/// </summary>
/// <param name="collectedEntries">The collected entries.</param>
/// <param name="activePath">The active path currently navigated.</param>
/// <param name="pathSpecification">The path specification.</param>
void CollectSearchIndexEntries(List<SearchIndexEntry> collectedEntries, NavigatedPath activePath, PathSpecification pathSpecification);
/// <param name="navigationContext">The navigation context.</param>
void CollectSearchIndexEntries(List<SearchIndexEntry> collectedEntries, NavigatedPath activePath, NavigationContext navigationContext);

/// <summary>
/// Gets the target URL with respect to the <see cref="PathSpecification"/>.
Expand Down
6 changes: 3 additions & 3 deletions src/DocNet/NavigatedPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public string CreateBreadCrumbsText(string relativePathToRoot)
/// aren't, are not expanded.
/// </summary>
/// <param name="relativePathToRoot">The relative path back to the URL root, e.g. ../.., so it can be used for links to elements in this path.</param>
/// <param name="pathSpecification">The path specification.</param>
/// <param name="navigationContext">The navigation context.</param>
/// <returns></returns>
public string CreateToCHTML(string relativePathToRoot, PathSpecification pathSpecification)
public string CreateToCHTML(string relativePathToRoot, NavigationContext navigationContext)
{
// the root container is the bottom element of this path. We use that container to build the root and navigate any node open along the navigated path.
var rootContainer = this.Reverse().FirstOrDefault() as NavigationLevel;
Expand All @@ -88,7 +88,7 @@ public string CreateToCHTML(string relativePathToRoot, PathSpecification pathSpe
// no root container, no TOC
return string.Empty;
}
return rootContainer.GenerateToCFragment(this, relativePathToRoot, pathSpecification);
return rootContainer.GenerateToCFragment(this, relativePathToRoot, navigationContext);
}
}
}
21 changes: 21 additions & 0 deletions src/DocNet/NavigationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Docnet
{
public class NavigationContext
{
public NavigationContext()
{
MaxLevel = 2;
}

public NavigationContext(PathSpecification pathSpecification, int maxLevel)
: this()
{
PathSpecification = pathSpecification;
MaxLevel = maxLevel;
}

public int MaxLevel { get; set; }

public PathSpecification PathSpecification { get; set; }
}
}
12 changes: 6 additions & 6 deletions src/DocNet/NavigationElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ public abstract class NavigationElement<T> : INavigationElement
/// </summary>
/// <param name="activeConfig">The active configuration to use for the output.</param>
/// <param name="activePath">The active path navigated through the ToC to reach this element.</param>
/// <param name="pathSpecification">The path specification.</param>
public abstract void GenerateOutput(Config activeConfig, NavigatedPath activePath, PathSpecification pathSpecification);
/// <param name="navigationContext">The navigation context.</param>
public abstract void GenerateOutput(Config activeConfig, NavigatedPath activePath, NavigationContext navigationContext);
/// <summary>
/// Generates the ToC fragment for this element, which can either be a simple line or a full expanded menu.
/// </summary>
/// <param name="navigatedPath">The navigated path to the current element, which doesn't necessarily have to be this element.</param>
/// <param name="relativePathToRoot">The relative path back to the URL root, e.g. ../.., so it can be used for links to elements in this path.</param>
/// <param name="pathSpecification">The path specification.</param>
/// <param name="navigationContext">The navigation context.</param>
/// <returns></returns>
public abstract string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, PathSpecification pathSpecification);
public abstract string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, NavigationContext navigationContext);
/// <summary>
/// Collects the search index entries. These are created from simple navigation elements found in this container, which aren't index element.
/// </summary>
/// <param name="collectedEntries">The collected entries.</param>
/// <param name="activePath">The active path currently navigated.</param>
/// <param name="pathSpecification">The path specification.</param>
public abstract void CollectSearchIndexEntries(List<SearchIndexEntry> collectedEntries, NavigatedPath activePath, PathSpecification pathSpecification);
/// <param name="navigationContext">The navigation context.</param>
public abstract void CollectSearchIndexEntries(List<SearchIndexEntry> collectedEntries, NavigatedPath activePath, NavigationContext navigationContext);

/// <summary>
/// Gets the target URL with respect to the <see cref="PathSpecification"/>.
Expand Down
26 changes: 13 additions & 13 deletions src/DocNet/NavigationLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public void Load(JObject dataFromFile)
/// </summary>
/// <param name="collectedEntries">The collected entries.</param>
/// <param name="activePath">The active path currently navigated.</param>
/// <param name="pathSpecification">The path specification.</param>
public override void CollectSearchIndexEntries(List<SearchIndexEntry> collectedEntries, NavigatedPath activePath, PathSpecification pathSpecification)
/// <param name="navigationContext">The navigation context.</param>
public override void CollectSearchIndexEntries(List<SearchIndexEntry> collectedEntries, NavigatedPath activePath, NavigationContext navigationContext)
{
activePath.Push(this);
foreach (var element in this.Value)
{
element.CollectSearchIndexEntries(collectedEntries, activePath, pathSpecification);
element.CollectSearchIndexEntries(collectedEntries, activePath, navigationContext);
}
activePath.Pop();
}
Expand All @@ -123,15 +123,15 @@ public override void CollectSearchIndexEntries(List<SearchIndexEntry> collectedE
/// </summary>
/// <param name="activeConfig">The active configuration to use for the output.</param>
/// <param name="activePath">The active path navigated through the ToC to reach this element.</param>
/// <param name="pathSpecification">The path specification.</param>
public override void GenerateOutput(Config activeConfig, NavigatedPath activePath, PathSpecification pathSpecification)
/// <param name="navigationContext">The navigation context.</param>
public override void GenerateOutput(Config activeConfig, NavigatedPath activePath, NavigationContext navigationContext)
{
activePath.Push(this);
int i = 0;
while (i < this.Value.Count)
{
var element = this.Value[i];
element.GenerateOutput(activeConfig, activePath, pathSpecification);
element.GenerateOutput(activeConfig, activePath, navigationContext);
i++;
}
activePath.Pop();
Expand All @@ -143,9 +143,9 @@ public override void GenerateOutput(Config activeConfig, NavigatedPath activePat
/// </summary>
/// <param name="navigatedPath">The navigated path to the current element, which doesn't necessarily have to be this element.</param>
/// <param name="relativePathToRoot">The relative path back to the URL root, e.g. ../.., so it can be used for links to elements in this path.</param>
/// <param name="pathSpecification">The path specification.</param>
/// <param name="navigationContext">The navigation context.</param>
/// <returns></returns>
public override string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, PathSpecification pathSpecification)
public override string GenerateToCFragment(NavigatedPath navigatedPath, string relativePathToRoot, NavigationContext navigationContext)
{
var fragments = new List<string>();
if (!this.IsRoot)
Expand All @@ -167,7 +167,7 @@ public override string GenerateToCFragment(NavigatedPath navigatedPath, string r

// first render the level header, which is the index element, if present or a label. The root always has an __index element otherwise we'd have stopped at load.
var elementStartTag = "<li><span class=\"navigationgroup\"><i class=\"fa fa-caret-down\"></i> ";
var indexElement = this.GetIndexElement(pathSpecification);
var indexElement = this.GetIndexElement(navigationContext.PathSpecification);
if (indexElement == null)
{
fragments.Add(string.Format("{0}{1}</span></li>", elementStartTag, this.Name));
Expand All @@ -176,26 +176,26 @@ public override string GenerateToCFragment(NavigatedPath navigatedPath, string r
{
if (this.IsRoot)
{
fragments.Add(indexElement.PerformGenerateToCFragment(navigatedPath, relativePathToRoot, pathSpecification));
fragments.Add(indexElement.PerformGenerateToCFragment(navigatedPath, relativePathToRoot, navigationContext));
}
else
{
fragments.Add(string.Format("{0}<a href=\"{1}{2}\">{3}</a></span></li>",
elementStartTag, relativePathToRoot, indexElement.GetFinalTargetUrl(pathSpecification), this.Name));
elementStartTag, relativePathToRoot, indexElement.GetFinalTargetUrl(navigationContext.PathSpecification), this.Name));
}
}
// then the elements in the container. Index elements are skipped here.
foreach (var element in this.Value)
{
fragments.Add(element.GenerateToCFragment(navigatedPath, relativePathToRoot, pathSpecification));
fragments.Add(element.GenerateToCFragment(navigatedPath, relativePathToRoot, navigationContext));
}
fragments.Add("</ul>");
}
else
{
// just a link
fragments.Add(string.Format("<span class=\"navigationgroup\"><i class=\"fa fa-caret-right\"></i> <a href=\"{0}{1}\">{2}</a></span>",
relativePathToRoot, this.GetFinalTargetUrl(pathSpecification), this.Name));
relativePathToRoot, this.GetFinalTargetUrl(navigationContext.PathSpecification), this.Name));
}
if (!this.IsRoot)
{
Expand Down
Loading