diff --git a/src/const.hpp b/src/const.hpp index e7371da..76678fa 100644 --- a/src/const.hpp +++ b/src/const.hpp @@ -3,6 +3,6 @@ #include 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#.]+)"; \ No newline at end of file +static const std::string MD_TAG_REGEX = R"((^|[[:blank:]])#{1}[^\s#.]+)"; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8c31432..ecd42fa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,14 +33,29 @@ int main(int argc, const char *argv[]) { auto context = file_cache.get()[(*config)["index"].as()]; - 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; } } diff --git a/src/parse/FileContext.cpp b/src/parse/FileContext.cpp index 59bb306..48f2425 100644 --- a/src/parse/FileContext.cpp +++ b/src/parse/FileContext.cpp @@ -3,23 +3,24 @@ namespace kc { FileContext::FileContext(kc::FileEntry entry) -: file_entry(entry) +: file_entry(std::make_shared(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(); } diff --git a/src/parse/FileContext.hpp b/src/parse/FileContext.hpp index bbc4b24..2d553a7 100644 --- a/src/parse/FileContext.hpp +++ b/src/parse/FileContext.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "../fs/FileEntry.hpp" @@ -14,8 +15,9 @@ class FileContext { FileContext(kc::FileEntry entry); - kc::FileEntry file_entry; + std::shared_ptr file_entry; std::vector links; + std::vector images; std::vector tags; void parse(); diff --git a/src/parse/FileContextCache.cpp b/src/parse/FileContextCache.cpp index 5666552..b28cfa6 100644 --- a/src/parse/FileContextCache.cpp +++ b/src/parse/FileContextCache.cpp @@ -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); + } + } } } } diff --git a/src/parse/FileContextCache.hpp b/src/parse/FileContextCache.hpp index 92c2931..8229c0c 100644 --- a/src/parse/FileContextCache.hpp +++ b/src/parse/FileContextCache.hpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include "FileContext.hpp" namespace kc { @@ -14,6 +16,8 @@ class FileContextCache { std::vector> get(); void parse_all(); + std::unordered_map>> tag_map; + private: std::vector> file_contexts; }; diff --git a/src/parse/Link.cpp b/src/parse/Link.cpp index 773bb6a..8385edd 100644 --- a/src/parse/Link.cpp +++ b/src/parse/Link.cpp @@ -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); + } } } \ No newline at end of file diff --git a/src/parse/Link.hpp b/src/parse/Link.hpp index dd9a487..843d345 100644 --- a/src/parse/Link.hpp +++ b/src/parse/Link.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace kc {