startgitStatic page generator for git repositories |
git clone git://git.dimitrijedobrota.com/startgit.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING | |
commit | aaa9cce708bd10b35e040b3c9a95cf1a3b4659c9 |
parent | 80d5e3b1a3bde33e5c9ced4778267bf8a362e56c |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Fri, 24 Jan 2025 20:47:59 +0100 |
Translate github url to local ones
Diffstat:M | CMakeLists.txt | | | +- |
M | source/arguments.hpp | | | + |
M | source/html.cpp | | | +++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | source/main.cpp | | | ++++ |
4 files changed, 57 insertions(+), 1 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)
project(
startgit
VERSION 0.1.33
VERSION 0.1.34
DESCRIPTION "Static page generator for git repositories"
HOMEPAGE_URL "https://git.dimitrijedobrota.com/stargit.git"
LANGUAGES CXX
diff --git a/source/arguments.hpp b/source/arguments.hpp
@@ -17,6 +17,7 @@ struct arguments_t
std::string author = "Dimitrije Dobrota";
std::string title = "Collection of git repositories";
std::string description = "Publicly available personal projects";
std::string github = "DimitrijeDobrota";
std::unordered_set<std::filesystem::path> special = {
"BUILDING.md",
"CODE_OF_CONDUCT.md",
diff --git a/source/html.cpp b/source/html.cpp
@@ -153,12 +153,63 @@ void md_html::render_html_escaped(const MD_CHAR* data, MD_SIZE size)
}
}
std::string translate_url(const MD_CHAR* data, MD_SIZE size)
{
auto url = std::string(data, size);
if (url.rfind("http", 0) != std::string::npos
|| url.rfind("www", 0) != std::string::npos)
{
const std::string github = "github.com/" + startgit::args.github;
const std::size_t gpos = url.find(github);
if (gpos != std::string::npos) {
url = startgit::args.base_url + url.substr(gpos + github.size());
static const std::string blob = "/blob";
const std::size_t bpos = url.find(blob);
if (bpos != std::string::npos) {
url.replace(bpos, blob.size(), "");
const std::size_t rslash = url.rfind('/');
auto itr = startgit::args.special.find(url.substr(rslash + 1));
if (itr != startgit::args.special.end()) {
auto cpy = *itr;
url = std::format("{}/{}.html",
url.substr(0, rslash),
cpy.replace_extension().string());
} else {
const std::size_t slash = url.find('/', bpos + 1);
url.replace(slash, 1, "/file/");
url += ".html";
}
} else {
url += "/master/log.html";
}
}
} else {
auto itr = startgit::args.special.find(url);
if (itr != startgit::args.special.end()) {
auto cpy = *itr;
url = std::format("./{}.html", cpy.replace_extension().string());
} else {
url = std::format("./file/{}.html", url);
}
}
return url;
}
void md_html::render_url_escaped(const MD_CHAR* data, MD_SIZE size)
{
static const MD_CHAR* hex_chars = "0123456789ABCDEF";
MD_OFFSET beg = 0;
MD_OFFSET off = 0;
const auto url = translate_url(data, size);
size = static_cast<unsigned>(url.size());
data = url.data();
while (true) {
while (off < size && !md_html::need_url_esc(data[off])) { // NOLINT
off++;
diff --git a/source/main.cpp b/source/main.cpp
@@ -745,6 +745,9 @@ int parse_opt(int key, const char* arg, poafloc::Parser* parser)
case 'd':
l_args->description = arg;
break;
case 'g':
l_args->github = arg;
break;
case 'f':
l_args->force = true;
break;
@@ -779,6 +782,7 @@ static const poafloc::option_t options[] = {
{"output", 'o', "DIR", 0, "Output directory"},
{"force", 'f', 0, 0, "Force write even if file exists"},
{"special", 's', "NAME", 0, "Comma separated files to be rendered to html"},
{"github", 'g', "USERNAME", 0, "Github username for url translation"},
{0, 0, 0, 0, "General information", 2},
{"base", 'b', "URL", 0, "Absolute destination URL"},
{"resource", 'r', "URL", 0, "URL that houses styles and scripts"},