Sha256: a51d12d16170d6eafcc33cc090b185e7a742bb8c58cecb47c168744da44b5af2

Contents?: true

Size: 854 Bytes

Versions: 1

Compression:

Stored size: 854 Bytes

Contents

module Ruco
  module SyntaxParser
    # textpow only offers certain syntax
    TEXTPOW_CONVERT = {
      'scss' => 'source.sass',
      'html+erb' => 'text.html.ruby',
      'rhtml' => 'text.html.ruby',
    }

    def self.syntax_for_line(line, languages)
      syntax_for_lines([line], languages).first
    end
    cmemoize :syntax_for_line

    def self.syntax_for_lines(lines, languages)
      if syntax = syntax_node(languages)
        processor = syntax.parse(lines.join("\n"),  Ruco::ArrayProcessor.new)
        processor.lines
      else
        []
      end
    end

    def self.syntax_node(languages)
      found = nil
      fallbacks = languages.map{|l| TEXTPOW_CONVERT[l] }.compact

      (languages + fallbacks).detect do |language|
        found = Textpow.syntax(language)
      end

      found
    end
    cmemoize :syntax_node
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruco-0.2.4 lib/ruco/syntax_parser.rb