validating links

This commit is contained in:
Andy Pack 2023-06-17 10:18:57 +01:00
parent 778af44768
commit 47a6e1e522
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
10 changed files with 123 additions and 36 deletions

View File

@ -13,7 +13,7 @@ project(kc
VERSION
1.0
DESCRIPTION
"C++ scratchpad"
"Knowledge crawler for analysing notes"
LANGUAGES
CXX)

View File

@ -5,6 +5,7 @@
parse/Link.cpp
parse/FileContext.cpp
parse/FileContextCache.cpp
valid/link.cpp
logging.cpp
config.cpp
)

View File

@ -14,9 +14,14 @@ std::shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
("help", "produce help message")
("path,p", po::value<std::string>()->default_value("."), "set root path of knowledge base")
("config", po::value<std::string>()->default_value("kc.ini"), "config file location")
("command", po::value<std::string>(), "command to execute")
("subargs", po::value<std::vector<std::string> >(), "Arguments for command")
("index", po::value<int>()->default_value(1), "index")
;
po::positional_options_description pos;
pos.add("command", 1).add("subargs", -1);
po::options_description cmdline_options;
cmdline_options.add(desc);
@ -30,6 +35,8 @@ std::shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
auto vm = std::make_shared<po::variables_map>();
po::store(po::command_line_parser(argc, argv)
.options(cmdline_options)
.positional(pos)
// .allow_unregistered()
.run(),
*vm);

View File

@ -1,5 +1,7 @@
#include "logging.hpp"
#include <iostream>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
@ -13,10 +15,11 @@ void init_logging()
(
keywords::file_name = "kc_%N.log",
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%] %Message%"
keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%] %Message%",
keywords::open_mode = std::ios::app
);
logging::add_console_log(std::cout, boost::log::keywords::format = "[%TimeStamp%] [%Severity%] >> %Message%");
// logging::add_console_log(std::cout, boost::log::keywords::format = "[%TimeStamp%] [%Severity%] >> %Message%");
logging::core::get()->set_filter
(
@ -25,3 +28,9 @@ void init_logging()
logging::add_common_attributes();
}
inline void print_and_log(std::string log_line)
{
BOOST_LOG_TRIVIAL(info) << log_line;
std::cout << log_line << std::endl;
}

View File

@ -8,3 +8,4 @@
#include <boost/log/utility/setup/console.hpp>
void init_logging();
void print_and_log(std::string log_line);

View File

@ -10,6 +10,10 @@
#include "config.hpp"
#include "fs/fs.hpp"
#include "parse/FileContextCache.hpp"
#include "valid/link.hpp"
void run_validate(std::shared_ptr<boost::program_options::variables_map> config);
int main(int argc, const char *argv[]) {
@ -24,6 +28,24 @@ int main(int argc, const char *argv[]) {
if(config)
{
if (config->count("command") == 1)
{
auto command = (*config)["command"].as<std::string>();
if (command == "validate")
{
run_validate(config);
}
}
else
{
BOOST_LOG_TRIVIAL(info) << "command not found";
}
}
}
void run_validate(std::shared_ptr<boost::program_options::variables_map> config)
{
auto env_path = (*config)["path"].as<std::string>();
BOOST_LOG_TRIVIAL(info) << "Loading knowledge base from " << env_path;
@ -31,32 +53,5 @@ int main(int argc, const char *argv[]) {
file_cache.load(env_path);
file_cache.parse_all();
auto context = file_cache.get()[(*config)["index"].as<int>()];
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 << " " << 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;
}
}
kc::validate_links(file_cache.get());
}

View File

@ -16,12 +16,20 @@ Link::Link(std::string original)
link = original_form.substr(opening_link + 1, closing_link - opening_link - 1);
auto display_pos = original_form.find('#', opening_link);
external = link.starts_with("http");
if(display_pos != std::string::npos)
auto sublink_pos = original_form.find('#', opening_link);
if(sublink_pos != std::string::npos)
{
display = original_form.substr(display_pos + 1, closing_link - display_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);
}
}
bool Link::is_external() const
{
return external;
}
}

View File

@ -12,10 +12,13 @@ class Link {
std::string display;
std::string link;
std::string sublink;
bool is_external() const;
Link(std::string original);
private:
bool external;
};
}

38
src/valid/link.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "link.hpp"
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
namespace kc {
std::vector<kc::FileLinkStateResult> validate_links(const std::vector<std::shared_ptr<kc::FileContext>> &contexts)
{
std::vector<kc::FileLinkStateResult> ret;
for (auto context : contexts)
{
if (context->links.size() > 0)
{
for (auto link: context->links)
{
if(!link.is_external()) {
auto composed = context->file_entry->file_entry.path().parent_path() / fs::path(link.link);
auto entry = fs::directory_entry(composed);
if(!entry.exists())
{
std::cout << link.link << " + " << context->file_entry->file_entry.path() << " = " << composed << std::endl;
}
}
}
}
}
return ret;
}
}

25
src/valid/link.hpp Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <memory>
#include <vector>
#include "../parse/FileContext.hpp"
namespace kc {
enum LinkState {
VALID, INVALID
};
struct LinkStateResult {
LinkState link_state;
kc::Link link;
};
struct FileLinkStateResult {
std::shared_ptr<FileContext> file_context;
std::vector<LinkStateResult> link_states;
};
std::vector<FileLinkStateResult> validate_links(const std::vector<std::shared_ptr<kc::FileContext>> &contexts);
}