-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_tidy.rb
75 lines (63 loc) · 1.97 KB
/
html_tidy.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
#!/usr/bin/env ruby
# html_tidy.rb
# cleans up html files
EMPTY_STRING = ''
SIMPLE_TAG_REPLACEMENTS = {
#closers
/\<\/b\>/i => '</strong>',
/\<\/i\>/i => '</em>',
/\<\/strong\><\/td\>/i => '</th>',
/\<\/u\>/i => '</div>',
#openers
/\<b\>/i => '<strong>',
/\<i\>/i => '<em>',
/\<td\>\<strong\>/i => '<th>',
/\<u\>/i => '<div style="text-decoration: underline;">',
# again, more as appropriate
}
TIDY_EXTENSION = '.tidy'
TIDY_OPTIONS = '-asxml -bc' # possible add -access 3
UNWANTED_REGEXES = [
/^<meta name=\"GENERATOR\" content=\"Microsoft FrontPage 5.0\">$/,
/^ *$/,
/^\n$/,
# more as appropriate
]
def declare_regexes_and_replacements()
replacement_of = Hash.new()
UNWANTED_REGEXES.each do |discard|
replacement_of[discard] = EMPTY_STRING
end
return replacement_of.merge(SIMPLE_TAG_REPLACEMENTS)
end
=begin rdoc
This lacks a ! suffix, because it duplicates the argument, and
returns the changes made to that duplicate, rather than overwriting.
=end
def perform_replacements_on_contents(contents)
output = contents.dup
replacement_of = declare_regexes_and_replacements()
replacement_of.keys.sort_by { |r| r.to_s }.each do |regex|
replace = replacement_of[regex]
output.each { |line| line.gsub!(regex, replace) }
end
return output
end
=begin rdoc
This has the ! suffix, because it destructively writes
into the filename argument provided.
=end
def perform_replacements_on_filename!(filename)
if (system('which tidy > /dev/null'))
new_filename = filename + TIDY_EXTENSION
system("tidy #{TIDY_OPTIONS} #{filename} > #{new_filename} 2> /dev/null")
contents = File.open(new_filename, 'r').readlines()
new_contents = perform_replacements_on_contents(contents)
File.open(new_filename, 'w') { |f| f.puts(new_contents) }
else
puts "Please install tidy.\n"
end
end
ARGV.each do |filename|
perform_replacements_on_filename!(filename)
end