/
format-post.rb
55 lines (48 loc) · 1.43 KB
/
format-post.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
output_lines = []
while line = gets
line.gsub! /\r/, ""
line.gsub! /\\n/, "\n"
line.gsub! /\\t/, ""
line.gsub! %r{http://www\.yes-no-cancel\.co\.uk(/\w)}, "\\1"
line.gsub! %r{/wp-content/uploads/}, "/static/"
line.gsub! /\[/, "\\["
line.gsub! /\]/, "\\]"
line.gsub! /<em>([^<]+)<\/em>/, "*\\1*"
line.gsub! /<i>([^<]+)<\/i>/, "*\\1*"
line.gsub! /<strong>([^<]+)<\/strong>/, "**\\1**"
line.gsub! /<b>([^<]+)<\/b>/, "**\\1**"
line.gsub! /<a href="([^"]+)">([^<]+)<\/a>/, "[\\2](\\1)"
output_line = ""
line.split(/ /).each do |word|
if output_line.length + word.length >= 100 || word =~ /<\w|^\[/
output_lines += output_line.split(/\n/)
output_line = word.dup
else
output_line << ' ' unless output_line.empty?
output_line << word
end
end
output_lines += output_line.split(/\n/)
end
output_lines << ''
starting_paragraph = true
paragraph_contains_html = false
first_line = nil
output_lines.each_with_index do |line, index|
line.strip!
if line.empty?
starting_paragraph = true
if paragraph_contains_html
unless output_lines[index - 1] =~ />$/
output_lines[first_line] = "<p>#{output_lines[first_line]}"
output_lines[index - 1] << "</p>"
end
paragraph_contains_html = false
end
else
first_line = index if starting_paragraph
paragraph_contains_html = true if line =~ /</
starting_paragraph = false
end
end
puts output_lines.join("\n")