Module: OKF::Markdown::Citations

Defined in:
lib/okf/markdown/citations.rb

Overview

Parses the conventional # Citations section of a concept body (spec §8): the block of external sources listed at the bottom of a document. Pure and fence-aware, mirroring Links; it reuses Links.extract to pull the citation link targets so citations and cross-links agree on what counts as a link.

Constant Summary collapse

HEADING =

A markdown ATX heading line: 1–6 #, whitespace, then the heading text.

/\A(\#{1,6})\s+(.*?)\s*\z/.freeze
CITATIONS =
/\ACitations\z/i.freeze

Class Method Summary collapse

Class Method Details

.section(body) ⇒ Object

The body text under a # Citations heading, up to the next heading at the same or higher level, or nil when there is no Citations section.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/okf/markdown/citations.rb', line 18

def section(body)
  lines = []
  level = nil
  in_fence = false
  body.to_s.each_line do |line|
    if Links::FENCE.match?(line.strip)
      in_fence = !in_fence
      lines << line unless level.nil?
      next
    end

    heading = in_fence ? nil : HEADING.match(line.strip)
    if level.nil?
      next unless heading && CITATIONS.match?(heading[2])

      level = heading[1].length
    elsif heading && heading[1].length <= level
      break
    else
      lines << line
    end
  end
  lines.join unless level.nil?
end

.targets(body) ⇒ Object

Citation link targets within the # Citations section (empty when absent).



44
45
46
# File 'lib/okf/markdown/citations.rb', line 44

def targets(body)
  Links.extract(section(body).to_s)
end