Example #1
0
 public static function parse($html, $url)
 {
     $recipe = new RecipeParser_Recipe();
     // Turn off libxml errors to prevent mismatched tag warnings.
     libxml_use_internal_errors(true);
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc = new DOMDocument();
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     // Title
     $nodes = $xpath->query('//*[@id="page-title"]');
     if ($nodes->length) {
         $line = RecipeParser_Text::formatTitle($nodes->item(0)->nodeValue);
         $recipe->title = $line;
     }
     // Times
     $nodes = $xpath->query('//*[@class="field-recipe-time"]');
     foreach ($nodes as $node) {
         $line = RecipeParser_Text::formatAsOneLine($node->nodeValue);
         if (strpos($line, "Hands-On Time") !== false) {
             $line = str_replace("Hands-On Time ", "", $line);
             $recipe->time["prep"] = RecipeParser_Times::toMinutes($line);
         } else {
             if (strpos($line, "Total Time") !== false) {
                 $line = str_replace("Total Time ", "", $line);
                 $recipe->time["total"] = RecipeParser_Times::toMinutes($line);
             }
         }
     }
     // Yield
     $nodes = $xpath->query('//*[@class="field-yield"]');
     if ($nodes->length) {
         $line = $nodes->item(0)->nodeValue;
         $line = RecipeParser_Text::formatYield($line);
         $recipe->yield = $line;
     }
     // Ingredients
     $nodes = $xpath->query('//*[@class="field-ingredients"]');
     foreach ($nodes as $node) {
         $line = $node->nodeValue;
         $line = RecipeParser_Text::formatAsOneLine($line);
         $recipe->appendIngredient($line);
     }
     // Instructions
     $nodes = $xpath->query('//*[@class="field-instructions"]//li');
     foreach ($nodes as $node) {
         $line = $node->nodeValue;
         $line = RecipeParser_Text::formatAsOneLine($line);
         $recipe->appendInstruction($line);
     }
     // Photo
     $nodes = $xpath->query('//*[@property="og:image"]');
     if ($nodes->length) {
         $photo_url = $nodes->item(0)->getAttribute('content');
         $recipe->photo_url = RecipeParser_Text::relativeToAbsolute($photo_url, $url);
     }
     return $recipe;
 }
 public static function parse($html, $url)
 {
     $recipe = RecipeParser_Parser_Microformat::parse($html, $url);
     libxml_use_internal_errors(true);
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc = new DOMDocument();
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     $hrecipe = $xpath->query('//section[@role="main"]');
     if ($hrecipe->length) {
         $hrecipe = $hrecipe->item(0);
         // Title is not marked up with class="fn"
         $nodes = $xpath->query('.//h1', $hrecipe);
         if ($nodes->length) {
             $value = $nodes->item(0)->nodeValue;
             $recipe->title = RecipeParser_Text::formatTitle($value);
         }
         // Yield -- Class names are conflated
         $nodes = $xpath->query('.//*[@class="info yield"]', $hrecipe);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
             $recipe->yield = RecipeParser_Text::formatYield($line);
         }
         $nodes = $xpath->query('.//span[@itemprop="recipeYield"]', $hrecipe);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
             $recipe->yield = RecipeParser_Text::formatYield($line);
         }
         // Prep Times -- Class names are conflated
         $nodes = $xpath->query('.//*[@class="info preptime"]', $hrecipe);
         if ($nodes->length) {
             $value = $nodes->item(0)->nodeValue;
             $recipe->time['prep'] = RecipeParser_Times::toMinutes($value);
         }
         // Total Time / Duration -- Class names are conflated
         $nodes = $xpath->query('.//*[@class="info duration"]', $hrecipe);
         if ($nodes->length) {
             $value = $nodes->item(0)->nodeValue;
             $recipe->time['total'] = RecipeParser_Times::toMinutes($value);
         }
     }
     // Photo
     $nodes = $xpath->query('//section[@class="content-unit"]/img');
     if ($nodes->length) {
         $photo_url = $nodes->item(0)->getAttribute('src');
         if ($photo_url) {
             $recipe->photo_url = RecipeParser_Text::relativeToAbsolute($photo_url, $url);
         }
     }
     // Remove recipe title intros -- e.g. "Sunday Dinner: Pork Ribs" changes to "Pork Ribs"
     if (strpos($recipe->title, ": ") !== false) {
         $recipe->title = preg_replace("/^[^:]+: (.+)/", "\$1", $recipe->title);
     }
     return $recipe;
 }
