Module: HH::Markup

Included in:
Manual
Defined in:
lib/ext/highlighter/markup.rb

Constant Summary collapse

TOKENIZER =
HH::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



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
69
70
71
72
73
74
75
76
77
78
# File 'lib/ext/highlighter/markup.rb', line 31

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| HH::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])
        tmp = fg(token, tr_color(colors[token.group][:stroke]))
        colors[token.group][:fill] ? bg(tmp, tr_color(colors[token.group][:fill])) : tmp
      elsif colors[:any]
        #span(token, colors[:any])
        tmp = fg(token, tr_color(colors[:any][:stroke]))
        colors[:any][:fill] ? bg(tmp, tr_color(colors[:any][:fill])) : tmp
      else
        token
      end
  end

  if pos.nil? or 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])
    tmp = fg(tokens[token_index], tr_color(colors[:matching][:stroke]))
    res[token_index] = colors[:matching][:fill] ? bg(tmp, tr_color(colors[:matching][:fill])) : tmp
    if matching_index
      #res[matching_index] = span(tokens[matching_index], colors[:matching])
      tmp = fg(tokens[matching_index], tr_color(colors[:matching][:stroke]))
      res[matching_index] = colors[:matching][:fill] ? bg(tmp, tr_color(colors[:matching][:fill])) : tmp
    end
  end

  res
end