Module: Kitchen::Directions::BakeToc
- Defined in:
- lib/kitchen/directions/bake_toc.rb
Class Method Summary collapse
- .li_for_chapter(chapter, options) ⇒ Object
- .li_for_composite_chapter(composite_chapter) ⇒ Object
- .li_for_page(page) ⇒ Object
- .li_for_unit(unit, options) ⇒ Object
- .v1(book:, options: { cases: false }) ⇒ Object
Class Method Details
.li_for_chapter(chapter, options) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kitchen/directions/bake_toc.rb', line 67 def self.li_for_chapter(chapter, ) chapter_children = chapter.element_children.map do |child| if child.instance_of?(PageElement) || child.instance_of?(CompositePageElement) li_for_page(child) elsif child.instance_of?(CompositeChapterElement) li_for_composite_chapter(child) end end.join("\n") <<~HTML <li class="os-toc-chapter" cnx-archive-shortid="" cnx-archive-uri="" data-toc-type="chapter"> <a href="##{chapter.title.id}"> <span class="os-number"><span class="os-part-text">#{I18n.t("chapter#{'.nominative' \ if [:cases]}")} </span>#{chapter.count_in(:book)}</span> <span class="os-divider"> </span> <span class="os-text" data-type="" itemprop="">#{chapter.title.first!('.os-text').text}</span> </a> <ol class="os-chapter"> #{chapter_children} </ol> </li> HTML end |
.li_for_composite_chapter(composite_chapter) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/kitchen/directions/bake_toc.rb', line 52 def self.li_for_composite_chapter(composite_chapter) pages = composite_chapter.element_children.only(CompositePageElement) <<~HTML <li class="os-toc-composite-chapter" cnx-archive-shortid="" cnx-archive-uri="" data-toc-type="sub-book-tree"> <a href="##{composite_chapter.title.id}"> #{composite_chapter.title.children} </a> <ol class="os-chapter"> #{pages.map { |page| li_for_page(page) }.join("\n")} </ol> </li> HTML end |
.li_for_page(page) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/kitchen/directions/bake_toc.rb', line 91 def self.li_for_page(page) # rubocop:disable Style/WordArray # li_page_type is for styling, toc_target_type is for rex toc markup (li_page_type, toc_target_type) = case page when PageElement if page.has_ancestor?(:chapter) if page.is_introduction? ['os-toc-chapter-page', 'intro'] else ['os-toc-chapter-page', 'numbered-section'] end elsif page.is_appendix? ['os-toc-appendix', 'appendix'] elsif page.is_preface? ['os-toc-preface', 'preface'] elsif page.is_handbook? ['os-toc-handbook', 'appendix'] elsif page.has_ancestor?(:unit) && ! page.has_ancestor?(:chapter) && ! page.has_ancestor?(:composite_chapter) ['os-toc-unit-page', 'intro'] else raise "could not detect which page type class to apply for page.id `#{page.id}` during baking the TOC. The classes on the page are: `#{page.classes}` #{page.say_source_or_nil}" end when CompositePageElement if page.is_index? || page.is_index_of_type? ['os-toc-index', 'index'] elsif page.is_citation_reference? ['os-toc-reference', 'composite-page'] elsif page.is_section_reference? ['os-toc-references', 'composite-page'] elsif page.has_ancestor?(:composite_chapter) && \ page.ancestor(:composite_chapter).is_answer_key? ['os-toc-chapter-composite-page', 'answer-key'] elsif page.has_ancestor?(:composite_chapter) || page.has_ancestor?(:chapter) ['os-toc-chapter-composite-page', 'composite-page'] else raise "could not detect which composite page type class to apply to TOC for page id \ `#{page.id}` during baking the TOC. The classes on the page are: `#{page.classes}` #{page.say_source_or_nil}" end else raise(ArgumentError, "could not detect any page type class to apply for `#{page.id}`" \ "during baking TOC#{page.say_source_or_nil}") end # rubocop:enable Style/WordArray title = page.title.copy # The part text gets inserted as a child to the number span part_text = title.first('.os-part-text') number = title.first('.os-number') if part_text && number part_text = part_text.cut number.prepend(child: part_text.paste) end <<~HTML <li class="#{li_page_type}" cnx-archive-shortid="" cnx-archive-uri="#{page.id}" data-toc-type="book-content" data-toc-target-type="#{toc_target_type}"> <a href="##{page.id}"> #{title.element_children.copy.paste} </a> </li> HTML end |
.li_for_unit(unit, options) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kitchen/directions/bake_toc.rb', line 33 def self.li_for_unit(unit, ) chapters = unit.element_children.only(ChapterElement) pages = unit.element_children.only(PageElement) <<~HTML <li cnx-archive-uri="" cnx-archive-shortid="" class="os-toc-unit" data-toc-type="unit"> <a href="#"> <span class="os-number"><span class="os-part-text">#{I18n.t(:unit)} </span>#{unit.count_in(:book)}</span> <span class="os-divider"> </span> <span data-type="" itemprop="" class="os-text">#{unit.title_text}</span> </a> <ol class="os-unit"> #{pages.map { |page| li_for_page(page) }.join("\n")} #{chapters.map { |chapter| li_for_chapter(chapter, ) }.join("\n")} </ol> </li> HTML end |
.v1(book:, options: { cases: false }) ⇒ Object
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 |
# File 'lib/kitchen/directions/bake_toc.rb', line 6 def self.v1(book:, options: { cases: false }) .reverse_merge!( cases: false ) = book.body.element_children.map do |element| case element when UnitElement li_for_unit(element, ) when ChapterElement li_for_chapter(element, ) when PageElement, CompositePageElement li_for_page(element) when CompositeChapterElement li_for_composite_chapter(element) end end.compact.join("\n") book.first!('nav').replace_children(with: <<~HTML <h1 class="os-toc-title">#{I18n.t(:toc_title)}</h1> <ol> #{} </ol> HTML ) end |