Class: ScriptRipper::Parser
- Inherits:
-
Object
- Object
- ScriptRipper::Parser
- Includes:
- Util
- Defined in:
- lib/script_ripper/parser.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#group_by ⇒ Object
readonly
Returns the value of attribute group_by.
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(contents, group_by = nil) ⇒ Parser
constructor
A new instance of Parser.
Methods included from Util
Constructor Details
#initialize(contents, group_by = nil) ⇒ Parser
Returns a new instance of Parser.
14 15 16 17 |
# File 'lib/script_ripper/parser.rb', line 14 def initialize(contents, group_by = nil) @contents = contents @group_by = group_by end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
11 12 13 |
# File 'lib/script_ripper/parser.rb', line 11 def contents @contents end |
#group_by ⇒ Object (readonly)
Returns the value of attribute group_by.
12 13 14 |
# File 'lib/script_ripper/parser.rb', line 12 def group_by @group_by end |
Class Method Details
.call(contents, group_by = nil) ⇒ Object
60 61 62 |
# File 'lib/script_ripper/parser.rb', line 60 def call(contents, group_by = nil) new(contents, group_by).call end |
Instance Method Details
#call ⇒ Object
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 |
# File 'lib/script_ripper/parser.rb', line 19 def call code_blocks = document.css("ol").map do |ordered_list| ordered_list.css("li").map do |list_item| list_item.css("code").map do |code_element| parent_p_element = find_p_parent(code_element) if parent_p_element # This means the code block is inside a <p/> element CodeBlock.new( description: parent_p_element.text, code: code_element.text, nokogiri_list_item: list_item ) else # This means the code block is preceded by zero or more <p/> elements CodeBlock.new( description: list_item.css("p").map(&:text).join("\n"), code: code_element.text, nokogiri_list_item: list_item ) end end end end.flatten.reject { |code_block| blank?(code_block.code) } return code_blocks if blank?(group_by) {}.tap do |result| code_blocks.each do |code_block| grouping = find_previous_grouping_element(find_list_parent(code_block.nokogiri_list_item)) next if grouping.nil? group_name = grouping.text result[group_name] ||= [] result[group_name] << code_block end end end |