Skip to content

Commit bf08160

Browse files
committed
canonicalize paths
1 parent 24f9ef5 commit bf08160

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

crates/lad_backends/mdbook_lad_preprocessor/src/argument_visitor.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//! Defines a visitor for function arguments of the `LAD` format.
22
3+
use std::path::PathBuf;
4+
35
use ladfile::{ArgumentVisitor, LadTypeId};
46

57
use crate::markdown::MarkdownBuilder;
68

79
pub(crate) struct MarkdownArgumentVisitor<'a> {
810
ladfile: &'a ladfile::LadFile,
911
buffer: MarkdownBuilder,
10-
linkifier: Box<dyn Fn(LadTypeId, &'a ladfile::LadFile) -> Option<String> + 'static>,
12+
linkifier: Box<dyn Fn(LadTypeId, &'a ladfile::LadFile) -> Option<PathBuf> + 'static>,
1113
pub raw_type_id_replacement: Option<&'static str>,
1214
}
1315
impl<'a> MarkdownArgumentVisitor<'a> {
@@ -25,7 +27,7 @@ impl<'a> MarkdownArgumentVisitor<'a> {
2527

2628
/// Create a new instance of the visitor with a custom linkifier function
2729
pub fn new_with_linkifier<
28-
F: Fn(LadTypeId, &'a ladfile::LadFile) -> Option<String> + 'static,
30+
F: Fn(LadTypeId, &'a ladfile::LadFile) -> Option<PathBuf> + 'static,
2931
>(
3032
ladfile: &'a ladfile::LadFile,
3133
linkifier: F,
@@ -69,6 +71,23 @@ impl ArgumentVisitor for MarkdownArgumentVisitor<'_> {
6971
let link_value = (self.linkifier)(type_id.clone(), self.ladfile);
7072
let link_display = type_identifier;
7173
if let Some(link_value) = link_value {
74+
// canonicalize to linux paths
75+
let link_value = link_value
76+
.components()
77+
.map(|c| match c {
78+
std::path::Component::RootDir => "".to_string(),
79+
std::path::Component::CurDir => ".".to_string(),
80+
std::path::Component::ParentDir => "..".to_string(),
81+
std::path::Component::Prefix(prefix_component) => {
82+
prefix_component.as_os_str().to_string_lossy().to_string()
83+
}
84+
std::path::Component::Normal(os_str) => {
85+
os_str.to_string_lossy().to_string()
86+
}
87+
})
88+
.collect::<Vec<_>>()
89+
.join("/");
90+
7291
self.buffer.link(link_display, link_value);
7392
} else {
7493
self.buffer.text(link_display);
@@ -149,10 +168,10 @@ mod test {
149168

150169
let mut visitor =
151170
MarkdownArgumentVisitor::new_with_linkifier(&ladfile, |type_id, ladfile| {
152-
Some(format!(
153-
"root/{}",
154-
ladfile.get_type_identifier(&type_id, None)
155-
))
171+
Some(
172+
PathBuf::from("root")
173+
.join(ladfile.get_type_identifier(&type_id, None).to_string()),
174+
)
156175
});
157176

158177
let first_type_id = ladfile.types.first().unwrap().0;

crates/lad_backends/mdbook_lad_preprocessor/src/sections.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<'a> Section<'a> {
286286
}
287287
SectionData::InstancesSummary => {
288288
let instances = self.ladfile.globals.iter().collect::<Vec<_>>();
289-
let types_directory = self.parent_path.join("types").to_string_lossy().to_string();
289+
let types_directory = PathBuf::from("/").join(self.parent_path.join("types"));
290290
vec![SectionItem::InstancesSummary {
291291
instances,
292292
ladfile: self.ladfile,
@@ -355,11 +355,7 @@ impl<'a> Section<'a> {
355355
]
356356
}
357357
SectionData::FunctionDetail { function } => {
358-
let types_directory = self
359-
.parent_path
360-
.join("../types")
361-
.to_string_lossy()
362-
.to_string();
358+
let types_directory = self.parent_path.join("../types");
363359
vec![SectionItem::FunctionDetails {
364360
function,
365361
ladfile: self.ladfile,
@@ -404,7 +400,7 @@ pub enum SectionItem<'a> {
404400
FunctionDetails {
405401
function: &'a LadFunction,
406402
ladfile: &'a ladfile::LadFile,
407-
types_directory: String,
403+
types_directory: PathBuf,
408404
},
409405
TypesSummary {
410406
types: Vec<&'a LadTypeId>,
@@ -414,7 +410,7 @@ pub enum SectionItem<'a> {
414410
InstancesSummary {
415411
ladfile: &'a ladfile::LadFile,
416412
instances: Vec<(&'a Cow<'static, str>, &'a LadInstance)>,
417-
types_directory: String,
413+
types_directory: PathBuf,
418414
},
419415
}
420416

@@ -576,7 +572,7 @@ impl IntoMarkdown for SectionItem<'_> {
576572
move |lad_type_id, ladfile| {
577573
let printed_type =
578574
linkify_filename(print_type(ladfile, &lad_type_id));
579-
Some(format!("/{types_directory}/{printed_type}.md"))
575+
Some(types_directory.join(printed_type).with_extension("md"))
580576
},
581577
);
582578
arg_visitor.visit(&v.type_kind);
@@ -650,7 +646,7 @@ impl IntoMarkdown for SectionItem<'_> {
650646
idx,
651647
arg,
652648
ladfile,
653-
types_directory,
649+
types_directory.clone(),
654650
builder,
655651
);
656652
}
@@ -663,7 +659,7 @@ impl IntoMarkdown for SectionItem<'_> {
663659
0,
664660
&function.return_type,
665661
ladfile,
666-
types_directory,
662+
types_directory.clone(),
667663
builder,
668664
)
669665
});
@@ -676,7 +672,7 @@ fn build_lad_function_argument_row(
676672
idx: usize,
677673
arg: &LadArgument,
678674
ladfile: &LadFile,
679-
types_directory: &str,
675+
types_directory: PathBuf,
680676
builder: &mut TableBuilder,
681677
) {
682678
// we exclude function call context as it's not something scripts pass down
@@ -691,7 +687,7 @@ fn build_lad_function_argument_row(
691687
let mut arg_visitor =
692688
MarkdownArgumentVisitor::new_with_linkifier(ladfile, move |lad_type_id, ladfile| {
693689
let printed_type = linkify_filename(print_type(ladfile, &lad_type_id));
694-
Some(format!("{}/{}.md", types_directory, printed_type))
690+
Some(types_directory.join(printed_type).with_extension("md"))
695691
});
696692
arg_visitor.visit(&arg.kind);
697693
let markdown = build_escaped_visitor(arg_visitor);

0 commit comments

Comments
 (0)