Skip to content

Instantly share code, notes, and snippets.

@Beej126
Last active January 18, 2017 05:43
Show Gist options
  • Save Beej126/dd7e65b360a73096252d9e12e16f5c0d to your computer and use it in GitHub Desktop.
Save Beej126/dd7e65b360a73096252d9e12e16f5c0d to your computer and use it in GitHub Desktop.
generate html page to catalog nested folders
#r "MarkdownSharp.dll"
using MarkdownSharp;
using System.Text.RegularExpressions;
using System.Text;
using System.Collections.Generic;
///////////////// SETTINGS ///////////////////////////////
var cliArgs = Environment.GetCommandLineArgs();
//base command line looks like below, "--" indicates command line args
// scripcs.exe script.csx --
if (cliArgs.Length < 5) throw new Exception("usage: arg1=root_path, arg2=output_file_path, arg3=base url");
readonly string rootPathLogical = cliArgs[3];
readonly string outputFilePath = cliArgs[4];
//readonly string baseUrl = cliArgs[5];
readonly Regex skipFiles = new Regex(@"ds_store|^\.|thumbs.db|index.htm", RegexOptions.IgnoreCase | RegexOptions.Compiled);
const int MAX_DEPTH = 3;
//////////////////////////////////////////////////////////
readonly DirectoryInfo rootDirInfo = new DirectoryInfo(rootPathLogical);
readonly string rootPath = rootDirInfo.FullName;
var markdownBuilder = new StringBuilder("# VirguleStar eBooks Catalog\r\n");
var nestedFileAndFolderList = rootDirInfo.GetFileSystemInfos(@"*.*", SearchOption.AllDirectories).OrderBy(f=>f.FullName);
var bailpath = "-1";
var previousFileName = "";
foreach (var f in nestedFileAndFolderList) {
if (f.Name.ToLower() == ".catalogBailLevel".ToLower()) bailpath = f.FullName.Replace(f.Name, "");
if (f.FullName.StartsWith(bailpath)) continue;
if (Path.GetFileNameWithoutExtension(f.Name) == previousFileName) continue;
if (skipFiles.IsMatch(f.Name)) continue;
var logicalPath = f.FullName.Replace(rootPath+@"\", "");
var nestLevel = logicalPath.Length - logicalPath.Replace(@"\", "").Length;
if (nestLevel > MAX_DEPTH) continue;
markdownBuilder.AppendLine(
String.Empty.PadLeft(nestLevel, ' ' ) +
(nestLevel == 0 ? "\r\n### " : "* ") +
(f is DirectoryInfo ? $"[{f.Name}]({System.Uri.EscapeUriString(logicalPath.Replace(@"\","/"))/*.Replace("&","%26")*/})" : f.Name)
);
previousFileName = Path.GetFileNameWithoutExtension(f.Name);
}
var mdStr = markdownBuilder.ToString();
//Console.WriteLine(mdStr);
File.WriteAllText($"{outputFilePath}", (new Markdown()).Transform(mdStr));
Console.WriteLine($"successfully wrote new file: {outputFilePath}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment