tiefdownlib 0.8.0

A library to manage and convert TiefDown projects.
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use crate::{
    consts::CURRENT_MANIFEST_VERSION, template_management::get_template_type_from_path,
    template_type::TemplateType,
};
use color_eyre::eyre::{Result, eyre};
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, sync::LazyLock};
use toml::Table;

/// Represents the manifest file for a TiefDown project.
///
/// # Fields
///
/// * `version` - The version of the manifest file.
/// * `markdown_projects` - A list of markdown projects in the project.
/// * `templates` - A list of template mappings.
/// * `custom_processors` - The custom processors and preprocessors available to the project.
/// * `smart_clean` - A flag indicating whether to enable smart clean on conversion.
/// * `smart_clean_threshold` - The threshold for smart clean.
/// * `shared_metadata` - Metadata for the project shared across markdown projects.
/// * `metadata_settings` - Metadata settings for the project.
/// * `profiles` - A list of profiles for the project.
#[derive(Deserialize, Serialize)]
pub struct Manifest {
    pub version: u32,
    pub markdown_projects: Option<Vec<MarkdownProject>>,
    pub templates: Vec<TemplateMapping>,
    pub custom_processors: Processors,
    pub smart_clean: Option<bool>,
    pub smart_clean_threshold: Option<u32>,
    pub shared_metadata: Option<Table>,
    pub metadata_settings: Option<MetadataSettings>,
    pub profiles: Option<Vec<Profile>>,
}

/// Represents a markdown project in a TiefDown project.
///
/// Markdown projects are used to specify the location of markdown files and their associated output.
///
/// # Fields
///
/// * `name` - The name of the markdown project.
/// * `path` - The path to the markdown directory.
/// * `output` - The path to the output directory.
/// * `metadata_fields` - Optional metadata fields specific to the markdown project.
/// * `default_profile` - The name of the default profile to use for conversion.
/// * `resources` - Optional resources to be copied alongside the combined markdown file.
#[derive(Deserialize, Serialize, Clone)]
pub struct MarkdownProject {
    pub name: String,
    pub path: PathBuf,
    pub output: PathBuf,
    pub metadata_fields: Option<Table>,
    pub default_profile: Option<String>,
    pub resources: Option<Vec<PathBuf>>,
}

/// Represents a metadata field when retrieving the metadata. This is not used for storing metadata.
///
/// # Fields
///
/// * `key` - The key of the metadata field.
/// * `value` - The value of the metadata field.
#[derive(Clone)]
pub struct MetadataField {
    pub key: String,
    pub value: String,
}

/// DTO containing the custom processors and preprocessors available to the project.
///
/// # Fields
///
/// * `preprocessors` - A list of preprocessors.
/// * `processors` - A list of processors.
#[derive(Deserialize, Serialize, Clone)]
pub struct Processors {
    pub preprocessors: Vec<PreProcessor>,
    pub processors: Vec<Processor>,
}

/// Represents preprocessors available to the project.
///
/// A preprocessor defines custom pandoc conversion arguments, replacing the default arguments.
///
/// # Fields
///
/// * `name` - The name of the preprocessor.
/// * `pandoc_args` - The arguments passed to the pandoc conversion process.
#[derive(Deserialize, Serialize, Clone)]
pub struct PreProcessor {
    pub name: String,
    pub pandoc_args: Vec<String>,
}

/// Represents processors available to the project.
///
/// A processor defines custom conversion arguments passed to the primary conversion process, supplementing the default arguments.
///
/// The primary conversion process is the process that processes the template. This could be XeLaTeX or Typst.
///
/// # Fields
///
/// * `name` - The name of the processor.
/// * `processor_args` - The arguments passed to the processor.
#[derive(Deserialize, Serialize, Clone)]
pub struct Processor {
    pub name: String,
    pub processor_args: Vec<String>,
}

/// The default pandoc arguments for LaTeX conversion.
pub static DEFAULT_TEX_PREPROCESSOR: LazyLock<PreProcessor> = LazyLock::new(|| PreProcessor {
    name: "default_tex_preprocessor".to_string(),
    pandoc_args: vec!["-o", "output.tex", "-t", "latex"]
        .iter()
        .map(|s| s.to_string())
        .collect(),
});

/// The default pandoc arguments for Typst conversion.
pub static DEFAULT_TYPST_PREPROCESSOR: LazyLock<PreProcessor> = LazyLock::new(|| PreProcessor {
    name: "default_typst_preprocessor".to_string(),
    pandoc_args: vec!["-o", "output.typ", "-t", "typst"]
        .iter()
        .map(|s| s.to_string())
        .collect(),
});

