summaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: 012e05a1fc90620009dac8be56243499c8bbbaa7 (plain)
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
use crate::parser::parse_scenario_snippet;
use crate::Bindings;
use crate::MatchedScenario;
use crate::PartialStep;
use crate::Scenario;
use crate::ScenarioStep;
use crate::StepKind;
use crate::{DotMarkup, GraphMarkup, PlantumlMarkup};
use crate::{Result, SubplotError};

use std::collections::HashSet;
use std::ops::Deref;
use std::path::{Path, PathBuf};

use pandoc_ast::{Attr, Block, Inline, Map, MetaValue, MutVisitor, Pandoc};
use serde::{Deserialize, Serialize};

/// The set of known (special) classes which subplot will always recognise
/// as being valid.
static SPECIAL_CLASSES: &[&str] = &["scenario", "file", "dot", "plantuml", "roadmap"];

/// The set of known (file-type) classes which subplot will always recognise
/// as being valid.
static KNOWN_FILE_CLASSES: &[&str] = &["rust", "yaml", "python", "sh", "shell", "markdown", "bash"];

/// The set of known (special-to-pandoc) classes which subplot will always recognise
/// as being valid.
static KNOWN_PANDOC_CLASSES: &[&str] = &["numberLines"];

/// A parsed Subplot document.
///
/// Pandoc works by parsing its various input files and constructing
/// an abstract syntax tree or AST. When Pandoc generates output, it
/// works based on the AST. This way, the input parsing and output
/// generation are cleanly separated.
///
/// A Pandoc filter can modify the AST before output generation
/// starts working. This allows the filter to make changes to what
/// gets output, without having to understand the input documents at
/// all.
///
/// This function is a Pandoc filter, to be use with
/// pandoc_ast::filter, for typesetting Subplot documents.
///
/// # Example
///
/// fix this example;
/// ~~~~
/// let markdown = "\
/// ---
/// title: Test Title
/// ...
/// This is a test document.
/// ";
///
/// use std::io::Write;
/// use tempfile::NamedTempFile;
/// let mut f = NamedTempFile::new().unwrap();
/// f.write_all(markdown.as_bytes()).unwrap();
/// let filename = f.path();
///
/// use subplot;
/// let basedir = std::path::Path::new(".");
/// let doc = subplot::Document::from_file(&basedir, filename).unwrap();
/// assert_eq!(doc.files(), &[]);
/// ~~~~
#[derive(Debug)]
pub struct Document {
    markdowns: Vec<PathBuf>,
    ast: Pandoc,
    meta: Metadata,
    files: DataFiles,
}