Example #3
0
 public static function parse($html, $url)
 {
     $recipe = RecipeParser_Parser_MicrodataDataVocabulary::parse($html, $url);
     libxml_use_internal_errors(true);
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc = new DOMDocument();
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     // Title missing?
     if (!$recipe->title) {
         $nodes = $xpath->query('//meta[@property="og:title"]');
         if ($nodes->length) {
             $line = $nodes->item(0)->getAttribute("content");
             $line = RecipeParser_Text::formatTitle($line);
             $recipe->title = $line;
         }
     }
     // Photo URL, use larger version found on MyRecipes
     $recipe->photo_url = str_replace('-l.jpg', '-x.jpg', $recipe->photo_url);
     // Credits
     $nodes = $xpath->query('//*[@class="link-list"]/h4');
     if ($nodes->length) {
         $line = trim($nodes->item(0)->nodeValue);
         if (strpos($line, "More from") === 0) {
             $line = str_replace("More from ", "", $line);
             $recipe->credits = $line;
         }
     }
     // Times
     $searches = array('prep' => 'prep: ', 'cook' => 'cook: ', 'total' => 'total: ');
     $nodes = $xpath->query('//*[@class="recipe-time-info"]');
     foreach ($nodes as $node) {
         $line = trim(strtolower($node->nodeValue));
         foreach ($searches as $key => $value) {
             if (strpos($line, $value) === 0) {
                 $line = str_replace($value, "", $line);
                 $recipe->time[$key] = RecipeParser_Times::toMinutes($line);
             }
         }
     }
     // Clean up each of the ingredients to remove "$Click to see savings"
     // These don't come through in the curl'ed test files
     for ($i = 0; $i < count($recipe->ingredients); $i++) {
         for ($j = 0; $j < count($recipe->ingredients[$i]['list']); $j++) {
             if (strpos($recipe->ingredients[$i]['list'][$j], "\$") > 0) {
                 $recipe->ingredients[$i]['list'][$j] = substr($recipe->ingredients[$i]['list'][$j], 0, strpos($recipe->ingredients[$i]['list'][$j], "\$"));
             }
         }
     }
     return $recipe;
 }
 public static function parse($html, $url)
 {
     $recipe = new RecipeParser_Recipe();
     libxml_use_internal_errors(true);
     $doc = new DOMDocument();
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     // Title
     $nodes = $xpath->query('//*[@property="v:name"]');
     if ($nodes->length) {
         $recipe->title = trim($nodes->item(0)->nodeValue);
     }
     // Summary
     $nodes = $xpath->query('//*[@property="v:summary"]');
     if ($nodes->length) {
         $value = trim($nodes->item(0)->nodeValue);
         $recipe->description = $value;
     }
     // Times
     $searches = array('v:prepTime' => 'prep', 'v:cookTime' => 'cook', 'v:totalTime' => 'total');
     foreach ($searches as $itemprop => $time_key) {
         $nodes = $xpath->query('//*[@property="' . $itemprop . '"]');
         if ($nodes->length) {
             if ($value = $nodes->item(0)->getAttribute('content')) {
                 $value = RecipeParser_Text::iso8601ToMinutes($value);
             } else {
                 $value = trim($nodes->item(0)->nodeValue);
                 $value = RecipeParser_Times::toMinutes($value);
             }
             if ($value) {
                 $recipe->time[$time_key] = $value;
             }
         }
     }
     // Yield
     $nodes = $xpath->query('//*[@property="v:yield"]');
     if ($nodes->length) {
         $line = trim($nodes->item(0)->nodeValue);
         $line = preg_replace('/\\s+/', ' ', $line);
         $recipe->yield = RecipeParser_Text::formatYield($line);
     }
     // Ingredients
     $nodes = null;
     // (data-vocabulary)
     $nodes = $xpath->query('//*[@rel="v:ingredient"]');
     foreach ($nodes as $node) {
         $value = $node->nodeValue;
         $value = RecipeParser_Text::formatAsOneLine($value);
         if (empty($value)) {
             continue;
         }
         if (RecipeParser_Text::matchSectionName($value)) {
             $value = RecipeParser_Text::formatSectionName($value);
             $recipe->addIngredientsSection($value);
         } else {
             $recipe->appendIngredient($value);
         }
     }
     // Instructions
     $found = false;
     // Some sites will use an "instruction" class for each line.
     if (!$found) {
         $nodes = $xpath->query('//*[@property="v:instructions"]//*[@property="v:instruction"]');
         if ($nodes->length) {
             RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
             $found = true;
         }
     }
     // Look for markup that uses <li>, <p> or other tags for each instruction.
     $search_sub_nodes = array("p", "li");
     while (!$found && ($tag = array_pop($search_sub_nodes))) {
         $nodes = $xpath->query('//*[@property="v:instructions"]//' . $tag);
         if ($nodes->length) {
             RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
             $found = true;
         }
     }
     // Either multiple instrutions nodes, or one node with a blob of text.
     if (!$found) {
         $nodes = $xpath->query('//*[@property="v:instructions"]');
         if ($nodes->length > 1) {
             // Multiple nodes
             RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
             $found = true;
         } else {
             if ($nodes->length == 1) {
                 // Blob
                 $str = $nodes->item(0)->nodeValue;
                 RecipeParser_Text::parseInstructionsFromBlob($str, $recipe);
                 $found = true;
             }
         }
     }
     // Photo
     $photo_url = "";
     $nodes = $xpath->query('//*[@rel="v:photo"]');
     if ($nodes->length) {
         $photo_url = $nodes->item(0)->getAttribute('src');
     }
     if (!$photo_url) {
         // for <img> as sub-node of rel="v:photo"
         $nodes = $xpath->query('//*[@rel="v:photo"]//img');
         if ($nodes->length) {
             $photo_url = $nodes->item(0)->getAttribute('src');
         }
     }
     if ($photo_url) {
         $recipe->photo_url = RecipeParser_Text::formatPhotoUrl($photo_url, $url);
     }
     // Credits
     $nodes = $xpath->query('//*[@property="v:author"]');
     if ($nodes->length) {
         $line = $nodes->item(0)->nodeValue;
         $recipe->credits = RecipeParser_Text::formatCredits($line);
     }
     return $recipe;
 }
 public static function parse($html, $url)
 {
     // Get all of the standard microdata stuff we can find.
     $recipe = RecipeParser_Parser_MicrodataSchema::parse($html, $url);
     // Turn off libxml errors to prevent mismatched tag warnings.
     libxml_use_internal_errors(true);
     $doc = new DOMDocument();
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     // --- Allrecipes allows for custom recipes that use a different
     // --- template than their standard content. This template is not currently
     // --- using schema.org/Recipe. So we'll look for fields that need to be
     // --- overridden.
     // Title
     if (!$recipe->title) {
         $node_list = $xpath->query('//h1[@id = "itemTitle"]');
         if ($node_list->length) {
             $value = $node_list->item(0)->nodeValue;
             $value = trim($value);
             $recipe->title = $value;
         }
     }
     // Yield
     if (!$recipe->yield) {
         $node_list = $xpath->query('//div[@class = "servings-form"]//span[@class = "yield yieldform"]');
         if ($node_list->length) {
             $value = $node_list->item(0)->nodeValue;
             $recipe->yield = $value;
         }
     }
     // Times
     $searches = array('liPrep' => 'prep', 'liCook' => 'cook', 'liTotal' => 'total');
     foreach ($searches as $id_name => $time_key) {
         $nodes = $xpath->query('.//*[@id="' . $id_name . '"]');
         if ($nodes->length) {
             $value = RecipeParser_Text::formatAsOneLine($nodes->item(0)->nodeValue);
             $value = trim(preg_replace("/(COOK|PREP|READY IN)/", "", $value));
             $value = RecipeParser_Times::toMinutes($value);
             if ($value) {
                 $recipe->time[$time_key] = $value;
             }
         }
     }
     // Ingredients
     if (!count($recipe->ingredients[0]["list"])) {
         $node_list = $xpath->query('//li[contains(concat(" ", normalize-space(@class), " "), " ingredient ")]');
         foreach ($node_list as $node) {
             $line = trim(strip_tags($node->nodeValue));
             if (preg_match("/^(.+):\$/", $line, $m)) {
                 $recipe->addIngredientsSection(ucfirst(strtolower($m[1])));
             } else {
                 if ($line) {
                     $recipe->appendIngredient($line);
                 }
             }
         }
     }
     // Instructions
     if (!count($recipe->instructions[0]["list"])) {
         $nodes = $xpath->query('//div[@class="directions"]//ol/li');
         foreach ($nodes as $node) {
             $line = RecipeParser_Text::formatAsOneLine($node->nodeValue);
             if (preg_match("/^(.+):\$/", $line, $m)) {
                 $recipe->addInstructionsSection(ucfirst(strtolower($m[1])));
             } else {
                 if ($line) {
                     $recipe->appendInstruction($line);
                 }
             }
         }
     }
     // Photo URL
     // Get larger images
     if ($recipe->photo_url) {
         $recipe->photo_url = str_replace('/userphoto/small/', '/userphoto/big/', $recipe->photo_url);
         $recipe->photo_url = str_replace('/userphotos/140x140/', '/userphotos/250x250/', $recipe->photo_url);
     }
     return $recipe;
 }
 public static function parse($html, $url)
 {
     $recipe = new RecipeParser_Recipe();
     libxml_use_internal_errors(true);
     $doc = new DOMDocument();
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     $microdata = null;
     $nodes = $xpath->query('//*[contains(@itemtype, "//schema.org/Recipe") or contains(@itemtype, "//schema.org/recipe")]');
     if ($nodes->length) {
         $microdata = $nodes->item(0);
     }
     // Parse elements
     if ($microdata) {
         // Title
         $nodes = $xpath->query('.//*[@itemprop="name"]', $microdata);
         if ($nodes->length) {
             $value = trim($nodes->item(0)->nodeValue);
             $recipe->title = RecipeParser_Text::formatTitle($value);
         }
         // Summary
         $nodes = $xpath->query('.//*[@itemprop="description"]', $microdata);
         if ($nodes->length) {
             $value = $nodes->item(0)->nodeValue;
             $value = RecipeParser_Text::formatAsParagraphs($value);
             $recipe->description = $value;
         }
         // Times
         $searches = array('prepTime' => 'prep', 'cookTime' => 'cook', 'totalTime' => 'total');
         foreach ($searches as $itemprop => $time_key) {
             $nodes = $xpath->query('.//*[@itemprop="' . $itemprop . '"]', $microdata);
             if ($nodes->length) {
                 if ($value = $nodes->item(0)->getAttribute('content')) {
                     $value = RecipeParser_Text::iso8601ToMinutes($value);
                 } else {
                     if ($value = $nodes->item(0)->getAttribute('datetime')) {
                         $value = RecipeParser_Text::iso8601ToMinutes($value);
                     } else {
                         $value = trim($nodes->item(0)->nodeValue);
                         $value = RecipeParser_Times::toMinutes($value);
                     }
                 }
                 if ($value) {
                     $recipe->time[$time_key] = $value;
                 }
             }
         }
         // Yield
         $nodes = $xpath->query('.//*[@itemprop="recipeYield"]', $microdata);
         if (!$nodes->length) {
             $nodes = $xpath->query('.//*[@itemprop="recipeyield"]', $microdata);
         }
         if ($nodes->length) {
             if ($nodes->item(0)->hasAttribute('content')) {
                 $line = $nodes->item(0)->getAttribute('content');
             } else {
                 $line = $nodes->item(0)->nodeValue;
             }
             $recipe->yield = RecipeParser_Text::formatYield($line);
         }
         // Ingredients
         $nodes = $xpath->query('//*[@itemprop="ingredients"]');
         foreach ($nodes as $node) {
             $value = $node->nodeValue;
             $value = RecipeParser_Text::formatAsOneLine($value);
             if (empty($value)) {
                 continue;
             }
             if (strlen($value) > 150) {
                 // probably a mistake, like a run-on of existing ingredients?
                 continue;
             }
             if (RecipeParser_Text::matchSectionName($value)) {
                 $value = RecipeParser_Text::formatSectionName($value);
                 $recipe->addIngredientsSection($value);
             } else {
                 $recipe->appendIngredient($value);
             }
         }
         // Instructions
         $found = false;
         // Look for markup that uses <li> tags for each instruction.
         if (!$found) {
             $nodes = $xpath->query('//*[@itemprop="recipeInstructions"]//li');
             if ($nodes->length) {
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             }
         }
         // Look for instructions as direct descendents of "recipeInstructions".
         if (!$found) {
             $nodes = $xpath->query('//*[@itemprop="recipeInstructions"]/*');
             if ($nodes->length) {
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             }
         }
         // Some sites will use an "instruction" class for each line.
         if (!$found) {
             $nodes = $xpath->query('.//*[@itemprop="recipeInstructions"]//*[contains(concat(" ", normalize-space(@class), " "), " instruction ")]');
             if ($nodes->length) {
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             }
         }
         // Either multiple recipeInstructions nodes, or one node with a blob of text.
         if (!$found) {
             $nodes = $xpath->query('.//*[@itemprop="recipeInstructions"]');
             if ($nodes->length > 1) {
                 // Multiple nodes
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             } else {
                 if ($nodes->length == 1) {
                     // Blob
                     $str = $nodes->item(0)->nodeValue;
                     RecipeParser_Text::parseInstructionsFromBlob($str, $recipe);
                     $found = true;
                 }
             }
         }
         // Photo
         $photo_url = "";
         if (!$photo_url) {
             // try to find open graph url
             $nodes = $xpath->query('//meta[@property="og:image"]');
             if ($nodes->length) {
                 $photo_url = $nodes->item(0)->getAttribute('content');
             }
         }
         if (!$photo_url) {
             $nodes = $xpath->query('.//*[@itemprop="image"]', $microdata);
             if ($nodes->length) {
                 $photo_url = $nodes->item(0)->getAttribute('src');
             }
         }
         if (!$photo_url) {
             // for <img> as sub-node of class="photo"
             $nodes = $xpath->query('.//*[@itemprop="image"]//img', $microdata);
             if ($nodes->length) {
                 $photo_url = $nodes->item(0)->getAttribute('src');
             }
         }
         if ($photo_url) {
             $recipe->photo_url = RecipeParser_Text::formatPhotoUrl($photo_url, $url);
         }
         // Credits
         $line = "";
         $nodes = $xpath->query('.//*[@itemprop="author"]', $microdata);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
         }
         $nodes = $xpath->query('.//*[@itemprop="publisher"]', $microdata);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
         }
         $recipe->credits = RecipeParser_Text::formatCredits($line);
     }
     return $recipe;
 }
