Skip to content

Commit

Permalink
Remove some functions from ruby code.
Browse files Browse the repository at this point in the history
Those pretty print and link functions are available in Javascript, too. To
avoid code duplication, I changed the few places where those functions were
used to use the Javascript ones.
  • Loading branch information
joto committed Jan 14, 2013
1 parent 9d732da commit 267f61d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 86 deletions.
80 changes: 0 additions & 80 deletions web/lib/utils.rb
Expand Up @@ -110,86 +110,6 @@ def get_total(type)
return @db.stats(key)
end

# see also web/public/js/taginfo.js
def pp_key(key)
if key == ''
return '<span class="badchar empty">empty string</span>'
end

pp_chars = '!"#$%&()*+,/;<=>?@[\\]^`{|}~' + "'";

result = ''
key.each_char do |c|
if (!pp_chars.index(c).nil?)
result += '<span class="badchar">' + c + '</span>'
elsif (c == ' ')
result += '<span class="badchar">&#x2423;</span>'
elsif (c.match(/\s/))
result += '<span class="whitespace">&nbsp;</span>'
else
result += c;
end
end

return result;
end

# see also web/public/js/taginfo.js
def pp_value(value)
if value == ''
return '<span class="badchar empty">empty string</span>'
end
return escape_html(value).gsub(/ /, '&#x2423;').gsub(/\s/, '<span class="whitespace">&nbsp;</span>')
end

def pp_rtype(rtype)
if rtype == ''
return '<span class="badchar empty">empty string</span>'
end

pp_chars = '!"#$%&()*+,/;<=>?@[\\]^`{|}~' + "'";

result = ''
rtype.each_char do |c|
if (!pp_chars.index(c).nil?)
result += '<span class="badchar">' + c + '</span>'
elsif (c == ' ')
result += '<span class="badchar">&#x2423;</span>'
elsif (c.match(/\s/))
result += '<span class="whitespace">&nbsp;</span>'
else
result += c;
end
end

return result;
end

def link_to_key(key, tab='')
k = escape(key)

if key.match(/[=\/]/)
return '<a href="/keys/?key=' + k + tab + '">' + pp_key(key) + '</a>'
else
return '<a href="/keys/' + k + tab + '">' + pp_key(key) + '</a>'
end
end

def link_to_value(key, value)
k = escape(key)
v = escape(value)

if key.match(/[=\/]/) || value.match(/[=\/]/)
return '<a href="/tags/?key=' + k + '&value=' + v + '">' + pp_value(value) + '</a>'
else
return '<a href="/tags/' + k + '=' + v + '">' + pp_value(value) + '</a>'
end
end

def link_to_tag(key, value)
return link_to_key(key) + '=' + link_to_value(key, value)
end

# Like the 'get' method but will add a redirect for the same path with trailing / added
def get!(path, &block)
get path, &block
Expand Down
3 changes: 2 additions & 1 deletion web/views/key.erb
Expand Up @@ -16,7 +16,7 @@
<span class="button disabled" title="Button disabled, because there are too many objects with this key">JOSM</span>
<% end %>
</div>
<h1><%= pp_key(@key) %></h1>
<h1></h1>
<p><%= @desc %></p>
</div>
<div id="tabs">
Expand Down Expand Up @@ -105,6 +105,7 @@
JS.raw(<<"JAVASCRIPT")
function page_init2() {
var key = #{ @key.to_json };
jQuery('h1').html(pp_key(key));
init_tabs([key, #{ @filter_type.to_json }, #{ r18n.locale.code.to_json }]);
create_chart({
key: key,
Expand Down
6 changes: 4 additions & 2 deletions web/views/relation.erb
@@ -1,5 +1,5 @@
<div class="pre">
<h1><%= t.pages.relation.name %> '<%= pp_rtype(@rtype) %>'</h1>
<h1></h1>
<p><%= @desc %></p>
</div>
<div id="tabs">
Expand All @@ -23,7 +23,9 @@
<% javascript do
JS.raw(<<"JAVASCRIPT")
function page_init2() {
init_tabs([#{ @rtype.to_json }]);
var rtype = #{ @rtype.to_json };
jQuery('h1').html("#{ t.pages.relation.name } '" + pp_rtype(rtype) + "'");
init_tabs([rtype]);
}
JAVASCRIPT
end
Expand Down
10 changes: 7 additions & 3 deletions web/views/tag.erb
Expand Up @@ -16,7 +16,7 @@
<span class="button disabled" title="Button disabled, because there are too many objects with this key">JOSM</span>
<% end %>
</div>
<h1><%= link_to_key(@key) %>=<%= pp_value(@value) %></h1>
<h1></h1>
<p><%= @desc %></p>
</div>
<div id="tabs">
Expand Down Expand Up @@ -50,7 +50,7 @@
<table id="grid-wiki">
</table>
<% else %>
<p class="empty"><%= t.pages.tag.wiki_pages.none_found %> <%= @wiki_count_key > 0 ? t.pages.tag.wiki_pages.suggest_key_wiki_page(link_to_key(@key, '#wiki')) : '' %></p>
<p class="empty"><%= t.pages.tag.wiki_pages.none_found %> <%= @wiki_count_key > 0 ? t.pages.tag.wiki_pages.suggest_key_wiki_page('<span id="keylink"></span>') : '' %></p>
<p><a class="extlink" target="_blank" rel="nofollow" href="http://wiki.openstreetmap.org/w/index.php?action=edit&title=Tag:<%= escape(@tag) %>"><%= t.pages.tag.wiki_pages.create %></a></p>
<% end %>
</div>
Expand All @@ -74,7 +74,11 @@
<% javascript do
JS.raw(<<"JAVASCRIPT")
function page_init2() {
init_tabs([#{ @key.to_json }, #{ @value.to_json }, #{ @filter_type.to_json }]);
var key = #{ @key.to_json },
value = #{ @value.to_json };
jQuery('h1').html(link_to_key(key) + '=' + pp_value(value));
jQuery('span#keylink').html(link_to_key(key));
init_tabs([key, value, #{ @filter_type.to_json }]);
}
JAVASCRIPT
end
Expand Down

0 comments on commit 267f61d

Please sign in to comment.