Skip to content

Commit

Permalink
Add configurable sidebar(table of contents)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkim219 committed Oct 25, 2021
1 parent 970cb1b commit 98cb9ec
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 89 deletions.
48 changes: 48 additions & 0 deletions kimchi-ssg/Style.cs
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace kimchi_ssg
{
class Style
{
public static string def = @"<style>
*{
background-color: #9999FF;
}
.container{
display: flex;
overflow: auto;
}
.left-nav{
width: 20%;
position: sticky;
align-self: flex-start;
top: 0;
}
.contents {
color: #FFFFFF;
width: 50%;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 5px;
}
li a {
display: block;
text-decoration: none;
padding: 5px;
}
</style>";
}
}
177 changes: 88 additions & 89 deletions kimchi-ssg/helpers.cs
Expand Up @@ -10,29 +10,10 @@

namespace kimchi_ssg
{

public class Helpers
{
static string generateHTMLStr(string[] source, string[] elements, string title, string extension)
static string generateHTMLStr(string title, string extension, string table, string[] elements = null)
{
string style = @"<style>
*{
background-color: #9999FF;
}
div {
color: #FFFFFF;
position: absolute;
width: 700px;
height: -100px;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>";

//Suhhee_lab02-Add regular expression for markdown files
var bold = new Regex(@"(\*\*|__) (?=\S) (.+?[*_]*) (?<=\S) \1");
var italic = new Regex(@"(\*|_) (?=\S) (.+?) (?<=\S) \1");
var anchor = new Regex(@"\[([^]]*)\]\(([^\s^\)]*)[\s\)]");
Expand All @@ -41,66 +22,58 @@ static string generateHTMLStr(string[] source, string[] elements, string title,
var hr = new Regex(@"(\---) (.*)");
var code = new Regex(@"\`([^\`].*?)\`");

//suhhee_lab02
List<string> toHtml = new List<string>();
int count = 0;
foreach (var x in source)
{
if (x.Contains("Filename"))
{
string temp = x.Replace("Filename", title);
toHtml.Add(temp);
}
else
toHtml.Add(x);

if (x.Contains("<head>"))
toHtml.Add(style);
toHtml.Add(@"<div class=""container"">");

if (x.Contains("<body>"))
//suhhee_lab02 - add to distinguish function for txt files and md files
if (extension == FileExtension.TEXT)
{
toHtml.Add(table);
toHtml.Add(@"<div class=""contents"">");
foreach (var element in elements)
{
toHtml.Add("<div>");
//suhhee_lab02 - add to distinguish function for txt files and md files
if (extension == FileExtension.TEXT)

if (count == 0)
{
foreach (var element in elements)
{

if (count == 0)
{
toHtml.Add("<h1>");
toHtml.Add(element);
toHtml.Add("</h1>");
}
else
{
toHtml.Add("<p>");
toHtml.Add(element);
toHtml.Add("</p>");
}
count++;
}
toHtml.Add("<h1>");
toHtml.Add(element);
toHtml.Add("</h1>");
}
else if (extension == FileExtension.MARKDOWN)
else
{
foreach (var line in elements)
{
var toBold = bold.Replace(line, @"<b>$2</b><br/>");
var toItalic = italic.Replace(toBold, @"<i>$2</i><br/>");
var toAnchor = anchor.Replace(toItalic, @"<a href='$1'>$2</a>");
var toH2 = h2.Replace(toAnchor, @"<h2>$2</h2></br>");
var toH1 = h1.Replace(toH2, @"<h1>$2</h1>");
var toHr = hr.Replace(toH1, @"<hr>");
var toCode = code.Replace(toH1, @"<code>$1</code>");
toHtml.Add(toCode);
}
toHtml.Add("<p>");
toHtml.Add(element);
toHtml.Add("</p>");
}
toHtml.Add("</div>");
count++;
}
}

string toHTMLfile = string.Join(Seperator.newLineSeperator, toHtml);
return toHTMLfile;
}
else if (extension == FileExtension.MARKDOWN)
{
toHtml.Add(table);
toHtml.Add(@"<div class=""contents"">");
foreach (var line in elements)
{
var toBold = bold.Replace(line, @"<b>$2</b><br/>");
var toItalic = italic.Replace(toBold, @"<i>$2</i><br/>");
var toAnchor = anchor.Replace(toItalic, @"<a href='$1'>$2</a>");
var toH2 = h2.Replace(toAnchor, @"<h2>$2</h2></br>");
var toH1 = h1.Replace(toH2, @"<h1>$2</h1>");
var toHr = hr.Replace(toH1, @"<hr>");
var toCode = code.Replace(toHr, @"<code>$1</code>");
toHtml.Add(toCode);
}
}
else
{
toHtml.Add(table);
}

toHtml.Add("</div></div>");
return generateInterporatedstring(title,Style.def ,string.Join(Seperator.newLineSeperator, toHtml));
}

// parse the json file getting all valid arguments
Expand Down Expand Up @@ -156,15 +129,16 @@ public static void parseJSON(string file, string output)
}
}

public static void generateHTMLfile(string html, string outputDir, string fileName)//
public static void generateHTMLfile(string html, string outputDir, string fileName)
{
try
{
if (!Directory.Exists(outputDir))
Directory.CreateDirectory(outputDir);

var doc = new HtmlDocument();
HtmlCommentNode hcn = doc.CreateComment("<!doctype html>");
HtmlCommentNode hcn = doc.CreateComment(@"<!doctype html>"
);

var node = HtmlNode.CreateNode(html);

Expand All @@ -181,21 +155,10 @@ public static void parseJSON(string file, string output)

public static void strToFile(string file, string outputFolder)
{
string HTMLstr = @"
<html lang=""en-CA"">
<head>
<meta charset = ""utf-8"">
<title> Filename </title>
<meta name = ""viewport"" content = ""width=device-width, initial-scale=1"">
</head>
<body>
</body>
</html>";

string[] html = HTMLstr.Split(Seperator.newLineSeperator);
string sCurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
string fileName = Path.GetFileNameWithoutExtension(sCurrentDirectory + Seperator.pathSeperator + file);
string extension = Path.GetExtension(sCurrentDirectory + Seperator.pathSeperator + file);
List<string> fileList = new List<string>();

//added safety check
if (outputFolder == "" || outputFolder == null)
Expand All @@ -206,28 +169,36 @@ public static void strToFile(string file, string outputFolder)
Directory.Delete(outputPath, true);
Directory.CreateDirectory(outputPath + outputFolder);



string toHTMLfile = string.Empty;
if (Path.GetExtension(file) == FileExtension.TEXT)
{
var text = File.ReadAllText(sCurrentDirectory + Seperator.pathSeperator + file);
string[] contents = text.Split(Seperator.newLineDoubleSeperator);
generateHTMLfile(generateHTMLStr(html, contents, fileName, extension), outputPath, fileName);
fileList.Add(fileName);
generateHTMLfile(generateHTMLStr("index", "html", generateTableOfContents(fileList)), outputPath, "index");
generateHTMLfile(generateHTMLStr(fileName, extension, generateTableOfContents(fileList), contents), outputPath, fileName);
}
else if (Path.GetExtension(file) == FileExtension.MARKDOWN)
{
var contents = File.ReadAllLines(sCurrentDirectory + Seperator.pathSeperator + file);
generateHTMLfile(generateHTMLStr(html, contents, fileName, extension), outputPath, fileName);
fileList.Add(fileName);
generateHTMLfile(generateHTMLStr("index", "html", generateTableOfContents(fileList)), outputPath, "index");
generateHTMLfile(generateHTMLStr(fileName, extension, generateTableOfContents(fileList),contents), outputPath, fileName);
}
else
{
List<string> txtList = new List<string>();
DirectoryInfo di = new DirectoryInfo(sCurrentDirectory + Seperator.pathSeperator + file);


foreach (var dir in di.EnumerateFiles().Where(x => x.ToString().EndsWith(FileExtension.TEXT) || x.ToString().EndsWith(FileExtension.MARKDOWN)))
{
txtList.Add(dir.ToString());
fileList.Add(Path.GetFileNameWithoutExtension(dir.ToString()));
}

foreach (var filePath in txtList)
{
// read the text's paragrah
Expand All @@ -240,13 +211,14 @@ public static void strToFile(string file, string outputFolder)

//get title
fileName = Path.GetFileNameWithoutExtension(filePath);
toHTMLfile = generateHTMLStr(html, contents, fileName, extension);
toHTMLfile = generateHTMLStr(fileName, extension, generateTableOfContents(fileList),contents);

//get saving loation
string saveLoc = Path.GetDirectoryName(filePath);

generateHTMLfile(toHTMLfile, outputPath, fileName);
}
generateHTMLfile(generateHTMLStr("index", "html", generateTableOfContents(fileList)), outputPath, "index");
}
}

Expand All @@ -255,6 +227,33 @@ public static bool isLinux()
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
}

public static string generateInterporatedstring(string title, string style ,string body)
{
return $@"
<html lang=""en-CA"">
<head>
<meta charset = ""utf-8"">
<title> {title} </title>
<meta name = ""viewport"" content = ""width=device-width, initial-scale=1"">
{style}
</head>
<body>
{body}
</body>
</html>";
}

public static string generateTableOfContents(List<string> file)
{
List<string> tableOfContents = new List<string>();
tableOfContents.Add(@"<div class=""left-nav""><nav>");
tableOfContents.Add($@"<ul><li><a href = ""index.html"">Home</a></li>");
foreach (var x in file)
tableOfContents.Add($@"<li><a href = ""{x}.html"">{x}</a></li>");
tableOfContents.Add("</ul></nav></div>");
return String.Join(Seperator.newLineSeperator, tableOfContents.ToArray());
}

public static string getOptions()
{
return @"
Expand All @@ -272,4 +271,4 @@ public static string getVersion()


}
}
}

0 comments on commit 98cb9ec

Please sign in to comment.