/// Represents the settings for metadata in the project.
///
/// # Fields
///
/// * `metadata_prefix` - The prefix to use for metadata fields.
/// ** This is used for defining the LaTeX macro and the name of the typst object containing the metadata.
#[derive(Deserialize, Serialize, Clone)]
pub struct MetadataSettings {
    pub metadata_prefix: Option<String>,
}

impl MetadataSettings {
    pub fn default() -> Self {
        Self {
            metadata_prefix: None,
        }
    }
}

/// Represents a conversion profile for the project.
///
/// Profiles are used to specify which templates to use for conversion. These can also be used for markdown projects.
///
/// # Fields
///
/// * `name` - The name of the profile.
/// * `templates` - A list of templates to use for conversion.
#[derive(Deserialize, Serialize, Clone)]
pub struct Profile {
    pub name: String,
    pub templates: Vec<String>,
}

/// Represents a template in the project.
///
/// # Fields
///
/// * `name` - The name of the template.
/// * `template_type` - The type of template.
/// * `template_file` - The path to the template file relative to the template directory.
/// * `output` - The path to the output file relative to the markdown project conversion directory.
/// * `filters` - A list of lua filters to apply to the template.
/// ** Can be a file or a directory.
/// * `preprocessor` - The name of the preprocessor to use for the template.
/// * `processor` - The name of the processor to use for the template.
#[derive(Deserialize, Serialize, Clone)]
pub struct TemplateMapping {
    pub name: String,
    pub template_type: TemplateType,
    pub template_file: Option<PathBuf>,
    pub output: Option<PathBuf>,
    pub filters: Option<Vec<String>>,
    pub preprocessor: Option<String>,
    pub processor: Option<String>,
}

pub(crate) fn upgrade_manifest(manifest: &mut Table, current_version: u32) -> Result<()> {
    if current_version != CURRENT_MANIFEST_VERSION {
        let mut updated_version = current_version;

        while updated_version < CURRENT_MANIFEST_VERSION {
            if updated_version == 0 {
                upgrade_manifest_v0_to_v1(manifest)?
            } else if updated_version == 1 {
                upgrade_manifest_v1_to_v2(manifest)?
            } else if updated_version == 2 {
                upgrade_manifest_v2_to_v3(manifest)?
            } else if updated_version == 3 {
                upgrade_manifest_v3_to_v4(manifest)?
            } else {
                return Err(eyre!(
                    "Manifest version {} is not supported for upgrades.",
                    updated_version
                ));
            }

            updated_version += 1;
        }
    }

    Ok(())
}

fn upgrade_manifest_v0_to_v1(manifest: &mut Table) -> Result<()> {
    manifest.insert("version".to_string(), toml::Value::Integer(1));

    if let Some(templates) = manifest.get("templates") {
        manifest.insert(
            "templates".to_string(),
            toml::Value::Array(
                templates
                    .as_array()
                    .unwrap_or(&Vec::new())
                    .iter()
                    .filter(|t| t.is_str())
                    .map(|template| {
                        let template_name = template.as_str().unwrap();
                        let template_type =
                            get_template_type_from_path(template_name).unwrap_or(TemplateType::Tex);
                        let mut table = Table::new();
                        table.insert(
                            "name".to_string(),
                            toml::Value::String(template_name.to_string()),
                        );

                        table.insert(
                            "template_type".to_string(),
                            toml::Value::String(template_type.to_string()),
                        );

                        toml::Value::Table(table)
                    })
                    .collect(),
            ),
        );
    }

    Ok(())
}

fn upgrade_manifest_v1_to_v2(manifest: &mut Table) -> Result<()> {
    manifest.insert("version".to_string(), toml::Value::Integer(2));

    manifest.insert(
        "custom_processors".to_string(),
        toml::Value::Table(Table::new()),
    );
    manifest["custom_processors"]
        .as_table_mut()
        .unwrap()
        .insert("preprocessors".to_string(), toml::Value::Array(Vec::new()));

    Ok(())
}

fn upgrade_manifest_v2_to_v3(manifest: &mut Table) -> Result<()> {
    manifest.insert("version".to_string(), toml::Value::Integer(3));

    manifest.insert(
        "metadata_fields".to_string(),
        toml::Value::Table(Table::new()),
    );
    manifest.insert(
        "metadata_settings".to_string(),
        toml::Value::Table(Table::new()),
    );
    manifest["custom_processors"]
        .as_table_mut()
        .unwrap()
        .insert("processors".to_string(), toml::Value::Array(Vec::new()));

    Ok(())
}

