use std::sync::Arc;
use crate::core::region::RegionsOutput;
use crate::core::settings::SettingsDecl;
use crate::stdlib::backends::lowering::REGIONS_SLOT;
use crate::model::names::ElementName;
use crate::model::options::Bind;
use crate::core::registry::{
BackendDef, BuildError, FeatureBuilder, FeatureInfo, FlmFeature, OutputDef, WorkflowDef,
};
use crate::lang::structural;
use crate::stdlib::VALIDATED;
use crate::stdlib::backends::lowering::{LoweredTrees, lowered_table_name};
use crate::stdlib::backends::pieces::{LoweredPieces, PiecesStage, lowered_pieces_table_name};
use crate::stdlib::backends::pres;
use super::emit::TEXT_OUTPUT;
use super::stages::{
BACKEND, EXTENSION, MEDIA_TYPE, TEXT_DOCUMENT, TEXT_REGIONS, TextRegionsStage,
TextRenderOptions, WORKFLOW, stages,
};
pub const FEATURE_NAME: &str = "text";
fn settings() -> SettingsDecl {
SettingsDecl::new(FEATURE_NAME)
.options::<TextRenderOptions>(
Bind::Render,
"the line width in columns (null: never wrap), default 80; the width of a tab in \
columns, default 4; the stop written after a run-in heading's title, default `.`, \
the empty text for none; whether a link to an address outside the document prints \
the address after its content, default on; whether a link whose content already \
shows its address prints the address too, default off; the heading level printed \
as a first-level heading, default 1; how a table is drawn, `none` (its columns two \
spaces apart, the default) or `box` (inside a frame of box-drawing characters); and \
the characters of the rules under the top two heading levels, `ascii` (`=` and \
`-`, the default) or `unicode` (`═` and `─`). Set per render request.",
)
.markup("run_in_stop")
}
pub const FLOW_THROUGH: [&str; 6] = [
structural::TEXT,
structural::GROUP,
structural::OPAQUE_WRAPPER,
structural::COMMENT,
structural::CONTAINER,
structural::PARBREAK,
];
pub fn native_elements() -> Vec<ElementName> {
pres::ALL.iter().chain(FLOW_THROUGH.iter()).map(|name| ElementName::from(*name)).collect()
}
#[derive(Debug, Default)]
pub struct TextBackend;
impl FlmFeature for TextBackend {
fn info(&self) -> FeatureInfo {
FeatureInfo::new(FEATURE_NAME, "Text backend")
.docs("plain-text output over the layout engine: the `text` output and workflow")
}
fn settings(&self) -> Option<SettingsDecl> {
Some(settings())
}
fn register(&self, b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
b.table_named::<LoweredTrees>(
lowered_table_name(&BACKEND.into()),
"the text backend's lowered trees, one per document tree",
)?;
b.table_named::<LoweredPieces>(
lowered_pieces_table_name(&BACKEND.into()),
"the pieces that the render request places, lowered for the text backend",
)?;
b.stage(Arc::new(PiecesStage::new(BACKEND)))?;
b.table(TEXT_REGIONS, "the output's one region: the printed text")?;
b.stage(Arc::new(TextRegionsStage))?;
b.contribute_to::<RegionsOutput>(
REGIONS_SLOT,
Arc::new(RegionsOutput {
output: BACKEND.into(),
backend: BACKEND.into(),
document: TEXT_DOCUMENT.name().into(),
capability: TEXT_DOCUMENT.name().into(),
regions: TEXT_REGIONS.handle(),
artifacts: Vec::new(),
}),
)?;
b.table(
TEXT_OUTPUT,
"the printed text, its position marks, which record where each presentation node \
was printed, and its style marks, which record the printed ranges whose content \
carries a property plain text cannot show",
)?;
b.table(TEXT_DOCUMENT, "the printed text as a one-artifact set")?;
let (lowering, emit, final_stage) = stages();
b.backend(
BackendDef::new(BACKEND, native_elements(), lowering, emit, final_stage)
.docs("plain text over the layout engine"),
)?;
b.output(
OutputDef::new(
BACKEND,
TEXT_DOCUMENT.name(),
TEXT_DOCUMENT.name(),
MEDIA_TYPE,
EXTENSION,
)
.docs("the document as plain text"),
)?;
b.workflow(
WorkflowDef::new(WORKFLOW)
.output(BACKEND)
.demand(VALIDATED)
.docs("render the document as plain text, running every check"),
)?;
Ok(())
}
}
pub fn feature() -> Arc<dyn FlmFeature> {
Arc::new(TextBackend)
}