Example #7
0
 public static function parse($html, $url)
 {
     // Turn off libxml errors to prevent mismatched tag warnings.
     libxml_use_internal_errors(true);
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc = new DOMDocument();
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     $recipe = RecipeParser_Parser_Microformat::parse($html, $url);
     // OVERRIDES FOR ABOUT.COM
     // Cook times
     $node_list = $xpath->query('//div[@id = "articlebody"]/h3');
     foreach ($node_list as $node) {
         $line = $node->nodeValue;
         $line = preg_replace('/[\\s\\"]+/', ' ', $line);
         $line = trim($line);
         if (preg_match("/prep time\\:(.+)/i", $line, $m)) {
             $recipe->time['prep'] = RecipeParser_Times::toMinutes($m[1]);
         } else {
             if (preg_match("/cook time\\:(.+)/i", $line, $m)) {
                 $recipe->time['cook'] = RecipeParser_Times::toMinutes($m[1]);
             }
         }
         // Total time is provided as part of microformat markup for About.com
     }
     // Instructions
     $recipe->resetInstructions();
     $nodes = $xpath->query('//div[@class = "instructions"]');
     foreach ($nodes as $node) {
         $text = trim($node->nodeValue);
         $lines = preg_split("/[\n\r]+/", $text);
         for ($i = count($lines) - 1; $i >= 0; $i--) {
             $lines[$i] = trim($lines[$i]);
             // Remove ends of lines that have the word "recipes" squashed up against
             // another word, which seems to happen with long lists of related
             // recipe links.
             // Remove lines that have the phrase "Xxxxx Recipes and More".
             // Remove lines that have the phrase "Xxxxx Recipes | Xxxxx".
             // Remove mentions of newsletters.
             $lines[$i] = preg_replace("/(.*)recipes\\w/i", "\$1", $lines[$i]);
             $lines[$i] = preg_replace("/(.*)More .* Recipes.*/", "\$1", $lines[$i]);
             $lines[$i] = preg_replace("/(.*)Recipes and More.*/", "\$1", $lines[$i]);
             $lines[$i] = preg_replace("/(.*)Recipes \\| .*/", "\$1", $lines[$i]);
             $lines[$i] = preg_replace("/(.*)Recipe Newsletter.*/", "\$1", $lines[$i]);
             // Look for a line in the instructions that looks like a yield.
             if (strpos($lines[$i], "Makes ") === 0) {
                 $recipe->yield = substr($lines[$i], 6);
                 $lines[$i] = '';
                 continue;
             }
         }
         foreach ($lines as $line) {
             $line = trim($line);
             if (empty($line)) {
                 continue;
             }
             // Match section names that read something like "---For the cake: Raise the oven temperature..."
             if (preg_match("/^(?:-{2,})?For the (.+)\\: (.*)\$/i", $line, $m)) {
                 $section = $m[1];
                 $section = RecipeParser_Text::formatSectionName($section);
                 $recipe->addInstructionsSection($section);
                 // Reset the value of $line, without the section name.
                 $line = ucfirst($m[2]);
             }
             $recipe->appendInstruction($line);
         }
     }
     return $recipe;
 }
 public static function parse($html, $url)
 {
     $recipe = new RecipeParser_Recipe();
     libxml_use_internal_errors(true);
     $doc = new DOMDocument();
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     // Find the top-level node for Recipe microdata
     $microdata = null;
     $nodes = $xpath->query('//*[@itemtype="http://data-vocabulary.org/Recipe"]');
     if ($nodes->length) {
         $microdata = $nodes->item(0);
     }
     // Parse elements
     if ($microdata) {
         // Title
         $nodes = $xpath->query('.//*[@itemprop="name"]', $microdata);
         if ($nodes->length) {
             $value = $nodes->item(0)->nodeValue;
             $value = RecipeParser_Text::formatTitle($value);
             $recipe->title = $value;
         }
         // Summary
         $nodes = $xpath->query('.//*[@itemprop="summary"]', $microdata);
         if ($nodes->length) {
             $value = trim($nodes->item(0)->nodeValue);
             $recipe->description = $value;
         }
         // Times
         $searches = array('prepTime' => 'prep', 'cookTime' => 'cook', 'totalTime' => 'total');
         foreach ($searches as $itemprop => $time_key) {
             $nodes = $xpath->query('.//*[@itemprop="' . $itemprop . '"]', $microdata);
             if ($nodes->length) {
                 if ($value = $nodes->item(0)->getAttribute('datetime')) {
                     $value = RecipeParser_Text::iso8601ToMinutes($value);
                 } else {
                     if ($value = $nodes->item(0)->getAttribute('content')) {
                         $value = RecipeParser_Text::iso8601ToMinutes($value);
                     } else {
                         $value = trim($nodes->item(0)->nodeValue);
                         $value = RecipeParser_Times::toMinutes($value);
                     }
                 }
                 if ($value) {
                     $recipe->time[$time_key] = $value;
                 }
             }
         }
         // Yield
         $line = "";
         $nodes = $xpath->query('.//*[@itemprop="yield"]', $microdata);
         if ($nodes->length) {
             $line = trim($nodes->item(0)->nodeValue);
         } else {
             $nodes = $xpath->query('.//*[@itemprop="servingSize"]', $microdata);
             if ($nodes->length) {
                 $line = trim($nodes->item(0)->nodeValue);
             }
         }
         if ($line) {
             $line = preg_replace('/\\s+/', ' ', $line);
             $recipe->yield = RecipeParser_Text::formatYield($line);
         }
         // Ingredients
         $nodes = null;
         // (data-vocabulary)
         if (!$nodes || !$nodes->length) {
             $nodes = $xpath->query('.//*[@itemprop="ingredient"]', $microdata);
         }
         if (!$nodes || !$nodes->length) {
             // non-standard
             $nodes = $xpath->query('.//*[@id="ingredients"]//li', $microdata);
         }
         if (!$nodes || !$nodes->length) {
             // non-standard
             $nodes = $xpath->query('.//*[@class="ingredients"]//li', $microdata);
         }
         foreach ($nodes as $node) {
             $value = $node->nodeValue;
             $value = RecipeParser_Text::formatAsOneLine($value);
             if (empty($value)) {
                 continue;
             }
             if (RecipeParser_Text::matchSectionName($value)) {
                 $value = RecipeParser_Text::formatSectionName($value);
                 $recipe->addIngredientsSection($value);
             } else {
                 $recipe->appendIngredient($value);
             }
         }
         // Instructions
         $found = false;
         // Look for markup that uses <li> tags for each instruction.
         if (!$found) {
             $nodes = $xpath->query('.//*[@itemprop="instructions"]//li', $microdata);
             if ($nodes->length) {
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             }
         }
         // Some sites will use an "instruction" class for each line.
         if (!$found) {
             $nodes = $xpath->query('.//*[@itemprop="instruction"]//*[contains(concat(" ", normalize-space(@class), " "), " instruction ")]', $microdata);
             if ($nodes->length) {
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             }
         }
         // Either multiple instrutions nodes, or one node with a blob of text.
         if (!$found) {
             $nodes = $xpath->query('.//*[@itemprop="instructions"]', $microdata);
             if ($nodes->length > 1) {
                 // Multiple nodes
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             } else {
                 if ($nodes->length == 1) {
                     // Blob
                     $str = $nodes->item(0)->nodeValue;
                     RecipeParser_Text::parseInstructionsFromBlob($str, $recipe);
                     $found = true;
                 }
             }
         }
         // Photo
         $photo_url = "";
         if (!$photo_url) {
             // try to find open graph url
             $nodes = $xpath->query('//meta[@property="og:image"]');
             if ($nodes->length) {
                 $photo_url = $nodes->item(0)->getAttribute('content');
             }
         }
         if (!$photo_url) {
             $nodes = $xpath->query('.//*[@itemprop="photo"]', $microdata);
             if ($nodes->length) {
                 if ($nodes->item(0)->hasAttribute('src')) {
                     $photo_url = $nodes->item(0)->getAttribute('src');
                 } else {
                     if ($nodes->item(0)->hasAttribute('content')) {
                         $photo_url = $nodes->item(0)->getAttribute('content');
                     }
                 }
             }
         }
         if (!$photo_url) {
             // for <img> as sub-node of class="photo"
             $nodes = $xpath->query('.//*[@itemprop="photo"]//img', $microdata);
             if ($nodes->length) {
                 $photo_url = $nodes->item(0)->getAttribute('src');
             }
         }
         if ($photo_url) {
             $recipe->photo_url = RecipeParser_Text::relativeToAbsolute($photo_url, $url);
         }
         // Credits
         $nodes = $xpath->query('.//*[@itemprop="author"]', $microdata);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
             $recipe->credits = RecipeParser_Text::formatCredits($line);
         }
     }
     return $recipe;
 }
