Class: OpenStax::Content::FragmentSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/content/fragment_splitter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processing_instructions, reference_view_url) ⇒ FragmentSplitter

Returns a new instance of FragmentSplitter.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/openstax/content/fragment_splitter.rb', line 8

def initialize(processing_instructions, reference_view_url)
  @processing_instructions = processing_instructions.map do |processing_instruction|
    OpenStruct.new(processing_instruction.to_h).tap do |pi_struct|
      pi_struct.fragments = [pi_struct.fragments].flatten.map do |fragment|
        fragment.to_s.split('_').map(&:capitalize).join
      end unless pi_struct.fragments.nil?
      pi_struct.only = [pi_struct.only].flatten.map(&:to_s) unless pi_struct.only.nil?
      pi_struct.except = [pi_struct.except].flatten.map(&:to_s) unless pi_struct.except.nil?
    end
  end

  @reference_view_url = reference_view_url
end

Instance Attribute Details

#processing_instructionsObject (readonly)

Returns the value of attribute processing_instructions.



6
7
8
# File 'lib/openstax/content/fragment_splitter.rb', line 6

def processing_instructions
  @processing_instructions
end

#reference_view_urlObject (readonly)

Returns the value of attribute reference_view_url.



6
7
8
# File 'lib/openstax/content/fragment_splitter.rb', line 6

def reference_view_url
  @reference_view_url
end

Instance Method Details

#split_into_fragments(root, type = nil) ⇒ Object

Splits the given root node into fragments according to the processing instructions



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
58
59
60
61
62
63
64
65
# File 'lib/openstax/content/fragment_splitter.rb', line 23

def split_into_fragments(root, type = nil)
  result = [root.dup]
  type_string = type.to_s

  pis = processing_instructions.reject do |processing_instruction|
    processing_instruction.css.nil? ||
    processing_instruction.css.empty? ||
    processing_instruction.fragments.nil? ||
    processing_instruction.fragments == ['Node'] ||
    (!processing_instruction.only.nil? && !processing_instruction.only.include?(type_string)) ||
    (!processing_instruction.except.nil? && processing_instruction.except.include?(type_string))
  end

  @media_nodes = []

  pis.each { |processing_instruction| result = process_array(result, processing_instruction) }

  # Flatten, remove empty nodes and transform remaining nodes into reading fragments
  result.map do |obj|
    next obj unless obj.is_a?(Nokogiri::XML::Node)

    fragment = OpenStax::Content::Fragment::Reading.new(
      node: obj, reference_view_url: reference_view_url
    )
    fragment unless fragment.blank?
  end.compact.tap do |result|
    @media_nodes.each do |node|
      # Media processing instructions
      node.css('[id], [name]', custom_css).each do |linkable|
        css_array = []
        css_array << "[href$=\"##{linkable[:id]}\"]" unless linkable[:id].nil?
        css_array << "[href$=\"##{linkable[:name]}\"]" unless linkable[:name].nil?
        css = css_array.join(', ')

        result.select(&:html?)
              .select { |fragment| fragment.has_css? css, custom_css }
              .each   { |fragment| fragment.append node.dup }
      end
    end

    result.select(&:html?).each(&:transform_links!)
  end
end