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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
#!/usr/bin/env ruby
require 'optparse'
require 'pathname'
class LineProcessor
MATCHERS = {
:layout => /^layout:\s*(\S*)$/i,
:single_line_redirect => /^redirect_\w+:\s*\S+$/i,
:base_url => /\{\{\s*site\.baseurl\s*\}\}(\S+)/i,
:asset_url => /\{\{\s*site\.baseurl\s*\}\}[^assets]\S*/i
}
attr_reader :line, :options
def initialize(line, options = {})
@line = line
@options = options
end
def process
@line = use_locale_layout
@line = remove_single_line_redirects
@line = replace_site_baseurl
@line
end
def english?
options[:lang] == 'en'
end
private
def use_locale_layout
return line if english?
match = line.match(MATCHERS[:layout])
if match
"layout: #{ options[:lang] }/#{ match[1] }"
else
line
end
end
def remove_single_line_redirects
return line if english?
match = line.match(MATCHERS[:single_line_redirect])
if match
''
else
line
end
end
def replace_site_baseurl
line.gsub(MATCHERS[:base_url]) do |match|
new_match = match =~ MATCHERS[:asset_url]
if new_match
match.gsub(/\{\{\s?site.baseurl\s?\}\}/, '{{ page.baseurl }}/')
else
match
end
end
end
end
class UnsupportedMatchChecker
UNSUPPORTED_MATCHERS = {
:multi_line_redirect => /^redirect_\w+:(\s*-{1}\s*.*)+$/i,
:include_template => /\{\%\s*include/i
}
attr_reader :warnings
def initialize(file_contents, file_name = 'File')
@warnings = []
UNSUPPORTED_MATCHERS.each do |name, regex|
if file_contents =~ regex
@warnings << "WARNING: #{ file_name } contains #{ name } which cannot be processed"
end
end
end
end
options = {}
optionparser = OptionParser.new do |opts|
opts.banner = "Usage: #{ __FILE__ } [options] source_file"
opts.on("--force", "Ignore warnings and continue processing the source_file") do |f|
options[:force] = f
end
opts.on("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on("--in-place", "Write the processed text back to the source_file") do |p|
options[:in_place] = p
end
opts.on("-l", "--lang=LANG", "Two-letter language code of the source_file") do |s|
options[:lang] = s.downcase
end
opts.on("-o", "--output-file=FILE", "File to write the processed text to") do |o|
options[:output_file] = o
end
end
optionparser.parse!
unless ARGV.length == 1
puts optionparser.help
exit 1
end
source_file = ARGV.shift
if options[:in_place]
options[:output_file] = source_file
end
unless options[:force]
source_file_contents = File.read(source_file)
source_file_path = Pathname.new(source_file).realpath
warnings = UnsupportedMatchChecker.new(source_file_contents, source_file_path).warnings
if warnings.any?
puts warnings
puts "Use --force to ignore warnings and continue processing the file"
exit 1
end
end
new_contents = []
File.readlines(source_file).map do |line|
new_contents << LineProcessor.new(line, options).process
end
if options[:output_file]
File.open(options[:output_file], 'w') { |f| f.puts(new_contents) }
else
puts(new_contents)
end
|