Skip to main content

tramlines_md/
lib.rs

1//! tramlines-md: the markdown format of tramlines. It checks a markdown
2//! document against a schema, repairs it, and derives the scaffold a
3//! schema implies.
4//!
5//! A schema is YAML in the vocabulary of the grammar: the core grammar of
6//! tramlines-core, with the markdown format grammar laid on it, which adds
7//! the sections, blocks and units a document of one kind must carry. The
8//! engine parses the document, holds it to the schema and reports each
9//! failure as a [`Finding`]. The items of tramlines-core are re-exported,
10//! so a host needs this crate alone.
11//!
12//! Compile a schema and check a document against it:
13//!
14//! ```
15//! use tramlines_md::{
16//!     Locator, RepoPath,
17//!     check::{CheckOptions, NullHost, check_against},
18//!     document::Document,
19//!     grammar::{FormatRegistry, RegexCache, compile, load_grammar},
20//!     markdown::Dialect,
21//!     yaml,
22//! };
23//!
24//! let schema = "line-limit: 0\nsections:\n  - heading: Groceries\n    level: 1\n    blocks:\n      - kind: bullet-list\n        each: { template: '${item:word} x${n:int}', example: Milk x2 }\n";
25//! let schema = compile(
26//!     &yaml::parse(schema, 1)?,
27//!     &load_grammar()?,
28//!     &FormatRegistry::default(),
29//!     &RegexCache::new(),
30//!     &RepoPath::parse("/list.yaml").expect("a path"),
31//!     "list",
32//!     &Dialect::default(),
33//! )
34//! .expect("the schema compiles");
35//!
36//! let text = "# Groceries\n\n- Milk x2\n- Bread\n";
37//! let path = RepoPath::parse("/groceries.md").expect("a path");
38//! let document = Document::parse(path, text, &Dialect::default());
39//! let states = schema.states(document.frontmatter.as_ref());
40//! let out = check_against(
41//!     document.view(),
42//!     &schema,
43//!     &states,
44//!     &NullHost,
45//!     CheckOptions::default(),
46//!     Locator::schema("list"),
47//! );
48//! assert_eq!(out.findings.len(), 1);
49//! assert_eq!(out.findings[0].criterion, "TL_74");
50//! assert_eq!(out.findings[0].line, Some(4));
51//! # Ok::<(), Box<dyn std::error::Error>>(())
52//! ```
53
54#![warn(missing_docs)]
55
56pub mod check;
57pub mod derive;
58pub mod document;
59pub mod edit;
60pub mod grammar;
61pub mod markdown;
62
63// sokf-implements: MD-9_AC-1
64// sokf-implements: MD-9_AC-3
65// sokf:begin reexports
66pub use tramlines_core::{
67    Error, Finding, Locator, RepoPath, RepoRoot, Result, Severity, Spanned, Yaml, YamlError, text,
68    yaml,
69};
70// sokf:end reexports