cooklang-import 0.3.2

A tool for importing recipes into Cooklang format
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use crate::extractors::{Extractor, ParsingContext};
use crate::model::Recipe;
use html_escape::decode_html_entities;
use log::debug;
use scraper::Selector;
use serde::Deserialize;
use serde_json::Value;
use std::convert::TryFrom;

pub struct JsonLdExtractor;

#[derive(Debug, Deserialize)]
struct JsonLdRecipe {
    name: String,
    description: DescriptionType,
    image: ImageType,
    #[serde(rename = "recipeIngredient", alias = "ingredients")]
    recipe_ingredient: Vec<String>,
    #[serde(rename = "recipeInstructions")]
    recipe_instructions: RecipeInstructions,
    // #[serde(rename = "recipeYield")]
    // recipe_yield: Option<RecipeYield>,
    // #[serde(rename = "prepTime")]
    // prep_time: Option<String>,
    // #[serde(rename = "cookTime")]
    // cook_time: Option<String>,
    // #[serde(rename = "totalTime")]
    // total_time: Option<String>,
    // #[serde(rename = "suitableForDiet")]
    // suitable_for_diet: Option<Vec<String>>,
    // #[serde(rename = "recipeCategory")]
    // recipe_category: Option<String>,
    // #[serde(rename = "recipeCuisine")]
    // recipe_cuisine: Option<String>,
    // keywords: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ImageObject {
    url: String,
}

