Add Markdown parser

Added initial C++ Markdown support.
This commit is contained in:
luca0N! 2022-02-25 18:52:16 -03:00
parent 5ca4519ad2
commit 333f59131a
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
3 changed files with 209 additions and 3 deletions

View File

@ -24,8 +24,8 @@ CXXFLAGS=-Wall -Wextra -g -O0 -std=c++17
.PHONY: clean .PHONY: clean
$(PROGRAM_NAME): build/ build/obj/ build/obj/main.o build/obj/ConfigUtils.o build/obj/WebsiteBuilder.o build/obj/SwgRuntime.o $(PROGRAM_NAME): build/ build/obj/ build/obj/main.o build/obj/ConfigUtils.o build/obj/WebsiteBuilder.o build/obj/SwgRuntime.o build/obj/MarkdownParser.o
$(CXXC) -o $(PROGRAM_NAME) build/obj/main.o build/obj/ConfigUtils.o build/obj/WebsiteBuilder.o build/obj/SwgRuntime.o $(CXXC) -o $(PROGRAM_NAME) build/obj/main.o build/obj/ConfigUtils.o build/obj/WebsiteBuilder.o build/obj/SwgRuntime.o build/obj/MarkdownParser.o
build/obj/main.o: src/main.cxx src/ConfigUtils.hxx src/SwgContext.hxx src/WebsiteBuilder.hxx src/Common.hxx build/obj/main.o: src/main.cxx src/ConfigUtils.hxx src/SwgContext.hxx src/WebsiteBuilder.hxx src/Common.hxx
$(CXXC) $(CXXFLAGS) -o build/obj/main.o -c src/main.cxx $(CXXC) $(CXXFLAGS) -o build/obj/main.o -c src/main.cxx
@ -33,12 +33,15 @@ build/obj/main.o: src/main.cxx src/ConfigUtils.hxx src/SwgContext.hxx src/Websit
build/obj/ConfigUtils.o: src/ConfigUtils.cxx src/SwgContext.hxx src/Common.hxx build/obj/ConfigUtils.o: src/ConfigUtils.cxx src/SwgContext.hxx src/Common.hxx
$(CXXC) $(CXXFLAGS) -o build/obj/ConfigUtils.o -c src/ConfigUtils.cxx $(CXXC) $(CXXFLAGS) -o build/obj/ConfigUtils.o -c src/ConfigUtils.cxx
build/obj/WebsiteBuilder.o: src/WebsiteBuilder.hxx src/WebsiteBuilder.cxx src/SwgRuntime.hxx src/Common.hxx build/obj/WebsiteBuilder.o: src/WebsiteBuilder.hxx src/WebsiteBuilder.cxx src/SwgRuntime.hxx src/Common.hxx src/MarkdownParser.hxx
$(CXXC) $(CXXFLAGS) -o build/obj/WebsiteBuilder.o -c src/WebsiteBuilder.cxx $(CXXC) $(CXXFLAGS) -o build/obj/WebsiteBuilder.o -c src/WebsiteBuilder.cxx
build/obj/SwgRuntime.o: src/SwgRuntime.hxx src/SwgRuntime.cxx build/obj/SwgRuntime.o: src/SwgRuntime.hxx src/SwgRuntime.cxx
$(CXXC) $(CXXFLAGS) -o build/obj/SwgRuntime.o -c src/SwgRuntime.cxx $(CXXC) $(CXXFLAGS) -o build/obj/SwgRuntime.o -c src/SwgRuntime.cxx
build/obj/MarkdownParser.o: src/MarkdownParser.hxx src/MarkdownParser.cxx
$(CXXC) $(CXXFLAGS) -o build/obj/MarkdownParser.o -c src/MarkdownParser.cxx
build/: build/:
mkdir -p build/ mkdir -p build/

173
src/MarkdownParser.cxx Normal file
View File

