cooklang-import 0.7.4

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
pub mod builder;
pub mod config;
pub mod error;
pub mod extractors;
pub mod model;
pub mod ocr;
pub mod providers;

// Public API exports
// Builder API (primary interface)
pub use builder::{ImportResult, LlmProvider, RecipeImporter};
// Error types
pub use error::ImportError;
// Types
pub use model::Recipe;
// Convenience functions (secondary interface) - exported at module level below
// Low-level API (for advanced users) - exported at module level below

use log::debug;
use reqwest::header::{HeaderMap, USER_AGENT};
use scraper::Html;

use crate::extractors::{Extractor, ParsingContext};

/// Fetches and extracts a recipe from a URL (convenience function).
///
/// This is a convenience wrapper around `fetch_recipe_with_timeout` with no timeout.
///
/// # Arguments
/// * `url` - The URL of the recipe webpage to fetch
///
/// # Returns
/// A `Recipe` struct containing the extracted recipe data
///
/// # Errors
/// Returns `ImportError::FetchError` if the URL cannot be fetched
/// Returns `ImportError::NoExtractorMatched` if no extractor can parse the recipe
pub async fn fetch_recipe(url: &str) -> Result<model::Recipe, ImportError> {
    fetch_recipe_with_timeout(url, None).await
}

