shiva 1.4.9

Shiva library: Implementation in Rust of a parser and generator for documents of any type
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use crate::core::Element::{Header, Hyperlink, Image, List, Paragraph, Table, Text};

use crate::core::{Document, Element, ListItem, TableHeader, TableRow, TransformerTrait};
use anyhow;
use bytes::Bytes;
use comemo::Prehashed;
use log::warn;
use std::path::Path;
use std::{collections::HashMap, io::Cursor};
use time::{OffsetDateTime, UtcOffset};

use typst::{
    diag::{FileError, FileResult},
    foundations::Datetime,
    syntax::{FileId, Source},
    text::{Font, FontBook},
    Library, World,
};

type TypstString = String;

pub struct ShivaWorld {
    fonts: Vec<Font>,
    book: Prehashed<FontBook>,
    library: Prehashed<Library>,
    source: Source,
    img_map: HashMap<String, typst::foundations::Bytes>,
}

impl ShivaWorld {
    pub fn new(source: String, img_map: HashMap<String, typst::foundations::Bytes>) -> Self {
        let source = Source::detached(source);

        let folder = "fonts";

        // Check if the "fonts" folder exists
        if !std::path::Path::new(folder).exists() {
            // Create the "fonts" folder
            std::fs::create_dir_all(folder).expect("Failed to create folder");

            // Download fonts
            let font_info = vec![
                ("DejaVuSansMono-Bold.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/DejaVuSansMono-Bold.ttf"),
                ("DejaVuSansMono.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/DejaVuSansMono.ttf"),
                ("FiraMath-Regular.otf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/FiraMath-Regular.otf"),
                ("IBMPlexSerif-Regular.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/IBMPlexSerif-Regular.ttf"),
                ("InriaSerif-BoldItalic.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/InriaSerif-BoldItalic.ttf"),
                ("InriaSerif-Regular.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/InriaSerif-Regular.ttf"),
                ("LinLibertine_R.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/LinLibertine_R.ttf"),
                ("LinLibertine_RB.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/LinLibertine_RB.ttf"),
                ("LinLibertine_RBI.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/LinLibertine_RBI.ttf"),
                ("LinLibertine_RI.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/LinLibertine_RI.ttf"),
                ("Nerd.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/Nerd.ttf"),
                ("NewCM10-Bold.otf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NewCM10-Bold.otf"),
                ("NewCM10-Regular.otf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NewCM10-Regular.otf"),
                ("NewCMMath-Book.otf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NewCMMath-Book.otf"),
                ("NewCMMath-Regular.otf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NewCMMath-Regular.otf"),
                ("NotoColorEmoji.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NotoColorEmoji.ttf"),
                ("NotoSansArabic-Regular.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NotoSansArabic-Regular.ttf"),
                ("NotoSansSymbols2-Regular.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NotoSansSymbols2-Regular.ttf"),
                ("NotoSerifCJKsc-Regular.otf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NotoSerifCJKsc-Regular.otf"),
                ("NotoSerifHebrew-Bold.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NotoSerifHebrew-Bold.ttf"),
                ("NotoSerifHebrew-Regular.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/NotoSerifHebrew-Regular.ttf"),
                ("PTSans-Regular.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/PTSans-Regular.ttf"),
                ("Roboto-Regular.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/Roboto-Regular.ttf"),
                ("TwitterColorEmoji.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/TwitterColorEmoji.ttf"),
                ("Ubuntu-Regular.ttf", "https://github.com/igumnoff/shiva/raw/main/lib/fonts/Ubuntu-Regular.ttf"),
            ];

            for (filename, url) in font_info {
                download_font(url, folder, filename);
            }
        }

        let fonts = std::fs::read_dir(folder)
            .unwrap()
            .map(Result::unwrap)
            .flat_map(|entry| {
                let path = entry.path();
                let bytes = std::fs::read(&path).unwrap();
                let buffer = typst::foundations::Bytes::from(bytes);
                let face_count = ttf_parser::fonts_in_collection(&buffer).unwrap_or(1);
                (0..face_count).map(move |face| {
                    Font::new(buffer.clone(), face).unwrap_or_else(|| {
                        panic!("failed to load font from {path:?} (face index {face})");
                    })
                })
            })
            .collect::<Vec<Font>>();

        Self {
            book: Prehashed::new(FontBook::from_fonts(&fonts)),
            fonts,
            library: Prehashed::new(Library::default()),
            source,
            img_map,
        }
    }
}

