module FoodIngredientParser::Strict::ToHtml
Public Instance Methods
to_html()
click to toggle source
Markup original ingredients list text in HTML.
The input text is returned as HTML, augmented with CSS classes on +span+s for name
, amount
, mark
and note
.
@return [String] HTML representation of ingredient list.
# File lib/food_ingredient_parser/strict/to_html.rb, line 16 def to_html node_to_html(self) end
Private Instance Methods
node_to_html(node, cls=nil, depth=0)
click to toggle source
# File lib/food_ingredient_parser/strict/to_html.rb, line 22 def node_to_html(node, cls=nil, depth=0) el_cls = {} # map of node instances to class names for contained elements terminal = node.terminal? # whether to look at children elements or not if node.is_a?(FoodIngredientParser::Strict::Grammar::AmountNode) cls ||= "amount" elsif node.is_a?(FoodIngredientParser::Strict::Grammar::NoteNode) cls ||= "note" terminal = true # NoteNodes may contain other NoteNodes, we want it flat. elsif node.is_a?(FoodIngredientParser::Strict::Grammar::IngredientNode) el_cls[node.name] = "name" if node.respond_to?(:name) el_cls[node.mark] = "mark" if node.respond_to?(:mark) if node.respond_to?(:contains) el_cls[node.contains] = "contains depth#{depth}" depth += 1 end elsif node.is_a?(FoodIngredientParser::Strict::Grammar::RootNode) if node.respond_to?(:contains) el_cls[node.contains] = "depth#{depth}" depth += 1 end end val = if terminal CGI.escapeHTML(node.text_value) else node.elements.map {|el| node_to_html(el, el_cls[el], depth) }.join("") end cls ? "<span class='#{cls}'>#{val}</span>" : val end