Class: MdTransformer::Markdown::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/md_transformer/markdown/section.rb

Overview

Class representing a section of a markdown file (header with content)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, options = {}) ⇒ MdTransformer::Markdown::Section

Creates the Section object

Parameters:

  • title (String)

    the title of the section

  • options (Hash) (defaults to: {})

    the options hash

Options Hash (options):

  • :level (Integer) — default: 0

    the precedence level of the section

  • :header_location (Range) — default: 0..0

    the section header’s location in the original content

  • :content (String) — default: ''

    the content of the section



24
25
26
27
28
29
# File 'lib/md_transformer/markdown/section.rb', line 24

def initialize(title, options = {})
  @title = title
  @level = options[:level] || 0
  @header_location = options[:header_location].nil? ? 0..0 : options[:header_location]
  @content = options[:content] || ''
end

Instance Attribute Details

#contentString (readonly)

Returns the section’s content.

Returns:

  • (String)

    the section’s content



15
16
17
# File 'lib/md_transformer/markdown/section.rb', line 15

def content
  @content
end

#header_locationRange (readonly)

Returns the section header’s location in the original content.

Returns:

  • (Range)

    the section header’s location in the original content



12
13
14
# File 'lib/md_transformer/markdown/section.rb', line 12

def header_location
  @header_location
end

#levelInteger (readonly)

Returns the precedence level of the section.

Returns:

  • (Integer)

    the precedence level of the section



9
10
11
# File 'lib/md_transformer/markdown/section.rb', line 9

def level
  @level
end

#titleString (readonly)

Returns the title of the section.

Returns:

  • (String)

    the title of the section



6
7
8
# File 'lib/md_transformer/markdown/section.rb', line 6

def title
  @title
end

Class Method Details

.generate_sections(content) ⇒ Array

Generates an array of Section objects from string Markdown content

Parameters:

  • content (String)

    the markdown content to parse

Returns:

  • (Array)

    the array of generated Section objects



34
35
36
37
38
39
40
41
# File 'lib/md_transformer/markdown/section.rb', line 34

def self.generate_sections(content)
  headers = header_locations(content)
  headers.each_with_index do |h, i|
    content_end = content.length
    headers[i] = { header: h, content: ((h.end + 1)..(content_end - 1)) }
  end
  headers.map! { |h| create_section_from_header(h, content) }
end