use std::path::PathBuf;
use markplus_core::parse_document;
use markplus_render::RenderEngine;
fn main() -> anyhow::Result<()> {
let md_path: PathBuf = std::env::args()
.nth(1)
.map(PathBuf::from)
.unwrap_or_else(|| {
eprintln!("No path given — using built-in example document.");
PathBuf::from("example.md")
});
let md_src = if md_path.exists() {
std::fs::read_to_string(&md_path)?
} else {
r#"---
title: MarkPlus Render Demo
author: Demo User
date: 2026-06-07
tags:
- demo
- markplus
---
# MarkPlus Render Demo
This is a **demonstration** document rendered by `markplus_render`.
## Features
- Headings with levels 1–6
- **Bold**, _italic_, and `inline code`
- Fenced code blocks with syntax names
- Math: inline $E = mc^2$ and display:
$$
\int_0^\infty e^{-x}\,dx = 1
$$
## Code Example
```rust
fn greet(name: &str) -> String {
format!("Hello, {name}!")
}
```
## Table
| Feature | Native | Wasm |
| ------- | ------ | ---- |
| HTML | ✓ | ✓ |
| Typst | ✓ | ✓ |
| PDF | ✓ | opt |
---
*Generated by markplus_render.*
"#.to_owned()
};
let asset = parse_document(&md_src)?;
println!("Parsed {} AST nodes", asset.ast.len());
let templates_dir = PathBuf::from(
std::env::var("MARKPLUS_TEMPLATES").unwrap_or_else(|_| "templates".into())
);
let engine = if templates_dir.exists() {
RenderEngine::builder()
.with_templates(&templates_dir)
.build()?
} else {
let html_tpl = include_str!("../templates/default/article.html.tera");
let typ_tpl = include_str!("../templates/default/article.typ.tera");
RenderEngine::builder()
.build_with_templates(std::collections::HashMap::from([
("default/article.html.tera".into(), html_tpl.into()),
("default/article.typ.tera".into(), typ_tpl.into()),
]))?
};
let html = engine.render_html(&asset, "default/article.html.tera")?;
let html_path = md_path.with_extension("html");
std::fs::write(&html_path, &html)?;
println!("HTML written → {}", html_path.display());
let typ_src = engine.render_typst_string(&asset, "default/article.typ.tera")?;
let typ_path = md_path.with_extension("typ");
std::fs::write(&typ_path, &typ_src)?;
println!("Typst written → {}", typ_path.display());
println!("Compiling PDF via typst-as-lib...");
match engine.compile_pdf(&typ_src) {
Ok(pdf_bytes) => {
let pdf_path = md_path.with_extension("pdf");
std::fs::write(&pdf_path, &pdf_bytes)?;
println!("PDF written → {} ({} bytes)", pdf_path.display(), pdf_bytes.len());
}
Err(e) => eprintln!("PDF compile skipped: {e}"),
}
Ok(())
}