This repository was archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattach_color_keywords.rb
96 lines (83 loc) · 2.83 KB
/
attach_color_keywords.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# attach_color_keywords.rb - Hiki plugin to highlight a keyword with a color
#
# Installation:
# Copy or symlink this file into misc/plugin, and activate this
# plugin and attach plugin from configuration interface
#
# Usage:
# Upload a text file using attach plugin and write the
# following code in the page:
# {{attach_color_keywords(FILENAME, 'KEYWORD', COLOR)}}
#
# FILENAME: name of the uploaded file
# KEYWORD: keyword(s) to be highlighted
# COLOR: color name of color code
#
# Copyright (C) 2007 zunda <zunda at freeshell.org>
#
# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#
def attach_color_keywords(file_name, keyword, color, page = @page)
return '' unless file_name =~ /\.(txt|rd|rb|c|pl|py|sh|java|html|htm|css|xml|xsl|sql|yaml)\z/i
page_file_name = "#{escape(page.untaint)}/#{escape(file_name.untaint)}"
path = "#{@conf.cache_path}/attach/#{page_file_name}"
unless File.exists?(path)
raise PluginError, "No such file:#{page_file_name}"
end
src = File.open(path){|f| f.read}
pre_color_keywords(src, {keyword => color}, @options)
end
def pre_color_keywords(string, colors = {}, options = {})
tabstop = ' ' * (options['attach.tabstop'] ? options['attach.tabstop'].to_i : 2)
unless colors.keys.empty?
span_string = ''
remaining = string.gsub(/(.*?)(\b#{Regexp.union(*colors.keys)}\b)/m) do
prefix = $1
key = $2
span_string += escapeHTML(prefix)
span_string += %Q|<span style="color:#{escapeHTML(colors[key])}">#{escapeHTML(key)}</span>|
''
end
span_string += escapeHTML(remaining)
else
span_string = escapeHTML(string)
end
'<pre>' + span_string.gsub(/^\t+/){|t| tabstop * t.size}.to_utf8 + '</pre>'
end
if __FILE__ == $0
require 'test/unit'
require 'cgi'
require 'nkf'
def escapeHTML(string)
CGI::escapeHTML(string)
end
class String
def to_utf8
NKF::nkf('-m0 -w', self)
end
end
class TestPreColorKeywords < Test::Unit::TestCase
def test_simple
assert_equal('<pre>test</pre>', pre_color_keywords('test'))
end
def test_escape
assert_equal('<pre><test></pre>', pre_color_keywords('<test>'))
end
def test_red_pill
assert_equal('<pre><span style="color:red">red</span> pill</pre>',
pre_color_keywords('red pill', {'red' => 'red'}))
end
def test_take_the_red_pill
assert_equal('<pre>Take the <span style="color:red">red</span> pill</pre>',
pre_color_keywords('Take the red pill', {'red' => 'red'}))
end
def test_pill_makes_ill
assert_equal('<pre>pill makes <span style="color:purple">ill</span>.</pre>', pre_color_keywords('pill makes ill.', {'ill' => 'purple'}))
end
def test_color_escape
assert_equal('<pre><span style="color:"bad"">bad</span></pre>', pre_color_keywords('bad', {'bad' => '"bad"'}))
end
end
end