fn upgrade_manifest_v3_to_v4(manifest: &mut Table) -> Result<()> {
    manifest.insert("version".to_string(), toml::Value::Integer(4));

    let metadata_fields = manifest["metadata_fields"].clone();
    if metadata_fields.is_table() && metadata_fields.as_table().unwrap().len() > 0 {
        manifest.insert("shared_metadata".to_string(), metadata_fields);
    }
    manifest.remove("metadata_fields");

    if let Some(markdown_dir) = manifest.get("markdown_dir") {
        if let Some(markdown_dir) = markdown_dir.as_str() {
            let mut markdown_project = Table::new();
            markdown_project.insert(
                "name".to_string(),
                toml::Value::String(markdown_dir.to_string()),
            );
            markdown_project.insert(
                "path".to_string(),
                toml::Value::String(markdown_dir.to_string()),
            );
            markdown_project.insert("output".to_string(), toml::Value::String(".".to_string()));

            manifest.insert(
                "markdown_projects".to_string(),
                toml::Value::Array(vec![toml::Value::Table(markdown_project)]),
            );
        }

        manifest.remove("markdown_dir");
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;

    #[rstest]
    fn test_upgrade_manifest_v0_to_v1() {
        let manifest_content = r#"
markdown_dir = "Custom Markdown Directory"
templates = ["template1.tex", "template2.typ"]"#;
        let mut manifest = toml::from_str(manifest_content).unwrap();

        let result = upgrade_manifest_v0_to_v1(&mut manifest);

        assert!(result.is_ok());

        let expected_manifest = r#"markdown_dir = "Custom Markdown Directory"
version = 1

[[templates]]
name = "template1.tex"
template_type = "Tex"

[[templates]]
name = "template2.typ"
template_type = "Typst"
"#;

        let actual_manifest = toml::to_string(&manifest).unwrap();
        assert_eq!(expected_manifest, actual_manifest);
    }

    #[rstest]
    fn test_upgrade_manifest_v1_to_v2() {
        let manifest_content = r#"
markdown_dir = "Custom Markdown Directory"
version = 1

[[templates]]
name = "template1.tex"
template_type = "Tex"

[[templates]]
name = "template2.typ"
template_type = "Typst"
"#;

        let mut manifest = toml::from_str(manifest_content).unwrap();
        let result = upgrade_manifest_v1_to_v2(&mut manifest);
        assert!(result.is_ok());

        let expected_manifest = r#"markdown_dir = "Custom Markdown Directory"
version = 2

[custom_processors]
preprocessors = []

[[templates]]
name = "template1.tex"
template_type = "Tex"

[[templates]]
name = "template2.typ"
template_type = "Typst"
"#;

        let actual_manifest = toml::to_string(&manifest).unwrap();
        assert_eq!(expected_manifest, actual_manifest);
    }

    #[rstest]
    fn test_upgrade_manifest_v2_to_v3() {
        let manifest_content = r#"markdown_dir = "Custom Markdown Directory"
version = 2

[custom_processors]
preprocessors = []

[[templates]]
name = "template1.tex"
template_type = "Tex"

[[templates]]
name = "template2.typ"
template_type = "Typst"
"#;

        let mut manifest = toml::from_str(manifest_content).unwrap();
        let result = upgrade_manifest_v2_to_v3(&mut manifest);
        assert!(result.is_ok());

        let expected_manifest = r#"markdown_dir = "Custom Markdown Directory"
version = 3

[custom_processors]
preprocessors = []
processors = []

[metadata_fields]

[metadata_settings]

[[templates]]
name = "template1.tex"
template_type = "Tex"

[[templates]]
name = "template2.typ"
template_type = "Typst"
"#;

        let actual_manifest = toml::to_string(&manifest).unwrap();
        assert_eq!(expected_manifest, actual_manifest);
    }

    #[rstest]
    fn test_upgrade_manifest_v3_to_v4() {
        let manifest_content = r#"markdown_dir = "Custom Markdown Directory"
version = 3

[custom_processors]
preprocessors = []
processors = []

[metadata_fields]
author = "Author Name"
title = "Document Title"

[metadata_settings]
metadata_prefix = "supermeta"

[[templates]]
name = "template1.tex"
template_type = "Tex"

[[templates]]
name = "template2.typ"
template_type = "Typst"
"#;

        let mut manifest = toml::from_str(manifest_content).unwrap();
        let result = upgrade_manifest_v3_to_v4(&mut manifest);
        assert!(result.is_ok());

        let expected_manifest = r#"version = 4

[custom_processors]
preprocessors = []
processors = []

[[markdown_projects]]
name = "Custom Markdown Directory"
output = "."
path = "Custom Markdown Directory"

[metadata_settings]
metadata_prefix = "supermeta"

[shared_metadata]
author = "Author Name"
title = "Document Title"

[[templates]]
name = "template1.tex"
template_type = "Tex"

[[templates]]
name = "template2.typ"
template_type = "Typst"
"#;

        let actual_manifest = toml::to_string(&manifest).unwrap();
        assert_eq!(expected_manifest, actual_manifest);
    }
}