@ -0,0 +1,173 @@
/*
* Copyright (C) 2022 luca0N!
*
* This file is part of Static Website Generator (swg).
*
* Static Website Generator (swg) is free software: you can redistribute it
* and/or modify it under the terms of the version 3 of the GNU Lesser General
* Public License as published by the Free Software Foundation.
*
* Static Website Generator (swg) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Static Website Generator (swg). If not, see
* <https://www.gnu.org/licenses/>.
*
* Contact luca0N! by e-mail: <luca0n [at] luca0n [dot] com>.
*/
#include "MarkdownParser.hxx"
#include <assert.h>
#define ASCII_DIGIT_START 48
namespace MarkdownParser {
std::string make_html(std::filesystem::path const &path) {
FILE *mdFile = fopen(path.string().c_str(), "r");
// TODO: Add proper error handling.
assert(mdFile != NULL);
std::string html;
int buflen = 64;
char buf[buflen];
// Tag flags
bool tag_b = false,
tag_i = false,
// If there is an escape character at the end of the buffer, use
// this flag to prevent ignoring it.
escaping = false,
newline = true,
manualBreak = false,
// Used to ignore spaces at the beginning of header titles.
ignoreSpace = false;
// For counting sub-headers (h1, h2, h3, and so on)
int tag_h = 0;
while (fgets(buf, buflen, mdFile) != NULL) {
manualBreak = false;
// Read character by character
for (int x = 0; x < buflen; x++) {
char c = buf[x];
if (c == '\0') break;
else if (c == '\n') {
// The next buffer iteration will hold the
// first (buflen) bytes of the next new line.
newline = true;
manualBreak = true;
// If we were in the middle of inserting a header tag, close it here.
if (tag_h > 0) {
html += "</h";
html += (ASCII_DIGIT_START + tag_h);
html += ">";
tag_h = 0;
}
html += '\n';
break;
}
switch (c) {
case '*':
// Bold check
// Check whether this character has been escaped.
if (buf[x-1] == '\\') {
html += "*";
break;
}
html += tag_b ? "</b>" : "<b>";
tag_b = !tag_b;
break;
case '_':
// Italics check
// Check whether this character has
// been escaped.
if (buf[x-1] == '\\') {
html += "_";
break;
}
html += tag_i ? "</i>" : "<i>";
tag_i = !tag_i;
break;
case '#':
// Header check
// Headers must be declared at the
// beginning of a new line. Ignore it
// if this is not a new line.
if (!newline) {
html += "#";
break;
}
// Check whether this character has
// been escaped.
if (buf[x-1] == '\\' ||
// Check if this header was specified
// right at the beginning of the line.
(tag_h == 0 && x != 0)) {
html += "#";
break;
}
// This seems like a header
// declaration.
//
// Increase the header count (for
// subheader support) and add it to the
// HTML output.
//
// Support up to 6 levels of headers.
// After that, ignore '#' characters
// and add them directly to the HTML
// output.
if (tag_h >= 6) {
html += "<h";
html += (ASCII_DIGIT_START + tag_h);
html += ">";
html += '#';
ignoreSpace = true;
break;
} else tag_h++;
// If we are done reading header
// characters, finally add the tag and
// then move on.
if (buf[x+1] != '#') {
html += "<h";
html += (ASCII_DIGIT_START + tag_h);
html += ">";
ignoreSpace = true;
break;
}
break;
case ' ':
if (ignoreSpace) {
ignoreSpace = false;
break;
} else html += ' ';
break;
default:
html += c;
break;
}
}
if (!manualBreak) newline = false;
}
fclose(mdFile);
return html;
}
};

30
src/MarkdownParser.hxx Normal file
View File

@ -0,0 +1,30 @@
/*
* Copyright (C) 2022 luca0N!
*
* This file is part of Static Website Generator (swg).
*
* Static Website Generator (swg) is free software: you can redistribute it
* and/or modify it under the terms of the version 3 of the GNU Lesser General
* Public License as published by the Free Software Foundation.
*
* Static Website Generator (swg) is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Static Website Generator (swg). If not, see
* <https://www.gnu.org/licenses/>.
*
* Contact luca0N! by e-mail: <luca0n [at] luca0n [dot] com>.
*/
#pragma once
#include <string>
#include <filesystem>
namespace MarkdownParser {
std::string make_html(std::filesystem::path const &path);
};