#[cfg(target_arch = "wasm32")]
fn download_font(url: &str, folder: &str, filename: &str) {
    use log::info;

    let font_path = Path::new(folder).join(filename);

    info!("Downloading font file {}...", font_path.display());

    let request = ehttp::Request::get(url);
    ehttp::fetch(request, move |result: ehttp::Result<ehttp::Response>| {
        let mut reader = Cursor::new(result.unwrap().bytes);
        let f = std::fs::File::create(&font_path).unwrap();
        let mut writer = std::io::BufWriter::new(f);

        let _bytes_io_count = std::io::copy(&mut reader, &mut writer).unwrap();

        info!("Font file {} downloaded successfully!", font_path.display());
    });
}

#[cfg(not(target_arch = "wasm32"))]
fn download_font(url: &str, folder: &str, filename: &str) {
    use log::info;

    let font_path = Path::new(folder).join(filename);

    info!("Downloading font file {}...", font_path.display());

    let request = ehttp::Request::get(url);
    let response = ehttp::fetch_blocking(&request);
    let mut reader = Cursor::new(response.unwrap().bytes);
    let f = std::fs::File::create(&font_path).unwrap();
    let mut writer = std::io::BufWriter::new(f);

    let _bytes_io_count = std::io::copy(&mut reader, &mut writer).unwrap();

    info!("Font file {} downloaded successfully!", font_path.display());
}

impl World for ShivaWorld {
    fn book(&self) -> &Prehashed<FontBook> {
        &self.book
    }

    fn library(&self) -> &Prehashed<Library> {
        &self.library
    }

    fn main(&self) -> Source {
        self.source.clone()
    }

    fn source(&self, _id: FileId) -> FileResult<Source> {
        Ok(self.source.clone())
    }

    fn font(&self, id: usize) -> Option<Font> {
        self.fonts.get(id).cloned()
    }

    // need to think how to implement path and file extraction
    fn file(&self, id: FileId) -> Result<typst::foundations::Bytes, FileError> {
        let path = id.vpath();

        let key = path.as_rootless_path().to_str().unwrap();
        let img = self.img_map.get(key).unwrap();

        Ok(img.clone())
    }

    fn today(&self, offset: Option<i64>) -> Option<Datetime> {
        // We are in UTC.
        let offset = offset.unwrap_or(0);
        let offset = UtcOffset::from_hms(offset.try_into().ok()?, 0, 0).ok()?;
        let time = OffsetDateTime::now_utc().checked_to_offset(offset)?;
        Some(Datetime::Date(time.date()))
    }
}

pub struct Transformer;

impl TransformerTrait for Transformer {
    #[allow(unused)]
    fn parse(document: &bytes::Bytes) -> anyhow::Result<Document> {
        todo!()
    }

    fn generate(document: &Document) -> anyhow::Result<bytes::Bytes> {
        let (text, _) = generate_document(document)?;
        let bytes = Bytes::from(text);
        Ok(bytes)
    }
}

