| 1 | use crate::dsl::{Align, Block, Document, Inline};
|
| 2 | use crate::io::PosMetadata;
|
| 3 |
|
| 4 | #[derive(Debug, Clone, PartialEq)]
|
| 5 | pub struct PreviewModel {
|
| 6 | pub lines: Vec<PreviewLine>,
|
| 7 | pub width_chars: u32,
|
| 8 | pub width_mm: f32,
|
| 9 | }
|
| 10 |
|
| 11 | #[derive(Debug, Clone, PartialEq)]
|
| 12 | pub enum PreviewLine {
|
| 13 | Text {
|
| 14 | content: String,
|
| 15 | align: Align,
|
| 16 | style: TextStyle,
|
| 17 | },
|
| 18 | Rule,
|
| 19 | Barcode {
|
| 20 | symbology: String,
|
| 21 | data: String,
|
| 22 | align: Align,
|
| 23 | },
|
| 24 | Qr {
|
| 25 | data: String,
|
| 26 | align: Align,
|
| 27 | },
|
| 28 | Image {
|
| 29 | label: String,
|
| 30 | align: Align,
|
| 31 | },
|
| 32 | Spacer(u8),
|
| 33 | }
|
| 34 |
|
| 35 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
| 36 | pub enum TextStyle {
|
| 37 | #[default]
|
| 38 | Normal,
|
| 39 | Heading1,
|
| 40 | Heading2,
|
| 41 | Bold,
|
| 42 | Italic,
|
| 43 | Code,
|
| 44 | }
|
| 45 |
|
| 46 | pub fn render_preview(document: &Document, metadata: &PosMetadata) -> PreviewModel {
|
| 47 | let width_chars = metadata.chars_per_line();
|
| 48 | let mut lines = Vec::new();
|
| 49 |
|
| 50 | for block in &document.blocks {
|
| 51 | match block {
|
| 52 | Block::Paragraph { align, inlines } => {
|
| 53 | let text = inlines_to_string(inlines);
|
| 54 | for line in text.split('\n') {
|
| 55 | lines.push(PreviewLine::Text {
|
| 56 | content: line.to_string(),
|
| 57 | align: *align,
|
| 58 | style: TextStyle::Normal,
|
| 59 | });
|
| 60 | }
|
| 61 | }
|
| 62 | Block::Heading { level, inlines } => {
|
| 63 | let style = if *level == 1 {
|
| 64 | TextStyle::Heading1
|
| 65 | } else {
|
| 66 | TextStyle::Heading2
|
| 67 | };
|
| 68 | lines.push(PreviewLine::Text {
|
| 69 | content: inlines_to_string(inlines),
|
| 70 | align: Align::Center,
|
| 71 | style,
|
| 72 | });
|
| 73 | }
|
| 74 | Block::Rule => lines.push(PreviewLine::Rule),
|
| 75 | Block::Feed(n) => lines.push(PreviewLine::Spacer(*n)),
|
| 76 | Block::Cut => lines.push(PreviewLine::Text {
|
| 77 | content: "--- cut ---".into(),
|
| 78 | align: Align::Center,
|
| 79 | style: TextStyle::Code,
|
| 80 | }),
|
| 81 | Block::Image { path, align, .. } => lines.push(PreviewLine::Image {
|
| 82 | label: format!("[image: {path}]"),
|
| 83 | align: *align,
|
| 84 | }),
|
| 85 | Block::Barcode {
|
| 86 | mode,
|
| 87 | symbology,
|
| 88 | data,
|
| 89 | align,
|
| 90 | ..
|
| 91 | } => lines.push(PreviewLine::Barcode {
|
| 92 | symbology: format!("{mode:?} {symbology:?}"),
|
| 93 | data: data.clone(),
|
| 94 | align: *align,
|
| 95 | }),
|
| 96 | Block::Qr {
|
| 97 | mode,
|
| 98 | data,
|
| 99 | align,
|
| 100 | ..
|
| 101 | } => lines.push(PreviewLine::Qr {
|
| 102 | data: format!("[{mode:?}] {data}"),
|
| 103 | align: *align,
|
| 104 | }),
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | PreviewModel {
|
| 109 | lines,
|
| 110 | width_chars,
|
| 111 | width_mm: metadata.page_width_mm,
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | fn inlines_to_string(inlines: &[Inline]) -> String {
|
| 116 | let mut out = String::new();
|
| 117 | for inline in inlines {
|
| 118 | match inline {
|
| 119 | Inline::Text(t) => out.push_str(t),
|
| 120 | Inline::Code(t) => out.push_str(t),
|
| 121 | Inline::Param(t) => out.push_str(&format!("${{{t}}}")),
|
| 122 | Inline::Bold(inner) => out.push_str(&inlines_to_string(inner)),
|
| 123 | Inline::Italic(inner) => out.push_str(&inlines_to_string(inner)),
|
| 124 | }
|
| 125 | }
|
| 126 | out
|
| 127 | }
|
| 128 |
|
| 129 | #[cfg(test)]
|
| 130 | mod tests {
|
| 131 | use super::*;
|
| 132 | use crate::dsl::parse;
|
| 133 |
|
| 134 | #[test]
|
| 135 | fn renders_paragraph_lines() {
|
| 136 | let doc = parse("Line1\nLine2").unwrap();
|
| 137 | let model = render_preview(&doc, &PosMetadata::default());
|
| 138 | assert!(model.lines.len() >= 2);
|
| 139 | }
|
| 140 |
|
| 141 | #[test]
|
| 142 | fn shows_unresolved_params_in_preview() {
|
| 143 | let doc = parse("${X}").unwrap();
|
| 144 | let model = render_preview(&doc, &PosMetadata::default());
|
| 145 | let PreviewLine::Text { content, .. } = &model.lines[0] else {
|
| 146 | panic!();
|
| 147 | };
|
| 148 | assert_eq!(content, "${X}");
|
| 149 | }
|
| 150 | }
|