Skip to content

Instantly share code, notes, and snippets.

@su8
Created August 29, 2024 14:40
Show Gist options
  • Save su8/9e2032b23001fe1ff8cdb5f4e5bd5864 to your computer and use it in GitHub Desktop.
Save su8/9e2032b23001fe1ff8cdb5f4e5bd5864 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <regex>
#include <unistd.h>
#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}) {
if (dir_entry.path().filename().string() == "." || dir_entry.path().filename().string() == "..") continue;
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::string openMd = "markdown/" + dir_entry.path().filename().string();
FILE *fp = NULL;
std::ifstream openUpEntry(openMd);
std::stringstream strStream;
if(openUpEntry.is_open()) {
strStream << openUpEntry.rdbuf();
puts(writeToIndex);
fp = fopen(writeToIndex, "w");
fprintf(fp, "%s", mdToHtml(strStream.str()).c_str());
fclose(fp);
}
openUpEntry.close();
genEntryDir[0] = '\0';
writeToIndex[0] = '\0';
//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