-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppblogfy.cpp
77 lines (68 loc) · 2.76 KB
/
cppblogfy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Copyright 08/29/2024 https://github.com/su8/cppblogfy
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <regex>
#include <unistd.h>
#include <dirent.h>
#include <cctype>
#include <fcntl.h>
#include <sys/stat.h>
#include "maddy/parser.h"
static std::string mdToHtml(const std::string &mdInput);
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] = {'\0'};
char writeToIndex[4096] = {'\0'};
snprintf(genEntryDir, sizeof(genEntryDir) - 1, "generated/%s", dir_entry.path().filename().string().c_str());
std::string generatedEntry = std::regex_replace(genEntryDir, std::regex(".md"), "");
std::filesystem::create_directories(generatedEntry);
snprintf(writeToIndex, sizeof(writeToIndex) - 1, "%s/index.html", generatedEntry.c_str());
std::string openMd = "markdown/" + dir_entry.path().filename().string();
std::ifstream openUpEntry(openMd);
std::stringstream strStream;
std::ofstream outdata;
if(openUpEntry.is_open()) {
strStream << openUpEntry.rdbuf();
puts(writeToIndex);
outdata.open(writeToIndex);
if (!outdata) { puts("Could not open file for writing."); break; }
outdata << mdToHtml(strStream.str()) << std::endl;
outdata.close();
}
openUpEntry.close();
}
puts("Done");
return EXIT_SUCCESS;
}
static 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;
}