/// Converts Document into a typst::model::Document
pub fn generate_document(
    document: &Document,
) -> anyhow::Result<(TypstString, HashMap<String, typst::foundations::Bytes>)> {
    // Array of methods to process Document object into a typst string repr
    fn process_header(source: &mut TypstString, level: usize, text: &str) -> anyhow::Result<()> {
        let header_depth = "=".repeat(level);
        let header_text = format!("{header_depth} {text}");
        source.push_str(&header_text);
        source.push('\n');

        Ok(())
    }

    fn process_text(
        source: &mut TypstString,
        _size: u8,
        text: &str,
        is_bold: bool,
    ) -> anyhow::Result<()> {
        if is_bold {
            let bold_text = format!("*{text}*");
            source.push_str(&bold_text);
        } else {
            source.push_str(text);
        }

        Ok(())
    }

    fn process_link(source: &mut TypstString, url: &str) -> anyhow::Result<()> {
        let link = format!("#link(\"{url}\")");

        source.push_str(&link);

        Ok(())
    }

    fn process_table(
        source: &mut TypstString,
        headers: &Vec<TableHeader>,
        rows: &Vec<TableRow>,
    ) -> anyhow::Result<()> {
        let mut headers_text = TypstString::new();

        for header in headers {
            match &header.element {
                Text { text, size } => {
                    headers_text.push('[');
                    process_text(&mut headers_text, *size, text, true)?;
                    headers_text.push(']');
                    headers_text.push(',');
                }
                _ => {
                    warn!(
                        "Should implement element for processing in inside table header - {:?}",
                        header.element
                    );
                }
            }
        }

        let mut cells_text = TypstString::new();

        for row in rows {
            for cell in &row.cells {
                match &cell.element {
                    Text { text, size } => {
                        cells_text.push('[');
                        process_text(&mut cells_text, *size, text, false)?;
                        cells_text.push(']');
                        cells_text.push(',');
                    }
                    _ => {
                        warn!(
                            "Should implement element for processing in inside cell - {:?}",
                            cell.element
                        );
                    }
                }
            }

            cells_text.push('\n');
        }

        let columns = headers.len();
        let table_text = format!(
            r#"
        #table(
            columns:{columns},
            {headers_text}
            {cells_text}
        )
        "#
        );

        source.push_str(&table_text);
        Ok(())
    }

    fn process_list(
        source: &mut TypstString,
        img_map: &mut HashMap<String, typst::foundations::Bytes>,
        list: &Vec<ListItem>,
        numbered: bool,
        depth: usize,
    ) -> anyhow::Result<()> {
        source.push_str(&" ".repeat(depth));
        for el in list {
            if let List { elements, numbered } = &el.element {
                process_list(source, img_map, elements, *numbered, depth + 1)?;
            } else {
                if numbered {
                    source.push_str("+ ")
                } else {
                    source.push_str("- ")
                };

                process_element(source, img_map, &el.element)?;
            }
        }

        Ok(())
    }

    fn process_image(
        source: &mut TypstString,
        bytes: &Bytes,
        title: &str,
        alt: &str,
        image_type: &str,
    ) -> anyhow::Result<()> {
        if !bytes.is_empty() {
            let image_text = format!(
                "
            #image(\"{title}{image_type}\", alt: \"{alt}\")
            "
            );
            source.push_str(&image_text);
        }
        // need to think how to implement using raw bytes
        Ok(())
    }

    fn process_element(
        source: &mut TypstString,
        img_map: &mut HashMap<String, typst::foundations::Bytes>,
        element: &Element,
    ) -> anyhow::Result<()> {
        match element {
            Header { level, text } => process_header(source, *level as usize, text),
            Paragraph { elements } => {
                for paragraph_element in elements {
                    process_element(source, img_map, paragraph_element)?;
                }

                Ok(())
            }
            Text { text, size } => {
                process_text(source, *size, text, false)?;
                source.push('\n');

                Ok(())
            }
            List { elements, numbered } => {
                process_list(source, img_map, elements, *numbered, 0)?;
                Ok(())
            }
            Hyperlink {
                url,
                title: _,
                alt: _,
                size: _,
            } => {
                process_link(source, url)?;
                source.push('\n');

                Ok(())
            }
            Table { headers, rows } => {
                process_table(source, headers, rows)?;
                Ok(())
            }
            Image(image) => {
                let key = format!("{}{}", image.title(), image.image_type());
                img_map.insert(key, typst::foundations::Bytes::from(image.bytes().to_vec()));
                process_image(
                    source,
                    image.bytes(),
                    image.title(),
                    image.alt(),
                    &image.image_type().to_string(),
                )?;
                source.push('\n');
                Ok(())
            } // _ => {
              //     warn!("Should implement element - {:?}", element);
              //     Ok(())
              // }
        }
    }

    // String to build off of
    let mut source = TypstString::new();
    // Mapping of connections between elements
    let mut img_map: HashMap<String, typst::foundations::Bytes> = HashMap::new();

    // Converting both headers and footers into a string repr of them in Typst
    let mut header_text = String::new();
    document.get_page_header().iter().for_each(|el| match el {
        Text { text, size: _ } => {
            header_text.push_str(text);
        }
        _ => {}
    });
    let mut footer_text = String::new();
    document.get_page_footer().iter().for_each(|el| match el {
        Text { text, size: _ } => {
            footer_text.push_str(text);
        }
        _ => {}
    });
    let footer_header_text = format!(
        "#set page(
        header: \"{header_text}\",
        footer: \"{footer_text}\",
    )\n"
    );

    // Converting Document repr to one of typst string
    source.push_str(&footer_header_text);
    for element in &document.get_all_elements() {
        process_element(&mut source, &mut img_map, element)?;
    }

    Ok((source, img_map))
}

#[cfg(test)]
mod test {
    use crate::core::{disk_image_loader, TransformerWithImageLoaderSaverTrait};
    use crate::markdown;
    use bytes::Bytes;

    use super::*;
    #[test]
    fn test_generate() -> anyhow::Result<()> {
        let document = std::fs::read("test/data/document.md")?;
        let documents_bytes = Bytes::from(document);
        let parsed = markdown::Transformer::parse_with_loader(
            &documents_bytes,
            disk_image_loader("test/data"),
        )?;
        let generated_result = crate::typst::Transformer::generate(&parsed)?;
        std::fs::write("test/data/document_from_md.typ", generated_result)?;

        Ok(())
    }

    #[test]
    fn test_generate_from_xml() -> anyhow::Result<()> {
        let document = std::fs::read("test/data/document.xml")?;
        let documents_bytes = Bytes::from(document);
        let parsed = crate::xml::Transformer::parse(&documents_bytes)?;
        let generated_result = crate::typst::Transformer::generate(&parsed)?;
        std::fs::write("test/data/document_from_xml.typ", generated_result)?;

        Ok(())
    }
}