-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_help_filenames_loader.rb
78 lines (63 loc) · 2.87 KB
/
rename_help_filenames_loader.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
#!/usr/bin/env ruby
#require File.dirname(__FILE__) + '/../../config/environment.rb'
require "rubygems"
require 'csv'
# The following script takes an export of html help files and renames/updates them to a form that TransAM can use for contextual help links.
#
# Help documentation for TransAM is generated by a tool called HelpnDoc
# http://www.helpndoc.com/
# Documentation is exported as a collection of HTML files
# Remove all old existing files except TransamUserGuide.html in public/user_guide
# Put new export other than TransamUserGuide.html in public/user_guide
# If TransamUserGuide.html has been updated, merge changes.
# Update db/data/help_filenames.csv if necessary. This file maps between old filenames and new ones for interactive help links.
# The first column is old file name (without the .html) and the second column is the controller__action it maps to. Note the double underscore.
# Consider C(R)UD when updating help_filenames
# C - Add new lines in the csv if there are new sections (ie. new html files)
# U - Update lines with changed titles (ie. changed html filenames)
# D - Deleted section (ie. removed html files) remove from the csv
# Run script.
# Note: Github does not like files with the same name but with case changes.
class RenameHelpFilenamesLoader
HELP_FOLDER = 'user_guide'
HELP_FILENAMES = %w{help_filenames.csv}
def run
puts "---------------------------------------------"
puts "---------------------------------------------"
puts "Refactoring and renaming help documentation at #{Time.now}"
puts "---------------------------------------------"
puts "---------------------------------------------"
# Read the CSV file
HELP_FILENAMES.each do |filename|
counter = 0
failed = 0
CSV.foreach(filename, :headers => true) do |row|
old_filename = File.join(HELP_FOLDER, "#{row[0]}.html")
new_filename = File.join(HELP_FOLDER, "#{row[1]}.html")
if File.exists?(old_filename)
# rename the file
if File.exists?(new_filename) && new_filename.downcase != old_filename.downcase # new file already exists
File.delete(new_filename)
end
File.rename(old_filename, new_filename)
# rename all links to the old filename
Dir["#{HELP_FOLDER}/*.html"].each do |file_name|
if !File.directory? file_name
text = File.read(file_name)
new_contents = text.gsub('href="'+"#{row[0]}.html", 'href="'+"#{row[1]}.html")
File.open(file_name, "w") {|file| file.puts new_contents }
end
end
counter += 1
else
puts old_filename
failed += 1
end
end
puts "Renamed #{counter} filenames. #{failed} failed."
end
puts "Loading terminated at #{Time.now}"
puts "---------------------------------------------"
end
self
end.new.run