impl<'a> Document {
    fn new(markdowns: Vec<PathBuf>, ast: Pandoc, meta: Metadata, files: DataFiles) -> Document {
        Document {
            markdowns,
            ast,
            meta,
            files,
        }
    }

    /// Construct a Document from a JSON AST
    pub fn from_json<P>(basedir: P, markdowns: Vec<PathBuf>, json: &str) -> Result<Document>
    where
        P: AsRef<Path>,
    {
        let mut ast: Pandoc = serde_json::from_str(json)?;
        let meta = Metadata::new(basedir, &ast)?;
        let files = DataFiles::new(&mut ast);
        Ok(Document::new(markdowns, ast, meta, files))
    }

    /// Construct a Document from a named file.
    ///
    /// The file can be in any format Pandoc understands. This runs
    /// Pandoc to parse the file into an AST, so it can be a little
    /// slow.
    pub fn from_file(basedir: &Path, filename: &Path) -> Result<Document> {
        let markdowns = vec![filename.to_path_buf()];

        let mut pandoc = pandoc::new();
        pandoc.add_input(&filename);
        pandoc.set_input_format(
            pandoc::InputFormat::Markdown,
            vec![pandoc::MarkdownExtension::Citations],
        );
        pandoc.set_output_format(pandoc::OutputFormat::Json, vec![]);
        pandoc.set_output(pandoc::OutputKind::Pipe);

        // Add external Pandoc filters.
        let citeproc = std::path::Path::new("pandoc-citeproc");
        pandoc.add_option(pandoc::PandocOption::Filter(citeproc.to_path_buf()));

        let output = match pandoc.execute()? {
            pandoc::PandocOutput::ToFile(_) => return Err(SubplotError::NotJson),
            pandoc::PandocOutput::ToBuffer(o) => o,
        };
        let doc = Document::from_json(basedir, markdowns, &output)?;
        Ok(doc)
    }

    /// Return the AST of a Document, serialized as JSON.
    ///
    /// This is useful in a Pandoc filter, so that the filter can give
    /// it back to Pandoc for typesetting.
    pub fn ast(&self) -> Result<String> {
        let json = serde_json::to_string(&self.ast)?;
        Ok(json)
    }

    /// Return the document's metadata.
    pub fn meta(&self) -> &Metadata {
        &self.meta
    }

    /// Return all source filenames for the document.
    ///
    /// The sources are any files that affect the output so that if
    /// the source file changes, the output needs to be re-generated.
    pub fn sources(&mut self) -> Vec<PathBuf> {
        let mut names = vec![];

        for x in self.meta().bindings_filenames() {
            names.push(PathBuf::from(x))
        }

        for x in self.meta().functions_filenames() {
            names.push(PathBuf::from(x))
        }

        for x in self.meta().bibliographies().iter() {
            names.push(PathBuf::from(x))
        }

        for x in self.markdowns.iter() {
            names.push(x.to_path_buf());
        }

        let mut visitor = ImageVisitor::new();
        visitor.walk_pandoc(&mut self.ast);
        for x in visitor.images().iter() {
            names.push(x.to_path_buf());
        }

        names
    }

    /// Return list of files embeddedin the document.
    pub fn files(&self) -> &[DataFile] {
        self.files.files()
    }

    /// Check the document for common problems.
    pub fn lint(&self) -> Result<()> {
        self.check_doc_has_title()?;
        self.check_filenames_are_unique()?;
        self.check_block_classes()?;
        Ok(())
    }

    // Check that all filenames for embedded files are unique.
    fn check_filenames_are_unique(&self) -> Result<()> {
        let mut known = HashSet::new();
        for filename in self.files().iter().map(|f| f.filename().to_lowercase()) {
            if known.contains(&filename) {
                return Err(SubplotError::DuplicateEmbeddedFilename(filename));
            }
            known.insert(filename);
        }
        Ok(())
    }

    // Check that document has a title in its metadata.
    fn check_doc_has_title(&self) -> Result<()> {
        if self.meta().title().is_empty() {
            Err(SubplotError::NoTitle)
        } else {
            Ok(())
        }
    }

    /// Check that all the block classes in the document are known
    fn check_block_classes(&self) -> Result<()> {
        let mut visitor = BlockClassVisitor::default();
        // Irritatingly we can't immutably visit the AST for some reason
        // This clone() is expensive and unwanted, but I'm not sure how
        // to get around it for now
        visitor.walk_pandoc(&mut self.ast.clone());
        // Build the set of known good classes
        let mut known_classes: HashSet<String> = HashSet::new();
        for class in std::iter::empty()
            .chain(SPECIAL_CLASSES.iter().map(Deref::deref))
            .chain(KNOWN_FILE_CLASSES.iter().map(Deref::deref))
            .chain(KNOWN_PANDOC_CLASSES.iter().map(Deref::deref))
            .chain(self.meta().classes())
        {
            known_classes.insert(class.to_string());
        }
        // Acquire the set of used names which are not known
        let unknown_classes: Vec<_> = visitor
            .classes
            .difference(&known_classes)
            .cloned()
            .collect();
        // If the unknown classes list is not empty, we had a problem and
        // we will report it to the user.
        if !unknown_classes.is_empty() {
            Err(SubplotError::UnknownClasses(unknown_classes.join(", ")))
        } else {
            Ok(())
        }
    }

    /// Typeset a Subplot document.
    pub fn typeset(&mut self) {
        let mut visitor = TypesettingVisitor::new(&self.meta.bindings);
        visitor.walk_pandoc(&mut self.ast);
    }

    /// Return all scenarios in a document.
    pub fn scenarios(&mut self) -> Result<Vec<Scenario>> {
        let mut visitor = StructureVisitor::new();
        visitor.walk_pandoc(&mut self.ast);

        let mut scenarios: Vec<Scenario> = vec![];

        let mut i = 0;
        while i < visitor.elements.len() {
            let (maybe, new_i) = extract_scenario(&visitor.elements[i..])?;
            if let Some(scen) = maybe {
                scenarios.push(scen);
            }
            i += new_i;
        }
        Ok(scenarios)
    }

    /// Return matched scenarios in a document.
    pub fn matched_scenarios(&mut self) -> Result<Vec<MatchedScenario>> {
        let scenarios = self.scenarios()?;
        let bindings = self.meta().bindings();
        let vec: Result<Vec<MatchedScenario>> = scenarios
            .iter()
            .map(|scen| MatchedScenario::new(scen, bindings))
            .collect();
        Ok(vec?)
    }
}

