Implements an incredibly simple transpiler from to prove feasability. It converts Typst documentation to basic xilem_html
Rust code, with the end goal of auto-generating code that can be included straight from the Rust application. The next step is to use syn rather than string manipulation, then robustness work and integrating into the app build process.
2N3KOCP74PCK2ETO5PCWBDR5PA57DDNT2KR4JLBPZPQPA56SAR4QC
[[package]]
name = "pandoc"
version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eb8469d27ed9fd7925629076a3675fea964c3f44c49662bdf549a8b7ddf0820"
dependencies = [
"itertools",
]
[[package]]
name = "pandoc_ast"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56f639abb4745696d0f9c43da6818011da7ba7a4923bfe2d18d28dfdc5a595c9"
dependencies = [
"serde",
"serde_derive",
"serde_json",
]
use std::path::Path;
use pandoc::{OutputFormat, PandocOutput};
use pandoc_ast::{Block, Inline};
pub fn transform_pandoc(path: &Path) -> String {
let mut pandoc = pandoc::new();
pandoc.add_input(path);
pandoc.set_output_format(OutputFormat::Json, Vec::new());
pandoc.set_output(pandoc::OutputKind::Pipe);
let output = if let PandocOutput::ToBuffer(json) = pandoc.execute().unwrap() {
json
} else {
panic!("Pandoc produced unexpected output format");
};
let ast = pandoc_ast::Pandoc::from_json(&output);
dbg!(&ast);
let blocks: String = ast
.blocks
.iter()
.map(transform_block)
.collect::<Vec<String>>()
.join(", ");
dbg!(&blocks);
blocks
}
fn transform_block(block: &Block) -> String {
match block {
Block::Header(level, _attr, inlines) => {
let transformed_inlines = transform_inlines(inlines);
format!(r#"el::h{}("{}")"#, level, transformed_inlines)
}
Block::Para(inlines) => {
let transformed_inlines = transform_inlines(inlines);
format!(r#"el::p("{}")"#, transformed_inlines)
}
_ => todo!(),
}
}
fn transform_inline(inline: &Inline) -> String {
match inline {
Inline::Str(text) => text.to_owned(),
Inline::Space => String::from(" "),
_ => todo!(),
}
}
fn transform_inlines(inlines: &Vec<Inline>) -> String {
let transformed: String = inlines.iter().map(transform_inline).collect();
dbg!(&transformed);
transformed
}
use std::path::Path;
use typst_rust_gen::transform_pandoc;
fn main() {
dbg!(transform_pandoc(Path::new("docs/test.typ")));
}
[package]
name = "typst_rust_gen"
version = "0.1.0"
edition = "2021"
[dependencies]
pandoc = "0.8.10"
pandoc_ast = "0.8.5"