use serde::{Deserialize, Serialize};
mod block_provenance;
mod evaluate;
mod navigation;
mod render;
mod transclude;
pub use evaluate::{
BlockEvaluator, BlockEvaluators, EvalOutcome, EvalOutput, EvaluationPolicy, evaluate_blocks,
parse_eval,
};
pub use render::GophermapContext;
pub use transclude::{
Fetched, TranscludeOutcome, TransclusionPolicy, parse_include, resolve_transclusions,
};
pub use block_provenance::{BlockProvenance, BlockProvenanceMap, ResolvedProvenance};
pub use navigation::{
DocumentAnchor, DocumentFold, DocumentNavigation, FoldKey, FoldMarkers, FoldState, InPageTarget,
};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EngineDocument {
pub address: String,
pub title: Option<String>,
pub content_type: String,
#[serde(default)]
pub lang: Option<String>,
#[serde(default)]
pub provenance: DocumentProvenance,
#[serde(default)]
pub trust: DocumentTrustState,
#[serde(default)]
pub diagnostics: Vec<DocumentDiagnostic>,
#[serde(default)]
pub navigation: DocumentNavigation,
pub blocks: Vec<Block>,
}
impl EngineDocument {
pub fn walk_inline_spans(&self) -> Vec<&InlineSpan> {
let mut out = Vec::new();
for block in &self.blocks {
collect_block_spans(block, &mut out);
}
out
}
pub fn outgoing_links(&self) -> Vec<&str> {
let mut links = Vec::new();
for block in &self.blocks {
collect_block_link_urls(block, &mut links);
}
links
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DocumentProvenance {
#[serde(default)]
pub source_kind: Option<String>,
#[serde(default)]
pub canonical_uri: Option<String>,
#[serde(default)]
pub fetched_at: Option<String>,
#[serde(default)]
pub source_label: Option<String>,
}
impl DocumentProvenance {
pub fn for_engine(engine_id: &str, address: &str) -> Self {
Self {
source_kind: Some(engine_id.to_string()),
canonical_uri: Some(address.to_string()),
fetched_at: None,
source_label: None,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentTrustState {
Trusted,
Tofu,
Insecure,
Broken,
#[default]
Unknown,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentDiagnostic {
UnsupportedConstruct(String),
DegradedRendering(String),
ParseWarning(String),
RawSourceFallback,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Block {
Presented {
presentation: BlockPresentation,
block: Box<Block>,
},
Heading { level: u8, spans: Vec<InlineSpan> },
Paragraph { spans: Vec<InlineSpan> },
CodeBlock {
language: Option<String>,
text: String,
},
Quote { blocks: Vec<Block> },
List {
ordered: bool,
items: Vec<Vec<Block>>,
},
Image { url: String, alt: String },
Preformatted { text: String },
Rule,
FeedHeader {
title: String,
subtitle: Option<String>,
summary: Option<String>,
source_url: Option<String>,
},
FeedEntry {
title: String,
date: Option<String>,
summary: Option<String>,
article_url: Option<String>,
source_url: Option<String>,
#[serde(default)]
published: Option<String>,
#[serde(default)]
updated: Option<String>,
#[serde(default)]
guid: Option<String>,
#[serde(default)]
enclosures: Vec<FeedEnclosure>,
#[serde(default)]
content_address: Option<String>,
},
MetadataRow { label: String, value: String },
Badge { text: String },
Table {
alignments: Vec<TableAlignment>,
header: Vec<Vec<InlineSpan>>,
rows: Vec<Vec<Vec<InlineSpan>>>,
},
Menu { rows: Vec<MenuRow> },
}
impl Block {
pub fn kind_name(&self) -> &'static str {
match self {
Block::Presented { block, .. } => block.kind_name(),
Block::Heading { .. } => "heading",
Block::Paragraph { .. } => "paragraph",
Block::CodeBlock { .. } => "code block",
Block::Quote { .. } => "quote",
Block::List { .. } => "list",
Block::Image { .. } => "image",
Block::Preformatted { .. } => "preformatted",
Block::Rule => "rule",
Block::FeedHeader { .. } => "feed header",
Block::FeedEntry { .. } => "feed entry",
Block::MetadataRow { .. } => "metadata row",
Block::Badge { .. } => "badge",
Block::Table { .. } => "table",
Block::Menu { .. } => "menu",
}
}
}
pub fn menu_fallback_blocks(rows: &[MenuRow]) -> Vec<Block> {
let mut blocks = Vec::new();
let mut run: Vec<String> = Vec::new();
let flush = |run: &mut Vec<String>, blocks: &mut Vec<Block>| {
if !run.is_empty() {
blocks.push(Block::Preformatted {
text: run.drain(..).collect::<Vec<_>>().join("\n"),
});
}
};
for row in rows {
match (row.kind, &row.target) {
(MenuItemKind::Info, None) => run.push(inline_text(&row.label)),
(MenuItemKind::Error, None) => run.push(format!("[error] {}", inline_text(&row.label))),
(_, Some(url)) => {
flush(&mut run, &mut blocks);
blocks.push(Block::Paragraph {
spans: vec![InlineSpan::Link {
url: url.clone(),
title: None,
spans: row.label.clone(),
predicate: None,
}],
});
},
(_, None) => {
flush(&mut run, &mut blocks);
blocks.push(Block::Paragraph {
spans: row.label.clone(),
});
},
}
}
flush(&mut run, &mut blocks);
blocks
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FeedEnclosure {
pub url: String,
#[serde(default)]
pub media_type: Option<String>,
#[serde(default)]
pub byte_length: Option<u64>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct MenuRow {
pub kind: MenuItemKind,
#[serde(default)]
pub marker: Option<char>,
pub label: Vec<InlineSpan>,
#[serde(default)]
pub target: Option<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum MenuItemKind {
Info,
Error,
Document,
Directory,
Search,
Binary,
Image,
Sound,
Telnet,
External,
Other,
}
impl MenuItemKind {
pub fn name(self) -> &'static str {
match self {
MenuItemKind::Info => "info",
MenuItemKind::Error => "error",
MenuItemKind::Document => "document",
MenuItemKind::Directory => "directory",
MenuItemKind::Search => "search",
MenuItemKind::Binary => "binary",
MenuItemKind::Image => "image",
MenuItemKind::Sound => "sound",
MenuItemKind::Telnet => "telnet",
MenuItemKind::External => "external",
MenuItemKind::Other => "other",
}
}
pub fn label(self) -> &'static str {
match self {
MenuItemKind::Info => "",
MenuItemKind::Error => "ERR",
MenuItemKind::Document => "TXT",
MenuItemKind::Directory => "DIR",
MenuItemKind::Search => "ASK",
MenuItemKind::Binary => "BIN",
MenuItemKind::Image => "IMG",
MenuItemKind::Sound => "SND",
MenuItemKind::Telnet => "TEL",
MenuItemKind::External => "URL",
MenuItemKind::Other => "???",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum TableAlignment {
#[default]
None,
Left,
Center,
Right,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum BlockAlignment {
#[default]
Start,
Center,
End,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlockPresentation {
pub alignment: BlockAlignment,
pub indent_level: u32,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct InlinePresentation {
pub foreground: Option<[u8; 3]>,
pub background: Option<[u8; 3]>,
pub underline: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum InlineSpan {
Presented {
presentation: InlinePresentation,
spans: Vec<InlineSpan>,
},
Text(String),
Code(String),
Emphasis(Vec<InlineSpan>),
Strong(Vec<InlineSpan>),
Link {
url: String,
title: Option<String>,
spans: Vec<InlineSpan>,
#[serde(default)]
predicate: Option<String>,
},
Submit {
target: String,
spans: Vec<InlineSpan>,
},
InPage {
target: InPageTarget,
spans: Vec<InlineSpan>,
},
LineBreak,
SoftBreak,
}
mod traversal;
pub use traversal::*;
#[cfg(test)]
mod tests;