fn extract_scenario(e: &[Element]) -> Result<(Option<Scenario>, usize)> {
    if e.is_empty() {
        // If we get here, it's a programming error.
        panic!("didn't expect empty list of elements");
    }

    match &e[0] {
        Element::Snippet(_) => Err(SubplotError::ScenarioBeforeHeading),
        Element::Heading(title, level) => {
            let mut scen = Scenario::new(&title);
            let mut prevkind = None;
            for (i, item) in e.iter().enumerate().skip(1) {
                match item {
                    Element::Heading(_, level2) => {
                        let is_subsection = *level2 > *level;
                        if is_subsection {
                            if scen.has_steps() {
                            } else {
                                return Ok((None, i));
                            }
                        } else if scen.has_steps() {
                            return Ok((Some(scen), i));
                        } else {
                            return Ok((None, i));
                        }
                    }
                    Element::Snippet(text) => {
                        for line in parse_scenario_snippet(&text) {
                            let step = ScenarioStep::new_from_str(line, prevkind)?;
                            scen.add(&step);
                            prevkind = Some(step.kind());
                        }
                    }
                }
            }
            if scen.has_steps() {
                Ok((Some(scen), e.len()))
            } else {
                Ok((None, e.len()))
            }
        }
    }
}

#[cfg(test)]
mod test_extract {
    use super::extract_scenario;
    use super::Element;
    use crate::Result;
    use crate::Scenario;
    use crate::SubplotError;

    fn h(title: &str, level: i64) -> Element {
        Element::Heading(title.to_string(), level)
    }

    fn s(text: &str) -> Element {
        Element::Snippet(text.to_string())
    }

    fn check_result(r: Result<(Option<Scenario>, usize)>, title: Option<&str>, i: usize) {
        eprintln!("checking result: {:?}", r);
        assert!(r.is_ok());
        let (actual_scen, actual_i) = r.unwrap();
        if title.is_none() {
            assert!(actual_scen.is_none());
        } else {
            assert!(actual_scen.is_some());
            let scen = actual_scen.unwrap();
            assert_eq!(scen.title(), title.unwrap());
        }
        assert_eq!(actual_i, i);
    }

    #[test]
    fn returns_nothing_if_there_is_no_scenario() {
        let elements: Vec<Element> = vec![h("title", 1)];
        let r = extract_scenario(&elements);
        check_result(r, None, 1);
    }

    #[test]
    fn returns_scenario_if_there_is_one() {
        let elements = vec![h("title", 1), s("given something")];
        let r = extract_scenario(&elements);
        check_result(r, Some("title"), 2);
    }

    #[test]
    fn skips_scenarioless_section_in_favour_of_same_level() {
        let elements = vec![h("first", 1), h("second", 1), s("given something")];
        let r = extract_scenario(&elements);
        check_result(r, None, 1);
        let r = extract_scenario(&elements[1..]);
        check_result(r, Some("second"), 2);
    }

    #[test]
    fn returns_parent_section_with_scenario_snippet() {
        let elements = vec![
            h("1", 1),
            s("given something"),
            h("1.1", 2),
            s("when something"),
            h("2", 1),
        ];
        let r = extract_scenario(&elements);
        check_result(r, Some("1"), 4);
        let r = extract_scenario(&elements[4..]);
        check_result(r, None, 1);
    }

