Skip to content

Commit

Permalink
refactor: improve readability of the project
Browse files Browse the repository at this point in the history
  * refactor: remove duplicate parsing in ArgsParser

  * refactor: extract to IndexFile from Generator

  * style: fix formatting and spelling

  * refactor: rename ArgsParser to CliArgs

    The name `ArgsParser` does not represent what the class does.
    The class stored parsed values from Cli,
    and it doesn't expose any methods to work with args
  • Loading branch information
Andrewnt219 committed Oct 13, 2021
1 parent d96a7ab commit 9c07965
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 79 deletions.
61 changes: 24 additions & 37 deletions modules/cli/ArgsParser.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

using System;
using System.Collections.Generic;
using CommandLine;

namespace paper_csharp.modules.cli
{

// Init available options for CLI
class Options
public class CliArgs
{
[Option('i', "input", HelpText = "Path to file(s)", Required = true)]
public IEnumerable<string> InputPaths { get; set; }
Expand All @@ -20,46 +21,32 @@ class Options
[Option('l', "lang", HelpText = "Locale of generated .html files", Default = "en-CA")]
public string Lang { get; set; }

}

public static CliArgs Parse(string[] args)
{
CliArgs options = null;

/// <summary>
/// Represents the parsed arguments from CLI
/// </summary>
public class ArgsParser
{
/// <summary>
/// output directory
/// </summary>
public string DistDirPath { get; private set; }
/// <summary>
/// link to stylesheet
/// </summary>
public string StylesheetUrl { get; private set; }
/// <summary>
/// input directories/files
/// </summary>
public IEnumerable<string> InputPaths { get; private set; }
/// <summary>
/// The lang of generated .html files
/// </summary>
public string Lang { get; private set; }
Parser.Default.ParseArguments<CliArgs>(args)
.WithParsed(opts => options = opts)
.WithNotParsed(CliArgs.HandleParseError);

return options;
}

public ArgsParser(string[] args)
private static void HandleParseError(IEnumerable<Error> errs)
{
Parser.Default
.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
// When it's --help or --version, o.InputPaths is null
InputPaths = o.InputPaths;

StylesheetUrl = o.StylesheetUrl;
DistDirPath = o.DistDirPath;
Lang = o.Lang;
});
if (errs.IsVersion())
{
Console.WriteLine("Version Request");
return;
}

if (errs.IsHelp())
{
Console.WriteLine("Help Request");
return;
}

Console.WriteLine("Parser Fail");
}
}

}
45 changes: 5 additions & 40 deletions modules/cli/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace paper_csharp.modules.cli
public class Generator
{
// Parsed arguments from CLI
public ArgsParser Args { get; private set; }
public CliArgs Args { get; private set; }

public Generator(string[] args)
{
Args = new ArgsParser(args);
Args = CliArgs.Parse(args);
}

/// <summary>
Expand All @@ -26,7 +26,7 @@ public Generator(string[] args)
public void Run()
{
// Don't run on --help or --version
if (Args.InputPaths == null)
if (Args == null)
{
return;
}
Expand Down Expand Up @@ -94,7 +94,6 @@ private void GenerateDistFromDir(string dirPath)
return;
}


Directory.CreateDirectory(Path.Join(Args.DistDirPath, dirPath));

string[] subpaths = Directory.GetFileSystemEntries(dirPath);
Expand Down Expand Up @@ -157,47 +156,13 @@ private ParseResult ParseFile(string filePath)
return result;
}



/// <summary>
/// Create the index files with links to all html files in the output directory
/// </summary>
private void GenerateIndexFile()
{
var indexFile = File.Create(Path.Join(Args.DistDirPath, "index.html"));

var linkList = ReadAllFilePaths(Args.DistDirPath).Select(filePath => $"<a style=\"display:block\" href=\"{filePath}\">{Path.GetFileNameWithoutExtension(filePath)}</a>");

indexFile.Write(Encoding.ASCII.GetBytes(string.Join("\n", linkList)));

indexFile.Close();
return;
}

/// <summary>
/// Returns all paths to .html files
/// </summary>
private List<string> ReadAllFilePaths(string path)
{
List<string> paths = new List<string>();

foreach (string subpath in Directory.GetFileSystemEntries(path))
{
if (Directory.Exists(subpath))
{
paths.AddRange(this.ReadAllFilePaths(subpath));
continue;
}

if (File.Exists(subpath) && Path.GetExtension(subpath) == ".html" && Path.GetFileName(subpath) != "index.html")
{
paths.Add(Path.GetRelativePath(Args.DistDirPath, subpath));
continue;
}

}

return paths;
IndexFile indexFile = new IndexFile(Args.DistDirPath);
indexFile.Generate();
}
}
}
2 changes: 1 addition & 1 deletion modules/file_parser/HtmlFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public HtmlFileOptions(string stylesheetLink, string lang)
Lang = lang;
}

public HtmlFileOptions(ArgsParser args) : this(args.StylesheetUrl, args.Lang) { }
public HtmlFileOptions(CliArgs args) : this(args.StylesheetUrl, args.Lang) { }
}

/// <summary>
Expand Down
67 changes: 67 additions & 0 deletions modules/file_parser/IndexFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace paper_csharp.modules.file_parser
{

/// <summary>
/// A collection of methods to generate an index file for a given dir path
/// </summary>
public class IndexFile
{
/// <summary>
/// Path to the directory with .html files to generate index
/// </summary>
public string SourceDirPath;

public IndexFile(string sourceDirPath)
{
SourceDirPath = sourceDirPath;
}


/// <summary>
/// Create the index files with links to all html files in the output directory
/// </summary>
public void Generate()
{
var indexFile = File.Create(Path.Join(this.SourceDirPath, "index.html"));

var linkList = ReadAllFilePaths(this.SourceDirPath).Select(filePath => $"<a style=\"display:block\" href=\"{filePath}\">{Path.GetFileNameWithoutExtension(filePath)}</a>");

indexFile.Write(Encoding.ASCII.GetBytes(string.Join("\n", linkList)));

indexFile.Close();
return;
}


/// <summary>
/// Returns all paths to .html files
/// </summary>
private List<string> ReadAllFilePaths(string path)
{
List<string> paths = new List<string>();

foreach (string subpath in Directory.GetFileSystemEntries(path))
{
if (Directory.Exists(subpath))
{
paths.AddRange(this.ReadAllFilePaths(subpath));
continue;
}

if (File.Exists(subpath) && Path.GetExtension(subpath) == ".html" && Path.GetFileName(subpath) != "index.html")
{
paths.Add(Path.GetRelativePath(this.SourceDirPath, subpath));
continue;
}

}

return paths;
}
}
}
2 changes: 1 addition & 1 deletion modules/file_parser/TextFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static public ParseResult Parse(string filePath)
{
title += line;
}
// If the 2nd and 3rd lines are blank then 1st line will be a title
// If the 2nd and 3rd line are blank then 1st line will be a title
else if (string.IsNullOrWhiteSpace(line) && i <= 2)
{
blankLineCount++;
Expand Down

1 comment on commit 9c07965

@vercel
Copy link

@vercel vercel bot commented on 9c07965 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.