Class: Gonzui::TextBeautifier

Inherits:
Object
  • Object
show all
Defined in:
lib/gonzui/webapp/markup.rb

Instance Method Summary collapse

Constructor Details

#initialize(content, digest, occurrences, search_uri) ⇒ TextBeautifier

Returns a new instance of TextBeautifier.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gonzui/webapp/markup.rb', line 18

def initialize(content, digest, occurrences, search_uri)
  @content = content
  @digest = digest # to be destroyed
  @occurrences = occurrences # to be destroyed
  @search_uri = search_uri

  @break_points = [0, @content.length]
  @lines = {}
  pos = lineno = 0
  @content.each_line {|line|
    lineno += 1
    @break_points.push(pos)
    @lines[pos] = [lineno, line]
    pos += line.length
  }
  @digest.each {|info|
    @break_points.push(info.byteno)
    @break_points.push(info.end_byteno)
  }
  @occurrences.each {|occ|
    @break_points.push(occ.byteno)
    @break_points.push(occ.end_byteno)
  }
  @break_points.uniq!
  @break_points.sort!

  @info = @digest.shift
  @occ = @occurrences.shift
  @seen_ids = {}
  @latest_id = "0"
end

Instance Method Details

#beautifyObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gonzui/webapp/markup.rb', line 106

def beautify
  code = []
  bp = @break_points.shift
  line = nil
  while not @break_points.empty?
    ep = @break_points.shift
    if @lines.include?(bp)
      code.push(line) if line
      line = make_lineno_part(bp)
    end
    part = markup_part(bp, ep)
    line.push(part)
    bp = ep
  end
  code.push(line) if line
  return code
end

#choose_target_typeObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gonzui/webapp/markup.rb', line 57

def choose_target_type
  case @info.type
  when :fundef 
    :funcall
  when :funcall
    :fundef
  when :fundecl
    :fundef
  else 
    assert_not_reached
  end
end

#decorate(text) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gonzui/webapp/markup.rb', line 70

def decorate(text)
  unless @seen_ids.include?(text)
    @seen_ids[text] = @latest_id
    @latest_id = @latest_id.next
  end
  id = @seen_ids[text]
  inner = [:span, {:class => id, :onmouseover => "hl('#{id}');"}, text]
  part = [:span, {:class => @info.type.to_s}, inner]
    
  if LangScan::Type.function?(@info.type)
    tt = choose_target_type
    query_string =  sprintf("%s:%s", tt, HTTPUtils.escape_form(text))
    uri = @search_uri + query_string
    part = [:a, {:href => uri}, part]
  end
  return part
end

#make_line_mark(lineno) ⇒ Object



50
51
52
53
54
55
# File 'lib/gonzui/webapp/markup.rb', line 50

def make_line_mark(lineno)
  anchor = "l" + lineno.to_s
  [:span, {:class => "lineno", :onclick => "olnc(this);"},
    [:a, {:id => anchor, :name => anchor, :href => "#" + anchor}, 
      sprintf("%5d: ", lineno)]]
end

#make_lineno_part(bp) ⇒ Object



101
102
103
104
# File 'lib/gonzui/webapp/markup.rb', line 101

def make_lineno_part(bp)
  lineno, line = @lines[bp]
  [:span, {:class => "line", :title => line}, make_line_mark(lineno)]
end

#markup_part(bp, ep) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gonzui/webapp/markup.rb', line 88

def markup_part(bp, ep)
  part =  @content[bp...ep]
  if @info and (bp == @info.byteno or @info.range.include?(bp))
    part = decorate(part)
    @info = @digest.shift if @info.end_byteno <= ep
  end
  if @occ and (bp == @occ.byteno or @occ.range.include?(bp))
    part = [:strong, {:class => "highlight"}, part]
    @occ = @occurrences.shift if @occ.end_byteno <= ep
  end
  return part
end