    #[test]
    fn skips_scenarioless_parent_heading() {
        let elements = vec![h("1", 1), h("1.1", 2), s("given something"), h("2", 1)];

        let r = extract_scenario(&elements);
        check_result(r, None, 1);

        let r = extract_scenario(&elements[1..]);
        check_result(r, Some("1.1"), 2);

        let r = extract_scenario(&elements[3..]);
        check_result(r, None, 1);
    }

    #[test]
    fn skips_scenarioless_deeper_headings() {
        let elements = vec![h("1", 1), h("1.1", 2), h("2", 1), s("given something")];

        let r = extract_scenario(&elements);
        check_result(r, None, 1);

        let r = extract_scenario(&elements[1..]);
        check_result(r, None, 1);

        let r = extract_scenario(&elements[2..]);
        check_result(r, Some("2"), 2);
    }

    #[test]
    fn returns_error_if_scenario_has_no_title() {
        let elements = vec![s("given something")];
        let r = extract_scenario(&elements);
        match r {
            Err(SubplotError::ScenarioBeforeHeading) => (),
            _ => panic!("unexpected result {:?}", r),
        }
    }
}

/// Metadata of a document, as needed by Subplot.
#[derive(Debug)]
pub struct Metadata {
    title: String,
    date: Option<String>,
    bindings_filenames: Vec<PathBuf>,
    bindings: Bindings,
    functions_filenames: Vec<PathBuf>,
    template: Option<String>,
    bibliographies: Vec<PathBuf>,
    /// Extra class names which should be considered 'correct' for this document
    classes: Vec<String>,
}

impl Metadata {
    /// Construct a Metadata from a Document, if possible.
    pub fn new<P>(basedir: P, doc: &Pandoc) -> Result<Metadata>
    where
        P: AsRef<Path>,
    {
        let title = get_title(&doc.meta)?;
        let date = get_date(&doc.meta);
        let bindings_filenames = get_bindings_filenames(basedir.as_ref(), &doc.meta);
        let functions_filenames = get_functions_filenames(basedir.as_ref(), &doc.meta);
        let template = get_template_name(&doc.meta)?;
        let mut bindings = Bindings::new();

        let bibliographies = get_bibliographies(basedir.as_ref(), &doc.meta);
        let classes = get_classes(&doc.meta);

        get_bindings(&bindings_filenames, &mut bindings)?;
        Ok(Metadata {
            title,
            date,
            bindings_filenames,
            bindings,
            functions_filenames,
            template,
            bibliographies,
            classes,
        })
    }

    /// Return title of document.
    pub fn title(&self) -> &str {
        &self.title
    }

    /// Return date of document, if any.
    pub fn date(&self) -> Option<&str> {
        if let Some(date) = &self.date {
            Some(&date)
        } else {
            None
        }
    }

    /// Return filename where bindings are specified.
    pub fn bindings_filenames(&self) -> Vec<&Path> {
        self.bindings_filenames.iter().map(|f| f.as_ref()).collect()
    }

    /// Return filename where functions are specified.
    pub fn functions_filenames(&self) -> Vec<&Path> {
        self.functions_filenames
            .iter()
            .map(|f| f.as_ref())
            .collect()
    }

    /// Return the name of the code template, if specified.
    pub fn template_name(&self) -> Option<&str> {
        match &self.template {
            Some(x) => Some(&x),
            None => None,
        }
    }

    /// Return the bindings.
    pub fn bindings(&self) -> &Bindings {
        &self.bindings
    }

    /// Return the bibliographies.
    pub fn bibliographies(&self) -> Vec<&Path> {
        self.bibliographies.iter().map(|x| x.as_path()).collect()
    }

    /// The classes which this document also claims are valid
    pub fn classes(&self) -> impl Iterator<Item = &str> {
        self.classes.iter().map(Deref::deref)
    }
}

type Mapp = Map<String, MetaValue>;

fn get_title(map: &Mapp) -> Result<String> {
    if let Some(s) = get_string(map, "title") {
        Ok(s)
    } else {
        Ok("".to_string())
    }
}

