Skip to content

Instantly share code, notes, and snippets.

@su8
Created August 29, 2024 14:18
Show Gist options
  • Save su8/651aa2f540661fe5e6a8e147793918ad to your computer and use it in GitHub Desktop.
Save su8/651aa2f540661fe5e6a8e147793918ad to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <regex>
#include "maddy/parser.h"
std::string mdToHtml(const std::string &mdInput);
std::string mdToHtml(const std::string &mdInput) {
std::shared_ptr<maddy::ParserConfig> config = std::make_shared<maddy::ParserConfig>();
std::istringstream markdownStream(mdInput);
std::shared_ptr<maddy::Parser> parser = std::make_shared<maddy::Parser>(config);
std::string htmlOutput = parser->Parse(markdownStream);
return htmlOutput;
}
int main(void) {
const std::filesystem::path generated{"generated"};
const std::filesystem::path mdSrc{"markdown"};
std::filesystem::create_directories(generated);
for (auto const &dir_entry : std::filesystem::recursive_directory_iterator{mdSrc}) {
char genEntryDir[4096] = {""};
char writeToIndex[4096] = {""};
snprintf(genEntryDir, sizeof(genEntryDir) - 1, "generated/%s", dir_entry.path().filename().string().c_str());
snprintf(writeToIndex, sizeof(writeToIndex) - 1, "%s/index.html", genEntryDir);
std::string mdEntry = std::regex_replace(genEntryDir, std::regex(".md"), "");
std::filesystem::create_directories(mdEntry);
std::ofstream indexHtml;
std::string openMd = "markdown/" + dir_entry.path().filename().string();
std::ifstream openUpEntry(openMd);
//std::string s;
std::stringstream strStream;
if(openUpEntry.is_open()) {
strStream << openUpEntry.rdbuf();
indexHtml.open(writeToIndex);
indexHtml << mdToHtml(strStream.str());
indexHtml.close();
}
openUpEntry.close();
//indexHtml.close();
}
std::cout << "Done\n" << std::flush;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment