You are here

function recipe_plaintext_import in Recipe 7.2

Same name and namespace in other branches
  1. 6 plugins/recipe_plaintext.module \recipe_plaintext_import()
  2. 7 includes/recipe_plaintext.module \recipe_plaintext_import()

Parsing instance for plain text recipes

2 string references to 'recipe_plaintext_import'
RecipeImportFormsTest::testPlainTextSingleImport in src/Tests/RecipeImportFormsTest.php
Test import a recipe in plain text format with the single import form.
recipe_plaintext_recipeio in modules/recipe_plaintext.module
Implements hook_recipeio().

File

modules/recipe_plaintext.module, line 55

Code

function recipe_plaintext_import($recipe_txt = NULL) {

  // region constants
  define('TITLE', 0);
  define('INGREDIENTS', 1);
  define('DIRECTIONS', 2);
  $recipe = array(
    'title' => '',
    'yield' => '1',
    'yield_unit' => 'Servings',
    'preptime' => 0,
    'cooktime' => 0,
    'categories' => array(),
    'ingredients' => array(),
    'instructions' => '',
    'notes' => '',
    'source' => '',
  );
  $region = TITLE;
  $recipe_lines = explode("\n", $recipe_txt);
  foreach ($recipe_lines as $line) {
    $line = trim($line);
    if ($region < DIRECTIONS && $line == '') {
      $region++;
      continue;
    }
    if ($region == TITLE) {
      $recipe['title'] = $line;
    }
    elseif ($region == INGREDIENTS) {

      // Replace a decimal quantity with a fractional one.  Needs to be done before ' ' to '-' below.
      $line = preg_replace_callback('/^(\\d+\\.\\d+)/', 'decimal_to_fraction_callback', $line);

      // Replace a space separated fraction with a '-' to normalize the input string.
      $line = preg_replace('/^(\\d+)[ \\-](\\d+)[\\/](\\d+)/', '${1}-${2}/${3}', $line);

      // 1 cup flour
      if (preg_match('/^([0-9\\-\\.\\/]+) +([A-Za-z\\.]+) +(.*)/', $line, $matches)) {
        $i = array();
        $i['quantity'] = recipe_ingredient_quantity_from_fraction($matches[1]);
        $i['unit_name'] = $matches[2];
        $i['ingredient_name'] = $matches[3];
        $i['ingredient_note'] = '';
        $i['unit_key'] = recipe_unit_fuzzymatch($i['unit_name']);
        if ($i['unit_key'] == FALSE) {
          $i['ingredient_note'] = $i['unit_name'] . '! ' . $i['ingredient_note'];
          $i['unit_key'] = 'unit';
          drupal_set_message(t('Could not find the ingredient units in %recipe (%line)', array(
            '%recipe' => $recipe['title'],
            '%line' => $line,
          )), 'warning');
        }

        // FALSE if no-match
        $i['ingred_obj'] = recipe_ingredient_match($i['ingredient_name']);
        $recipe['ingredients'][] = $i;
      }
      elseif (preg_match('/^([0-9\\-\\.\\/]+) +(.*)/', $line, $matches)) {
        $i = array();
        $i['quantity'] = recipe_ingredient_quantity_from_fraction($matches[1]);
        $i['unit_name'] = 'Unit';
        $i['ingredient_name'] = $matches[2];
        $i['ingredient_note'] = '';
        $i['unit_key'] = recipe_unit_fuzzymatch($i['unit_name']);
        if ($i['unit_key'] == FALSE) {
          $i['ingredient_note'] = $i['unit_name'] . '! ' . $i['ingredient_note'];
          $i['unit_key'] = 'unit';
          drupal_set_message(t('Could not find the ingredient units in %recipe (%line)', array(
            '%recipe' => $recipe['title'],
            '%line' => $line,
          )), 'warning');
        }

        // FALSE if no-match
        $i['ingred_obj'] = recipe_ingredient_match($i['ingredient_name']);
        $recipe['ingredients'][] = $i;
      }
      else {
        $i = array();
        $i['quantity'] = 0;
        $i['unit_name'] = 'Unit';
        $i['ingredient_name'] = "failed: " . $line;
        $i['unit_key'] = FALSE;
        $i['ingred_obj'] = FALSE;
        $recipe['ingredients'][] = $i;
      }
    }
    elseif ($region == DIRECTIONS) {
      $recipe['instructions'] .= $line . "\n";
    }
  }
  return $recipe;
}