fn get_date(map: &Mapp) -> Option<String> {
    if let Some(s) = get_string(map, "date") {
        Some(s)
    } else {
        None
    }
}

fn get_bindings_filenames<P>(basedir: P, map: &Mapp) -> Vec<PathBuf>
where
    P: AsRef<Path>,
{
    get_paths(basedir, map, "bindings")
}

fn get_functions_filenames<P>(basedir: P, map: &Mapp) -> Vec<PathBuf>
where
    P: AsRef<Path>,
{
    get_paths(basedir, map, "functions")
}

fn get_template_name(map: &Mapp) -> Result<Option<String>> {
    match get_string(map, "template") {
        Some(s) => Ok(Some(s)),
        None => Ok(None),
    }
}

fn get_paths<P>(basedir: P, map: &Mapp, field: &str) -> Vec<PathBuf>
where
    P: AsRef<Path>,
{
    match map.get(field) {
        None => vec![],
        Some(v) => pathbufs(basedir, v),
    }
}

fn get_string(map: &Mapp, field: &str) -> Option<String> {
    let v = match map.get(field) {
        None => return None,
        Some(s) => s,
    };
    let v = match v {
        pandoc_ast::MetaValue::MetaString(s) => s.to_string(),
        pandoc_ast::MetaValue::MetaInlines(vec) => join(&vec),
        _ => panic!("don't know how to handle: {:?}", v),
    };
    Some(v)
}

fn get_bibliographies<P>(basedir: P, map: &Mapp) -> Vec<PathBuf>
where
    P: AsRef<Path>,
{
    let v = match map.get("bibliography") {
        None => return vec![],
        Some(s) => s,
    };
    pathbufs(basedir, v)
}

fn pathbufs<P>(basedir: P, v: &MetaValue) -> Vec<PathBuf>
where
    P: AsRef<Path>,
{
    let mut bufs = vec![];
    push_pathbufs(basedir, v, &mut bufs);
    bufs
}

fn get_classes(map: &Mapp) -> Vec<String> {
    let mut ret = Vec::new();
    if let Some(classes) = map.get("classes") {
        push_strings(classes, &mut ret);
    }
    ret
}

fn push_strings(v: &MetaValue, strings: &mut Vec<String>) {
    match v {
        MetaValue::MetaString(s) => strings.push(s.to_string()),
        MetaValue::MetaInlines(vec) => strings.push(join(&vec)),
        MetaValue::MetaList(values) => {
            for value in values {
                push_strings(value, strings);
            }
        }
        _ => panic!("don't know how to handle: {:?}", v),
    };
}

fn push_pathbufs<P>(basedir: P, v: &MetaValue, bufs: &mut Vec<PathBuf>)
where
    P: AsRef<Path>,
{
    match v {
        MetaValue::MetaString(s) => bufs.push(basedir.as_ref().join(Path::new(s))),
        MetaValue::MetaInlines(vec) => bufs.push(basedir.as_ref().join(Path::new(&join(&vec)))),
        MetaValue::MetaList(values) => {
            for value in values {
                push_pathbufs(basedir.as_ref(), value, bufs);
            }
        }
        _ => panic!("don't know how to handle: {:?}", v),
    };
}

fn join(vec: &[Inline]) -> String {
    let mut buf = String::new();
    join_into_buffer(vec, &mut buf);
    buf
}

fn join_into_buffer(vec: &[Inline], buf: &mut String) {
    for item in vec {
        match item {
            pandoc_ast::Inline::Str(s) => buf.push_str(&s),
            pandoc_ast::Inline::Emph(v) => join_into_buffer(v, buf),
            pandoc_ast::Inline::Strong(v) => join_into_buffer(v, buf),
            pandoc_ast::Inline::Strikeout(v) => join_into_buffer(v, buf),
            pandoc_ast::Inline::Superscript(v) => join_into_buffer(v, buf),
            pandoc_ast::Inline::Subscript(v) => join_into_buffer(v, buf),
            pandoc_ast::Inline::SmallCaps(v) => join_into_buffer(v, buf),
            pandoc_ast::Inline::Space => buf.push_str(" "),
            pandoc_ast::Inline::SoftBreak => buf.push_str(" "),
            pandoc_ast::Inline::LineBreak => buf.push_str(" "),
            _ => panic!("unknown pandoc_ast::Inline component {:?}", item),
        }
    }
}

