cooklang-format 0.36.0

Render Cooklang recipes as Markdown, LaTeX, Typst, JSON, terminal text, or Cooklang source
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
//! Format a recipe as [schema.org/Recipe](https://schema.org/Recipe) JSON-LD.
//!
//! This is the shape search engines read, so the field names are fixed by the
//! vocabulary rather than chosen here.

use crate::quantity::grouped_quantity_fmt;
use cooklang::{convert::Converter, model::Item, Recipe};
use serde_json::{json, Value};
use std::io;

/// Write `recipe` as a schema.org `Recipe` JSON-LD object.
///
/// `pretty` indents the JSON. Nothing here can fail except the write itself,
/// which `serde_json` reports as the underlying [`io::Error`].
pub fn print_schema(
    recipe: &Recipe,
    name: &str,
    scale: f64,
    converter: &Converter,
    writer: impl io::Write,
    pretty: bool,
) -> io::Result<()> {
    let schema = create_schema_object(recipe, name, scale, converter);

    if pretty {
        serde_json::to_writer_pretty(writer, &schema)?;
    } else {
        serde_json::to_writer(writer, &schema)?;
    }

    Ok(())
}

/// Build the JSON-LD object. Infallible: every field is optional, and a
/// missing or unparseable one is simply left out.
fn create_schema_object(recipe: &Recipe, name: &str, scale: f64, converter: &Converter) -> Value {
    let mut schema = json!({
        "@context": "https://schema.org",
        "@type": "Recipe",
        "name": if scale != 1.0 {
            format!("{name} (scaled {scale}x)")
        } else {
            name.to_string()
        }
    });

    // Add description if present
    if let Some(desc) = recipe.metadata.description() {
        schema["description"] = json!(desc);
    }

    // Add tags as keywords
    if let Some(tags) = recipe.metadata.tags() {
        schema["keywords"] = json!(tags.join(", "));
    }

    // Add author information if present
    if let Some(author) = recipe.metadata.author() {
        let author_name = author.name().unwrap_or(author.url().unwrap_or(""));
        schema["author"] = json!({
            "@type": "Person",
            "name": author_name
        });
    }

    // Add source URL if present
    if let Some(source) = recipe.metadata.source() {
        if let Some(url) = source.url() {
            schema["url"] = json!(url);
        } else if let Some(name) = source.name() {
            schema["url"] = json!(name);
        }
    }

    // Add servings/yield
    if let Some(servings) = recipe.metadata.servings() {
        use cooklang::metadata::Servings;
        match servings {
            Servings::Number(n) => {
                // Apply scaling to servings
                let scaled_servings = if scale != 1.0 {
                    (n as f64 * scale).round() as u32
                } else {
                    n
                };
                schema["recipeYield"] = json!(format!("{} servings", scaled_servings));
            }
            Servings::Text(text) => {
                schema["recipeYield"] = json!(text);
            }
        }
    }

    // Add timing information
    add_time_fields(&mut schema, recipe);

    // Add nutrition information if present
    add_nutrition_info(&mut schema, recipe);

    // Add ingredients
    let ingredients = create_ingredients_list(recipe, converter);
    if !ingredients.is_empty() {
        schema["recipeIngredient"] = json!(ingredients);
    }

    // Add cookware as tools
    let tools = create_tools_list(recipe, converter);
    if !tools.is_empty() {
        schema["tool"] = json!(tools);
    }

    // Add instructions
    let instructions = create_instructions_list(recipe);
    if !instructions.is_empty() {
        schema["recipeInstructions"] = json!(instructions);
    }

    // Add recipe category and cuisine if available
    if let Some(category) = recipe.metadata.map.get("category") {
        if let Some(cat_str) = category.as_str() {
            schema["recipeCategory"] = json!(cat_str);
        }
    }

    if let Some(cuisine) = recipe.metadata.map.get("cuisine") {
        if let Some(cuisine_str) = cuisine.as_str() {
            schema["recipeCuisine"] = json!(cuisine_str);
        }
    }

    // Add image if present
    if let Some(image) = recipe.metadata.map.get("image") {
        if let Some(image_str) = image.as_str() {
            schema["image"] = json!(image_str);
        }
    }

    schema
}

