Module: Vivlio::Starter::CLI::PostProcessCommands::SectionWrapper

Defined in:
lib/vivlio/starter/cli/post_process/section_wrapper.rb

Overview

Module: SectionWrapper


【役割】

  • theme.style: image のとき、section.level2 内の h2 を<article class=“section-topic”> でラップ

【処理内容】

  • <section class=“level2”> の直下にある <h2> を検出

  • 直後にある .section-lead も一緒にラップ(存在すれば)

  • CSS グリッドの対象構造を整える

Class Method Summary collapse

Class Method Details

.find_section_lead_for(h2) ⇒ Nokogiri::XML::Element?

h2 に続く .section-lead ノードを探索して返す

Parameters:

  • h2 (Nokogiri::XML::Element)

    h2要素

Returns:

  • (Nokogiri::XML::Element, nil)

    section-lead要素またはnil



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vivlio/starter/cli/post_process/section_wrapper.rb', line 55

def find_section_lead_for(h2)
  node = h2.next_sibling
  while node
    return node if node.element? && (node['class']&.split || []).include?('section-lead')

    # ゼロ幅文字のみの段落はスキップ
    if node.element? && node.name == 'p' && HtmlParser.zero_width_text?(node.text)
      node = node.next_sibling
      next
    end

    node = node.element? ? nil : node.next_sibling
  end
  nil
end

.wrap_h2_with_article_if_image_style!(html) ⇒ String

theme.style が image の場合に、章セクションの見出しをラップ

Parameters:

  • html (String)

    HTML文字列

Returns:

  • (String)

    変換後のHTML文字列



28
29
30
31
32
33
34
# File 'lib/vivlio/starter/cli/post_process/section_wrapper.rb', line 28

def wrap_h2_with_article_if_image_style!(html)
  return html unless Common::CONFIG.dig('theme', 'style') == 'image'

  doc = HtmlParser.parse_html_document(html)
  transformed = wrap_sections_with_article!(doc)
  transformed ? HtmlParser.render_html_document(doc) : html
end

.wrap_sections_with_article!(doc) ⇒ Boolean

section.level2 直下の h2 を article.section-topic でラップする

Parameters:

  • doc (Nokogiri::HTML::Document)

    Nokogiriドキュメント

Returns:

  • (Boolean)

    変更があったかどうか



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vivlio/starter/cli/post_process/section_wrapper.rb', line 39

def wrap_sections_with_article!(doc)
  changed = false
  doc.css('section.level2').each do |section|
    h2 = section.at_css('> h2')
    next unless h2 && h2.ancestors('article.section-topic').empty?

    lead = find_section_lead_for(h2)
    wrap_with_article(section, h2, lead)
    changed = true
  end
  changed
end

.wrap_with_article(section, h2, lead) ⇒ Object

h2 と(存在すれば)section-lead を article.section-topic で包む

Parameters:

  • section (Nokogiri::XML::Element)

    section要素

  • h2 (Nokogiri::XML::Element)

    h2要素

  • lead (Nokogiri::XML::Element, nil)

    section-lead要素



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vivlio/starter/cli/post_process/section_wrapper.rb', line 75

def wrap_with_article(section, h2, lead)
  article = Nokogiri::XML::Node.new('article', section.document)
  article['class'] = 'section-topic'
  h2.add_previous_sibling("\n")
  h2.add_previous_sibling(article)
  article.add_child(h2)
  return unless lead

  article.add_child("\n")
  article.add_child(lead)
end