Module: MarkdownMetrics

Defined in:
lib/markdown_metrics.rb,
lib/markdown_metrics/version.rb,
lib/markdown_metrics/file_lines.rb,
lib/markdown_metrics/elements/base.rb,
lib/markdown_metrics/line_low_element.rb,
lib/markdown_metrics/line_top_element.rb,
lib/markdown_metrics/elements/block/pre.rb,
lib/markdown_metrics/elements/inline/h1.rb,
lib/markdown_metrics/elements/inline/h2.rb,
lib/markdown_metrics/elements/inline/h3.rb,
lib/markdown_metrics/elements/inline/h4.rb,
lib/markdown_metrics/elements/inline/h5.rb,
lib/markdown_metrics/elements/inline/h6.rb,
lib/markdown_metrics/elements/block/list.rb,
lib/markdown_metrics/elements/inline/pre.rb,
lib/markdown_metrics/low_elements_parser.rb,
lib/markdown_metrics/top_elements_parser.rb,
lib/markdown_metrics/elements/block/quote.rb,
lib/markdown_metrics/elements/block/table.rb,
lib/markdown_metrics/elements/inline/image.rb,
lib/markdown_metrics/elements/sentence/base.rb,
lib/markdown_metrics/elements/sentence/bold.rb,
lib/markdown_metrics/elements/sentence/code.rb,
lib/markdown_metrics/elements/sentence/link.rb,
lib/markdown_metrics/elements/sentence/text.rb,
lib/markdown_metrics/elements/sentence/italic.rb,
lib/markdown_metrics/elements/inline/paragraph.rb,
lib/markdown_metrics/elements/inline/empty_line.rb

Defined Under Namespace

Modules: Elements, Version Classes: FileLines, LineLowElement, LineTopElement, LowElementsParser, TopElementsParser

Constant Summary collapse

ELEMENTS_WITH_LOW_ELEMENTS =
%i[paragraph list h1 h2 h3 h4 h5 h6]

Class Method Summary collapse

Class Method Details

.generate(file_path:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/markdown_metrics.rb', line 41

def generate(file_path:)
  file_lines = MarkdownMetrics::FileLines.from(file_path)
  top_elements_parser = MarkdownMetrics::TopElementsParser.new(file_lines)
  top_elements_parser.parse

  top_elements_parser.elements.map do |element|
    unless ELEMENTS_WITH_LOW_ELEMENTS.include?(element[:name])
      element
    else
      if element[:name] == :table
        new_rows = []
        element[:value][:rows].each do |r|
          new_rows << r.map do |row|
            low_elements(row)
          end
        end

        element[:value].merge!(rows: new_rows)
        element
      else
        low_level_values = if element[:value].kind_of?(Array)
          element[:value].map { |v| low_elements(v) }
        else
          low_elements(element[:value])
        end

        element.merge(value: low_level_values)
      end
    end
  end
end