Skip to content

Commit

Permalink
Merge branch 'main' into issue-10
Browse files Browse the repository at this point in the history
  • Loading branch information
Yousef-Majidi committed Sep 25, 2023
2 parents f4ec76c + 9b83415 commit 8197c77
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 60 deletions.
107 changes: 49 additions & 58 deletions src/HtmlProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@ namespace Learn2Blog
{
public partial class HtmlProcessor
{
public static string ConvertTextToHtml(string title, string text)
public static string HtmlBuilder(string title, string body)
{
// check for a title
int titleStart = text.IndexOf("\r\n\r\n\r\n");
if (titleStart > 0)
{
title = text[..titleStart].Trim();
text = text[(titleStart + 3)..].Trim();
}

StringBuilder htmlBuilder = new();

// html
Expand All @@ -31,11 +23,33 @@ public static string ConvertTextToHtml(string title, string text)

// body
htmlBuilder.AppendLine("<body>");
htmlBuilder.AppendLine(body);
htmlBuilder.AppendLine("</body>");
// body end

htmlBuilder.AppendLine("</html>");
// html end

return htmlBuilder.ToString();
}

public static string ProcessText(string text)
{
string title = "";
StringBuilder stringBuilder = new();

// check for a title
int titleStart = text.IndexOf("\r\n\r\n\r\n");
if (titleStart > 0)
{
title = text[..titleStart].Trim();
text = text[(titleStart + 3)..].Trim();
}

// title -- only if title is specified in the input file
if (titleStart > 0)
{
htmlBuilder.AppendLine($"<h1>{title}</h1>");
stringBuilder.AppendLine($"<h1>{title}</h1>");
}

// split text into paragraphs based on new lines
Expand All @@ -45,38 +59,16 @@ public static string ConvertTextToHtml(string title, string text)
foreach (string paragraph in paragraphs)
{
// replace line breaks with spaces to keep the text in the same line
string paragraphText = paragraph.Replace("\r\n", " ");
htmlBuilder.AppendLine($"<p>{paragraphText.Trim()}</p>");
string paragraphText = paragraph.Replace("\r\n", " ").Trim();
stringBuilder.AppendLine($"<p>{paragraphText}</p>");
}

htmlBuilder.AppendLine("</body>");
// body end

htmlBuilder.AppendLine("</html>");
// html end

return htmlBuilder.ToString();
return stringBuilder.ToString();
}

public static string ConvertMdToHtml(string title, string text)
public static string ProcessMarkdown(string text)
{

StringBuilder htmlBuilder = new();

// Creating start of HTML file
htmlBuilder.AppendLine("<!DOCTYPE html>");
htmlBuilder.AppendLine("<html lang=\"en\">");

// Creating header
htmlBuilder.AppendLine("<head>");
htmlBuilder.AppendLine("<meta charset=\"utf-8\">");
htmlBuilder.AppendLine($"<title>{title}</title>");
htmlBuilder.AppendLine("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
htmlBuilder.AppendLine("</head>");
// End of header

// Start of body
htmlBuilder.AppendLine("<body>");
StringBuilder stringBuilder = new();

// Split text into lines
string[] lines = text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
Expand All @@ -92,65 +84,61 @@ public static string ConvertMdToHtml(string title, string text)
{
if (paragraphOpen)
{
htmlBuilder.AppendLine("</p>");
stringBuilder.AppendLine("</p>");
paragraphOpen = false;
}
continue;
}

// Replace **text** with <strong>text</strong>
string lineText = Regex.Replace(line, @"\*\*(.*?)\*\*", m => $"<strong>{m.Groups[1].Value}</strong>");

string lineText = StrongSyntaxRegex().Replace(line, m => $"<strong>{m.Groups[1].Value}</strong>");

// Replace line breaks with spaces to keep the text in the same line
lineText = lineText.Replace("\r\n", " ").Trim();

// Open a new <p> tag if not already open
if (!paragraphOpen)
{
htmlBuilder.AppendLine("<p>");
stringBuilder.AppendLine("<p>");
paragraphOpen = true;
}

// Add line text
htmlBuilder.AppendLine(lineText);
stringBuilder.AppendLine(lineText);
}

// Close the last <p> tag if it's still open
if (paragraphOpen)
{
htmlBuilder.AppendLine("</p>");
stringBuilder.AppendLine("</p>");
}

htmlBuilder.AppendLine("</body>");
// Body end

htmlBuilder.AppendLine("</html>");
// HTML end

return htmlBuilder.ToString();
return stringBuilder.ToString();
}

public static void ProcessFile(string inputPath, string outputPath)
{

string ext = Path.GetExtension(inputPath);
// if the file is a text or markdown file, then try to convert it
if (Path.GetExtension(inputPath) == ".txt" || Path.GetExtension(inputPath) == ".md")
if (ext == ".txt" || ext == ".md")
{
try
{
string text = File.ReadAllText(inputPath);
string html = "\0";
string body = "";
string html = "";

if (Path.GetExtension(inputPath) == ".txt")
if (ext == ".txt")
{
html = ConvertTextToHtml(Path.GetFileNameWithoutExtension(inputPath), text);

body = ProcessText(text);
}
else if (Path.GetExtension(inputPath) == ".md")
else
{
html = ConvertMdToHtml(Path.GetFileNameWithoutExtension(inputPath), text);
body = ProcessMarkdown(text);
}

html = HtmlBuilder(Path.GetFileNameWithoutExtension(inputPath), body);

string outputFileName = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(inputPath) + ".html");

// if a file with the same name already exists, append a number to the file name
Expand Down Expand Up @@ -190,5 +178,8 @@ public static void ProcessFile(string inputPath, string outputPath)

[GeneratedRegex("\\n\\s*\\n")]
private static partial Regex NewParagraphRegex();

[GeneratedRegex("\\*\\*(.*?)\\*\\*")]
private static partial Regex StrongSyntaxRegex();
}
}
3 changes: 1 addition & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ static void Main(string[] args)
{
CommandLineUtils.CreateOutputDirectory(outputPath);

HtmlProcessor.ProcessFile(inputPath, outputPath);
HtmlProcessor.ProcessFile(inputPath, outputPath);
}
else if (Directory.Exists(inputPath))
{
// Get all files in the directory and save them to the files array
string[] files = Directory.GetFiles(inputPath, "*.txt").Union(Directory.GetFiles(inputPath, "*.md")).ToArray();


if (files.Length == 0)
{
CommandLineUtils.Logger($"No .txt or .md files found in directory {inputPath}");
Expand Down

0 comments on commit 8197c77

Please sign in to comment.