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_html.rb
71 lines (70 loc) · 2.45 KB
/
attach_html.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
# -*- coding: utf-8 -*-
def attach_html(file_name, start_no=1, page=@page)
tabstop = ' ' * (@options['attach.tabstop'] ? @options['attach.tabstop'].to_i : 2)
file = "#{@cache_path}/attach/#{escape(page.untaint)}/#{escape(file_name.untaint)}"
code = File.read(file).to_utf8.gsub(/^\t+/) {|t| tabstop * t.size}.sub(/\n\z/,'')
s = ""
s << %Q!<pre>!
line = start_no !=- 1 ? start_no : 1
s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
code.scan(/(\{\{.+?\}\})|(<%.+?%>)|(<!.+?>)|(<.+?>)|(&[a-zA-Z\#0-9]+;?)|([^<{\n]+)|(\n)|./m) {|str|
m = $~
if m[1] # Web::Template
str = CGI::escapeHTML(m[0])
str.each_line {|x|
s << "<span class=\"html_webtemplate\">" << x << "</span>"
if x=~/\n\z/
line += 1
s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
end
}
elsif m[2] # ERb
str = CGI::escapeHTML(m[0])
str.each_line {|x|
s << "<span class=\"html_erb\">" << x << "</span>"
if x=~/\n\z/
line += 1
s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
end
}
elsif m[3] # comment
str = CGI::escapeHTML(m[0])
str.each_line {|x|
s << "<span class=\"html_comment\">" << x << "</span>"
if x=~/\n\z/
line += 1
s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
end
}
elsif m[4] # tag
str = CGI::escapeHTML(m[0])
tag_m = str.match(/<\/?([a-z0-9]+)/i)
if tag_m[1]=~/^(?:html|head|body|p|h[1-6]|ul|ol|pre|dl|div|blockquote|form|hr|table)/i
str.each_line {|x|
s << "<span class=\"html_tag_block\">" << x << "</span>"
if x=~/\n\z/
line += 1
s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
end
}
else
str.each_line {|x|
s << "<span class=\"html_tag\">" << x << "</span>"
if x=~/\n\z/
line += 1
s << "<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
end
}
end
elsif m[5] # charref
s << "<span class=\"html_charref\">" << CGI::escapeHTML(m[0]) << "</span>"
elsif m[6] # text
s << CGI::escapeHTML(m[0])
elsif m[7] # lf
line += 1
s << "\n<span class=\"html_linenumber\">%4d|</span>" % line if start_no != -1
end
}
s << %Q!</pre>!
s
end