wdl-doc 0.16.0

Documentation generator for Workflow Description Language (WDL) documents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Create HTML documentation for WDL tasks.

use std::path::PathBuf;

use maud::Markup;
use maud::html;
use wdl_ast::AstNode;
use wdl_ast::AstToken;
use wdl_ast::SupportedVersion;
use wdl_ast::v1::CommandSection;
use wdl_ast::v1::RuntimeSection;
use wdl_ast::v1::TaskDefinition;

use super::*;
use crate::command_section::CommandSectionExt;
use crate::docs_tree::Header;
use crate::docs_tree::PageSections;
use crate::meta::DESCRIPTION_KEY;
use crate::meta::main_container;
use crate::meta::parse_metadata_items;
use crate::page::DeclarationHero;
use crate::parameter::Parameter;

/// A task in a WDL document.
#[derive(Debug)]
pub struct Task {
    /// The name of the task.
    name: String,
    /// The [`VersionBadge`] which displays the WDL version of the task.
    version: VersionBadge,
    /// The meta of the task.
    meta: MetaMap,
    /// The input parameters of the task.
    inputs: Vec<Parameter>,
    /// The output parameters of the task.
    outputs: Vec<Parameter>,
    /// The runtime section of the task.
    runtime_section: Option<RuntimeSection>,
    /// The command section of the task.
    command_section: Option<CommandSection>,
    /// The path from the root of the WDL workspace to the WDL document which
    /// contains this task.
    ///
    /// Used to render the "run with" component.
    wdl_path: Option<PathBuf>,
}

impl DefinitionMeta for Task {
    fn meta(&self) -> &MetaMap {
        &self.meta
    }
}

impl Task {
    /// Create a new task.
    ///
    /// If `wdl_path` is omitted, no "run with" component will be
    /// rendered.
    pub fn new(
        name: String,
        version: SupportedVersion,
        definition: TaskDefinition,
        wdl_path: Option<PathBuf>,
        enable_doc_comments: bool,
    ) -> Self {
        let mut meta = match definition.metadata() {
            Some(mds) => parse_metadata_items(mds.items()),
            _ => MetaMap::default(),
        };

        if enable_doc_comments && let Some(comments) = definition.doc_comments() {
            // Doc comments take precedence
            meta.append(&mut doc_comments(comments));
        }

        let parameter_meta = match definition.parameter_metadata() {
            Some(pmds) => parse_metadata_items(pmds.items()),
            _ => MetaMap::default(),
        };
        let inputs = match definition.input() {
            Some(is) => parse_inputs(&is, &parameter_meta, enable_doc_comments),
            _ => Vec::new(),
        };
        let outputs = match definition.output() {
            Some(os) => parse_outputs(&os, &meta, &parameter_meta, enable_doc_comments),
            _ => Vec::new(),
        };

        Self {
            name,
            version: VersionBadge::new(version),
            meta,
            inputs,
            outputs,
            runtime_section: definition.runtime(),
            command_section: definition.command(),
            wdl_path,
        }
    }

    /// Render the meta section of the task as HTML.
    ///
    /// This will render all metadata key-value pairs except for `description`
    /// and `outputs`.
    pub fn render_meta(&self, _assets: &Path) -> Option<Markup> {
        self.meta().render_remaining(&[DESCRIPTION_KEY, "outputs"])
    }

    /// Render the runtime section of the task as HTML.
    pub fn render_runtime_section(&self) -> Markup {
        match &self.runtime_section {
            Some(runtime_section) => {
                let rows = runtime_section
                    .items()
                    .map(|entry| {
                        {
                            html! {
                                div class="main__grid-row" {
                                    div class="main__grid-cell" {
                                        code { (entry.name().text()) }
                                    }
                                    div class="main__grid-cell" {
                                        code { ({let e = entry.expr(); e.text().to_string()}) }
                                    }
                                }
                            }
                        }
                        .into_string()
                    })
                    .collect::<Vec<_>>()
                    .join(&html! { div class="main__grid-row-separator" {} }.into_string());

                html! {
                    div class="main__section" {
                        h2 id="runtime" class="main__section-header" { "Default Runtime Attributes" }
                        div class="main__grid-container" {
                            div class="main__grid-runtime-container" {
                                div class="main__grid-header-cell" { "Attribute" }
                                div class="main__grid-header-cell" { "Value" }
                                div class="main__grid-header-separator" {}
                                (PreEscaped(rows))
                            }
                        }
                    }
                }
            }
            _ => {
                html! {}
            }
        }
    }

    /// Render the command section of the task as HTML.
    pub fn render_command_section(&self) -> Markup {
        match &self.command_section {
            Some(command_section) => {
                html! {
                    div class="main__section" {
                        h2 id="command" class="main__section-header" { "Command" }
                        sprocket-code language="bash" class="pt-8" copyable expandable line-numbers {
                            (command_section.script())
                        }
                    }
                }
            }
            _ => {
                html! {}
            }
        }
    }

    /// Render the task as HTML.
    pub fn render(
        &self,
        assets: &Path,
        links: &PageLinkIndex,
        page_dir: &Path,
    ) -> (Markup, PageSections) {
        let mut headers = PageSections::default();

        let (input_markup, inner_headers) = self.render_inputs(assets, links, page_dir);
        headers.extend(inner_headers);

        let mut hero = DeclarationHero::new("Task", self.name(), self.render_description(false))
            .kind_class("text-brand-violet-400")
            .pagefind_type("task")
            .badge(self.render_version());
        if let Some(path) = self.wdl_path.as_deref() {
            hero = hero.source_path(path);
        }

        let markup = html! {
            (hero.render(assets))
            @if let Some(body) = self.meta().render_authored_body(assets) {
                (body)
            }
            (self.render_run_with(assets))
            @if let Some(meta) = self.render_meta(assets) {
                div class="main__section" {
                    (meta)
                }
            }
            (input_markup)
            (self.render_outputs(assets, links, page_dir))
            (self.render_runtime_section())
            (self.render_command_section())
        };
        headers.push(Header::Header("Outputs".to_string(), "outputs".to_string()));
        headers.push(Header::Header("Runtime".to_string(), "runtime".to_string()));
        headers.push(Header::Header("Command".to_string(), "command".to_string()));

        (
            main_container("task", self.wdl_path.is_none(), markup),
            headers,
        )
    }
}

