Module: Kramdown::AsciiDoc::Preprocessors

Defined in:
lib/kramdown-asciidoc/preprocessors.rb

Class Method Summary collapse

Class Method Details

.extract_front_matter(source, attributes) ⇒ String

Skims off the front matter from the top of the Markdown source and store the data in the provided attributes Hash.

Parameters:

  • source (String)

    the Markdown source from which to extract the front matter.

  • attributes (Hash)

    the attributes in which to store the key/value pairs from the front matter.

Returns:

  • (String)

    the Markdown source with the front matter removed.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kramdown-asciidoc/preprocessors.rb', line 12

def self.extract_front_matter source, attributes
  if (line_i = (lines = source.each_line).first) && line_i.chomp == '---'
    lines = lines.drop 1
    front_matter = []
    while (line = lines.shift) && line.chomp != '---'
      front_matter << line
    end
    return source unless line && line.chomp == '---' && !(front_matter.include? ?\n)
    lines.shift while (line = lines[0]) && line == ?\n
    (::YAML.safe_load front_matter.join, permitted_classes: [::Date, ::Time]).each do |key, val|
      if key == 'layout'
        attributes['page-layout'] = val unless val == 'default'
      else
        attributes[key] = val.to_s
      end
    end unless front_matter.empty?
    lines.join
  else
    source
  end
end

.replace_toc(source, attributes) ⇒ String

Replaces the Markdown TOC, if found, with the AsciiDoc toc macro and set the toc attribute to macro.

Parameters:

  • source (String)

    the Markdown source in which the TOC should be replaced.

  • attributes (Hash)

    a map of AsciiDoc attributes to set on the output document.

Returns:

  • (String)

    the Markdown source with the TOC replaced, if found.



40
41
42
43
44
45
46
47
# File 'lib/kramdown-asciidoc/preprocessors.rb', line 40

def self.replace_toc source, attributes
  if source.include? TocDirectiveTip
    attributes['toc'] = 'macro'
    source.gsub TocDirectiveRx, 'toc::[]'
  else
    source
  end
end

.trim_before_leading_comment(markdown, _attributes) ⇒ String

Trims space characters that precede a leading XML comment in the Markdown source.

Parameters:

  • markdown (String)

    the Markdown source to process.

  • attributes (Hash)

    a map of AsciiDoc attributes to set on the output document.

Returns:

  • (String)

    the Markdown source with the space characters preceding a leading XML comment removed.



55
56
57
# File 'lib/kramdown-asciidoc/preprocessors.rb', line 55

def self.trim_before_leading_comment markdown, _attributes
  (markdown.start_with? ' ', TAB) && (markdown.lstrip.start_with? '<!--') ? markdown.lstrip : markdown
end