Module: Shoes::Highlighter::Markup

Defined in:
lib/shoes/highlighter/markup.rb

Constant Summary collapse

TOKENIZER =
Shoes::Highlighter::Syntax.load "ruby"
COLORS =
{
    comment: { stroke: "#887" },
    keyword: { stroke: "#111" },
    method: { stroke: "#C09", weight: "bold" },
    # :class => {:stroke => "#0c4", :weight => "bold"},
    # :module => {:stroke => "#050"},
    # :punct => {:stroke => "#668", :weight => "bold"},
    symbol: { stroke: "#C30" },
    string: { stroke: "#C90" },
    number: { stroke: "#396" },
    regex: { stroke: "#000", fill: "#FFC" },
    # :char => {:stroke => "#f07"},
    attribute: { stroke: "#369" },
    # :global => {:stroke => "#7FB" },
    expr: { stroke: "#722" },
    # :escape => {:stroke => "#277" }
    ident: { stroke: "#994c99" },
    constant: { stroke: "#630", weight: "bold" },
    class: { stroke: "#630", weight: "bold" },
    matching: { stroke: "#ff0", weight: "bold" },
}

Instance Method Summary collapse

Instance Method Details

#highlight(str, pos = nil, colors = COLORS) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/shoes/highlighter/markup.rb', line 29

def highlight(str, pos = nil, colors = COLORS)
  tokens = []
  TOKENIZER.tokenize(str) do |t|
    if t.group == :punct
      # split punctuation into single characters tokens
      # TODO: to it in the parser
      tokens += t.split('').map { |s| Shoes::Highlighter::Syntax::Token.new(s, :punct) }
    else
      # add token as is
      tokens << t
    end
  end

  res = []
  tokens.each do |token|
    res <<
      if colors[token.group]
        span(token, colors[token.group])
      elsif colors[:any]
        span(token, colors[:any])
      else
        token
      end
  end

  if pos.nil? || pos < 0
    return res
  end

  token_index, matching_index = matching_token(tokens, pos)

  if token_index
    res[token_index] = span(tokens[token_index], colors[:matching])
    if matching_index
      res[matching_index] = span(tokens[matching_index], colors[:matching])
    end
  end

  res
end