class Forspell::Loaders::Markdown

Constants

PARSER
SPECIAL_CHARS_MAP

Public Instance Methods

extract_words() click to toggle source
# File lib/forspell/loaders/markdown.rb, line 39
def extract_words
  document = Kramdown::Document.new(@input, input: PARSER)
  tree = FilteredHash.new.convert(document.root, document.options)
  chunks = extract_chunks(tree)
  result = []
  return result if chunks.empty?

  group_by_location = chunks.group_by { |res| res[:location] }
                            .transform_values do |lines|
    lines.map { |v| SPECIAL_CHARS_MAP[v[:value]] || v[:value] }
      .join.split(%r{[[:punct:]]&&[^-'_./\\:]|\s})
  end
  
  group_by_location.each_pair do |location, words|
    words.reject(&:empty?)
         .each { |word| result << Word.new(@file, location || 0, word) }
  end

  result
rescue RuntimeError => e
  raise Forspell::Loaders::ParsingError, e.message
end

Private Instance Methods

extract_chunks(tree) click to toggle source
# File lib/forspell/loaders/markdown.rb, line 64
def extract_chunks(tree)
  tree[:children].grep(Hash).flat_map do |child|
    if child[:children]
      extract_chunks(child)
    else
      {
        location: child[:location],
        value: child[:value]
      }
    end
  end
end