Module: VivlioStarter::CLI::PostProcessCommands::SectionWrapper
- Defined in:
- lib/vivlio_starter/cli/post_process/section_wrapper.rb
Overview
================================================================
Module: SectionWrapper
【役割】
-
theme.style: image のとき、section.level2 内の h2 を
でラップ
【処理内容】
-
の直下にある を検出
- 直後にある .section-lead も一緒にラップ(存在すれば)
- CSS グリッドの対象構造を整える
================================================================
Class Method Summary collapse
-
.find_section_lead_for(h2) ⇒ Nokogiri::XML::Element?
h2 に続く .section-lead ノードを探索して返す.
-
.wrap_h2_with_article_if_image_style!(html) ⇒ String
theme.style が image の場合に、章セクションの見出しをラップ.
-
.wrap_sections_with_article!(doc) ⇒ Boolean
section.level2 直下の h2 を article.section-topic でラップする.
-
.wrap_with_article(section, h2, lead) ⇒ Object
h2 と(存在すれば)section-lead を article.section-topic で包む.
Class Method Details
.find_section_lead_for(h2) ⇒ Nokogiri::XML::Element?
h2 に続く .section-lead ノードを探索して返す
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vivlio_starter/cli/post_process/section_wrapper.rb', line 54 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 の場合に、章セクションの見出しをラップ
27 28 29 30 31 32 33 |
# File 'lib/vivlio_starter/cli/post_process/section_wrapper.rb', line 27 def wrap_h2_with_article_if_image_style!(html) return html unless Common::CONFIG.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 でラップする
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/vivlio_starter/cli/post_process/section_wrapper.rb', line 38 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 で包む
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/vivlio_starter/cli/post_process/section_wrapper.rb', line 74 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 |