#[derive(Debug, Deserialize)]
struct TextObject {
    text: String,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum DescriptionType {
    String(String),
    Object(TextObject),
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum ImageType {
    None,
    String(String),
    Object(ImageObject),
    // potentially multiple images as objects
    MultipleStrings(Vec<String>),
    MultipleObjects(Vec<ImageObject>),
}

#[derive(Debug, Deserialize)]
struct RecipeInstructionObject {
    text: String,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum RecipeInstructions {
    String(String),
    Multiple(Vec<String>),
    MultipleObject(Vec<RecipeInstructionObject>),
    HowTo(Vec<HowTo>),
}

#[derive(Debug, Deserialize)]
#[serde(tag = "@type")]
enum HowTo {
    HowToStep(HowToStep),
    HowToSection(HowToSection),
}

#[derive(Debug, Deserialize)]
#[serde(tag = "@type")]
struct HowToStep {
    text: Option<String>,
    description: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "@type")]
struct HowToSection {
    #[serde(rename = "itemListElement")]
    item_list_element: Vec<HowToStep>,
}

impl TryFrom<Option<&Value>> for JsonLdRecipe {
    type Error = serde_json::Error;

    fn try_from(value: Option<&Value>) -> Result<Self, Self::Error> {
        serde_json::from_value(value.unwrap().clone())
    }
}

fn decode_html_symbols(text: &str) -> String {
    // for some reason need to decode twice to get the correct string
    decode_html_entities(&decode_html_entities(text)).into_owned()
}

impl From<JsonLdRecipe> for Recipe {
    fn from(json_ld_recipe: JsonLdRecipe) -> Self {
        Recipe {
            name: decode_html_symbols(&json_ld_recipe.name),
            description: match json_ld_recipe.description {
                DescriptionType::String(desc) => Some(decode_html_symbols(&desc)),
                DescriptionType::Object(desc) => Some(decode_html_symbols(&desc.text)),
            },
            image: match json_ld_recipe.image {
                ImageType::String(img) => vec![decode_html_symbols(&img)],
                ImageType::MultipleStrings(imgs) => imgs
                    .into_iter()
                    .map(|img| decode_html_symbols(&img))
                    .collect(),
                ImageType::MultipleObjects(imgs) => imgs.into_iter().map(|img| img.url).collect(),
                ImageType::None => vec![],
                ImageType::Object(img) => vec![img.url],
            },
            ingredients: json_ld_recipe
                .recipe_ingredient
                .into_iter()
                .map(|ing| decode_html_symbols(&ing))
                .collect::<Vec<String>>()
                .join("\n"),
            instructions: match json_ld_recipe.recipe_instructions {
                RecipeInstructions::String(instructions) => decode_html_symbols(&instructions),
                RecipeInstructions::Multiple(instructions) => instructions
                    .into_iter()
                    .map(|step| decode_html_symbols(&step))
                    .collect::<Vec<String>>()
                    .join(" "),
                RecipeInstructions::MultipleObject(instructions) => instructions
                    .iter()
                    .map(|obj| decode_html_symbols(&obj.text))
                    .collect::<Vec<String>>()
                    .join(" "),
                RecipeInstructions::HowTo(sections) => sections
                    .into_iter()
                    .flat_map(|section| match section {
                        HowTo::HowToStep(step) => {
                            let mut texts = Vec::new();
                            if let Some(text) = step.text {
                                texts.push(text);
                            }
                            if let Some(desc) = step.description {
                                texts.push(desc);
                            }
                            texts
                        }
                        HowTo::HowToSection(section) => section
                            .item_list_element
                            .into_iter()
                            .flat_map(|step| {
                                let mut texts = Vec::new();
                                if let Some(text) = step.text {
                                    texts.push(text);
                                }
                                if let Some(desc) = step.description {
                                    texts.push(desc);
                                }
                                texts
                            })
                            .collect(),
                    })
                    .map(|text| decode_html_symbols(&text))
                    .collect::<Vec<String>>()
                    .join(" "),
            },
        }
    }
}

impl Extractor for JsonLdExtractor {
    fn can_parse(&self, context: &ParsingContext) -> bool {
        let selector = Selector::parse("script[type='application/ld+json']").unwrap();

        let document = &context.document;

        debug!("Document: {}", document.html());

        // Check all script elements
        document.select(&selector).any(|script| {
            debug!("Script: {}", script.inner_html());

            // Use the sanitize function before parsing
            let cleaned_json = sanitize_json(&script.inner_html());
            debug!("Cleaned JSON: {}", cleaned_json);
            if let Ok(json_ld) = serde_json::from_str::<Value>(&cleaned_json) {
                debug!("JSON-LD: {:#?}", json_ld);

                if json_ld.is_array() {
                    json_ld
                        .as_array()
                        .and_then(|arr| {
                            arr.iter()
                                .find(|item| item.get("recipeInstructions").is_some())
                        })
                        .is_some()
                } else if json_ld.get("recipeInstructions").is_some() {
                    debug!(
                        "Recipe Instructions: {:#?}",
                        json_ld.get("recipeInstructions")
                    );
                    true
                } else if let Some(graph) = json_ld.get("@graph") {
                    debug!("Graph: {:#?}", graph);
                    graph
                        .as_array()
                        .and_then(|arr| {
                            arr.iter().find(|item| {
                                item.get("@type") == Some(&Value::String("Recipe".to_string()))
                            })
                        })
                        .is_some()
                } else {
                    debug!("No valid recipe found in JSON-LD");
                    false
                }
            } else {
                false
            }
        })
    }

    fn parse(&self, context: &ParsingContext) -> Result<Recipe, Box<dyn std::error::Error>> {
        let selector = Selector::parse("script[type='application/ld+json']").unwrap();
        let document = &context.document;

        // Try each script element until we find a valid recipe
        for script in document.select(&selector) {
            // Use the sanitize function before parsing
            let cleaned_json = sanitize_json(&script.inner_html());
            if let Ok(json_ld) = serde_json::from_str::<Value>(&cleaned_json) {
                debug!("Trying JSON-LD: {:#?}", json_ld);

                let recipe_result: Option<JsonLdRecipe> = if json_ld.is_array() {
                    let extracted_recipe = json_ld.as_array().and_then(|arr| {
                        arr.iter()
                            .find(|item| item.get("recipeInstructions").is_some())
                    });

                    match JsonLdRecipe::try_from(extracted_recipe) {
                        Ok(recipe) => Some(recipe),
                        Err(e) => {
                            debug!("Failed to parse recipe: {}", e);
                            None
                        }
                    }
                } else if json_ld.get("recipeInstructions").is_some() {
                    debug!(
                        "Recipe Instructions: {:#?}",
                        json_ld.get("recipeInstructions")
                    );

                    match JsonLdRecipe::try_from(Some(&json_ld)) {
                        Ok(recipe) => Some(recipe),
                        Err(e) => {
                            debug!("Failed to parse recipe: {}", e);
                            None
                        }
                    }
                } else if let Some(graph) = json_ld.get("@graph") {
                    debug!("Graph: {:#?}", graph);
                    let extracted_recipe = graph.as_array().and_then(|arr| {
                        arr.iter().find(|item| {
                            item.get("@type") == Some(&Value::String("Recipe".to_string()))
                        })
                    });

                    match JsonLdRecipe::try_from(extracted_recipe) {
                        Ok(recipe) => Some(recipe),
                        Err(e) => {
                            debug!("Failed to parse recipe: {}", e);
                            None
                        }
                    }
                } else {
                    debug!("None of the conditions were met");
                    None
                };

                debug!("Recipe Result: {:#?}", recipe_result);

                // If we found a valid recipe, return it
                if let Some(recipe) = recipe_result {
                    debug!("Found valid recipe: {:#?}", recipe);
                    return Ok(Recipe::from(recipe));
                }
                // Otherwise continue to the next script block
            }
        }

        Err("No valid recipe found in any JSON-LD script".into())
    }
}

fn sanitize_json(json_str: &str) -> String {
    debug!("Original JSON: {}", json_str);

    let mut minified = String::with_capacity(json_str.len());
    let mut in_string = false;
    let mut prev_char = None;
    let mut depth = 0;
    let chars: Vec<char> = json_str.chars().collect();

    for (i, &c) in chars.iter().enumerate() {
        match c {
            '"' if prev_char != Some('\\') => {
                in_string = !in_string;
                if !in_string {
                    // We're ending a string - check if we need a comma
                    let rest_chars = chars.get(i + 1..).unwrap_or(&[]);
                    let next_char = rest_chars.iter().find(|c| !c.is_whitespace());
                    if !matches!(prev_char, Some(',') | Some('[') | Some('{'))
                        && matches!(next_char, Some('"' | '[' | '{'))
                    {
                        debug!("Adding missing comma after string");
                        minified.push('"');
                        minified.push(',');
                        prev_char = Some(',');
                        continue;
                    }
                }
                minified.push(c);
            }
            '[' | '{' if !in_string => {
                depth += 1;
                minified.push(c);
            }
            ']' | '}' if !in_string => {
                depth -= 1;
                minified.push(c);
                // Check if we need a comma after array/object closing
                if let Some(rest_chars) = chars.get(i + 1..) {
                    let next_char = rest_chars.iter().find(|&&c| !c.is_whitespace());
                    if depth > 0 && matches!(next_char, Some(&'"')) {
                        debug!("Adding missing comma after array/object closing");
                        minified.push(',');
                        prev_char = Some(',');
                        continue;
                    }
                }
            }
            ',' if !in_string => {
                // Avoid duplicate commas
                if prev_char != Some(',') {
                    minified.push(c);
                }
            }
            ':' if !in_string => {
                // Handle malformed key-value pairs
                if prev_char == Some(',') {
                    minified.pop(); // Remove the extra comma
                }
                minified.push(c);
            }
            _ => {
                if in_string || !c.is_whitespace() {
                    minified.push(c);
                }
            }
        }
        prev_char = Some(c);
    }

    // Clean up any remaining issues
    let cleaned = minified
        .replace(",]", "]")
        .replace(",}", "}")
        .replace(",,", ",")
        .replace(",:,", ":")
        .replace(":,", ":")
        .replace(",:", ":");

    debug!("Sanitized JSON: {}", cleaned);
    cleaned
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::extractors::ParsingContext;
    use scraper::Html;

    fn create_html_document(json_ld: &str) -> String {
        format!(
            r#"
            <!DOCTYPE html>
            <html>
            <head>
                <script type="application/ld+json">
                    {}
                </script>
            </head>
            <body></body>
            </html>
            "#,
            json_ld
        )
    }

    #[test]
    fn test_can_parse() {
        let html = "<html><body>Test</body></html>";
        let document = Html::parse_document(html);
        let context = ParsingContext {
            url: "http://example.com".to_string(),
            document,
            texts: None,
        };
        let extractor = JsonLdExtractor;
        assert!(extractor.can_parse(&context));
    }

    #[test]
    fn test_parse_basic_recipe() {
        let extractor = JsonLdExtractor;
        let json_ld = r#"
        {
            "@context": "https://schema.org/",
            "@type": "Recipe",
            "name": "Chocolate Chip Cookies",
            "description": "Delicious homemade cookies",
            "image": "https://example.com/cookie.jpg",
            "recipeIngredient": ["flour", "sugar", "chocolate chips"],
            "recipeInstructions": "Mix ingredients. Bake at 350F for 10 minutes."
        }
        "#;
        let html_str = create_html_document(json_ld);
        let document = Html::parse_document(&html_str);
        let context = ParsingContext {
            url: "http://example.com".to_string(),
            document,
            texts: None,
        };

        let result = extractor.parse(&context).unwrap();

        assert_eq!(result.name, "Chocolate Chip Cookies");
        assert_eq!(
            result.description,
            Some("Delicious homemade cookies".to_string())
        );
        assert_eq!(result.image, vec!["https://example.com/cookie.jpg"]);
        assert_eq!(result.ingredients, "flour\nsugar\nchocolate chips");
        assert_eq!(
            result.instructions,
            "Mix ingredients. Bake at 350F for 10 minutes."
        );
    }

    #[test]
    fn test_parse_recipe_with_array() {
        let extractor = JsonLdExtractor;
        let json_ld = r#"
        [
            {
                "@context": "https://schema.org/",
                "@type": "Recipe",
                "name": "Pasta Carbonara",
                "description": "Classic Italian pasta dish",
                "image": ["https://example.com/carbonara1.jpg", "https://example.com/carbonara2.jpg"],
                "recipeIngredient": ["spaghetti", "eggs", "bacon", "cheese"],
                "recipeInstructions": [
                    {"@type": "HowToStep", "text": "Cook pasta"},
                    {"@type": "HowToStep", "text": "Fry bacon"},
                    {"@type": "HowToStep", "text": "Mix eggs and cheese"},
                    {"@type": "HowToStep", "text": "Combine all ingredients"}
                ]
            },
            {
                "@type": "WebSite",
                "name": "Recipe Website"
            }
        ]
        "#;
        let html_str = create_html_document(json_ld);
        let document = Html::parse_document(&html_str);
        let context = ParsingContext {
            url: "http://example.com".to_string(),
            document,
            texts: None,
        };

        let result = extractor.parse(&context).unwrap();

        assert_eq!(result.name, "Pasta Carbonara");
        assert_eq!(
            result.description,
            Some("Classic Italian pasta dish".to_string())
        );
        assert_eq!(
            result.image,
            vec![
                "https://example.com/carbonara1.jpg",
                "https://example.com/carbonara2.jpg"
            ]
        );
        assert_eq!(result.ingredients, "spaghetti\neggs\nbacon\ncheese");
        assert_eq!(
            result.instructions,
            "Cook pasta Fry bacon Mix eggs and cheese Combine all ingredients"
        );
    }
}