You are here

function format_plaintext_ingredients in Recipe 6

Same name and namespace in other branches
  1. 7 includes/recipe_plaintext.module \format_plaintext_ingredients()
1 call to format_plaintext_ingredients()
recipe_plaintext_export in plugins/recipe_plaintext.module

File

plugins/recipe_plaintext.module, line 183

Code

function format_plaintext_ingredients($node = NULL) {
  $col_widths = array(
    'quantity' => 0,
    'unit' => 0,
  );
  foreach (array_keys($node->ingredients) as $key) {

    // Translate decimal quantity to text fraction.
    $node->ingredients[$key]['str_quantity'] = recipe_ingredient_quantity_from_decimal($node->ingredients[$key]['quantity'], TRUE);

    // Strip html entities.
    $node->ingredients[$key]['str_quantity'] = str_replace('⁄', '/', $node->ingredients[$key]['str_quantity']);

    // Collect column widths.
    if (strlen($node->ingredients[$key]['str_quantity']) > $col_widths['quantity']) {
      $col_widths['quantity'] = strlen($node->ingredients[$key]['str_quantity']);
    }
    $node->ingredients[$key]['str_ingredient'] = strlen($node->ingredients[$key]['note']) > 0 ? $node->ingredients[$key]['name'] . ' (' . $node->ingredients[$key]['note'] . ')' : $node->ingredients[$key]['name'];

    // Display units or abbreviation.
    // Print the abbreviation if recipe_unit_display says too or the abbreviation is blank (ie = Unit, which we don't print).
    if (variable_get('recipe_unit_display', 0) == 0 || $node->ingredients[$key]['abbreviation'] == ' ') {
      $node->ingredients[$key]['str_unit'] = $node->ingredients[$key]['abbreviation'];
    }
    else {
      $node->ingredients[$key]['str_unit'] = recipe_unit_name($node->ingredients[$key]['unit_id']);
    }
    if (strlen($node->ingredients[$key]['str_unit']) > $col_widths['unit']) {
      $col_widths['unit'] = strlen($node->ingredients[$key]['str_unit']);
    }
  }
  $wrap_pad = str_repeat(" ", $col_widths['unit'] + $col_widths['quantity'] + 2);

  // Render output with the correct column padding.
  $output = '';
  foreach ($node->ingredients as $key => $i) {
    $output .= wordwrap(sprintf("%" . $col_widths['quantity'] . "s %-" . $col_widths['unit'] . "s %s\n", $i['str_quantity'], $i['str_unit'], $i['str_ingredient']), 75, "\n{$wrap_pad}");
  }
  return $output;
}