#[cfg(test)]
mod test_join {
    use super::join;
    use pandoc_ast::Inline;

    #[test]
    fn join_all_kinds() {
        let v = vec![
            Inline::Str("a".to_string()),
            Inline::Emph(vec![Inline::Str("b".to_string())]),
            Inline::Strong(vec![Inline::Str("c".to_string())]),
            Inline::Strikeout(vec![Inline::Str("d".to_string())]),
            Inline::Superscript(vec![Inline::Str("e".to_string())]),
            Inline::Subscript(vec![Inline::Str("f".to_string())]),
            Inline::SmallCaps(vec![Inline::Str("g".to_string())]),
            Inline::Space,
            Inline::SoftBreak,
            Inline::LineBreak,
        ];
        assert_eq!(join(&v), "abcdefg   ");
    }
}

fn get_bindings<P>(filenames: &[P], bindings: &mut Bindings) -> Result<()>
where
    P: AsRef<Path>,
{
    for filename in filenames {
        bindings.add_from_file(filename)?;
    }
    Ok(())
}

/// Visitor for the pandoc AST.
///
/// This includes rendering stuff which we find as we go
struct TypesettingVisitor<'a> {
    bindings: &'a Bindings,
}

impl<'a> TypesettingVisitor<'a> {
    fn new(bindings: &'a Bindings) -> Self {
        TypesettingVisitor { bindings }
    }
}

// Visit interesting parts of the Pandoc abstract syntax tree. The
// document top level is a vector of blocks and we visit that and
// replace any fenced code block with the scenario tag with a typeset
// paragraph. Also, replace fenced code blocks with known graph markup
// with the rendered SVG image.
impl<'a> MutVisitor for TypesettingVisitor<'a> {
    fn visit_vec_block(&mut self, vec_block: &mut Vec<Block>) {
        for block in vec_block {
            match block {
                Block::CodeBlock(attr, s) => {
                    if is_class(attr, "scenario") {
                        *block = scenario_snippet(&self.bindings, s)
                    } else if is_class(attr, "file") {
                        *block = file_block(attr, s)
                    } else if is_class(attr, "dot") {
                        *block = dot_to_block(s)
                    } else if is_class(attr, "plantuml") {
                        *block = plantuml_to_block(s)
                    } else if is_class(attr, "roadmap") {
                        *block = roadmap_to_block(s)
                    }
                }
                _ => {
                    self.visit_block(block);
                }
            }
        }
    }
}

// Is a code block marked as being of a given type?
fn is_class(attr: &Attr, class: &str) -> bool {
    let (_id, classes, _kvpairs) = attr;
    classes.iter().any(|s| s == class)
}

/// Typeset an error as a Pandoc AST Block element.
pub fn error(err: SubplotError) -> Block {
    let msg = format!("ERROR: {}", err.to_string());
    Block::Para(error_msg(&msg))
}

// Typeset an error message a vector of inlines.
pub fn error_msg(msg: &str) -> Vec<Inline> {
    vec![Inline::Strong(vec![inlinestr(msg)])]
}

/// Typeset a code block tagged as a file.
pub fn file_block(attr: &Attr, text: &str) -> Block {
    let filename = inlinestr(&attr.0);
    let filename = Inline::Strong(vec![filename]);
    let intro = Block::Para(vec![inlinestr("File:"), space(), filename]);
    let codeblock = Block::CodeBlock(attr.clone(), text.to_string());
    let noattr = ("".to_string(), vec![], vec![]);
    Block::Div(noattr, vec![intro, codeblock])
}

