Skip to content

Commit 544a27c

Browse files
committed
Improve matches for ./docs directory
1 parent 7cc41a7 commit 544a27c

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

source/mdsplit/mdsplitter.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,11 @@ namespace mdsplit {
371371
// [some text](URL) -> std::regex{R"([^!]*\[(.*)\]\((.*)\))"};
372372
// ![some text](URL) -> std::regex{R"(!\[(.*)\]\((.*)\))"};
373373
std::regex url_or_img_regex{R"(\[([^\[\]]*)\]\(([^\)]*)\))"};
374+
374375
// [![example_area_1](docs/examples/line_plot/area/area_1.svg)](examples/line_plot/area/area_1.cpp)
375376
std::regex second_order_url_or_img_regex{
376377
R"(\[(!?)\[([^\[\]]*)\]\(([^\)]*)\)\]\(([^\)]*)\))"};
378+
377379
std::vector<std::pair<std::regex, size_t>> url_regexes = {
378380
{url_or_img_regex, 2}, {second_order_url_or_img_regex, 4}};
379381

@@ -387,17 +389,17 @@ namespace mdsplit {
387389
if (in_codeblock) {
388390
continue;
389391
}
390-
std::smatch match;
392+
std::smatch matches;
391393
auto line_begin = line.cbegin();
392394
auto line_end = line.cend();
393395
size_t replacement_size = 0;
394-
while (std::regex_search(line_begin, line_end, match,
396+
while (std::regex_search(line_begin, line_end, matches,
395397
url_regex)) {
396398
size_t line_begin_pos = line_begin - line.cbegin();
397-
std::string url = match[url_idx];
399+
std::string url = matches[url_idx];
398400
if (!is_external(url)) {
399-
bool same_file = url.rfind('#', 0) == 0;
400-
if (!same_file) {
401+
bool is_anchor = url.rfind('#', 0) == 0;
402+
if (!is_anchor) {
401403
bool is_single_dot_directory =
402404
url.rfind("./", 0) == 0;
403405
if (is_single_dot_directory) {
@@ -413,18 +415,30 @@ namespace mdsplit {
413415
}
414416
size_t pos =
415417
std::distance(line.cbegin(), line_begin) +
416-
match.position(url_idx);
417-
line.replace(pos, match[url_idx].length(),
418+
matches.position(url_idx);
419+
line.replace(pos, matches[url_idx].length(),
418420
relative_url.string());
419421
replacement_size = relative_url.string().size();
420422
} else {
423+
// look for section
421424
std::string header_slug = url.substr(1);
422425
auto it = std::find_if(
423426
sections_.begin(), sections_.end(),
424427
[&](const mdsection &s) {
425428
return slugify(s.header_name) ==
426429
header_slug;
427430
});
431+
432+
// if it's a empty section, point to next
433+
// section this is important for empty
434+
// categories for which mkdocs creates no links
435+
if (it != sections_.end()) {
436+
if (!it->has_content()) {
437+
++it;
438+
}
439+
}
440+
441+
// if link to valid section
428442
if (it != sections_.end()) {
429443
fs::path absolute_destination =
430444
fs::current_path() / it->filepath;
@@ -437,12 +451,14 @@ namespace mdsplit {
437451
}
438452
size_t pos = std::distance(line.cbegin(),
439453
line_begin) +
440-
match.position(url_idx);
441-
line.replace(pos, match[url_idx].length(),
454+
matches.position(url_idx);
455+
line.replace(pos, matches[url_idx].length(),
442456
relative_url.string());
443457
replacement_size =
444458
relative_url.string().size();
445459
} else {
460+
// not link to a valid section
461+
// we hope we didn't get here
446462
if (trace_) {
447463
std::cout
448464
<< "Cannot find file for a header "
@@ -460,10 +476,10 @@ namespace mdsplit {
460476
replacement_size = url.size();
461477
}
462478
if (replacement_size == 0) {
463-
replacement_size = match[url_idx].length();
479+
replacement_size = matches[url_idx].length();
464480
}
465481
size_t new_begin_pos = line_begin_pos +
466-
match.position(url_idx) +
482+
matches.position(url_idx) +
467483
replacement_size;
468484
if (new_begin_pos < line.size()) {
469485
line_begin = line.cbegin() + new_begin_pos;
@@ -711,16 +727,19 @@ namespace mdsplit {
711727
const fs::path &base) {
712728
fs::path base_dir = fs::is_directory(base) ? base : base.parent_path();
713729
bool is_in_docs_dir = is_subdirectory(path, output_dir_);
714-
if (is_in_docs_dir) {
730+
bool is_docs_dir = path == this->output_dir_;
731+
if (is_in_docs_dir && !is_docs_dir) {
715732
fs::path r = fs::relative(path, base_dir);
716733
std::string ext = r.extension().string();
717734
// GitHub does not convert ".md#anchor" links automatically
735+
/*
718736
bool is_anchor_inside_docs = ext.rfind(".md#", 0) == 0;
719737
if (is_anchor_inside_docs) {
720738
ext.replace(0, 4, ".html#");
721739
r = r.parent_path() / r.stem();
722740
r += ext;
723741
}
742+
*/
724743
return r;
725744
} else {
726745
bool is_in_project_dir = is_subdirectory(path, fs::current_path());

0 commit comments

Comments
 (0)