link decomp, tag cache, image capture

This commit is contained in:
Andy Pack 2023-06-10 01:06:56 +01:00
parent e7dd0a7886
commit 70a4c5ee4a
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
8 changed files with 71 additions and 14 deletions

View File

@ -3,6 +3,6 @@
#include <string>
static const std::string MD_LINK_REGEX = R"(\[.*?\]\(.*?\))";
static const std::string MD_MD_LINK_REGEX = R"(\[.*?\]\(.*?\.md\))";
static const std::string MD_MD_LINK_REGEX = R"(\[.*?\]\(.*?\.md(#.*?)*\))";
static const std::string MD_IMAGE_LINK_REGEX = R"(!\[.*?\]\(.*?\.png\))";
static const std::string MD_TAG_REGEX = R"(#{1}[^\s#.]+)";
static const std::string MD_TAG_REGEX = R"((^|[[:blank:]])#{1}[^\s#.]+)";

View File

@ -33,14 +33,29 @@ int main(int argc, const char *argv[]) {
auto context = file_cache.get()[(*config)["index"].as<int>()];
std::cout << context->file_entry.get_content() << std::endl << std::endl << std::endl;
std::cout << context->file_entry->get_content() << std::endl << std::endl << std::endl;
std::cout << "links: " << context->links.size() << std::endl;
std::cout << "images: " << context->images.size() << std::endl;
std::cout << "tags: " << context->tags.size() << std::endl << std::endl << std::endl;;
for (auto link : context->links)
{
std::cout << link.original_form << std::endl;
std::cout << link.original_form << " " << link.display << " --- " << link.link << std::endl;
}
std::cout << "tag cache: " << file_cache.tag_map.size() << std::endl;
for (auto tag : file_cache.tag_map)
{
std::cout << tag.first << ": ";
for (auto tag_entry: tag.second)
{
std::cout << tag_entry->relative_path << ", ";
}
std::cout << std::endl;
}
}

View File

@ -3,23 +3,24 @@
namespace kc {
FileContext::FileContext(kc::FileEntry entry)
: file_entry(entry)
: file_entry(std::make_shared<kc::FileEntry>(entry))
{
}
void FileContext::parse()
{
if (!file_entry.content_loaded())
if (!file_entry->content_loaded())
{
throw std::logic_error("cannot parse from file entry as it has not been loaded");
}
links.clear();
images.clear();
tags.clear();
std::regex link_regex(MD_MD_LINK_REGEX);
std::string file_content = file_entry.get_content();
std::string file_content = file_entry->get_content();
std::smatch link_match;
while(std::regex_search(file_content, link_match, link_regex)) {
@ -27,12 +28,21 @@ void FileContext::parse()
file_content = link_match.suffix();
}
std::regex tag_regex(MD_TAG_REGEX);
file_content = file_entry.get_content();
std::regex image_regex(MD_IMAGE_LINK_REGEX);
file_content = file_entry->get_content();
std::smatch image_match;
while(std::regex_search(file_content, image_match, image_regex)) {
images.push_back(image_match.str());
file_content = image_match.suffix();
}
std::regex tag_regex(MD_TAG_REGEX, std::regex::multiline);
file_content = file_entry->get_content();
std::smatch tag_match;
while(std::regex_search(file_content, tag_match, tag_regex)) {
tags.push_back(tag_match.str());
tags.push_back(tag_match.str().substr(1));
file_content = tag_match.suffix();
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include <memory>
#include <regex>
#include "../fs/FileEntry.hpp"
@ -14,8 +15,9 @@ class FileContext {
FileContext(kc::FileEntry entry);
kc::FileEntry file_entry;
std::shared_ptr<kc::FileEntry> file_entry;
std::vector<kc::Link> links;
std::vector<kc::Link> images;
std::vector<std::string> tags;
void parse();

View File

@ -25,11 +25,20 @@ void FileContextCache::load(std::string root_path)
void FileContextCache::parse_all()
{
tag_map.clear();
for (auto context: file_contexts)
{
if (context->file_entry.relative_path.extension() == ".md")
if (context->file_entry->relative_path.extension() == ".md")
{
context->parse();
if (context->tags.size() != 0)
{
for (auto tag : context->tags)
{
tag_map[tag].push_back(context->file_entry);
}
}
}
}
}

View File

@ -2,6 +2,8 @@
#include <vector>
#include <memory>
#include <string>
#include <unordered_map>
#include "FileContext.hpp"
namespace kc {
@ -14,6 +16,8 @@ class FileContextCache {
std::vector<std::shared_ptr<kc::FileContext>> get();
void parse_all();
std::unordered_map<std::string, std::vector<std::shared_ptr<kc::FileEntry>>> tag_map;
private:
std::vector<std::shared_ptr<kc::FileContext>> file_contexts;
};

View File

@ -3,9 +3,25 @@
namespace kc {
Link::Link(std::string original)
: original_form(original)
{
original_form = std::regex_replace(original, std::regex("%20"), " ");
auto opening_display = original_form.find('[');
auto closing_display = original_form.find(']', opening_display);
display = original_form.substr(opening_display + 1, closing_display - opening_display - 1);
auto opening_link = closing_display + 1;
auto closing_link = original_form.rfind(')');
link = original_form.substr(opening_link + 1, closing_link - opening_link - 1);
auto display_pos = original_form.find('#', opening_link);
if(display_pos != std::string::npos)
{
display = original_form.substr(display_pos + 1, closing_link - display_pos - 1);
}
}
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <regex>
namespace kc {