class Rabbit::Parser::Ext::Rouge::RabbitFormatter

Public Instance Methods

stream(tokens) { |custom_tag("syntax-#{tag_name}", text_element)| ... } click to toggle source
# File lib/rabbit/parser/ext/rouge.rb, line 46
def stream(tokens)
  tokens.each do |token, value|
    escaped_text = Escape.escape_meta_character(value)
    text_element = SyntaxHighlightingText.new(Text.new(escaped_text))
    tag_name = compute_tag_name(token)
    if Utils.syntax_highlighting_debug?
      p [tag_name, token.qualname, value]
    end
    yield(CustomTag.new("syntax-#{tag_name}", text_element))
  end
end

Private Instance Methods

compute_tag_name(token) click to toggle source
# File lib/rabbit/parser/ext/rouge.rb, line 59
def compute_tag_name(token)
  group = token.token_chain.first.name
  case group
  when :Keyword
    case token.name
    when :Constant
      tag_namenize(token.name)
    else
      tag_namenize(group)
    end
  when :Name
    case token.name
    when :Namespace
      "include"
    else
      case token.parent.name
      when :Variable
        "#{tag_namenize(token.name)}_variable"
      else
        tag_namenize(token.name)
      end
    end
  when :Literal
    if match_token?("Literal.String", token)
      case token.name
      when :Symbol
        tag_namenize(token.name)
      else
        "string"
      end
    elsif match_token?("Literal.Number", token)
      if match_token?("Literal.Number.Float", token)
        "float"
      elsif match_token?("Literal.Number.Integer", token)
        "integer"
      else
        tag_namenize(token.name)
      end
    else
      tag_namenize(token.name)
    end
  when :Generic
    tag_name = tag_namenize(token.name)
    case tag_name
    when "deleted"
      "delete"
    when "inserted"
      "insert"
    else
      tag_name
    end
  else
    tag_namenize(group)
  end
end
match_token?(name, token) click to toggle source
# File lib/rabbit/parser/ext/rouge.rb, line 119
def match_token?(name, token)
  ::Rouge::Token[name].matches?(token)
end
tag_namenize(name) click to toggle source
# File lib/rabbit/parser/ext/rouge.rb, line 115
def tag_namenize(name)
  name.to_s.downcase
end