Class: Kitchen::Directions::BakeReferences::V1

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/directions/bake_references/v1.rb

Instance Method Summary collapse

Instance Method Details

#bake(book:, metadata_source:, numbered_title:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
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
58
59
60
61
62
63
64
65
# File 'lib/kitchen/directions/bake_references/v1.rb', line 7

def bake(book:, metadata_source:, numbered_title:)
  book.chapters.each do |chapter|
    chapter.search('[data-type="cite"]').each do |link|
      link.prepend(child:
        <<~HTML.chomp
          <sup class="os-citation-number">#{link.count_in(:chapter)}</sup>
        HTML
      )

      next if link.nil?

      link_sibling = link.previous unless link.preceded_by_text?
      next if link_sibling.nil?

      next unless link_sibling&.raw&.attr('data-type') == 'cite'

      link.prepend(sibling:
        <<~HTML.strip.chomp
          <span class="os-reference-link-separator"><sup>, </sup></span>
        HTML
      )
    end

    chapter.references.each do |reference|
      reference.prepend(child:
        <<~HTML.chomp
          <span class="os-reference-number">#{reference.count_in(:chapter)}. </span>
        HTML
      )
    end

    chapter_references = chapter.pages.references.cut

    chapter_title = if numbered_title
                      chapter.title.search('.os-number, .os-divider, .os-text')
                    else
                      chapter.title.search('.os-text')
                    end

    chapter.append(child:
      <<~HTML
        <div class="os-chapter-area">
          <h2 data-type="document-title">#{chapter_title}</h2>
          #{chapter_references.paste}
        </div>
      HTML
    )
  end

  chapter_area_references = book.chapters.search('.os-chapter-area').cut

  Kitchen::Directions::CompositePageContainer.v1(
    container_key: 'reference',
    uuid_key: '.reference',
    metadata_source: ,
    content: chapter_area_references.paste,
    append_to: book.body
  )
end