Class: Htmlize
Overview
Given information about two Ruby files, and their semantic diff, creates an HTML file to represent the diff information in an intuitive and appealing visual format.
Instance Attribute Summary collapse
-
#uid_count ⇒ Object
Returns the value of attribute uid_count.
-
#uid_hash ⇒ Object
Returns the value of attribute uid_hash.
Instance Method Summary collapse
-
#apply_tags(s, tags) ⇒ String
Does HTML escaping on both tags and text, and places the tags around the appropriate text.
-
#change_class(change) ⇒ String
Determines whether the change is an insertion, deletion or modification.
-
#change_tags(changes, side) ⇒ Array
Works through the Change objects in the diff, creating the appropriate HTML tags for each.
- #clear_uid ⇒ Object
-
#create_html(changes, file1, file2, text1, text2) ⇒ String
Takes in the information about the diff and writes out a file of HTML.
-
#html_footer ⇒ String
Create the html for the bottom of the page.
-
#html_header ⇒ String
Construct the HTML for the top of the file.
-
#initialize ⇒ Htmlize
constructor
A new instance of Htmlize.
-
#link_start(change, side) ⇒ String
Create anchor tags for a Change object.
-
#qs(s) ⇒ String
Takes a string and returns it in quotes.
-
#span_start(change) ⇒ String
Takes a Change and creates a span tag.
-
#uid(node) ⇒ Fixnum
Give the node a uid, place it in the uid hash and up the count.
-
#write_html(text, side) ⇒ String
Takes in the text and outputs html.
Methods included from Utils
get_install_path, inside_anchor?, node_end, node_start
Constructor Details
#initialize ⇒ Htmlize
Returns a new instance of Htmlize.
55 56 57 58 |
# File 'lib/smart_diff/htmlize.rb', line 55 def initialize @uid_count = -1 @uid_hash = {} end |
Instance Attribute Details
#uid_count ⇒ Object
Returns the value of attribute uid_count.
53 54 55 |
# File 'lib/smart_diff/htmlize.rb', line 53 def uid_count @uid_count end |
#uid_hash ⇒ Object
Returns the value of attribute uid_hash.
53 54 55 |
# File 'lib/smart_diff/htmlize.rb', line 53 def uid_hash @uid_hash end |
Instance Method Details
#apply_tags(s, tags) ⇒ String
Does HTML escaping on both tags and text, and places the tags around the appropriate text.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/smart_diff/htmlize.rb', line 182 def (s, ) = .sort_by { |x| [x.idx, -x.start] } curr = 0 out = "" .each do |t| while curr < t.idx && curr < s.length out << CGI::escapeHTML(s[curr]) curr += 1 end out << t.tag end while curr < s.length out << CGI::escapeHTML(s[curr]) curr += 1 end return out end |
#change_class(change) ⇒ String
Determines whether the change is an insertion, deletion or modification.
250 251 252 253 254 255 256 257 258 |
# File 'lib/smart_diff/htmlize.rb', line 250 def change_class(change) if !change.old_node return 'd' elsif !change.new_node return 'i' else return 'c' end end |
#change_tags(changes, side) ⇒ Array
Works through the Change objects in the diff, creating the appropriate HTML tags for each.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/smart_diff/htmlize.rb', line 209 def (changes, side) = [] changes.each do |c| key = side =~ /left/ ? c.old_node : c.new_node if key nd_start = node_start(key) nd_end = node_end(key) if c.old_node && c.new_node if inside_anchor?(, nd_start, nd_end) if change_class(c) =~ /c/ # no op # we don't nest anchors inside other anchors else # In this case, we have an insertion or deletion << Tag.new(span_start(c), nd_start) << Tag.new('</span>', nd_end, nd_start) end else # Link up the matching nodes with anchor tags << Tag.new(link_start(c, side), nd_start) << Tag.new('</a>', nd_end, nd_start) end else # Wrap a span around the insertion or deletion. << Tag.new(span_start(c), nd_start) << Tag.new('</span>', nd_end, nd_start) end end end return end |
#clear_uid ⇒ Object
60 61 62 63 |
# File 'lib/smart_diff/htmlize.rb', line 60 def clear_uid() @uid_count = -1 @uid_hash = {} end |
#create_html(changes, file1, file2, text1, text2) ⇒ String
Takes in the information about the diff and writes out a file of HTML.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/smart_diff/htmlize.rb', line 156 def create_html(changes, file1, file2, text1, text2) = (changes, 'left') = (changes, 'right') tagged_text1 = (text1, ) tagged_text2 = (text2, ) output_filename = "#{Pathname.new(file1).basename}-#{Pathname.new(file2).basename}.html" File.open(output_filename, "w") do |f| f.write html_header f.write write_html(tagged_text1, 'left') f.write write_html(tagged_text2, 'right') f.write end output_filename end |
#html_footer ⇒ String
Create the html for the bottom of the page.
115 116 117 118 |
# File 'lib/smart_diff/htmlize.rb', line 115 def out = %Q{</body>\n </html>\n} end |
#html_header ⇒ String
Construct the HTML for the top of the file.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/smart_diff/htmlize.rb', line 88 def html_header install_path = get_install_path js_filename = Pathname.new(install_path).join('web/nav.js') js_text = js_filename.read css_filename = Pathname.new(install_path).join('web/diff.css') css_text = css_filename.read out = %Q{<html>\n <head>\n <META http-equiv="Content-Type" content="text/html; charset=utf-8">\n <style>\n #{css_text} \n</style>\n <script type="text/javascript">\n #{js_text} \n</script>\n </head>\n <body>\n} end |
#link_start(change, side) ⇒ String
Create anchor tags for a Change object.
279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/smart_diff/htmlize.rb', line 279 def link_start(change, side) cls = change_class(change) if side == 'left' me, other = change.old_node, change.new_node else me, other = change.new_node, change.old_node end "<a id=#{qs(uid(me))} tid=#{qs(uid(other))} class=#{qs(cls)}>" end |
#qs(s) ⇒ String
Takes a string and returns it in quotes.
298 299 300 |
# File 'lib/smart_diff/htmlize.rb', line 298 def qs(s) "'#{s}'" end |
#span_start(change) ⇒ String
Takes a Change and creates a span tag.
267 268 269 |
# File 'lib/smart_diff/htmlize.rb', line 267 def span_start(change) "<span class=#{qs(change_class(change))}>" end |
#uid(node) ⇒ Fixnum
Give the node a uid, place it in the uid hash and up the count. If it already has one, fetch it from the hash and return it.
74 75 76 77 78 79 80 81 |
# File 'lib/smart_diff/htmlize.rb', line 74 def uid(node) if @uid_hash.has_key?(node) return @uid_hash[node] end @uid_count = @uid_count + 1 @uid_hash[node] = @uid_count.to_s end |
#write_html(text, side) ⇒ String
Takes in the text and outputs html.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/smart_diff/htmlize.rb', line 128 def write_html(text, side) out = "" out << '<div id="' + side + '" class="src">' out << '<pre>' if side == 'left' out << '<a id="leftstart" tid="rightstart"></a>' else out << '<a id="rightstart" tid="leftstart"></a>' end out << text out << '</pre>' out << '</div>' end |