Class: Kitchen::Directions::BakeChapterGlossary::V1

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

Defined Under Namespace

Classes: Definition

Instance Method Summary collapse

Instance Method Details

#bake(chapter:, metadata_source:, append_to: nil, uuid_prefix: '', has_para: false) ⇒ Object



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
# File 'lib/kitchen/directions/bake_chapter_glossary/v1.rb', line 24

def bake(chapter:, metadata_source:, append_to: nil, uuid_prefix: '', has_para: false)
  @glossary = []

  # Use for books created by Adaptarr, where dd contains paragraphs
  # More info: https://github.com/openstax/kitchen/issues/284
  if has_para
    chapter.glossaries.search('dd').each do |description|
      description_content = description.first('p').text
      description.replace_children with: description_content
    end
  end

  chapter.glossaries.search('dl').each do |definition_element|
    @glossary.push(Definition.new(definition_element.cut))
  end

  chapter.glossaries.trash

  content = @glossary.sort.map { |definition| definition.element.paste }.join

  return if content.empty?

  Kitchen::Directions::CompositePageContainer.v1(
    container_key: 'glossary',
    uuid_key: "#{uuid_prefix}glossary",
    metadata_source: ,
    content: content,
    append_to: append_to || chapter
  )
end