/// Fetches and extracts a recipe from a URL.
///
/// This function performs the following steps:
/// 1. Fetches the webpage content with appropriate headers
/// 2. Attempts to extract recipe data using multiple extractors in sequence
/// 3. Returns the first successful extraction
///
/// # Arguments
/// * `url` - The URL of the recipe webpage to fetch
/// * `timeout` - Optional timeout for the HTTP request
///
/// # Returns
/// A `Recipe` struct containing the extracted recipe data
///
/// # Errors
/// Returns `ImportError::FetchError` if the URL cannot be fetched
/// Returns `ImportError::NoExtractorMatched` if no extractor can parse the recipe
pub async fn fetch_recipe_with_timeout(
    url: &str,
    timeout: Option<std::time::Duration>,
) -> Result<model::Recipe, ImportError> {
    // Set up headers with a user agent
    let mut headers = HeaderMap::new();
    headers.insert(USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36".parse()?);

    // Create client with optional timeout
    let mut client_builder = reqwest::Client::builder();
    if let Some(timeout_duration) = timeout {
        client_builder = client_builder.timeout(timeout_duration);
    }
    let client = client_builder.build()?;

    // Fetch the webpage content with headers
    let body = client
        .get(url)
        .headers(headers.clone())
        .send()
        .await?
        .text()
        .await?;

    let context = ParsingContext {
        url: url.to_string(),
        document: Html::parse_document(&body),
        texts: None,
    };

    let extractors_list: Vec<Box<dyn Extractor>> = vec![
        Box::new(extractors::JsonLdExtractor),
        Box::new(extractors::MicroDataExtractor),
        Box::new(extractors::HtmlClassExtractor),
        Box::new(extractors::PlainTextLlmExtractor),
    ];

    for extractor in extractors_list {
        match extractor.parse(&context) {
            Ok(recipe) => {
                debug!("{:#?}", recipe);
                return Ok(recipe);
            }
            Err(e) => {
                debug!("Extractor failed: {}", e);
            }
        }
    }

    Err(ImportError::NoExtractorMatched)
}

pub fn generate_frontmatter(metadata: &std::collections::HashMap<String, String>) -> String {
    if metadata.is_empty() {
        return String::new();
    }

    let mut frontmatter = String::from("---\n");

    // Sort keys for consistent output
    let mut keys: Vec<_> = metadata.keys().collect();
    keys.sort();

    for key in keys {
        if let Some(value) = metadata.get(key) {
            // Escape values that contain special characters
            if value.contains('\n') || value.contains('"') || value.contains(':') {
                frontmatter.push_str(&format!("{}: \"{}\"\n", key, value.replace('"', "\\\"")));
            } else {
                frontmatter.push_str(&format!("{key}: {value}\n"));
            }
        }
    }

    frontmatter.push_str("---\n\n");
    frontmatter
}

/// Converts a recipe to Cooklang format with explicit configuration.
///
/// This function allows passing API key and model directly instead of relying on
/// config files or environment variables.
///
/// # Arguments
/// * `recipe` - The recipe to convert
/// * `provider_name` - Optional provider name ("openai", "anthropic", "google", "ollama", "azure_openai")
/// * `api_key` - Optional API key to use
/// * `model` - Optional model name to use
///
/// # Returns
/// A string containing the recipe in Cooklang format, including frontmatter
///
/// # Errors
/// Returns `ImportError::ConversionError` if the conversion fails
pub async fn convert_recipe_with_config(
    recipe: &model::Recipe,
    provider_name: Option<&str>,
    api_key: Option<String>,
    model: Option<String>,
) -> Result<String, ImportError> {
    use crate::config::ProviderConfig;
    use crate::providers::{AnthropicProvider, OpenAIProvider};

    let name = provider_name.unwrap_or("anthropic");

    // Create provider based on name
    let converter: Box<dyn crate::providers::LlmProvider> = match name {
        "openai" => {
            if let Some(key) = api_key {
                // Create config with provided key
                let config = ProviderConfig {
                    enabled: true,
                    model: model.unwrap_or_else(|| "gpt-4".to_string()),
                    temperature: 0.7,
                    max_tokens: 4000,
                    api_key: Some(key),
                    base_url: None,
                    endpoint: None,
                    deployment_name: None,
                    api_version: None,
                    project_id: None,
                };
                Box::new(OpenAIProvider::new(&config).map_err(|e| {
                    ImportError::ConversionError(format!("Failed to create OpenAI provider: {}", e))
                })?)
            } else {
                Box::new(OpenAIProvider::from_env().map_err(|e| {
                    ImportError::ConversionError(format!("Failed to create OpenAI provider: {}", e))
                })?)
            }
        }
        "anthropic" => {
            // Create config with provided key or None (will fall back to env)
            let config = ProviderConfig {
                enabled: true,
                model: model.unwrap_or_else(|| "claude-sonnet-4-20250514".to_string()),
                temperature: 0.7,
                max_tokens: 4000,
                api_key,
                base_url: None,
                endpoint: None,
                deployment_name: None,
                api_version: None,
                project_id: None,
            };
            Box::new(AnthropicProvider::new(&config)
                .map_err(|e| ImportError::ConversionError(format!("Failed to create Anthropic provider: {}. Make sure ANTHROPIC_API_KEY is set or pass api_key to builder.", e)))?)
        }
        _ => {
            return Err(ImportError::ConversionError(format!(
                "Provider '{}' requires a config.toml file or use convert_recipe_with_provider",
                name
            )));
        }
    };

    // Convert using the provider
    let mut cooklang_recipe = converter
        .convert(&recipe.content)
        .await
        .map_err(|e| ImportError::ConversionError(e.to_string()))?;

    // Prepend frontmatter if there's metadata
    let frontmatter = generate_frontmatter(&recipe.metadata);
    if !frontmatter.is_empty() {
        cooklang_recipe = format!("{}{}", frontmatter, cooklang_recipe);
    }

    Ok(cooklang_recipe)
}

/// Converts a recipe to Cooklang format using a custom provider.
///
/// This function converts recipe ingredients and instructions from markdown
/// format to Cooklang format using the specified LLM provider.
///
/// # Arguments
/// * `recipe` - The recipe to convert
/// * `provider_name` - Optional provider name ("openai", "anthropic", "google", "ollama", "azure_openai")
///   If None, uses the default provider from config
///
/// # Returns
/// A string containing the recipe in Cooklang format, including frontmatter
///
/// # Errors
/// Returns `ImportError::ConversionError` if the conversion fails
/// Returns `ImportError::ConfigError` if the configuration is invalid
pub async fn convert_recipe_with_provider(
    recipe: &model::Recipe,
    provider_name: Option<&str>,
) -> Result<String, ImportError> {
    use crate::config::AiConfig;
    use crate::providers::{OpenAIProvider, ProviderFactory};

    // Try to load config, but continue if it fails (will use env vars)
    let config_result = AiConfig::load();

    let converter: Box<dyn crate::providers::LlmProvider> = match config_result {
        Ok(config) => {
            // Use provided name or fall back to default provider from config
            let name = provider_name.unwrap_or(&config.default_provider);

            // Get provider config
            let provider_config = config.providers.get(name).ok_or_else(|| {
                ImportError::ConversionError(format!(
                    "Provider '{}' not found in configuration",
                    name
                ))
            })?;

            // Create provider from factory
            ProviderFactory::create(name, provider_config)
                .map_err(|e| ImportError::ConversionError(e.to_string()))?
        }
        Err(_) => {
            // No config file - fall back to environment variables
            let name = provider_name.unwrap_or("openai");

            match name {
                "openai" => Box::new(OpenAIProvider::from_env().map_err(|e| {
                    ImportError::ConversionError(format!(
                        "Failed to create OpenAI provider from environment: {}",
                        e
                    ))
                })?),
                "anthropic" => {
                    use crate::providers::AnthropicProvider;
                    // Create a minimal config that will use environment variables
                    let config = crate::config::ProviderConfig {
                        enabled: true,
                        model: "claude-sonnet-4-20250514".to_string(),
                        temperature: 0.7,
                        max_tokens: 4000,
                        api_key: None, // Will fall back to ANTHROPIC_API_KEY env var
                        base_url: None,
                        endpoint: None,
                        deployment_name: None,
                        api_version: None,
                        project_id: None,
                    };
                    Box::new(AnthropicProvider::new(&config)
                        .map_err(|e| ImportError::ConversionError(format!("Failed to create Anthropic provider from environment: {}. Make sure ANTHROPIC_API_KEY is set.", e)))?)
                }
                _ => {
                    return Err(ImportError::ConversionError(
                        format!(
                            "No configuration file found. Provider '{}' requires a config.toml file. \
                            For OpenAI and Anthropic, you can use environment variables (OPENAI_API_KEY or ANTHROPIC_API_KEY).",
                            name
                        )
                    ));
                }
            }
        }
    };

    // Convert using the provider
    let mut cooklang_recipe = converter
        .convert(&recipe.content)
        .await
        .map_err(|e| ImportError::ConversionError(e.to_string()))?;

    // Prepend frontmatter if there's metadata
    let frontmatter = generate_frontmatter(&recipe.metadata);
    if !frontmatter.is_empty() {
        cooklang_recipe = frontmatter + &cooklang_recipe;
    }

    Ok(cooklang_recipe)
}

/// Fetches a recipe from a URL and converts it to Cooklang format.
///
/// This is a convenience function that wraps the builder API for simple use cases.
/// For more control (e.g., custom provider, timeout), use `RecipeImporter::builder()` directly.
///
/// # Arguments
/// * `url` - The URL of the recipe to fetch
///
/// # Returns
/// A `String` containing the recipe in Cooklang format
///
/// # Errors
/// Returns `ImportError` if the URL cannot be fetched, parsed, or converted
///
/// # Examples
/// ```no_run
/// use cooklang_import::import_from_url;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let cooklang = import_from_url("https://example.com/recipe").await?;
///     println!("{}", cooklang);
///     Ok(())
/// }
/// ```
pub async fn import_from_url(url: &str) -> Result<String, ImportError> {
    match RecipeImporter::builder().url(url).build().await? {
        builder::ImportResult::Cooklang(s) => Ok(s),
        builder::ImportResult::Recipe(_) => unreachable!("Default mode is Cooklang"),
    }
}

/// Fetches a recipe from a URL and returns it as a Recipe struct.
///
/// This extracts the recipe without converting to Cooklang format.
/// For more control, use `RecipeImporter::builder()` directly.
///
/// # Arguments
/// * `url` - The URL of the recipe to fetch
///
/// # Returns
/// A `Recipe` struct containing the recipe content
///
/// # Errors
/// Returns `ImportError` if the URL cannot be fetched or parsed
///
/// # Examples
/// ```no_run
/// use cooklang_import::extract_recipe_from_url;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let recipe = extract_recipe_from_url("https://example.com/recipe").await?;
///     println!("Content: {}", recipe.content);
///     Ok(())
/// }
/// ```
pub async fn extract_recipe_from_url(url: &str) -> Result<Recipe, ImportError> {
    match RecipeImporter::builder()
        .url(url)
        .extract_only()
        .build()
        .await?
    {
        builder::ImportResult::Recipe(r) => Ok(r),
        builder::ImportResult::Cooklang(_) => unreachable!("extract_only sets Recipe mode"),
    }
}

/// Converts plain text to Cooklang format.
///
/// This is a convenience function that wraps the builder API.
/// Use this when you have a recipe in unstructured plain text format.
/// The LLM will parse the text to extract ingredients and instructions.
/// For more control, use `RecipeImporter::builder()` directly.
///
/// # Arguments
/// * `text` - The recipe text in plain format
///
/// # Returns
/// A `String` containing the recipe in Cooklang format
///
/// # Errors
/// Returns `ImportError` if the text is invalid or conversion fails
///
/// # Examples
/// ```no_run
/// use cooklang_import::convert_text_to_cooklang;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let recipe_text = "Take 2 eggs and 1 cup of flour. Mix them together and bake at 350F for 30 minutes.";
///     let cooklang = convert_text_to_cooklang(recipe_text).await?;
///     println!("{}", cooklang);
///     Ok(())
/// }
/// ```
pub async fn convert_text_to_cooklang(text: &str) -> Result<String, ImportError> {
    match RecipeImporter::builder().text(text).build().await? {
        builder::ImportResult::Cooklang(s) => Ok(s),
        builder::ImportResult::Recipe(_) => unreachable!("Text + Cooklang mode"),
    }
}