forked from tammersaleh/tammersaleh.com
/
fix_posts.rb
executable file
·168 lines (140 loc) · 3.84 KB
/
fix_posts.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env ruby
require 'fileutils'
def debug?
ENV["DEBUG"]
end
def debug(line)
puts "debug: #{line}" if debug?
end
class String
def gsubcap!(*arg)
gsub!(*arg) { yield *$~.captures }
end
end
class Translator
def self.translate_line(line)
translations = {
%r{\s*\*\s+} => lambda {"- "},
%r{"([^"]+)":(\S+[?/A-Za-z0-9])} => lambda {|s,u| "[#{s}](#{u})" },
%r{^%<(.+)%} => lambda {|s| "![Image](#{s})\n{.img_left}" },
%r{^%>(.+)%} => lambda {|s| "![Image](#{s})\n{.img_right}" },
%r{^%(.+)%} => lambda {|s| "![Image](#{s})" },
%r{^h(\d)\.} => lambda {|d| "#" * d.to_i },
%r{(.?)\*([^*]+)\*(.?)} => lambda {|b,s,e| "#{b}**#{s}**#{e}" },
%r{(.?)_([^_]+)_(.?)} => lambda {|b,s,e| "#{b}*#{s}*#{e}" },
%r{(.?)@([^@]+)@(.?)} => lambda {|b,s,e| "#{b}`#{s}`#{e}" },
}
translations.each do |regex, lam|
debug(" Trying #{regex.inspect} on #{line.inspect}")
line.gsubcap!(regex, &lam)
end
return line
end
def initialize(contents)
@rest = normalize(contents)
@output = []
end
def normalize(input)
input.split("\n").map {|s| s.sub(/\r$/, "")}
end
def translate
until @rest.empty?
line = @rest.first
[Preamble, CodeBlock, BlockQuote].each do |klass|
debug "Trying #{klass} against #{line.inspect}"
if klass::START_REGEX.match(line)
debug "We have a match on #{line.inspect}"
section, @rest = klass.new(@rest).translate
@output.concat(section)
break
end
end
@output << Translator.translate_line(@rest.shift)
end
end
def output
@output.join("\n")
end
class Processor
def initialize(content)
@rest = content.to_a
@section = []
end
def process_line(line)
line
end
def process_first_line(line)
line
end
def process_last_line(line)
line
end
def single_line?
false
end
def translate
first_line = @rest.shift
unless first_line =~ self.class::START_REGEX
raise "#{first_line.inspect} does not match #{self.class::START_REGEX.inspect}"
end
if single_line?
debug " processing #{first_line}"
@section << process_line(first_line)
return @section, @rest
else
@section << process_first_line(first_line)
while line = @rest.shift
debug " processing #{line}"
if line =~ self.class::END_REGEX
@section << process_last_line(line)
debug "returning #{@section.inspect} and #{@rest.inspect}"
return @section, @rest
else
@section << process_line(line)
end
end
end
end
end
class BlockQuote < Processor
START_REGEX = /^<blockquote>\s*$/
END_REGEX = %r{^</blockquote>\s*$}
def process_line(line)
"> #{Translator.translate_line(line)}"
end
def process_first_line(line)
""
end
def process_last_line(line)
""
end
end
class CodeBlock < Processor
START_REGEX = /^@@@.*$/
END_REGEX = /^@@@\s*$/
def process_first_line(line)
line.sub("@@@", "~~~")
end
def process_last_line(line)
line.sub("@@@", "~~~")
end
end
class Preamble < Processor
START_REGEX = /^---\s*$/
END_REGEX = /^---\s*$/
end
end
files = ARGV
files.empty? && files = Dir["source/posts/*.textile"]
files.each do |path|
puts "Translating #{path}"
translator = Translator.new(File.read(path))
translator.translate
new_path = path.sub(".textile", ".markdown")
if debug?
puts translator.output
else
File.open(new_path, "w") { |file| file << translator.output }
FileUtils.rm(path)
end
end