Module: Coradoc::Mirror::Handlers::Structural

Defined in:
lib/coradoc/mirror/handlers/structural.rb

Constant Summary collapse

SECTION_STYLE_TO_JS_TYPE =

Map CoreModel section style/title hints to JS SECTION_TYPES. When coradoc propagates AsciiDoc style attributes ([appendix], [bibliography], etc.) into SectionElement.attributes, this table is used to pick the right JS section type. Default fallback is ‘clause` (the JS generic section type).

{
  'appendix' => 'annex',
  'annex' => 'annex',
  'bibliography' => 'references',
  'references' => 'references',
  'abstract' => 'abstract',
  'foreword' => 'foreword',
  'introduction' => 'introduction',
  'acknowledgements' => 'acknowledgements',
  'terms' => 'terms',
  'definitions' => 'definitions'
}.freeze

Class Method Summary collapse

Class Method Details

.document(element, context:) ⇒ Object

Top-level document handler. Stays flat; structural partitioning (preface/sections/bibliography) is opted into via the partition_structural: kwarg on CoreModelToMirror#call, which delegates to Mirror::Partitioner.



11
12
13
14
15
16
17
# File 'lib/coradoc/mirror/handlers/structural.rb', line 11

def self.document(element, context:)
  content = context.extract_content(element)
  Node::Document.new(
    attrs: Node::Document::Attrs.new(title: element.title, id: element.id),
    content: content
  )
end

.header(element, context:) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/coradoc/mirror/handlers/structural.rb', line 71

def self.header(element, context:)
  content = context.extract_content(element)
  Node::Header.new(
    attrs: Node::Header::Attrs.new(
      title: element.title,
      level: element.heading_level
    ),
    content: content
  )
end

.preamble(element, context:) ⇒ Object



66
67
68
69
# File 'lib/coradoc/mirror/handlers/structural.rb', line 66

def self.preamble(element, context:)
  content = context.extract_content(element)
  Node::Preamble.new(content: content)
end

.section(element, context:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/coradoc/mirror/handlers/structural.rb', line 37

def self.section(element, context:)
  content = context.extract_content(element)
  type = context.partition_structural ? section_type_for(element) : 'section'

  Node::Section.new(
    type: type,
    attrs: Node::Section::Attrs.new(
      id: element.id,
      title: element.title,
      level: element.heading_level
    ),
    content: content
  )
end

.section_style(element) ⇒ Object

Reads ‘style` then `role` from SectionElement#attributes via the Metadata#[] accessor — no intermediate hash allocation per call.



59
60
61
62
63
64
# File 'lib/coradoc/mirror/handlers/structural.rb', line 59

def self.section_style(element)
  attrs = element.attributes
  return nil unless attrs.is_a?(Coradoc::CoreModel::Metadata)

  attrs['style'] || attrs['role']
end

.section_type_for(element) ⇒ Object



52
53
54
55
# File 'lib/coradoc/mirror/handlers/structural.rb', line 52

def self.section_type_for(element)
  style = section_style(element)
  SECTION_STYLE_TO_JS_TYPE[style] || 'clause'
end