Class: Coradoc::CoreModel::IncludeLevelOffset

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/core_model/include_level_offset.rb

Overview

A leveloffset value parsed from an include directive.

asciidoctor supports two forms:

leveloffset=+N   → relative shift (heading.level += N)
leveloffset=-N   → relative shift (heading.level -= N)
leveloffset=N    → absolute set (heading.level = N)

The parsed form keeps the mode and delta separate so that the selector that applies the offset does not need to re-parse the string each time it walks a section (DRY).

Instance Attribute Summary

Attributes inherited from Base

#element_attributes, #id, #metadata_entries, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#accept, #attr, #flat_text, #metadata, #semantically_equivalent?, #set_attr, #set_metadata

Class Method Details

.parse(raw) ⇒ IncludeLevelOffset?

Construct from a raw asciidoctor-style string (“+2”, “-1”, “3”). Returns nil if the input is nil or unparsable.

Parameters:

  • raw (String, nil)

Returns:



28
29
30
31
32
# File 'lib/coradoc/core_model/include_level_offset.rb', line 28

def self.parse(raw)
  return nil if raw.nil? || raw.strip.empty?

  matched_offset(raw.strip)
end

Instance Method Details

#apply(level) ⇒ Integer

Apply this offset to a heading level (1-indexed asciidoctor level).

Parameters:

  • level (Integer)

    original section level

Returns:

  • (Integer)

    new section level, clamped to >= 0



38
39
40
41
42
43
44
# File 'lib/coradoc/core_model/include_level_offset.rb', line 38

def apply(level)
  case mode
  when 'relative' then [level + delta, 0].max
  when 'absolute' then [delta, 0].max
  else level
  end
end

#to_sString

Render back to the asciidoctor wire form (“+2”, “-1”, “3”).

Returns:

  • (String)


49
50
51
52
53
54
55
# File 'lib/coradoc/core_model/include_level_offset.rb', line 49

def to_s
  case mode
  when 'relative' then format('%+d', delta)
  when 'absolute' then delta.to_s
  else ''
  end
end