forked from WriterLighter/NML-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnml.coffee
More file actions
227 lines (205 loc) · 6.87 KB
/
nml.coffee
File metadata and controls
227 lines (205 loc) · 6.87 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
module.exports = class @NML
mode = 0
# mode
# 1:html
# 2:plain
multi_line = ""
multi_line_mode = 0
# mode
# 0:not multi line
# 1:html
# 2:plain
list_indent = -1
constructor: (@text) ->
to: (t) ->
switch t
when "html"
mode = 1
s = ''
body = @text.split('\n')
body.forEach (@text) ->
s = s + checkLine(@text) + '\n'
s = checkRemoveDiv('<section class="page"><div class="meaning-paragraph">' + s.substr( 0, s.length-1 ) + '</div></section>')
return s
when "plain"
mode = 2
s = ''
body = @text.split('\n')
body.forEach (@text) ->
s = s + checkLine(@text) + '\n'
s.substr( 0, s.length-1 )
return s
else
"Error!"
checkLine = (line) ->
line = checkSpace(line)
line = checkReturn(line)
line = checkRuby(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 = checkNextLine(line)
line = checkDefinitionList(list)
line
#形式段落
checkSpace = (line) ->
if line.match(/^[ \s]/)
if mode is 1
line = '<p>' + line.replace(/^[ \s]/,"") + '</p>'
line
#意味段落
checkReturn = (line) ->
if line is ''
if mode is 1
line = '</div><div class="meaning-paragraph">'
line
#ルビ
checkRuby = (line) ->
myRe = /[\||].*?[\((].*?[\))]/g
myArray = undefined
while (myArray = myRe.exec(line)) != null
text = myArray[0]
if mode is 1
moji = text.match(/[\||].*?[\((]/)[0]
moji = moji.substring(1, moji.length - 1)
ruby = text.match(/[\((].*?[\))]/)[0]
ruby = ruby.substring(1, ruby.length - 1)
line = line.replace(text, '<ruby>' + moji + '<rp>(</rp><rt>' + ruby + '</rt><rp>)</rp></ruby>')
else
moji = text.match(/[\||].*?[\((]/)[0]
moji = moji.substring(1, moji.length - 1)
ruby = text.match(/[\((].*?[\))]/)[0]
ruby = ruby.substring(1, ruby.length - 1)
line = line.replace(text, moji + "(" + ruby + ")")
line
#改ページ
checkNewPage = (line) ->
if line.match(/^[-ーis]{3,}$/)
if mode is 1
line = '</div></section><section class="page"><div class="meaning-paragraph">'
else if mode is 2
line = ''
line
#見出し
checkSharp = (line) ->
if mode is 1
md = undefined
if (md = line.match(/^[##]*/)[0]) != ''
count = if md.length > 6 then 6 else md.length
line = line.replace(/^[##]*/, '')
line = '<h' + count + '>' + line.replace(/^[ \s]/,"") + '</h' + count + '>'
else if mode is 2
line = line.replace(/^[##]*/, '')
line
#打ち消し線
checkStrikethrough = (line) ->
myRe = /[\~〜]{2}.*?[\~〜]{2}/g
myArray = undefined
while (myArray = myRe.exec(line)) != null
text = myArray[0]
if mode is 1
line = line.replace(text, '<s>' + text.substring(2, text.length - 2) + '</s>')
else if mode is 2
line = line.replace(text, text.substring(2, text.length - 2))
line
#斜体
checkItalic = (line) ->
myRe = /[\__**]{1}.*?[\__**]{1}/g
myArray = undefined
while (myArray = myRe.exec(line)) != null
text = myArray[0]
if mode is 1
if !text.match(/^[\__**]{2}$/)
line = line.replace(text, '<i>' + text.substring(1, text.length - 1) + '</i>')
else if mode is 2
if !text.match(/^[\__**]{2}$/)
line = line.replace(text, text.substring(1, text.length - 1))
line
#太字
checkBold = (line) ->
myRe = /[\__**]{2}.*?[\__**]{2}/g
myArray = undefined
while (myArray = myRe.exec(line)) != null
text = myArray[0]
if mode is 1
line = line.replace(text, '<b>' + text.substring(2, text.length - 2) + '</b>')
else if mode is 2
line = line.replace(text, text.substring(2, text.length - 2))
line
#引用
checkBlockquotes = (line) ->
if line.match(/^[>>]/)
if mode is 1
line = '<blockquote>' + line.substring(1) + '</blockquote>'
line
#リンク/画像
checkLink = (line) ->
myRe = /[!!]*\[.*?\][\((].*?[\))]/g
myArray = undefined
while (myArray = myRe.exec(line)) != null
text = myArray[0]
if text.match(/[!!]{1,}\[.*?\][\((].*?[\))]/)
if mode is 1
if text.match(/\".*\"/)
linkText = line.match(/\[.*?\]/)[0]
url = line.match(/[\((].*?[\"]/)[0].replace(RegExp(' ', 'g'), '')
name = line.match(/[\"].*?[\"]/)[0]
linkText = linkText.substring(1, linkText.length - 1)
url = url.substring(1, url.length - 1)
name = name.substring(1, name.length - 1)
line = line.replace(text, '<img src="' + url + '" alt="' + linkText + '" title="' + name + '"/>')
else
linkText = line.match(/\[.*?\]/)[0]
url = line.match(/[\((].*?[\))]/)[0].replace(RegExp(' ', 'g'), '')
linkText = linkText.substring(1, linkText.length - 1)
url = url.substring(1, url.length - 1)
line = line.replace(text, '<img src="' + url + '" alt="' + linkText + '"/>')
else if mode is 2
line = line.replace(text, "")
else
if mode is 1
if text.match(/\".*\"/)
linkText = line.match(/\[.*?\]/)[0]
url = line.match(/[\((].*?[\"]/)[0].replace(RegExp(' ', 'g'), '')
name = line.match(/[\"].*?[\"]/)[0]
linkText = linkText.substring(1, linkText.length - 1)
url = url.substring(1, url.length - 1)
name = name.substring(1, name.length - 1)
line = line.replace(text, '<a href="' + url + '" title="' + name + '">' + linkText + '</a>')
else
linkText = line.match(/\[.*?\]/)[0]
url = line.match(/[\((].*?[\))]/)[0].replace(RegExp(' ', 'g'), '')
linkText = linkText.substring(1, linkText.length - 1)
url = url.substring(1, url.length - 1)
line = line.replace(text, '<a href="' + url + '">' + linkText + '</a>')
else if mode is 2
line = line.replace(text, "")
line
#数字区切り
checkPunctuationNumber = (line) ->
if line.match(/^[0-9]+\.$/)
if mode is 1
line = '<hr class="number"/>'
line
#記号区切り
checkPunctuationSymbol = (line) ->
if line.match(/^[-*ー*]+\.$/)
if mode is 1
line = '<hr class="symbol"/>'
line
#改行
checkNextLine = (line) ->
if line.match(/[ \s]{3,}$/)
line = line + "<br/>"
line
#冗長なdivを削除
checkRemoveDiv = (line) ->
while (line.match(/<div class="format-paragraph"><\/div>/))
line = line.replace(/<div class="format-paragraph"><\/div>/, '')
line