class Headown::Extractor
Attributes
headers[R]
Public Class Methods
new(file_path)
click to toggle source
# File lib/headown.rb, line 16 def initialize(file_path) raise Headown::NotMarkdownError.new(file_path: file_path) if File.extname(file_path) != '.md' file_data = URI.open(file_path, &:read) extract_headers(file_data) end
Private Instance Methods
build_header(node)
click to toggle source
# File lib/headown.rb, line 34 def build_header(node) text = '' node.each { |subnode| text = subnode.string_content if subnode.type == :text } '#' * node.header_level + " #{text}" end
extract_headers(file_data)
click to toggle source
# File lib/headown.rb, line 25 def extract_headers(file_data) @headers = header_nodes(file_data).map { |node| build_header(node) } end
header_nodes(data)
click to toggle source
# File lib/headown.rb, line 29 def header_nodes(data) doc = CommonMarker.render_doc(data) [].tap { |nodes| doc.walk { |node| nodes << node if node.type == :header } } end