Skip to content

Commit

Permalink
feat: support static images (#8)
Browse files Browse the repository at this point in the history
* parse markdown img

  * add class MarkdownParser

  * parse markdown img

* copy static files to dist

  * add CreateDirForce

  * add DirectoryCopy

  * add CreateDirSoft

  * copy static files

* rewrite staitc files src

* show full path in index file

* update build
  • Loading branch information
Andrewnt219 committed Oct 28, 2021
1 parent 9c07965 commit 99ad730
Show file tree
Hide file tree
Showing 25 changed files with 6,194 additions and 25 deletions.
16 changes: 11 additions & 5 deletions build/index.html
@@ -1,5 +1,11 @@
<a style="display:block" href="sample\Silver Blaze.html">Silver Blaze</a>
<a style="display:block" href="sample\The Adventure of the Six Napoleans.html">The Adventure of the Six Napoleans</a>
<a style="display:block" href="sample\The Adventure of the Speckled Band.html">The Adventure of the Speckled Band</a>
<a style="display:block" href="sample\The Naval Treaty.html">The Naval Treaty</a>
<a style="display:block" href="sample\The Red Headed League.html">The Red Headed League</a>
<a style="display:block" href="sample\markdown\Gallery.html">sample\markdown\Gallery.html</a>
<a style="display:block" href="sample\markdown\Silver Blaze.html">sample\markdown\Silver Blaze.html</a>
<a style="display:block" href="sample\markdown\The Adventure of the Six Napoleans.html">sample\markdown\The Adventure of the Six Napoleans.html</a>
<a style="display:block" href="sample\markdown\The Adventure of the Speckled Band.html">sample\markdown\The Adventure of the Speckled Band.html</a>
<a style="display:block" href="sample\markdown\The Naval Treaty.html">sample\markdown\The Naval Treaty.html</a>
<a style="display:block" href="sample\markdown\The Red Headed League.html">sample\markdown\The Red Headed League.html</a>
<a style="display:block" href="sample\Silver Blaze.html">sample\Silver Blaze.html</a>
<a style="display:block" href="sample\The Adventure of the Six Napoleans.html">sample\The Adventure of the Six Napoleans.html</a>
<a style="display:block" href="sample\The Adventure of the Speckled Band.html">sample\The Adventure of the Speckled Band.html</a>
<a style="display:block" href="sample\The Naval Treaty.html">sample\The Naval Treaty.html</a>
<a style="display:block" href="sample\The Red Headed League.html">sample\The Red Headed League.html</a>
23 changes: 23 additions & 0 deletions build/sample/markdown/Gallery.html
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en-CA">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>h1 {
font-size: 3rem;
color: aquamarine;
}

p {
margin-bottom: 1rem;
}
</style>
<title>Gallery</title>
</head>
<body
style="margin-left: auto; margin-right: auto; max-width: min(100%, 65ch)"
>
<p>Picture #1</p><p><img style="width:100%" alt="1" src="/static/1.jpg" /></p><p>Picture #2</p><p><img style="width:100%" alt="2" src="/static/2.jpg" /></p><p>Picture #3</p><p><img style="width:100%" alt="3" src="/static/3.jpg" /></p><p>Picture #4</p><p><img style="width:100%" alt="4" src="/static/4.jpg" /></p>
</body>
</html>
23 changes: 23 additions & 0 deletions build/sample/markdown/Silver Blaze.html

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions build/sample/markdown/The Adventure of the Six Napoleans.html

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions build/sample/markdown/The Adventure of the Speckled Band.html

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions build/sample/markdown/The Naval Treaty.html

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions build/sample/markdown/The Red Headed League.html

Large diffs are not rendered by default.

Binary file added build/static/1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/static/2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/static/3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added build/static/4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 11 additions & 8 deletions modules/cli/Generator.cs
@@ -1,9 +1,8 @@
using System.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using paper_csharp.modules.file_parser;
using paper_csharp.modules.utils;
namespace paper_csharp.modules.cli
{

Expand All @@ -14,6 +13,7 @@ public class Generator
{
// Parsed arguments from CLI
public CliArgs Args { get; private set; }
public static string sourceStaticDir = @"static";

public Generator(string[] args)
{
Expand All @@ -34,19 +34,15 @@ public void Run()
this.CreateDistDir();
this.GenerateDistFiles();
this.GenerateIndexFile();
this.GenerateStaticFiles();
}

/// <summary>
/// Create the default or custom output directory
/// </summary>
private void CreateDistDir()
{
if (Directory.Exists(Args.DistDirPath))
{
Directory.Delete(Args.DistDirPath, true);
}

Directory.CreateDirectory(Args.DistDirPath);
DirectoryUtils.CreateDirForce(Args.DistDirPath);
}


Expand Down Expand Up @@ -164,5 +160,12 @@ private void GenerateIndexFile()
IndexFile indexFile = new IndexFile(Args.DistDirPath);
indexFile.Generate();
}

private void GenerateStaticFiles()
{
DirectoryInfo dir = DirectoryUtils.CreateDirSoft(Generator.sourceStaticDir);

DirectoryUtils.DirectoryCopy(dir.ToString(), Path.Combine(Args.DistDirPath, dir.ToString()), true);
}
}
}
2 changes: 1 addition & 1 deletion modules/file_parser/IndexFile.cs
Expand Up @@ -29,7 +29,7 @@ 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>");
var linkList = ReadAllFilePaths(this.SourceDirPath).Select(filePath => $"<a style=\"display:block\" href=\"{filePath}\">{filePath}</a>");

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

