Class: Kitchen::Directions::BakeChapterIntroductions::V2

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/directions/bake_chapter_introductions/v2.rb

Instance Method Summary collapse

Instance Method Details

#bake(book:, strategy_options:) ⇒ Object



5
6
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
# File 'lib/kitchen/directions/bake_chapter_introductions/v2.rb', line 5

def bake(book:, strategy_options:)
  book.chapters.each do |chapter|
    introduction_page = chapter.introduction_page
    title = bake_title(introduction_page: introduction_page)

    chapter_intro_html =
      Kitchen::Directions::BakeChapterIntroductions.bake_chapter_objectives(
        chapter: chapter,
        strategy: strategy_options[:strategy]
      )

    if strategy_options[:bake_chapter_outline]
      chapter_intro_html =
        Kitchen::Directions::BakeChapterIntroductions.bake_chapter_outline(
          chapter_objectives_html: chapter_intro_html
        )
    end

    case strategy_options[:introduction_order]
    when :v1
      v1_introduction_order(
        introduction_page: introduction_page,
        chapter_intro_html: chapter_intro_html,
        title: title
      )
    when :v2
      v2_introduction_order(
        introduction_page: introduction_page,
        chapter_intro_html: chapter_intro_html,
        title: title
      )
    end
  end

  Kitchen::Directions::BakeChapterIntroductions.v1_update_selectors(book)
end

#bake_title(introduction_page:) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/kitchen/directions/bake_chapter_introductions/v2.rb', line 81

def bake_title(introduction_page:)
  introduction_page.search(
    'div[data-type="description"], div[data-type="abstract"]'
  ).each(&:trash)

  title = introduction_page.title.cut
  title.name = 'h2'
  Kitchen::Directions::MoveTitleTextIntoSpan.v1(title: title)
end

#v1_introduction_order(introduction_page:, chapter_intro_html:, title:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kitchen/directions/bake_chapter_introductions/v2.rb', line 42

def v1_introduction_order(introduction_page:, chapter_intro_html:, title:)
  intro_content = introduction_page.search(
    "> :not([data-type='metadata']):not(.splash):not(.has-splash)"
  ).cut

  introduction_page.append(child:
    <<~HTML
      <div class="intro-body">
        #{chapter_intro_html}
        <div class="intro-text">
          #{title.paste}
          #{intro_content.paste}
        </div>
      </div>
    HTML
  )
end

#v2_introduction_order(introduction_page:, chapter_intro_html:, title:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kitchen/directions/bake_chapter_introductions/v2.rb', line 60

def v2_introduction_order(introduction_page:, chapter_intro_html:, title:)
  if chapter_intro_html.empty?
    chapter_intro_html = introduction_page.notes('$.chapter-objectives').first&.cut&.paste
  end
  extra_content = introduction_page.search(
    '> :not([data-type="metadata"]):not(.splash):not(.has-splash)'
  ).cut

  introduction_page.append(child:
    <<~HTML
      <div class="intro-body">
        #{chapter_intro_html}
        <div class="intro-text">
          #{title.paste}
          #{extra_content.paste}
        </div>
      </div>
    HTML
  )
end