impl Runnable for Task {
    fn name(&self) -> &str {
        &self.name
    }

    fn version(&self) -> &VersionBadge {
        &self.version
    }

    fn inputs(&self) -> &[Parameter] {
        &self.inputs
    }

    fn outputs(&self) -> &[Parameter] {
        &self.outputs
    }

    fn wdl_path(&self) -> Option<&Path> {
        self.wdl_path.as_deref()
    }
}

#[cfg(test)]
mod tests {
    use wdl_ast::Document;
    use wdl_ast::version::V1;

    use super::*;

    #[test]
    fn test_task() {
        let (doc, _) = Document::parse(
            r#"
            version 1.0

            ## This comment should be ignored.
            task my_task {
                input {
                    String name
                }
                output {
                    String greeting = "Hello, ${name}!"
                }
                runtime {
                    docker: "ubuntu:latest"
                }
                meta {
                    description: "A simple task"
                }
            }
            "#,
            None,
        );

        let doc_item = doc.ast().into_v1().unwrap().items().next().unwrap();
        let ast_task = doc_item.into_task_definition().unwrap();

        let task = Task::new(
            ast_task.name().text().to_owned(),
            SupportedVersion::V1(V1::Zero),
            ast_task,
            None,
            false,
        );

        assert_eq!(task.name(), "my_task");
        assert_eq!(
            task.meta()
                .get("description")
                .unwrap()
                .clone()
                .into_meta()
                .unwrap()
                .unwrap_string()
                .text()
                .unwrap()
                .text(),
            "A simple task"
        );
        assert_eq!(task.inputs().len(), 1);
        assert_eq!(task.outputs().len(), 1);
    }

    /// Parses a single task from `source` and builds a [`Task`] documented at
    /// `wdl_path`.
    fn task_at_path(source: &str, wdl_path: &str) -> Task {
        let (doc, _) = Document::parse(source, None);
        let doc_item = doc.ast().into_v1().unwrap().items().next().unwrap();
        let ast_task = doc_item.into_task_definition().unwrap();
        Task::new(
            ast_task.name().text().to_owned(),
            SupportedVersion::V1(V1::Zero),
            ast_task,
            Some(PathBuf::from(wdl_path)),
            false,
        )
    }

    #[test]
    fn run_with_control_has_static_initial_markup() {
        // A nested path contains a separator, so the Unix/Windows toggle is
        // offered.
        let task = task_at_path(
            r#"
            version 1.0
            task my_task {
                command <<<
                echo hello
                >>>
            }
            "#,
            "modules/tasks.wdl",
        );
        let html = task.render_run_with(Path::new("assets")).into_string();

        assert!(html.contains("main__run-with-toggle-label--unix"));
        assert!(html.contains("main__run-with-toggle-label--windows"));
        assert!(html.contains("main__run-with-path--unix"));
        assert!(html.contains("main__run-with-path--windows"));
        assert!(!html.contains("x-bind:class"));
        assert!(!html.contains("x-show"));
    }

    #[test]
    fn run_with_control_omits_toggle_without_path_separator() {
        // A top-level file has no path separator, so the Unix/Windows paths are
        // identical and the toggle must not be offered.
        let task = task_at_path(
            r#"
            version 1.0
            task my_task {
                command <<<
                echo hello
                >>>
            }
            "#,
            "tasks.wdl",
        );
        let html = task.render_run_with(Path::new("assets")).into_string();

        assert!(html.contains("main__run-with-container"));
        assert!(!html.contains("main__run-with-toggle"));
    }

    #[test]
    fn task_with_doc_comments() {
        let (doc, _) = Document::parse(
            r#"
            version 1.0

            ## This is my task. It greets people.
            task my_task {
                input {
                    ## The name to greet.
                    String name
                }
                output {
                    ## The generated greeting.
                    String greeting = "Hello, ${name}!"
                }
                runtime {
                    docker: "ubuntu:latest"
                }
                meta {
                    description: "This description should be overwritten."
                }
            }
            "#,
            None,
        );

        let doc_item = doc.ast().into_v1().unwrap().items().next().unwrap();
        let ast_task = doc_item.into_task_definition().unwrap();

        let task = Task::new(
            ast_task.name().text().to_owned(),
            SupportedVersion::V1(V1::Zero),
            ast_task,
            None,
            true,
        );

        assert_eq!(task.name(), "my_task");
        assert_eq!(
            task.meta()
                .get("description")
                .unwrap()
                .clone()
                .text()
                .unwrap(),
            "This is my task. It greets people."
        );
        assert_eq!(task.inputs().len(), 1);
        let input = &task.inputs()[0];
        assert_eq!(
            input
                .meta()
                .get("description")
                .unwrap()
                .clone()
                .text()
                .unwrap(),
            "The name to greet."
        );

        assert_eq!(task.outputs().len(), 1);
        let output = &task.outputs()[0];
        assert_eq!(
            output
                .meta()
                .get("description")
                .unwrap()
                .clone()
                .text()
                .unwrap(),
            "The generated greeting."
        );
    }
}