Class: Grenweb::HTMLRendeler

Inherits:
Object
  • Object
show all
Includes:
Rack::Utils
Defined in:
lib/grenweb/html_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(script_name) ⇒ HTMLRendeler

Returns a new instance of HTMLRendeler.



16
17
18
# File 'lib/grenweb/html_renderer.rb', line 16

def initialize(script_name)
  @script_name = Pathname(script_name)
end

Instance Method Details

#empty_summaryObject



173
174
175
176
177
178
179
# File 'lib/grenweb/html_renderer.rb', line 173

def empty_summary()
  <<EOS
  <div class='search-summary'>
<p>gren web検索</p>
  </div>
EOS
end


44
45
46
47
48
49
50
51
52
53
# File 'lib/grenweb/html_renderer.rb', line 44

def footer
  <<EOS
</div>

<div class="footer">
</div>
</body>
</html>
EOS
end


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/grenweb/html_renderer.rb', line 80

def footer_home(package, files)
  <<EOS
</div>

<div class="footer_home">
  <!-- <a href="#{fullpath('::search/p:*')}">#{package}</a>のパッケージ , -->
  <a href="#{fullpath('::search/f:*')}">#{files}</a>のファイル<br>
  <a href="#{fullpath('::help')}">ヘルプ</a> , 
  <a href="http://ongaeshi.github.com/gren">grenについて</a>
</div>
</div>
</body>
</html>
EOS
end

#fullpath(path) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/grenweb/html_renderer.rb', line 233

def fullpath(path)
  unless (path == '')
    (@script_name + path).to_s
  else
    @script_name.to_s
  end
end

#header(title, header1) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/grenweb/html_renderer.rb', line 20

def header(title, header1)
  <<EOS
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <!-- <meta name="robot" content="noindex,nofollow" /> -->
  <title>#{title}</title>
  <link rel="stylesheet" href="#{fullpath('css/gren.css')}"  type="text/css" media="all" />
</head>
<body>
<div class="header">
  <h1>
<a href="#{fullpath('')}"><img src="#{fullpath('images/gren-icon-mini.png')}" alt="gren-icon" border="0"/></a>
#{header1}
  </h1>
</div>

<div class="content">
EOS
end

#header_home(title, header1, version) ⇒ Object



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/grenweb/html_renderer.rb', line 55

def header_home(title, header1, version)
  <<EOS
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <!-- <meta name="robot" content="noindex,nofollow" /> -->
  <title>#{title}</title>
  <link rel="stylesheet" href="#{fullpath('css/gren.css')}"  type="text/css" media="all" />
</head>
<body>
<div align="center">
<div class="header_home">
  <h1>
<a href="#{fullpath('')}"><img src="#{fullpath('images/gren-icon.png')}" alt="gren-icon" border="0" height="100px"/></a>
#{header1} <font class="version">#{version}</font>
  </h1>
</div>

<div class="content">
EOS
end

#line(lineno, line, match_datas) ⇒ Object



152
153
154
# File 'lib/grenweb/html_renderer.rb', line 152

def line(lineno, line, match_datas)
  sprintf("%5d: %s", lineno, match_strong(Rack::Utils::escape_html(line), match_datas))
end


229
230
231
# File 'lib/grenweb/html_renderer.rb', line 229

def link(keyword)
  "<a href='#{fullpath('::search' + '/' + Rack::Utils::escape_html(keyword))}'>#{keyword}</a>"
end

#match_strong(line, match_datas) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/grenweb/html_renderer.rb', line 156

def match_strong(line, match_datas)
  match_datas.each do |m|
    line = line.split(m[0]).join('<strong>' + m[0] + '</strong>') unless (m.nil?)
  end
  
  line
end


164
165
166
167
# File 'lib/grenweb/html_renderer.rb', line 164

def pagination_link(page, label)
  href = "?page=#{page}"
  pagination_span("<a href='#{href}'>#{label}</a>")
end

#pagination_span(content) ⇒ Object



169
170
171
# File 'lib/grenweb/html_renderer.rb', line 169

def pagination_span(content)
  "<span class='pagination-link'>#{content}</span>\n"
end

#record_content(record) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/grenweb/html_renderer.rb', line 133

def record_content(record)
  <<EOS
<pre>
#{record_content_line(record)}
</pre>
EOS
end

#record_content_line(record) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/grenweb/html_renderer.rb', line 141

def record_content_line(record)
  str = ""

  grep = Grep.new(record.content)
  grep.content.each_with_index do |l, index|
    str << line(index + 1, l, []) + "\n"
  end

  str
end

#result_record(record, patterns, nth = 1) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/grenweb/html_renderer.rb', line 96

def result_record(record, patterns, nth=1)
  if (patterns.size > 0)
    <<EOS
<dt class='result-record'><a href='#{fullpath("::view/" + Rack::Utils::escape_html(record.shortpath))}'>#{record.shortpath}</a></dt>
<dd>
  <pre class='lines'>
#{result_record_match_line(record, patterns, nth)}
  </pre>
</dd>
EOS
  else
    <<EOS
<dt class='result-record'><a href='#{fullpath("::view/" + Rack::Utils::escape_html(record.shortpath))}'>#{record.shortpath}</a></dt>
EOS
  end
end

#result_record_match_line(record, patterns, nth) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/grenweb/html_renderer.rb', line 113

def result_record_match_line(record, patterns, nth)
  str = ""
  
  grep = Grep.new(record.content)
  lines = grep.match_lines_or(patterns)

  unless (lines.empty?)
    index = lines[0].index
    
    (index - nth..index + nth).each do |i|
      if (0 <= i && i < grep.content.size)
        match_datas = (i == index) ? lines[0].match_datas : []
        str << line(i + 1, grep.content[i], match_datas) + "\n"
      end
    end
  end

  str
end

#sample_codeObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/grenweb/html_renderer.rb', line 210

def sample_code
  <<EOS
  <div class='sample-code'>
  <ol>
<li>キーワードで検索<br>
  #{link('def open')}
<li>1フレーズとして検索<br>
  #{link('"def open"')}
<li>パッケージ名で絞り込み<br>
  #{link('def open p:gren')}
<li>ファイル名や拡張子で絞り込み<br>
  #{link('def open f:test s:rb')}
<li>組み合わせ<br>
  #{link('p:gren p:tidtools s:rb f:test assert f:cli')}
  </ol>
  </div>
EOS
end

#search_box(text = "") ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/grenweb/html_renderer.rb', line 199

def search_box(text = "")
  <<EOS
<form method="post" action="#{fullpath('::search')}">
  <p>
<input name="query" type="text" size="60" value="#{text}" />
<input type="submit" value="検索" />
  </p>
</form>
EOS
end

#search_summary(keyword, total_records, range, elapsed) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/grenweb/html_renderer.rb', line 181

def search_summary(keyword, total_records, range, elapsed)
  <<EOS
  <div class='search-summary'>
<span class="keyword">#{keyword}</span>の検索結果:
<span class="total-entries">#{total_records}</span>件中
<span class="display-range">#{range.first} - #{range.last}</span>件(#{elapsed}秒)
  </div>
EOS
end

#view_summary(path, elapsed) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/grenweb/html_renderer.rb', line 191

def view_summary(path, elapsed)
  <<EOS
  <div class='search-summary'>
<span class="keyword">#{path}</span>(#{elapsed}秒)
  </div>
EOS
end