fn add_time_fields(schema: &mut Value, recipe: &Recipe) {
    // Get prep time from metadata
    if let Some(prep_time_val) = recipe.metadata.get("prep time") {
        if let Some(prep_time_str) = prep_time_val.as_str() {
            schema["prepTime"] = json!(format_iso_duration(prep_time_str));
        }
    }

    // Get cook time from metadata
    if let Some(cook_time_val) = recipe.metadata.get("cook time") {
        if let Some(cook_time_str) = cook_time_val.as_str() {
            schema["cookTime"] = json!(format_iso_duration(cook_time_str));
        }
    }

    // Calculate total time if both prep and cook times are available
    let has_prep = recipe.metadata.get("prep time").is_some();
    let has_cook = recipe.metadata.get("cook time").is_some();
    if has_prep && has_cook {
        // Simplified - just sum the minutes from both times
        let mut total_minutes = 0;
        if let Some(prep_val) = recipe.metadata.get("prep time").and_then(|v| v.as_str()) {
            total_minutes += extract_number(prep_val).unwrap_or(0);
        }
        if let Some(cook_val) = recipe.metadata.get("cook time").and_then(|v| v.as_str()) {
            total_minutes += extract_number(cook_val).unwrap_or(0);
        }
        schema["totalTime"] = json!(format!("PT{}M", total_minutes));
    }
}

fn format_iso_duration(time_str: &str) -> String {
    // Convert time strings like "30 minutes" or "1 hour" to ISO 8601 duration format
    // This is a simplified implementation
    let lower = time_str.to_lowercase();

    if lower.contains("hour") {
        if let Some(hours) = extract_number(&lower) {
            return format!("PT{hours}H");
        }
    } else if lower.contains("min") {
        if let Some(minutes) = extract_number(&lower) {
            return format!("PT{minutes}M");
        }
    }

    // Fallback: assume minutes if just a number
    if let Some(minutes) = extract_number(&lower) {
        return format!("PT{minutes}M");
    }

    "PT0M".to_string()
}

fn extract_number(s: &str) -> Option<i32> {
    s.chars()
        .filter(|c| c.is_numeric())
        .collect::<String>()
        .parse::<i32>()
        .ok()
}

fn add_nutrition_info(schema: &mut Value, recipe: &Recipe) {
    let mut nutrition = json!({
        "@type": "NutritionInformation"
    });

    let mut has_nutrition = false;

    // Check for various nutrition fields in metadata
    if let Some(calories) = recipe.metadata.map.get("calories") {
        if let Some(cal_str) = calories.as_str() {
            nutrition["calories"] = json!(format!("{} calories", cal_str));
            has_nutrition = true;
        } else if let Some(cal_num) = calories.as_u64() {
            nutrition["calories"] = json!(format!("{} calories", cal_num));
            has_nutrition = true;
        }
    }

    if let Some(protein) = recipe.metadata.map.get("protein") {
        if let Some(prot_str) = protein.as_str() {
            nutrition["proteinContent"] = json!(prot_str);
            has_nutrition = true;
        }
    }

    if let Some(fat) = recipe.metadata.map.get("fat") {
        if let Some(fat_str) = fat.as_str() {
            nutrition["fatContent"] = json!(fat_str);
            has_nutrition = true;
        }
    }

    if let Some(carbs) = recipe.metadata.map.get("carbohydrates") {
        if let Some(carbs_str) = carbs.as_str() {
            nutrition["carbohydrateContent"] = json!(carbs_str);
            has_nutrition = true;
        }
    }

    if let Some(fiber) = recipe.metadata.map.get("fiber") {
        if let Some(fiber_str) = fiber.as_str() {
            nutrition["fiberContent"] = json!(fiber_str);
            has_nutrition = true;
        }
    }

    if let Some(sugar) = recipe.metadata.map.get("sugar") {
        if let Some(sugar_str) = sugar.as_str() {
            nutrition["sugarContent"] = json!(sugar_str);
            has_nutrition = true;
        }
    }

    if let Some(sodium) = recipe.metadata.map.get("sodium") {
        if let Some(sodium_str) = sodium.as_str() {
            nutrition["sodiumContent"] = json!(sodium_str);
            has_nutrition = true;
        }
    }

    if has_nutrition {
        schema["nutrition"] = nutrition;
    }
}

fn create_ingredients_list(recipe: &Recipe, converter: &Converter) -> Vec<String> {
    let mut ingredients = Vec::new();

    for entry in recipe.group_ingredients(converter) {
        let ingredient = entry.ingredient;

        if !ingredient.modifiers().should_be_listed() {
            continue;
        }

        let mut ingredient_text = String::new();

        if !entry.quantity.is_empty() {
            ingredient_text.push_str(&grouped_quantity_fmt(&entry.quantity));
            ingredient_text.push(' ');
        }

        ingredient_text.push_str(&ingredient.display_name());

        if ingredient.modifiers().is_optional() {
            ingredient_text.push_str(" (optional)");
        }

        if let Some(note) = &ingredient.note {
            ingredient_text.push_str(&format!(", {note}"));
        }

        ingredients.push(ingredient_text);
    }

    ingredients
}