Example #9
0
 public static function parse($html, $url)
 {
     $recipe = new RecipeParser_Recipe();
     libxml_use_internal_errors(true);
     $doc = new DOMDocument();
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     // Title
     $nodes = $xpath->query('//h3[@class = "title"]');
     if ($nodes->length) {
         $value = trim($nodes->item(0)->nodeValue);
         $recipe->title = $value;
     }
     // Cook times
     $nodes = $xpath->query('//div[@class = "recipe-metadata"]/ul/li');
     foreach ($nodes as $node) {
         $sub_nodes = $node->childNodes;
         $key = null;
         $value = null;
         foreach ($sub_nodes as $sub_node) {
             if ($sub_node->nodeName == 'h5') {
                 $key = trim($sub_node->nodeValue);
             }
             if ($sub_node->nodeName == 'p') {
                 $value = trim($sub_node->nodeValue);
             }
         }
         // Inspect keys/values we've found.
         if ($key == 'Total Time:') {
             $value = self::cleanupTime($value);
             $recipe->time['total'] = RecipeParser_Times::toMinutes($value);
         }
         if ($key == 'Prep Time:') {
             $value = self::cleanupTime($value);
             $recipe->time['prep'] = RecipeParser_Times::toMinutes($value);
         }
     }
     $node_list = $xpath->query('//dd[@class = "preptime"]');
     if ($node_list->length) {
         $value = $node_list->item(0)->nodeValue;
         $recipe->time['prep'] = RecipeParser_Times::toMinutes($value);
     }
     $node_list = $xpath->query('//dd[@class = "cooktime"]');
     if ($node_list->length) {
         $value = $node_list->item(0)->nodeValue;
         $recipe->time['cook'] = RecipeParser_Times::toMinutes($value);
     }
     $node_list = $xpath->query('//dd[@class = "duration totaltime special"]');
     if ($node_list->length) {
         $value = $node_list->item(0)->nodeValue;
         $recipe->time['total'] = RecipeParser_Times::toMinutes($value);
     }
     // Ingredients, Yield, Description, Notes, etc.
     $nodes = $xpath->query('//div[@class = "recipe-body"]/*');
     $section_title = null;
     foreach ($nodes as $node) {
         // Section titles
         if ($node->nodeName == 'h4') {
             $value = $node->nodeValue;
             $value = trim(strtolower($value));
             $section_title = $value;
             continue;
         }
         $in_section = false;
         if ($node->nodeName == 'div') {
             // Ensure that we're in a <div class="section"> node.
             foreach ($node->attributes as $attr_name => $attr_node) {
                 if ($attr_name == 'class' && $attr_node->value == 'section') {
                     $in_section = true;
                 }
             }
             if (!$in_section) {
                 continue;
             }
             // Description should be first text, before any section titles.
             if (!$section_title) {
                 $value = $node->nodeValue;
                 $value = preg_replace("/^(Drink\\:|Top Chef).*\$/m", '', $value);
                 $value = str_replace("\n\n", "\n", $value);
                 $value = trim($value);
                 $recipe->description = $value;
                 // Yield
             } else {
                 if ($section_title == 'yield') {
                     $value = trim($node->nodeValue);
                     $recipe->yield = $value;
                     // Notes
                 } else {
                     if ($section_title == 'notes') {
                         $value = trim($node->nodeValue);
                         $value = str_replace("\n\n", "\n", $value);
                         $recipe->notes = $value;
                         // Ingredients
                     } else {
                         if ($section_title == 'ingredients') {
                             $sub_nodes = $node->childNodes;
                             foreach ($sub_nodes as $sub_node) {
                                 if ($sub_node->nodeName == 'h5') {
                                     $value = RecipeParser_Text::formatSectionName($sub_node->nodeValue);
                                     $recipe->addIngredientsSection($value);
                                 } else {
                                     if ($sub_node->nodeName == 'ul') {
                                         $li_nodes = $sub_node->childNodes;
                                         foreach ($li_nodes as $li_node) {
                                             $value = trim($li_node->nodeValue);
                                             $recipe->appendIngredient($value);
                                         }
                                     }
                                 }
                             }
                             // Instructions
                         } else {
                             if ($section_title == 'directions') {
                                 $sub_nodes = $node->childNodes;
                                 foreach ($sub_nodes as $sub_node) {
                                     $value = trim($sub_node->nodeValue);
                                     // Section titles appear in all-caps.
                                     if ($value && ($value == strtoupper($value) || preg_match('/:$/', $value))) {
                                         $value = RecipeParser_Text::formatSectionName($value);
                                         $recipe->addInstructionsSection($value);
                                     } else {
                                         $value = RecipeParser_Text::stripLeadingNumbers($value);
                                         $recipe->appendInstruction($value);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // Source / Chef
     $nodes = $xpath->query('//div[@class = "recipe-sidebar"]/div/*');
     $section_title = null;
     $chef_name = null;
     $show_name = 'Bravo TV';
     foreach ($nodes as $node) {
         if ($node->nodeName == 'h4') {
             $value = trim($node->nodeValue);
             $section_title = strtolower($value);
             continue;
         }
         if ($node->nodeName == 'small') {
             if ($section_title == 'chef' || $section_title == 'author') {
                 $value = trim($node->nodeValue);
                 $chef_name = $value;
                 break;
             }
         }
     }
     $nodes = $xpath->query('//div[@class = "section"]/p[1]');
     if ($nodes->length) {
         $value = $nodes->item(0)->nodeValue;
         if (strpos($value, 'Top Chef Masters') !== false) {
             $show_name = 'Top Chef Masters';
         } else {
             if (strpos($value, 'Top Chef') !== false) {
                 $show_name = 'Top Chef';
             }
         }
     }
     $recipe->credits = $chef_name . ', ' . $show_name;
     $nodes = $xpath->query('//div[@class = "recipe-header clearfix"]//img');
     if ($nodes->length) {
         $photo_url = $nodes->item(0)->getAttribute('src');
         $photo_url = str_replace('/medium/', '/original/', $photo_url);
         $recipe->photo_url = RecipeParser_Text::formatPhotoUrl($photo_url, $url);
     }
     return $recipe;
 }
Example #10
0
 public static function parse($html, $url)
 {
     if (strpos($url, "www.nytimes.com/recipes/") !== false) {
         //
         // "RECIPES" SECTION
         //
         $recipe = new RecipeParser_Recipe();
         libxml_use_internal_errors(true);
         $doc = new DOMDocument();
         $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
         $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
         $xpath = new DOMXPath($doc);
         // Title
         $nodes = $xpath->query('//h1[@class="recipe-title recipeName"]');
         if ($nodes->length) {
             $value = $nodes->item(0)->nodeValue;
             $value = RecipeParser_Text::formatTitle($value);
             $recipe->title = $value;
         }
         // Yield
         $nodes = $xpath->query('//*[@itemprop="recipeYield"]');
         if ($nodes->length) {
             $value = $nodes->item(0)->nodeValue;
             $value = RecipeParser_Text::formatYield($value);
             $recipe->yield = $value;
         }
         // Ingredients
         $nodes = $xpath->query('//div[@class="ingredientsGroup"]/*');
         foreach ($nodes as $node) {
             if ($node->nodeName == "h3") {
                 $value = trim($node->nodeValue);
                 if (!preg_match('/^Ingredients:?$/i', $value)) {
                     $value = RecipeParser_Text::formatSectionName($value);
                     $recipe->addIngredientsSection($value);
                 }
             } else {
                 foreach ($node->childNodes as $child) {
                     $value = trim($child->nodeValue);
                     $recipe->appendIngredient($value);
                 }
             }
         }
         // Instructions
         $nodes = $xpath->query('//*[@itemprop="recipeInstructions"]/dd');
         foreach ($nodes as $node) {
             $value = $node->nodeValue;
             $value = RecipeParser_Text::formatAsOneLine($value);
             $recipe->appendInstruction($value);
         }
         // Notes
         if (!$recipe->notes) {
             $nodes = $xpath->query('//div[@class="yieldNotesGroup"]//*[@class="note"]');
             if ($nodes->length) {
                 $value = trim($nodes->item(0)->nodeValue);
                 $value = preg_replace("/^Notes?:?\\s*/i", '', $value);
                 $recipe->notes = trim($value);
             }
         }
     } else {
         //
         // DINING SECTION RECIPES
         //
         $recipe = new RecipeParser_Recipe();
         libxml_use_internal_errors(true);
         $doc = new DOMDocument();
         $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
         $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
         $xpath = new DOMXPath($doc);
         // Title
         $nodes = $xpath->query('//div[@id = "article"]//h1');
         if ($nodes->length) {
             $value = trim($nodes->item(0)->nodeValue);
             $recipe->title = $value;
         }
         // Time and Yield
         $nodes = $xpath->query('//div[@id = "article"]//p');
         foreach ($nodes as $node) {
             $text = trim($node->nodeValue);
             if (preg_match('/^Yield:? (.+)/', $text, $m)) {
                 $recipe->yield = RecipeParser_Text::formatYield($m[1]);
             } else {
                 if (preg_match('/^Time:? (.+)/', $text, $m)) {
                     $str = trim($m[1]);
                     $str = preg_replace('/About (.+)/', '$1', $str);
                     $str = preg_replace('/(.+) plus.*/', '$1', $str);
                     $recipe->time['total'] = RecipeParser_Times::toMinutes($str);
                 }
             }
         }
         // Ingredients
         $nodes = $xpath->query('//div[@class="recipeIngredientsList"]/p');
         foreach ($nodes as $node) {
             $line = trim($node->nodeValue);
             // Section names
             if ($line && $line == strtoupper($line)) {
                 $line = RecipeParser_Text::formatSectionName($line);
                 $recipe->addIngredientsSection($line);
                 continue;
             }
             $recipe->appendIngredient($line);
         }
         // Instructions and notes
         $nodes = $xpath->query('//div[@class="articleBody"]//p');
         if (!$nodes->length) {
             $nodes = $xpath->query('//div[@id="articleBody"]//p');
         }
         $notes = '';
         $in_notes_section = false;
         foreach ($nodes as $node) {
             $line = trim($node->nodeValue);
             // Skip some of the useless lines
             if (preg_match('/^(Adapted from|Time|Yield)/i', $line)) {
                 continue;
             }
             // Instructions start with line numbers
             if (!$in_notes_section && preg_match('/^\\d+\\./', $line)) {
                 $line = RecipeParser_Text::stripLeadingNumbers($line);
                 $recipe->appendInstruction($line);
                 continue;
             }
             // Look for lines that start the notes section.
             $note = '';
             if (preg_match('/^Notes?:?(.*)/i', $line, $m)) {
                 $in_notes_section = true;
                 $note = trim($m[1]);
             } else {
                 if ($in_notes_section) {
                     $note = $line;
                 }
             }
             if ($note) {
                 $notes .= $note . "\n\n";
             }
         }
         if ($notes) {
             $notes = str_replace("  ", " ", $notes);
             // Some unnecessary spaces
             $notes = trim($notes);
             $recipe->notes = $notes;
         }
         // Photo
         $nodes = $xpath->query('//div[@class="image"]//img');
         if ($nodes->length) {
             $photo_url = $nodes->item(0)->getAttribute('src');
             $photo_url = str_replace('-articleInline.jpg', '-popup.jpg', $photo_url);
             $recipe->photo_url = RecipeParser_Text::formatPhotoUrl($photo_url, $url);
         }
     }
     return $recipe;
 }
Example #11
0
 public static function parse($html, $url)
 {
     $recipe = RecipeParser_Parser_MicrodataSchema::parse($html, $url);
     libxml_use_internal_errors(true);
     $doc = new DOMDocument();
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     // Times
     $nodes = $xpath->query('//*[@class="recipePartAttributes recipePartPrimaryAttributes"]//li');
     if ($nodes->length) {
         foreach ($nodes as $node) {
             if (trim($node->childNodes->item(1)->nodeValue) == "Prep Time") {
                 $line = trim($node->childNodes->item(3)->nodeValue);
                 $recipe->time['prep'] = RecipeParser_Times::toMinutes($line);
                 continue;
             }
             if (trim($node->childNodes->item(1)->nodeValue) == "Total Time") {
                 $line = trim($node->childNodes->item(3)->nodeValue);
                 $recipe->time['total'] = RecipeParser_Times::toMinutes($line);
                 continue;
             }
         }
     }
     // Yield
     $nodes = $xpath->query('//*[@class="recipePartAttributes recipePartSecondaryAttributes"]//li');
     if ($nodes->length) {
         foreach ($nodes as $node) {
             if (trim($node->childNodes->item(1)->nodeValue) == "Servings") {
                 $line = trim($node->childNodes->item(3)->nodeValue);
                 $recipe->yield = RecipeParser_Text::formatYield($line);
             }
         }
     }
     // Ingredients
     $recipe->resetIngredients();
     $groups = $xpath->query('//*[@class="recipePartIngredientGroup"]');
     foreach ($groups as $group) {
         $nodes = $xpath->query('.//h2', $group);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
             $line = RecipeParser_Text::formatSectionName($line);
             $recipe->addIngredientsSection($line);
         }
         $nodes = $xpath->query('.//*[@itemprop="ingredients"]', $group);
         foreach ($nodes as $node) {
             $line = $node->nodeValue;
             $line = RecipeParser_Text::formatAsOneLine($line);
             $recipe->appendIngredient($line);
         }
     }
     // Notes / footnotes
     $notes = array();
     $nodes = $xpath->query('//div[@class="recipePartTipsInfo"]');
     foreach ($nodes as $node) {
         $line = trim($node->nodeValue);
         $notes[] = $line;
     }
     $recipe->notes = implode("\n\n", $notes);
     $recipe->notes = RecipeParser_Text::formatAsParagraphs($recipe->notes);
     // Fix description
     $recipe->description = trim(preg_replace("/Servings \\# \\d+/", "", $recipe->description));
     return $recipe;
 }
Example #12
0
 public static function parse($html, $url)
 {
     $recipe = new RecipeParser_Recipe();
     libxml_use_internal_errors(true);
     $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
     $doc = new DOMDocument();
     $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new DOMXPath($doc);
     $hrecipe = null;
     if (!$hrecipe) {
         $nodes = $xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " hrecipe ")]');
         if ($nodes->length) {
             $hrecipe = $nodes->item(0);
         }
     }
     if (!$hrecipe) {
         $nodes = $xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " hRecipe ")]');
         if ($nodes->length) {
             $hrecipe = $nodes->item(0);
         }
     }
     if ($hrecipe) {
         // Title
         $nodes = $xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " fn ")]', $hrecipe);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
             $recipe->title = RecipeParser_Text::formatTitle($line);
         }
         // Summary
         $nodes = $xpath->query('.//*[@class="summary"]', $hrecipe);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
             $recipe->description = RecipeParser_Text::formatAsParagraphs($line);
         }
         // Credits
         $nodes = $xpath->query('.//*[@class="author"]', $hrecipe);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
             $recipe->credits = RecipeParser_Text::formatCredits($line);
         }
         // Photo
         $photo_url = "";
         $nodes = $xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " photo ")]', $hrecipe);
         if ($nodes->length) {
             $photo_url = $nodes->item(0)->getAttribute('src');
         }
         if (!$photo_url) {
             // for <img> as sub-node of class="photo"
             $nodes = $xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " photo ")]//img', $hrecipe);
             if ($nodes->length) {
                 $photo_url = $nodes->item(0)->getAttribute('src');
             }
         }
         if ($photo_url) {
             $recipe->photo_url = RecipeParser_Text::relativeToAbsolute($photo_url, $url);
         }
         // Yield
         $nodes = $xpath->query('.//*[@class="yield"]', $hrecipe);
         if ($nodes->length) {
             $line = $nodes->item(0)->nodeValue;
             $recipe->yield = RecipeParser_Text::formatYield($line);
         }
         // Prep Times
         $nodes = $xpath->query('.//*[@class="prepTime"]//*[@class="value-title"]', $hrecipe);
         if ($nodes->length) {
             $value = $nodes->item(0)->getAttribute('title');
             $recipe->time['prep'] = RecipeParser_Text::iso8601ToMinutes($value);
         } else {
             $nodes = $xpath->query('.//*[@class="preptime"]', $hrecipe);
             if ($nodes->length) {
                 $value = $nodes->item(0)->nodeValue;
                 $recipe->time['prep'] = RecipeParser_Times::toMinutes($value);
             }
         }
         // Cook Times
         $nodes = $xpath->query('.//*[@class="cookTime"]//*[@class="value-title"]', $hrecipe);
         if ($nodes->length) {
             $value = $nodes->item(0)->getAttribute('title');
             $recipe->time['cook'] = RecipeParser_Text::iso8601ToMinutes($value);
         } else {
             $nodes = $xpath->query('.//*[@class="cooktime"]', $hrecipe);
             if ($nodes->length) {
                 $value = $nodes->item(0)->nodeValue;
                 $recipe->time['cook'] = RecipeParser_Times::toMinutes($value);
             }
         }
         // Total Time / Duration
         $nodes = $xpath->query('.//*[@class="totalTime"]//*[@class="value-title"]', $hrecipe);
         if ($nodes->length) {
             $value = $nodes->item(0)->getAttribute('title');
             $recipe->time['total'] = RecipeParser_Text::iso8601ToMinutes($value);
         } else {
             $nodes = $xpath->query('.//*[@class="duration"]//*[@class="value-title"]', $hrecipe);
             if ($nodes->length) {
                 $value = $nodes->item(0)->getAttribute('title');
                 $recipe->time['total'] = RecipeParser_Text::iso8601ToMinutes($value);
             } else {
                 $nodes = $xpath->query('.//*[@class="duration"]', $hrecipe);
                 if ($nodes->length) {
                     $value = $nodes->item(0)->nodeValue;
                     $recipe->time['total'] = RecipeParser_Times::toMinutes($value);
                 }
             }
         }
         // Ingredients
         $nodes = $xpath->query('//*[contains(concat(" ", normalize-space(@class), " "), " ingredient ")]');
         foreach ($nodes as $node) {
             $line = $node->nodeValue;
             $line = trim($line);
             $line = RecipeParser_Text::formatAsOneLine($line);
             // Skip lines that contain no word-like characters (sometimes used as section dividers).
             if (!preg_match("/\\w/", $line)) {
                 continue;
             }
             // Section name delineated with dashes. E.g. "---Cake---"
             if (preg_match('/^\\-+([^\\-]{1}.*[^\\-]{1})\\-+$/', $line, $m)) {
                 $line = RecipeParser_Text::formatSectionName($m[1]);
                 $recipe->addIngredientsSection($line);
                 continue;
             }
             // Section name with colon.
             if (preg_match('/^(.+)\\:$/', $line, $m)) {
                 $line = RecipeParser_Text::formatSectionName($m[1]);
                 $recipe->addIngredientsSection($line);
                 continue;
             }
             $recipe->appendIngredient($line);
         }
         // Instructions
         $found = false;
         // Look for usage of <li> to denote each step of the instructions.
         if (!$found) {
             $nodes = $xpath->query('//*[contains(concat(" ", normalize-space(@class), " "), " instructions ")]//li');
             if ($nodes->length) {
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             }
         }
         // Look for "instruction class for each step of the instructions.
         if (!$found) {
             $query = '//*[contains(concat(" ", normalize-space(@class), " "), " instructions ")]' . '//*[contains(concat(" ", normalize-space(@class), " "), " instruction ")]';
             $nodes = $xpath->query($query);
             if ($nodes->length) {
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             }
         }
         // Default. Multiple instructions nodes, or one with a blob of text.
         if (!$found) {
             $nodes = $xpath->query('//*[contains(concat(" ", normalize-space(@class), " "), " instructions ")]');
             if ($nodes->length > 1) {
                 // Multiple nodes
                 RecipeParser_Text::parseInstructionsFromNodes($nodes, $recipe);
                 $found = true;
             } else {
                 if ($nodes->length == 1) {
                     // Blob
                     $str = $nodes->item(0)->nodeValue;
                     RecipeParser_Text::parseInstructionsFromBlob($str, $recipe);
                     $found = true;
                 }
             }
         }
     }
     return $recipe;
 }
 public function test_strip_label_from_time()
 {
     $this->assertEquals("50", RecipeParser_Times::toMinutes("total: 50 min"), "Failed to strip 'total:' from time.");
     $this->assertEquals("60", RecipeParser_Times::toMinutes("Prep: 60 min"), "Failed to strip 'prep:' from time.");
     $this->assertEquals("70", RecipeParser_Times::toMinutes("cook: 1 hr, 10 min"), "Failed to strip 'cook:' from time.");
 }