Class: Coradoc::Markdown::Serializer::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/markdown/serializer/context.rb

Overview

Per-document mutable state threaded through every serializer call, AND the single dispatch interface element serializers use to recurse into child elements.

Threading dispatch through the context (not the runner) means serializers stay stateless and don’t need a back-reference to the runner — they call ‘ctx.serialize(child)` and `ctx.serialize_inline(child)`.

Holds counters (footnotes, link references) and accumulators (footnote definitions emitted at document end, etc.). Created fresh per top-level ‘call` — never shared between documents.

Defined Under Namespace

Classes: LinkRef

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, registry:, runner:) ⇒ Context

Returns a new instance of Context.



22
23
24
25
26
27
28
29
30
# File 'lib/coradoc/markdown/serializer/context.rb', line 22

def initialize(config:, registry:, runner:)
  @config = config
  @registry = registry
  @runner = runner
  @footnote_defs = []
  @link_refs = []
  @footnote_counter = 0
  @link_counter = 0
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/coradoc/markdown/serializer/context.rb', line 18

def config
  @config
end

#footnote_counterObject (readonly)

Returns the value of attribute footnote_counter.



18
19
20
# File 'lib/coradoc/markdown/serializer/context.rb', line 18

def footnote_counter
  @footnote_counter
end

#footnote_defsObject (readonly)

Returns the value of attribute footnote_defs.



18
19
20
# File 'lib/coradoc/markdown/serializer/context.rb', line 18

def footnote_defs
  @footnote_defs
end

Returns the value of attribute link_counter.



18
19
20
# File 'lib/coradoc/markdown/serializer/context.rb', line 18

def link_counter
  @link_counter
end

Returns the value of attribute link_refs.



18
19
20
# File 'lib/coradoc/markdown/serializer/context.rb', line 18

def link_refs
  @link_refs
end

#registryObject (readonly)

Returns the value of attribute registry.



18
19
20
# File 'lib/coradoc/markdown/serializer/context.rb', line 18

def registry
  @registry
end

#runnerObject (readonly)

Returns the value of attribute runner.



18
19
20
# File 'lib/coradoc/markdown/serializer/context.rb', line 18

def runner
  @runner
end

Instance Method Details

#next_footnote_idObject



44
45
46
47
# File 'lib/coradoc/markdown/serializer/context.rb', line 44

def next_footnote_id
  @footnote_counter += 1
  "fn#{@footnote_counter}"
end


49
50
51
52
# File 'lib/coradoc/markdown/serializer/context.rb', line 49

def next_link_ref_id
  @link_counter += 1
  @link_counter.to_s
end

#register_footnote_def(definition_text) ⇒ Object



54
55
56
# File 'lib/coradoc/markdown/serializer/context.rb', line 54

def register_footnote_def(definition_text)
  @footnote_defs << definition_text unless @footnote_defs.include?(definition_text)
end


58
59
60
# File 'lib/coradoc/markdown/serializer/context.rb', line 58

def register_link_ref(id, url, title: nil)
  @link_refs << LinkRef.new(id: id, url: url, title: title)
end

#serialize(element) ⇒ Object



32
33
34
# File 'lib/coradoc/markdown/serializer/context.rb', line 32

def serialize(element)
  runner.serialize(element, self)
end

#serialize_inline(element) ⇒ Object



36
37
38
# File 'lib/coradoc/markdown/serializer/context.rb', line 36

def serialize_inline(element)
  runner.serialize_inline(element, self)
end

#serialize_inline_join(elements) ⇒ Object



40
41
42
# File 'lib/coradoc/markdown/serializer/context.rb', line 40

def serialize_inline_join(elements)
  Array(elements).map { |e| serialize_inline(e) }.join
end