/// Typeset a scenario snippet as a Pandoc AST Block.
///
/// Typesetting here means producing the Pandoc abstract syntax tree
/// nodes that result in the desired output, when Pandoc processes
/// them.
///
/// The snippet is given as a text string, which is parsed. It need
/// not be a complete scenario, but it should consist of complete steps.
pub fn scenario_snippet(bindings: &Bindings, snippet: &str) -> Block {
    let lines = parse_scenario_snippet(snippet);
    let mut steps = vec![];
    let mut prevkind: Option<StepKind> = None;

    for line in lines {
        let (this, thiskind) = step(bindings, line, prevkind);
        steps.push(this);
        prevkind = thiskind;
    }
    Block::LineBlock(steps)
}

// Typeset a single scenario step as a sequence of Pandoc AST Inlines.
fn step(
    bindings: &Bindings,
    text: &str,
    defkind: Option<StepKind>,
) -> (Vec<Inline>, Option<StepKind>) {
    let step = ScenarioStep::new_from_str(text, defkind);
    if step.is_err() {
        return (
            error_msg(&format!("Could not parse step: {}", text)),
            defkind,
        );
    }
    let step = step.unwrap();

    let m = bindings.find(&step);
    if m.is_none() {
        eprintln!("Could not finding binding for: {}", text);
        return (
            error_msg(&format!("Could not find binding for: {}", text)),
            defkind,
        );
    }
    let m = m.unwrap();

    let mut inlines = Vec::new();

    inlines.push(keyword(&step));
    inlines.push(space());

    for part in m.parts() {
        #[allow(unused_variables)]
        match part {
            PartialStep::UncapturedText(s) => inlines.push(uncaptured(s.text())),
            PartialStep::CapturedText { name, text } => inlines.push(captured(text)),
        }
    }

    (inlines, Some(step.kind()))
}

// Typeset first word, which is assumed to be a keyword, of a scenario
// step.
fn keyword(step: &ScenarioStep) -> Inline {
    let word = inlinestr(step.keyword());
    Inline::Emph(vec![word])
}

fn inlinestr(s: &str) -> Inline {
    Inline::Str(String::from(s))
}

// Typeset a space between words.
fn space() -> Inline {
    Inline::Space
}

// Typeset an uncaptured part of a step.
fn uncaptured(s: &str) -> Inline {
    inlinestr(s)
}

// Typeset a captured part of a step.
fn captured(s: &str) -> Inline {
    Inline::Strong(vec![inlinestr(s)])
}

// Take a dot graph, render it as SVG, and return an AST Block
// element. The Block will contain the SVG data. This allows the graph
// to be rendered without referending external entities.
fn dot_to_block(dot: &str) -> Block {
    match DotMarkup::new(dot).as_svg() {
        Ok(svg) => typeset_svg(svg),
        Err(err) => {
            eprintln!("dot failed: {}", err);
            error(err)
        }
    }
}

// Take a PlantUML graph, render it as SVG, and return an AST Block
// element. The Block will contain the SVG data. This allows the graph
// to be rendered without referending external entities.
fn plantuml_to_block(markup: &str) -> Block {
    match PlantumlMarkup::new(markup).as_svg() {
        Ok(svg) => typeset_svg(svg),
        Err(err) => {
            eprintln!("plantuml failed: {}", err);
            error(err)
        }
    }
}

/// Typeset a project roadmap expressed as textual YAML, and render it
/// as an SVG image.
fn roadmap_to_block(yaml: &str) -> Block {
    match roadmap::from_yaml(yaml) {
        Ok(ref mut roadmap) => {
            roadmap.set_missing_statuses();
            let width = 50;
            match roadmap.format_as_dot(width) {
                Ok(dot) => dot_to_block(&dot),
                Err(e) => Block::Para(vec![inlinestr(&e.to_string())]),
            }
        }
        Err(e) => Block::Para(vec![inlinestr(&e.to_string())]),
    }
}

// Typeset an SVG, represented as a byte vector, as a Pandoc AST Block
// element.
fn typeset_svg(svg: Vec<u8>) -> Block {
    let url = svg_as_data_url(svg);
    let attr = ("".to_string(), vec![], vec![]);
    let img = Inline::Image(attr, vec![], (url, "".to_string()));
    Block::Para(vec![img])
}

// Convert an SVG, represented as a byte vector, into a data: URL,
// which can be inlined so the image can be rendered without
// referencing external files.
fn svg_as_data_url(svg: Vec<u8>) -> String {
    let svg = base64::encode(&svg);
    format!("data:image/svg+xml;base64,{}", svg)
}

