#![deny(unsafe_code)]
use std::collections::HashMap;
pub type LinkRefs = HashMap<String, (String, Option<String>)>;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Document {
pub blocks: Vec<Block>,
pub link_refs: LinkRefs,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Align {
None,
Left,
Center,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListKind {
Bullet,
Ordered { start: u64 },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListItem {
pub task: Option<bool>,
pub blocks: Vec<Block>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Field {
pub key: String,
pub value: FieldValue,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FieldValue {
Scalar(String),
List(Vec<String>),
}
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Block {
FrontMatter {
fields: Vec<Field>,
source_line: usize,
},
Heading {
level: u8,
content: Vec<Inline>,
id: String,
source_line: usize,
},
Paragraph {
content: Vec<Inline>,
source_line: usize,
},
CodeBlock {
lang: Option<String>,
lines: Vec<String>,
fenced: bool,
source_line: usize,
},
List {
kind: ListKind,
tight: bool,
items: Vec<ListItem>,
source_line: usize,
},
Quote {
blocks: Vec<Block>,
source_line: usize,
},
Table {
align: Vec<Align>,
head: Vec<Vec<Inline>>,
rows: Vec<Vec<Vec<Inline>>>,
source_line: usize,
},
ThematicBreak {
source_line: usize,
},
Html {
lines: Vec<String>,
source_line: usize,
},
FootnoteDef {
label: String,
blocks: Vec<Block>,
source_line: usize,
},
}
impl Block {
pub fn source_line(&self) -> usize {
match self {
Block::FrontMatter { source_line, .. }
| Block::Heading { source_line, .. }
| Block::Paragraph { source_line, .. }
| Block::CodeBlock { source_line, .. }
| Block::List { source_line, .. }
| Block::Quote { source_line, .. }
| Block::Table { source_line, .. }
| Block::ThematicBreak { source_line }
| Block::Html { source_line, .. }
| Block::FootnoteDef { source_line, .. } => *source_line,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Inline {
Text(String),
Code(String),
Emph(Vec<Inline>),
Strong(Vec<Inline>),
Strike(Vec<Inline>),
Link {
text: Vec<Inline>,
url: String,
title: Option<String>,
},
Image {
alt: String,
url: String,
},
Autolink(String),
SoftBreak,
HardBreak,
FootnoteRef(String),
Html(String),
}
pub fn inline_text(items: &[Inline]) -> String {
let mut out = String::new();
push_text(items, &mut out);
out
}
fn push_text(items: &[Inline], out: &mut String) {
for it in items {
match it {
Inline::Text(s) | Inline::Code(s) | Inline::Autolink(s) => out.push_str(s),
Inline::Emph(k) | Inline::Strong(k) | Inline::Strike(k) => push_text(k, out),
Inline::Link { text, .. } => push_text(text, out),
Inline::Image { alt, .. } => out.push_str(alt),
Inline::SoftBreak | Inline::HardBreak => out.push(' '),
Inline::FootnoteRef(l) => {
out.push_str("[^");
out.push_str(l);
out.push(']');
}
Inline::Html(_) => {}
}
}
}
pub fn slugify(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for ch in text.chars() {
if ch.is_alphanumeric() {
for lc in ch.to_lowercase() {
out.push(lc);
}
} else if ch == '-' || ch == '_' {
out.push(ch);
} else if ch == ' ' || ch == '\t' {
out.push('-');
}
}
out
}
#[derive(Debug, Default)]
pub struct SlugSet {
seen: HashMap<String, usize>,
}
impl SlugSet {
pub fn new() -> Self {
Self::default()
}
pub fn unique(&mut self, text: &str) -> String {
let base = slugify(text);
match self.seen.get_mut(&base) {
Some(n) => {
*n += 1;
format!("{}-{}", base, *n)
}
None => {
self.seen.insert(base.clone(), 0);
base
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn t(s: &str) -> Inline {
Inline::Text(s.to_string())
}
#[test]
fn slug_matches_github_shape() {
assert_eq!(slugify("How to read this"), "how-to-read-this");
assert_eq!(slugify("Sample/ supersedes old/"), "sample-supersedes-old");
assert_eq!(slugify("§2g-i: DNS & tiering!"), "2g-i-dns--tiering");
assert_eq!(slugify("`provisioner migrate`"), "provisioner-migrate");
assert_eq!(slugify("Example Codex"), "example-codex");
}
#[test]
fn slug_dedup_suffixes() {
let mut set = SlugSet::new();
assert_eq!(set.unique("References"), "references");
assert_eq!(set.unique("References"), "references-1");
assert_eq!(set.unique("References"), "references-2");
assert_eq!(set.unique("Other"), "other");
}
#[test]
fn inline_text_flattens_nesting() {
let items = vec![
t("a "),
Inline::Strong(vec![Inline::Emph(vec![t("b")])]),
Inline::Link {
text: vec![t(" c")],
url: "u".into(),
title: None,
},
Inline::SoftBreak,
Inline::Code("d".into()),
Inline::Image {
alt: "e".into(),
url: "u".into(),
},
Inline::FootnoteRef("1".into()),
];
assert_eq!(inline_text(&items), "a b c de[^1]");
}
#[test]
fn block_source_line_accessor() {
let b = Block::ThematicBreak { source_line: 7 };
assert_eq!(b.source_line(), 7);
let h = Block::Heading {
level: 2,
content: vec![t("x")],
id: "x".into(),
source_line: 12,
};
assert_eq!(h.source_line(), 12);
}
}