@@ -371,9 +371,11 @@ namespace mdsplit {
371
371
// [some text](URL) -> std::regex{R"([^!]*\[(.*)\]\((.*)\))"};
372
372
//  -> std::regex{R"(!\[(.*)\]\((.*)\))"};
373
373
std::regex url_or_img_regex{R"( \[([^\[\]]*)\]\(([^\)]*)\))" };
374
+
374
375
// [](examples/line_plot/area/area_1.cpp)
375
376
std::regex second_order_url_or_img_regex{
376
377
R"( \[(!?)\[([^\[\]]*)\]\(([^\)]*)\)\]\(([^\)]*)\))" };
378
+
377
379
std::vector<std::pair<std::regex, size_t >> url_regexes = {
378
380
{url_or_img_regex, 2 }, {second_order_url_or_img_regex, 4 }};
379
381
@@ -387,17 +389,17 @@ namespace mdsplit {
387
389
if (in_codeblock) {
388
390
continue ;
389
391
}
390
- std::smatch match ;
392
+ std::smatch matches ;
391
393
auto line_begin = line.cbegin ();
392
394
auto line_end = line.cend ();
393
395
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 ,
395
397
url_regex)) {
396
398
size_t line_begin_pos = line_begin - line.cbegin ();
397
- std::string url = match [url_idx];
399
+ std::string url = matches [url_idx];
398
400
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 ) {
401
403
bool is_single_dot_directory =
402
404
url.rfind (" ./" , 0 ) == 0 ;
403
405
if (is_single_dot_directory) {
@@ -413,18 +415,30 @@ namespace mdsplit {
413
415
}
414
416
size_t pos =
415
417
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 (),
418
420
relative_url.string ());
419
421
replacement_size = relative_url.string ().size ();
420
422
} else {
423
+ // look for section
421
424
std::string header_slug = url.substr (1 );
422
425
auto it = std::find_if (
423
426
sections_.begin (), sections_.end (),
424
427
[&](const mdsection &s) {
425
428
return slugify (s.header_name ) ==
426
429
header_slug;
427
430
});
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
428
442
if (it != sections_.end ()) {
429
443
fs::path absolute_destination =
430
444
fs::current_path () / it->filepath ;
@@ -437,12 +451,14 @@ namespace mdsplit {
437
451
}
438
452
size_t pos = std::distance (line.cbegin (),
439
453
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 (),
442
456
relative_url.string ());
443
457
replacement_size =
444
458
relative_url.string ().size ();
445
459
} else {
460
+ // not link to a valid section
461
+ // we hope we didn't get here
446
462
if (trace_) {
447
463
std::cout
448
464
<< " Cannot find file for a header "
@@ -460,10 +476,10 @@ namespace mdsplit {
460
476
replacement_size = url.size ();
461
477
}
462
478
if (replacement_size == 0 ) {
463
- replacement_size = match [url_idx].length ();
479
+ replacement_size = matches [url_idx].length ();
464
480
}
465
481
size_t new_begin_pos = line_begin_pos +
466
- match .position (url_idx) +
482
+ matches .position (url_idx) +
467
483
replacement_size;
468
484
if (new_begin_pos < line.size ()) {
469
485
line_begin = line.cbegin () + new_begin_pos;
@@ -711,16 +727,19 @@ namespace mdsplit {
711
727
const fs::path &base) {
712
728
fs::path base_dir = fs::is_directory (base) ? base : base.parent_path ();
713
729
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) {
715
732
fs::path r = fs::relative (path, base_dir);
716
733
std::string ext = r.extension ().string ();
717
734
// GitHub does not convert ".md#anchor" links automatically
735
+ /*
718
736
bool is_anchor_inside_docs = ext.rfind(".md#", 0) == 0;
719
737
if (is_anchor_inside_docs) {
720
738
ext.replace(0, 4, ".html#");
721
739
r = r.parent_path() / r.stem();
722
740
r += ext;
723
741
}
742
+ */
724
743
return r;
725
744
} else {
726
745
bool is_in_project_dir = is_subdirectory (path, fs::current_path ());
0 commit comments