Expand Down
55 changes: 44 additions & 11 deletions modules/file_parser/MarkdownFile.cs
@@ -1,3 +1,4 @@
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
Expand All @@ -20,7 +21,6 @@ static public ParseResult Parse(string filePath)

string title = Path.GetFileNameWithoutExtension(filePath);
string body = "";

// Separates elements by double newline
IEnumerable<string> elements = Regex.Split(fileContent, "(\r\r|\n\n|\r\n\r\n)").Where(str => !string.IsNullOrWhiteSpace(str));

Expand All @@ -37,44 +37,77 @@ static public ParseResult Parse(string filePath)
/// </summary>
private static string ParseElement(string element)
{
MarkdownParser parser = new MarkdownParser(element);

element = MarkdownFile.ReplaceSingleNewline(element);
element = MarkdownFile.ParseBoldText(element);
element = MarkdownFile.ParseHorizontalLine(element);
parser.ReplaceSingleNewline().ParseBoldText().ParseHorizontalLine().ParseImage();

return $"<p>{element}</p>";
return $"<p>{parser.Element}</p>";
}


}

public class MarkdownParser
{
public string Element;

public MarkdownParser(string element)
{
Element = element;
}

/// <summary>
/// Replace single newlines with space
/// </summary>
private static string ReplaceSingleNewline(string element)
public MarkdownParser ReplaceSingleNewline()
{
return Regex.Replace(element, "(\r|\n|\r\n)", " ");
Element = Regex.Replace(Element, "(\r|\n|\r\n)", " ");

return this;
}

/// <summary>
/// Parse markdown bold to tag `strong`
/// </summary>
private static string ParseBoldText(string element)
public MarkdownParser ParseBoldText()
{
// Match anything between two double-asterisk or two double-lodash
string boldPattern = @"\*{2}(.*?)\*{2}|_{2}(.*?)_{2}";
string boldReplacement = "<strong>$1</strong>";

return Regex.Replace(element, boldPattern, boldReplacement);
Element = Regex.Replace(Element, boldPattern, boldReplacement);

return this;
}

/// <summary>
/// Parse markdown --- to tag `hr`
/// </summary>
private static string ParseHorizontalLine(string element)
public MarkdownParser ParseHorizontalLine()
{
// Match if --- is the only chars in the line
string hrPattern = @"^---$";
string hrReplacement = "<hr />";

return Regex.Replace(element, hrPattern, hrReplacement);
Element = Regex.Replace(Element, hrPattern, hrReplacement);

return this;
}

/// <summary>
/// Parse markdown [alt](src) to tag `img`
/// </summary>
public MarkdownParser ParseImage()
{
string imgPattern = @"\[(?<alt>.*)\]\((?<src>.*)\)";
var match = Regex.Match(Element, imgPattern);

if (match.Success)
{
Element = $"<img style=\"width:100%\" alt=\"{match.Groups["alt"]}\" src=\"/static/{match.Groups["src"]}\" />";
}

return this;
}
}
}
70 changes: 70 additions & 0 deletions modules/utils/DirectoryUtils.cs
@@ -0,0 +1,70 @@
using System.IO;


namespace paper_csharp.modules.utils
{
public class DirectoryUtils
{

// Overwrite or create a new directory at path
public static DirectoryInfo CreateDirForce(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}

return Directory.CreateDirectory(path);
}

// Create a new directory if not exist at path
public static DirectoryInfo CreateDirSoft(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);

if (dir.Exists)
{
return dir;
}

return Directory.CreateDirectory(dir.ToString());
}

public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);

if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}

DirectoryInfo[] dirs = dir.GetDirectories();

// If the destination directory doesn't exist, create it.
Directory.CreateDirectory(destDirName);

// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string tempPath = Path.Combine(destDirName, file.Name);
file.CopyTo(tempPath, false);
}

// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string tempPath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, tempPath, copySubDirs);
}
}
}
}

}
15 changes: 15 additions & 0 deletions sample/markdown/Gallery.md
@@ -0,0 +1,15 @@
Picture #1

[1](1.jpg)

Picture #2

[2](2.jpg)

Picture #3

[3](3.jpg)

Picture #4

[4](4.jpg)

1 comment on commit 99ad730

@vercel
Copy link

@vercel vercel bot commented on 99ad730 Oct 28, 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.