compiler-cli
compiler-core
···
2
2
3
3
use camino::{Utf8Path, Utf8PathBuf};
4
4
5
5
-
use crate::{cli, hex::ApiKeyCommand, http::HttpClient};
5
5
+
use crate::{cli, fs::ProjectIO, hex::ApiKeyCommand, http::HttpClient};
6
6
use gleam_core::{
7
7
analyse::TargetSupport,
8
8
build::{Codegen, Mode, Options, Package},
···
129
129
config,
130
130
compiled.modules.as_slice(),
131
131
&pages,
132
132
+
ProjectIO::new(),
132
133
SystemTime::now(),
133
134
);
134
135
···
14
14
config::{DocsPage, PackageConfig},
15
15
docs::source_links::SourceLinker,
16
16
format,
17
17
-
io::{Content, OutputFile},
17
17
+
io::{Content, FileSystemReader, OutputFile},
18
18
package_interface::PackageInterface,
19
19
paths::ProjectPaths,
20
20
pretty,
···
29
29
30
30
const MAX_COLUMNS: isize = 65;
31
31
32
32
-
pub fn generate_html(
32
32
+
pub fn generate_html<IO: FileSystemReader>(
33
33
paths: &ProjectPaths,
34
34
config: &PackageConfig,
35
35
analysed: &[Module],
36
36
docs_pages: &[DocsPage],
37
37
+
fs: IO,
37
38
rendering_timestamp: SystemTime,
38
39
) -> Vec<OutputFile> {
39
40
let modules = analysed
···
91
92
92
93
// Generate user-supplied (or README) pages
93
94
for page in docs_pages {
94
94
-
let content = std::fs::read_to_string(&page.source).unwrap_or_default();
95
95
+
let content = fs.read(&page.source).unwrap_or_default();
95
96
let rendered_content = render_markdown(&content, MarkdownSource::Standalone);
96
97
let unnest = page_unnest(&page.path);
97
98
···
1
1
---
2
2
source: compiler-core/src/docs/tests.rs
3
3
-
expression: "compile_with_docs_pages(config, vec![], pages)"
3
3
+
expression: "compile_with_markdown_pages(config, vec![], pages)"
4
4
---
5
5
-
//// one.md.html
5
5
+
//// one.html
6
6
7
7
<!DOCTYPE html>
8
8
<html>
9
9
<head>
10
10
<meta charset="utf-8"/>
11
11
<meta name="viewport" content="width=device-width, initial-scale=1"/>
12
12
-
<title>one.md · · v0.1.0</title>
12
12
+
<title>one · · v0.1.0</title>
13
13
<meta name="description" content=""/>
14
14
<meta name="theme-color" content="#ffaff3" media="(prefers-color-scheme: light)"/>
15
15
<meta name="theme-color" content="#33384d" media="(prefers-color-scheme: dark)"/>
···
208
208
<h2>Pages</h2>
209
209
<ul>
210
210
211
211
-
<li><a href="./one.md.html">one.md</a></li>
211
211
+
<li><a href="./one.html">one</a></li>
212
212
213
213
</ul>
214
214
215
215
216
216
217
217
+
<h2>Links</h2>
218
218
+
<ul>
219
219
+
220
220
+
<li><a href="https://hex.pm/packages/">Hex</a></li>
221
221
+
222
222
+
</ul>
223
223
+
217
224
218
225
<h2>Modules</h2>
219
226
<ul>
···
225
232
226
233
<main class="content">
227
234
235
235
+
<p>This is an example code snippet that should be indented</p>
236
236
+
<pre><code class="language-gleam">pub fn indentation_test() {
237
237
+
todo as "This line should be indented by two spaces"
238
238
+
}
239
239
+
</code></pre>
228
240
229
241
230
242
</main>
···
13
13
use ecow::EcoString;
14
14
use itertools::Itertools;
15
15
16
16
-
fn compile_with_docs_pages(
16
16
+
fn compile_with_markdown_pages(
17
17
config: PackageConfig,
18
18
modules: Vec<(&str, &str)>,
19
19
-
docs_pages: Vec<(&str, &str)>,
19
19
+
markdown_pages: Vec<(&str, &str)>,
20
20
) -> EcoString {
21
21
let fs = InMemoryFileSystem::new();
22
22
for (name, src) in modules {
23
23
-
fs.write(&Utf8PathBuf::from(format!("/src/{}", name)), src)
23
23
+
fs.write(&Utf8PathBuf::from(format!("/src/{name}")), src)
24
24
+
.unwrap();
25
25
+
}
26
26
+
27
27
+
// We're saving the pages under a different `InMemoryFileSystem` for these
28
28
+
// tests so we don't have to juggle with borrows and lifetimes.
29
29
+
// The package compiler is going to take ownership of `fs` but later
30
30
+
// `generate_html` also needs a `FileSystemReader` to go and read the
31
31
+
// markdown pages' content.
32
32
+
let pages_fs = InMemoryFileSystem::new();
33
33
+
for (title, src) in markdown_pages.iter() {
34
34
+
pages_fs
35
35
+
.write(&Utf8PathBuf::from(format!("{title}.md")), src)
24
36
.unwrap();
25
37
}
26
38
···
53
65
module.attach_doc_and_module_comments();
54
66
}
55
67
56
56
-
let docs_pages = docs_pages
68
68
+
let docs_pages = markdown_pages
57
69
.into_iter()
58
58
-
.map(|(title, source)| DocsPage {
70
70
+
.map(|(title, _)| DocsPage {
59
71
title: (*title).into(),
60
60
-
path: format!("{}.html", title).into(),
61
61
-
source: (*source).into(),
72
72
+
path: format!("{title}.html"),
73
73
+
source: format!("{title}.md").into(),
62
74
})
63
75
.collect_vec();
64
76
···
67
79
&config,
68
80
&modules,
69
81
&docs_pages,
82
82
+
pages_fs,
70
83
SystemTime::UNIX_EPOCH,
71
84
)
72
85
.into_iter()
···
87
100
}
88
101
89
102
pub fn compile(config: PackageConfig, modules: Vec<(&str, &str)>) -> EcoString {
90
90
-
compile_with_docs_pages(config, modules, vec![])
103
103
+
compile_with_markdown_pages(config, modules, vec![])
91
104
}
92
105
93
106
#[test]
···
206
219
fn markdown_code_from_standalone_pages_is_not_trimmed() {
207
220
let config = PackageConfig::default();
208
221
let pages = vec![(
209
209
-
"one.md",
210
210
-
"```gleam
211
211
-
testing
212
212
-
indentation
222
222
+
"one",
223
223
+
"
224
224
+
This is an example code snippet that should be indented
225
225
+
```gleam
226
226
+
pub fn indentation_test() {
227
227
+
todo as \"This line should be indented by two spaces\"
228
228
+
}
213
229
```",
214
230
)];
215
215
-
insta::assert_snapshot!(compile_with_docs_pages(config, vec![], pages));
231
231
+
insta::assert_snapshot!(compile_with_markdown_pages(config, vec![], pages));
232
232
+
}
233
233
+
234
234
+
#[test]
235
235
+
fn markdown_code_from_function_comment_is_trimmed() {
236
236
+
let config = PackageConfig::default();
237
237
+
let modules = vec![(
238
238
+
"app.gleam",
239
239
+
"
240
240
+
/// Here's an example code snippet:
241
241
+
/// ```
242
242
+
/// wibble
243
243
+
/// |> wobble
244
244
+
/// ```
245
245
+
///
246
246
+
pub fn indentation_test() {
247
247
+
todo
248
248
+
}
249
249
+
",
250
250
+
)];
251
251
+
insta::assert_snapshot!(compile(config, modules));
252
252
+
}
253
253
+
254
254
+
#[test]
255
255
+
fn markdown_code_from_module_comment_is_trimmed() {
256
256
+
let config = PackageConfig::default();
257
257
+
let modules = vec![(
258
258
+
"app.gleam",
259
259
+
"
260
260
+
//// Here's an example code snippet:
261
261
+
//// ```
262
262
+
//// wibble
263
263
+
//// |> wobble
264
264
+
//// ```
265
265
+
////
266
266
+
",
267
267
+
)];
268
268
+
insta::assert_snapshot!(compile(config, modules));
216
269
}