-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.rb
192 lines (162 loc) · 4.53 KB
/
Main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
require 'sinatra'
require 'uri'
require 'cgi'
if development?
require 'sinatra/reloader'
Sinatra.register Sinatra::Reloader
end
post '/' do
s = ''
bodyText = request.body.read.force_encoding('UTF-8')
bodyText = bodyText.gsub(/(\r\n|\r|\n)/, "\n")
body = bodyText.split("\n")
body.each do |line|
s << checkLine(line) << "\n"
end
s = "<div class=\"page\"><div>#{s[0, s.length - 1]}</div></div>"
return CGI.pretty(s)
end
post '/html' do
bodyText = request.body.read.force_encoding('UTF-8')
if bodyText =~ /^zxcv=/
bodyText = bodyText[5, bodyText.length]
bodyText = URI.unescape(bodyText)
bodyText.tr!('+', ' ')
end
bodyText = bodyText.gsub(/(\r\n|\r|\n)/, "\n")
body = bodyText.split("\n")
s = ''
body.each do |line|
s << checkLine(line) << "\n"
end
s = "<html lang=\"ja\"><head><title>NNML</title></head><body><div class=\"page\"><div>#{s[0, s.length - 1]}</div></div></body></html>"
return CGI.pretty(s)
end
def checkLine(line)
line = checkSpace(line)
line = checkRuby(line)
line = checkReturn(line)
line = checkNewPage(line)
line = checkSharp(line)
line = checkStrikethrough(line)
line = checkItalic(line)
line = checkBold(line)
line = checkBlockquotes(line)
line = checkLink(line)
line = checkPunctuationNumber(line)
line = checkPunctuationSymbol(line)
line
end
# 形式段落
def checkSpace(line)
line = "<p>#{line}</p>" if line =~ /^[ \s]/
line
end
# 意味段落
def checkReturn(line)
line = '</div><div>' if line == ''
line
end
# ルビ
def checkRuby(line)
s = line.scan(/[\||].*?[\((].*?[\))]/)
s.each do |text|
moji = text[/[\||].*?[\((]/]
moji = moji[1, moji.length - 2]
ruby = text[/[\((].*?[\))]/]
ruby = ruby[1, ruby.length - 2]
line.gsub!(text, "<ruby>#{moji}<rt>#{ruby}</rt></ruby>")
end
line
end
# 改ページ
def checkNewPage(line)
line = '</div></div><div class="page"><div>' if line =~ /^[-ー==]{3,}$/
line
end
# 見出し
def checkSharp(line)
if (md = line.match(/^[##]*/).to_s) != ''
count = md.length > 6 ? 6 : md.length
line.sub!(/^[##]*/, '')
line = "<h#{count}>#{line}</h#{count}>"
end
line
end
# 打ち消し線
def checkStrikethrough(line)
s = line.scan(/[\~〜]{2}.*?[\~〜]{2}/)
s.each do |text|
line.gsub!(text, "<s>#{text[2, text.length - 4]}</s>")
end
line
end
# 斜体
def checkItalic(line)
s = line.scan(/[\__**]{1}.*?[\__**]{1}/)
s.each do |text|
line.gsub!(text, "<i>#{text[1, text.length - 2]}</i>") unless text == '__' || text == '__'
end
line
end
# 太字
def checkBold(line)
s = line.scan(/[\__**]{2}.*?[\__**]{2}/)
s.each do |text|
line.gsub!(text, "<b>#{text[2, text.length - 4]}</b>")
end
line
end
# 引用 (複数行にまたがるときの挙動が今一つ)
def checkBlockquotes(line)
if line =~ /^[>>]/
line = "<blockquote>#{line[1, line.length]}</blockquote>"
end
line
end
# リンク/画像
def checkLink(line)
s = line.scan(/[!!]*\[.*?\][\((].*?[\))]/)
s.each do |text|
if text =~ /[!!]{1,}\[.*?\][\((].*?[\))]/
if text =~ /\".*\"/
# ""に該当
linkText = line.match(/\[.*?\]/).to_s
url = line.match(/[\((].*?[\"]/).to_s.delete(' ')
name = line.match(/[\"].*?[\"]/).to_s
line.gsub!(text, "<img src=\"#{url[1, url.length - 2]}\" alt=\"#{linkText[1, linkText.length - 2]}\" title=\"#{name[1, name.length - 2]}\">")
else
linkText = line.match(/\[.*?\]/).to_s
url = line.match(/[\((].*?[\))]/).to_s.delete(' ')
line.gsub!(text, "<img src=\"#{url[1, url.length - 2]}\" alt=\"#{linkText[1, linkText.length - 2]}\" >")
end
else # 画像じゃないければ
if text =~ /\".*\"/
# ""に該当
linkText = line.match(/\[.*?\]/).to_s
url = line.match(/[\((].*?[\"]/).to_s.delete(' ')
name = line.match(/[\"].*?[\"]/).to_s
line.gsub!(text, "<a href=\"#{url[1, url.length - 2]}\" title=\"#{name[1, name.length - 2]}\">#{linkText[1, linkText.length - 2]}</a>")
else
linkText = line.match(/\[.*?\]/).to_s
url = line.match(/[\((].*?[\))]/).to_s.delete(' ')
line.gsub!(text, "<a href=\"#{url[1, url.length - 2]}\">#{linkText[1, linkText.length - 2]}</a>")
end
end
end
line
end
#数字区切り
def checkPunctuationNumber(line)
if line =~ /^[0-9]+\.$/
line = "<hr class=\"number\">"
end
line
end
#記号区切り
def checkPunctuationSymbol(line)
if line =~ /^[-*ー*]+\.$/
line = "<hr class=\"symbol\">"
end
line
end