// A structure element in the document: a heading or a scenario snippet.
#[derive(Debug)]
enum Element {
    // Headings consist of the text and the level of the heading.
    Heading(String, i64),

    // Scenario snippets consist just of the unparsed text.
    Snippet(String),
}

impl Element {
    pub fn heading(text: &str, level: i64) -> Element {
        Element::Heading(text.to_string(), level)
    }

    pub fn snippet(text: &str) -> Element {
        Element::Snippet(text.to_string())
    }
}

// A MutVisitor for extracting document structure.
struct StructureVisitor {
    elements: Vec<Element>,
}

impl StructureVisitor {
    pub fn new() -> Self {
        Self { elements: vec![] }
    }
}

impl MutVisitor for StructureVisitor {
    fn visit_vec_block(&mut self, vec_block: &mut Vec<Block>) {
        for block in vec_block {
            match block {
                Block::Header(level, _attr, inlines) => {
                    let text = join(inlines);
                    let heading = Element::heading(&text, *level);
                    self.elements.push(heading);
                }
                Block::CodeBlock(attr, s) => {
                    if is_class(attr, "scenario") {
                        let snippet = Element::snippet(s);
                        self.elements.push(snippet);
                    }
                }
                _ => {
                    self.visit_block(block);
                }
            }
        }
    }
}

/// A data file embedded in the document.
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct DataFile {
    filename: String,
    contents: String,
}

impl DataFile {
    fn new(filename: &str, contents: &str) -> DataFile {
        DataFile {
            filename: filename.to_string(),
            contents: contents.to_string(),
        }
    }

    pub fn filename(&self) -> &str {
        &self.filename
    }

    pub fn contents(&self) -> &str {
        &self.contents
    }
}

/// A collection of data files embedded in document.
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
struct DataFiles {
    files: Vec<DataFile>,
}

impl DataFiles {
    fn new(ast: &mut Pandoc) -> DataFiles {
        let mut files = DataFiles { files: vec![] };
        files.walk_pandoc(ast);
        files
    }

    fn files(&self) -> &[DataFile] {
        &self.files
    }
}

impl MutVisitor for DataFiles {
    fn visit_vec_block(&mut self, vec_block: &mut Vec<Block>) {
        for block in vec_block {
            match block {
                Block::CodeBlock(attr, contents) => {
                    if is_class(attr, "file") {
                        self.files
                            .push(DataFile::new(&get_filename(attr), &contents));
                    }
                }
                _ => {
                    self.visit_block(block);
                }
            }
        }
    }
}

fn get_filename(attr: &Attr) -> String {
    attr.0.to_string()
}

struct ImageVisitor {
    images: Vec<PathBuf>,
}

impl ImageVisitor {
    fn new() -> Self {
        ImageVisitor { images: vec![] }
    }

    fn images(&self) -> Vec<PathBuf> {
        self.images.clone()
    }
}

impl MutVisitor for ImageVisitor {
    fn visit_inline(&mut self, inline: &mut Inline) {
        if let Inline::Image(_attr, _inlines, target) = inline {
            self.images.push(PathBuf::from(&target.0));
        }
    }
}

#[derive(Default)]
struct BlockClassVisitor {
    classes: HashSet<String>,
}

impl MutVisitor for BlockClassVisitor {
    fn visit_vec_block(&mut self, vec_block: &mut Vec<Block>) {
        for block in vec_block {
            match block {
                Block::CodeBlock(attr, _) => {
                    for class in &attr.1 {
                        self.classes.insert(class.to_string());
                    }
                }
                _ => {
                    self.visit_block(block);
                }
            }
        }
    }
}

/// Get the base directory given the name of the markdown file.
///
/// All relative filename, such as bindings files, are resolved
/// against the base directory.
pub fn get_basedir_from(filename: &Path) -> Result<PathBuf> {
    let dirname = match filename.parent() {
        None => return Err(SubplotError::BasedirError(filename.to_path_buf())),
        Some(x) => x.to_path_buf(),
    };
    Ok(dirname)
}