ide suggestions

This commit is contained in:
Andy Pack 2024-01-27 07:16:39 +00:00
parent 313688622a
commit 1bee729664
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
14 changed files with 48 additions and 55 deletions

View File

@ -5,9 +5,7 @@
namespace kc { namespace kc {
AppContext::AppContext() AppContext::AppContext(): config_loaded(false) {
{
} }
void AppContext::load_config(int argc, const char *argv[]) void AppContext::load_config(int argc, const char *argv[])
@ -18,16 +16,15 @@ void AppContext::load_config(int argc, const char *argv[])
void AppContext::load_and_parse_cache() void AppContext::load_and_parse_cache()
{ {
auto env_path = (*config)["path"].as<std::string>(); const auto env_path = (*config)["path"].as<std::string>();
print_and_log("> Loading knowledge base from " + env_path); print_and_log("Loading knowledge base from " + env_path);
file_cache = std::make_shared<kc::FileContextCache>(); file_cache = std::make_shared<kc::FileContextCache>();
file_cache->load(env_path); file_cache->load(env_path);
file_cache->parse_all(); file_cache->parse_all();
} }
std::string AppContext::command() std::string AppContext::command() const {
{
if (config->count("command") == 1) if (config->count("command") == 1)
{ {
return (*config)["command"].as<std::string>(); return (*config)["command"].as<std::string>();

View File

@ -20,7 +20,7 @@ public:
void load_config(int argc, const char *argv[]); void load_config(int argc, const char *argv[]);
void load_and_parse_cache(); void load_and_parse_cache();
std::string command(); [[nodiscard]] std::string command() const;
private: private:

View File

@ -41,20 +41,19 @@ std::shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
.run(), .run(),
*vm); *vm);
if (vm->count("config")) if (vm->contains("config"))
{ {
auto config_path = (*vm)["config"].as<std::string>(); auto config_path = (*vm)["config"].as<std::string>();
BOOST_LOG_TRIVIAL(info) << "Attempting file config load for " << config_path; BOOST_LOG_TRIVIAL(info) << "Attempting file config load for " << config_path;
std::ifstream ifs{config_path.c_str()}; if (std::ifstream ifs{config_path.c_str()}) {
if (ifs) {
BOOST_LOG_TRIVIAL(info) << "File opened, loading..."; BOOST_LOG_TRIVIAL(info) << "File opened, loading...";
po::store(po::parse_config_file(ifs, config_file_options), *vm); po::store(po::parse_config_file(ifs, config_file_options), *vm);
} }
} }
po::notify(*vm); po::notify(*vm);
if (vm->count("help")) { if (vm->contains("help")) {
std::cout << desc; std::cout << desc;
return nullptr; return nullptr;

View File

@ -2,13 +2,12 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <utility>
namespace kc { namespace kc {
FileEntry::FileEntry(fs::directory_entry entry) FileEntry::FileEntry(fs::directory_entry entry)
: file_entry(entry) : file_entry(std::move(entry)), loaded(false) {
{
} }
bool FileEntry::content_loaded() const bool FileEntry::content_loaded() const

View File

@ -12,14 +12,14 @@ namespace kc {
class FileEntry { class FileEntry {
public: public:
FileEntry(fs::directory_entry entry); explicit FileEntry(fs::directory_entry entry);
fs::directory_entry file_entry; fs::directory_entry file_entry;
fs::path relative_path; fs::path relative_path;
bool content_loaded() const; [[nodiscard]] bool content_loaded() const;
std::string load_content(); std::string load_content();
std::string get_content() const; [[nodiscard]] std::string get_content() const;
void clear_content(); void clear_content();
private: private:

View File

@ -8,14 +8,14 @@ static const std::string exclusions[] = {".git", ".obsidian"};
std::vector<std::shared_ptr<kc::FileEntry>> kc::walk_dir(const std::string dir) std::vector<std::shared_ptr<kc::FileEntry>> kc::walk_dir(const std::string dir)
{ {
auto matched = std::vector<std::shared_ptr<kc::FileEntry>>(); auto matched = std::vector<std::shared_ptr<kc::FileEntry>>();
auto base_path = fs::path(dir); const auto base_path = fs::path(dir);
for (auto const& dir_entry : fs::recursive_directory_iterator(base_path)) for (auto const& dir_entry : fs::recursive_directory_iterator(base_path))
{ {
if (dir_entry.is_directory()) continue; if (dir_entry.is_directory()) continue;
auto excluded = false; auto excluded = false;
auto dir_entry_path = dir_entry.path(); const auto& dir_entry_path = dir_entry.path();
auto dir_entry_path_string = dir_entry_path.string(); auto dir_entry_path_string = dir_entry_path.string();
for (auto const& exclusion: exclusions) for (auto const& exclusion: exclusions)

View File

@ -14,8 +14,8 @@
#include "valid/link.hpp" #include "valid/link.hpp"
#include "image/img.hpp" #include "image/img.hpp"
void run_validate(kc::AppContext app_context); void run_validate(const kc::AppContext &app_context);
void run_img(kc::AppContext app_context); void run_img(const kc::AppContext &app_context);
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
@ -32,7 +32,7 @@ int main(int argc, const char *argv[]) {
if(app_context.config) if(app_context.config)
{ {
auto command = app_context.command(); const auto command = app_context.command();
if (!command.empty()) if (!command.empty())
{ {
if (command == "validate") if (command == "validate")
@ -58,12 +58,12 @@ int main(int argc, const char *argv[]) {
return 1; return 1;
} }
void run_validate(kc::AppContext app_context) void run_validate(const kc::AppContext &app_context)
{ {
kc::validate_links(app_context.file_cache->get()); kc::validate_links(app_context.file_cache->get());
} }
void run_img(kc::AppContext app_context) void run_img(const kc::AppContext &app_context)
{ {
kc::image_proc(app_context.file_cache->get()); kc::image_proc(app_context.file_cache->get());
} }

View File

@ -1,11 +1,11 @@
#include "FileContext.hpp" #include "FileContext.hpp"
#include <utility>
namespace kc { namespace kc {
FileContext::FileContext(std::shared_ptr<kc::FileEntry> entry) FileContext::FileContext(std::shared_ptr<kc::FileEntry> entry)
: file_entry(entry) : file_entry(std::move(entry)), links_parsed(false) {
{
} }
void FileContext::parse() void FileContext::parse()

View File

@ -14,7 +14,7 @@ namespace kc {
class FileContext { class FileContext {
public: public:
FileContext(std::shared_ptr<kc::FileEntry> entry); explicit FileContext(std::shared_ptr<kc::FileEntry> entry);
std::shared_ptr<kc::FileEntry> file_entry; std::shared_ptr<kc::FileEntry> file_entry;
std::vector<kc::Link> links; std::vector<kc::Link> links;

View File

@ -8,14 +8,14 @@
namespace kc { namespace kc {
void FileContextCache::load(const std::string root_path) void FileContextCache::load(const std::string &root_path)
{ {
BOOST_LOG_TRIVIAL(trace) << "Beginning cache load"; BOOST_LOG_TRIVIAL(trace) << "Beginning cache load";
auto entries = kc::walk_dir(root_path); const auto entries = kc::walk_dir(root_path);
this->root_path.assign(root_path); this->root_path.assign(root_path);
for (auto entry : entries) for (const auto& entry : entries)
{ {
if (entry->relative_path.extension() == ".md") if (entry->relative_path.extension() == ".md")
{ {
@ -43,9 +43,9 @@ void FileContextCache::parse_all()
{ {
context->parse(); context->parse();
if (context->tags.size() != 0) if (!context->tags.empty())
{ {
for (auto tag : context->tags) for (const auto& tag : context->tags)
{ {
tag_map[tag].push_back(context->file_entry); tag_map[tag].push_back(context->file_entry);
} }

View File

@ -10,12 +10,12 @@ namespace kc {
class FileContextCache { class FileContextCache {
public: public:
void load(std::string root_path); void load(const std::string &root_path);
void clear(); void clear();
size_t size() const; [[nodiscard]] size_t size() const;
std::vector<std::shared_ptr<kc::FileContext>> get() const; [[nodiscard]] std::vector<std::shared_ptr<kc::FileContext>> get() const;
void parse_all(); void parse_all();
std::string get_root_path() const; [[nodiscard]] std::string get_root_path() const;
std::unordered_map<std::string, std::vector<std::shared_ptr<kc::FileEntry>>> tag_map; std::unordered_map<std::string, std::vector<std::shared_ptr<kc::FileEntry>>> tag_map;

View File

@ -2,25 +2,24 @@
namespace kc { namespace kc {
Link::Link(std::string original) Link::Link(const std::string &original)
{ {
original_form = std::regex_replace(original, std::regex("%20"), " "); original_form = std::regex_replace(original, std::regex("%20"), " ");
auto opening_display = original_form.find('['); const auto opening_display = original_form.find('[');
auto closing_display = original_form.find(']', opening_display); const auto closing_display = original_form.find(']', opening_display);
display = original_form.substr(opening_display + 1, closing_display - opening_display - 1); display = original_form.substr(opening_display + 1, closing_display - opening_display - 1);
auto opening_link = closing_display + 1; const auto opening_link = closing_display + 1;
auto closing_link = original_form.rfind(')'); const auto closing_link = original_form.rfind(')');
link = original_form.substr(opening_link + 1, closing_link - opening_link - 1); link = original_form.substr(opening_link + 1, closing_link - opening_link - 1);
external = link.starts_with("http"); external = link.starts_with("http");
auto sublink_pos = original_form.find('#', opening_link); if(const auto sublink_pos = original_form.find('#', opening_link);
sublink_pos != std::string::npos)
if(sublink_pos != std::string::npos)
{ {
sublink = original_form.substr(sublink_pos + 1, closing_link - sublink_pos - 1); sublink = original_form.substr(sublink_pos + 1, closing_link - sublink_pos - 1);
link = original_form.substr(opening_link + 1, sublink_pos - opening_link - 1); link = original_form.substr(opening_link + 1, sublink_pos - opening_link - 1);

View File

@ -12,9 +12,9 @@ class Link {
std::string display; std::string display;
std::string link; std::string link;
std::string sublink; std::string sublink;
bool is_external() const; [[nodiscard]] bool is_external() const;
Link(std::string original); explicit Link(const std::string &original);
private: private:

View File

@ -15,19 +15,18 @@ std::vector<kc::FileLinkStateResult> validate_links(const std::vector<std::share
auto invalid_counter = 0; auto invalid_counter = 0;
for (auto context : contexts) for (const auto& context : contexts)
{ {
if (context->links.size() > 0) if (!context->links.empty())
{ {
for (auto link: context->links) for (const auto& link: context->links)
{ {
if(!link.is_external()) { if(!link.is_external()) {
auto composed = context->abs_path(link); auto composed = context->abs_path(link);
auto entry = fs::directory_entry(composed); if(auto entry = fs::directory_entry(composed);
!entry.exists())
if(!entry.exists())
{ {
print_and_log("Invalid link: " + std::string(context->file_entry->file_entry.path()) + " -> " + link.original_form); print_and_log("Invalid link: " + std::string(context->file_entry->file_entry.path()) + " -> " + link.original_form);
invalid_counter++; invalid_counter++;