Start tokenizing markdown files

This commit is contained in:
Nick Chambers 2024-12-28 07:04:15 -06:00
parent 11a30a87a8
commit 6bf1b7179a
4 changed files with 62 additions and 7 deletions

View file

@ -1,4 +1,6 @@
#ifndef _CHIMNEY_HPP__ #ifndef _CHIMNEY_HPP__
#define _CHIMNEY_HPP__ #define _CHIMNEY_HPP__
#include <chimney/markdown.hpp>
#endif #endif

View file

@ -0,0 +1,13 @@
#ifndef _CHIMNEY_MARKDOWN_HPP__
#define _CHIMNEY_MARKDOWN_HPP__
#include <string>
namespace chimney {
class MarkdownFile {
public:
MarkdownFile(std::string contents);
};
}
#endif

View file

@ -1,21 +1,22 @@
#include <algorithm> #include <algorithm>
#include <chimney.hpp>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <iostream> #include <iostream>
#include <string>
#include <vector> #include <vector>
namespace fs = std::filesystem; namespace fs = std::filesystem;
int main(int argc, char **argv) { int main(int argc, char **argv) {
/* /*
* config stub * Configuration stub
*/ */
std::vector<std::string> ignored_dirs { ".git" }; std::vector<std::string> ignored_dirs { ".git" };
std::vector<std::string> ignored_files { "README.md", "LICENSE" }; std::vector<std::string> ignored_files { "README.md", "LICENSE" };
/* /*
* argument parser stub * Argument parser stub
*/ */
std::string dir = "."; std::string dir = ".";
@ -25,19 +26,19 @@ int main(int argc, char **argv) {
} }
/* /*
* directory tree builder * Directory tree builder & compiler
*/ */
auto entry = fs::recursive_directory_iterator(dir); auto entry = fs::recursive_directory_iterator(dir);
for(; entry != fs::recursive_directory_iterator(); entry++) { for(; entry != fs::recursive_directory_iterator(); entry++) {
std::string filename = entry->path().filename(); std::vector<std::string> haystack = ignored_files;
auto haystack = ignored_files;
if(entry->is_directory()) { if(entry->is_directory()) {
haystack = ignored_dirs; haystack = ignored_dirs;
} }
std::string filename = entry->path().filename();
auto iter = std::find(haystack.begin(), haystack.end(), filename); auto iter = std::find(haystack.begin(), haystack.end(), filename);
bool ignored = iter != haystack.end(); bool ignored = iter != haystack.end();
@ -45,7 +46,11 @@ int main(int argc, char **argv) {
entry.disable_recursion_pending(); entry.disable_recursion_pending();
} else if(!entry->is_directory()) { } else if(!entry->is_directory()) {
if(entry->path().extension() == ".md") { if(entry->path().extension() == ".md") {
// process markdown file std::ifstream reader { entry->path().c_str() };
std::string contents(entry->file_size(), ' ');
reader.read(&contents[0], entry->file_size());
// Loading the file into memory might need a bit of refactoring.
chimney::MarkdownFile md_file { contents };
} else { } else {
// copy file to output directory // copy file to output directory
} }

35
src/markdown.cpp Normal file
View file

@ -0,0 +1,35 @@
#include <cctype>
#include <chimney/markdown.hpp>
#include <ostream>
#include <vector>
namespace {
class MarkdownWord {
public:
bool start;
bool end;
std::string word;
MarkdownWord(bool start, bool end, std::string word)
: start(start), end(end), word(word) { }
};
}
chimney::MarkdownFile::MarkdownFile(std::string contents) {
std::vector<MarkdownWord> words;
std::string word;
bool start = true;
for(auto rune: contents) {
if(!std::isspace(rune)) {
word += rune;
} else if(!word.empty()) {
words.push_back(MarkdownWord {
start, rune == '\n', word
});
start = rune == '\n';
word.clear();
}
}
}