fn create_tools_list(recipe: &Recipe, converter: &Converter) -> Vec<String> {
    let mut tools = Vec::new();

    for item in recipe.group_cookware(converter) {
        let cw = item.cookware;

        let mut tool_text = String::new();

        if !item.quantity.is_empty() {
            tool_text.push_str(&grouped_quantity_fmt(&item.quantity));
            tool_text.push(' ');
        }

        tool_text.push_str(cw.display_name());

        if cw.modifiers().is_optional() {
            tool_text.push_str(" (optional)");
        }

        if let Some(note) = &cw.note {
            tool_text.push_str(&format!(", {note}"));
        }

        tools.push(tool_text);
    }

    tools
}

fn build_step(recipe: &Recipe, step: &cooklang::model::Step, step_number: usize) -> Value {
    let mut step_text = String::new();
    let mut total_seconds: f64 = 0.0;
    let mut has_timers = false;

    for item in &step.items {
        match item {
            Item::Text { value } => {
                step_text.push_str(value);
            }
            &Item::Ingredient { index } => {
                let igr = &recipe.ingredients[index];
                step_text.push_str(&igr.display_name());
            }
            &Item::Cookware { index } => {
                let cw = &recipe.cookware[index];
                step_text.push_str(&cw.name);
            }
            &Item::Timer { index } => {
                let t = &recipe.timers[index];
                if let Some(name) = &t.name {
                    step_text.push_str(&format!("{name} for "));
                }
                if let Some(quantity) = &t.quantity {
                    step_text.push_str(&quantity.to_string());

                    // Accumulate timer duration for timeRequired.
                    // Range values are averaged (e.g. 15-30 min → 22.5 min).
                    // Note: the parser produces Range only from scaling operations,
                    // not from syntax like ~{15-30%min} (which parses as Text).
                    let seconds = match quantity.value() {
                        cooklang::quantity::Value::Number(n) => Some(n.value()),
                        cooklang::quantity::Value::Range { start, end } => {
                            Some((start.value() + end.value()) / 2.0)
                        }
                        cooklang::quantity::Value::Text(_) => None,
                    };
                    if let Some(val) = seconds {
                        let multiplier = match quantity.unit().map(|u| u.to_lowercase()).as_deref()
                        {
                            Some("s" | "sec" | "second" | "seconds") => Some(1.0),
                            Some("h" | "hr" | "hour" | "hours") => Some(3600.0),
                            // Cooklang timers without an explicit unit default to minutes
                            Some("m" | "min" | "minute" | "minutes") | None => Some(60.0),
                            Some(_) => None, // unknown unit — skip rather than guess
                        };
                        if let Some(m) = multiplier {
                            total_seconds += val * m;
                            has_timers = true;
                        }
                    }
                }
            }
            &Item::InlineQuantity { index } => {
                let q = &recipe.inline_quantities[index];
                step_text.push_str(&q.to_string());
            }
        }
    }

    let mut instruction = json!({
        "@type": "HowToStep",
        "position": step_number,
        "text": step_text.trim()
    });

    // Add timeRequired if the step has timers with a positive total duration.
    // The total_secs > 0 guard also prevents emitting a bare "PT" string
    // when all timers round to zero (e.g. ~{0%minutes}).
    let total_secs = total_seconds.round() as i64;
    if has_timers && total_secs > 0 {
        let hours = total_secs / 3600;
        let mins = (total_secs % 3600) / 60;
        let secs = total_secs % 60;

        let mut dur = "PT".to_string();
        if hours > 0 {
            dur.push_str(&format!("{hours}H"));
        }
        if mins > 0 {
            dur.push_str(&format!("{mins}M"));
        }
        if secs > 0 {
            dur.push_str(&format!("{secs}S"));
        }
        instruction["timeRequired"] = json!(dur);
    }

    instruction
}

fn create_instructions_list(recipe: &Recipe) -> Vec<Value> {
    let mut instructions = Vec::new();
    // Step position is global across all sections, consistent with Google's
    // structured data examples for Recipe.
    let mut step_number = 0;

    for section in &recipe.sections {
        let mut section_steps = Vec::new();
        for content in &section.content {
            if let cooklang::Content::Step(step) = content {
                step_number += 1;
                section_steps.push(build_step(recipe, step, step_number));
            }
        }

        if section_steps.is_empty() {
            continue;
        }

        if let Some(name) = &section.name {
            // Wrap in HowToSection only for named sections
            let section_obj = json!({
                "@type": "HowToSection",
                "name": name,
                "itemListElement": section_steps
            });
            instructions.push(section_obj);
        } else {
            // Unnamed sections emit steps flat
            instructions.extend(section_steps);
        }
    }

    instructions
}