-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMarkdown.scala
More file actions
369 lines (275 loc) · 9.1 KB
/
TestMarkdown.scala
File metadata and controls
369 lines (275 loc) · 9.1 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import java.io._
import scala.io.Source
import scala.util.matching.Regex
class TestMarkdown {
val HtmlCode = Map(
"!" -> "!",
"\"" -> """,
"#" -> "#",
"##" -> "##",
"###" -> "###",
"####" -> "#####",
"#####" -> "######",
"######" -> "#######",
"*" -> "*",
"**" -> "**",
"-" -> "-",
"----" -> "----",
">" -> ">",
"_" -> "_",
"`" -> "`",
"~" -> "~",
"[" -> "[",
"]" -> "]",
"(" -> "(",
")" -> ")"
)
def getMarkdown(str: String):String ={
var input = str
input = this.replaceMail(input)
input = this.replaceUrl(input)
input = this.replaceAnchor(input)
input = this.replaceCode(input)
input = this.replaceHeading(input)
input = this.replaceBold(input)
input = this.replaceBlockquote(input)
input = this.replaceItalic(input)
input = this.replaceParagraph(input)
input = this.replaceStrike(input)
input = this.replaceUnderline(input)
input = this.replaceHR(input)
input = this.replaceBR(input)
input = this.replaceImage(input)
input = this.replaceList(input)
input = this.replaceOptionalTags(input)
input
}
def getPlainText(str: String, regex: Regex, marker1: String, marker2: String, groupNo: Int): String = {
var input = str
regex.findAllIn(str).matchData.foreach((m) => {
val markedText = if(m.group(groupNo).trim().length != 0) {
s"$marker1${m.group(groupNo).trim()}$marker2"
} else {
""
}
input = input.replaceFirst(regex.toString(), markedText)
})
input
}
def setHtmlcode(str: String): String = {
var input = str
var regex1 = ""
}
def replaceBold(str: String): String = {
val regex = new Regex("<(b|strong)>(.*?)</\\1>")
val marker1 = HtmlCode("**")
val marker2 = HtmlCode("**")
this.getPlainText(str,regex,marker1,marker2,2)
}
def replaceItalic(str: String): String ={
var input = str
val regex = new Regex("<(em|italic)>(.*?)</\\1>")
val marker1 = HtmlCode("-")
val marker2 = HtmlCode("-")
this.getPlainText(str,regex,marker1,marker2,2)
}
def replaceUnderline(str: String): String ={
var input = str
val regex = new Regex("<(?:u)>(.*?)</(?:u)>")
val marker1 = HtmlCode("_")
val marker2 = HtmlCode("_")
this.getPlainText(str,regex,marker1,marker2,1)
}
def replaceStrike(str: String): String ={
var input = str
val regex = new Regex("<(del|strike)>(.*?)</\\1>")
val marker1 = HtmlCode("~")
val marker2 = HtmlCode("~")
this.getPlainText(str,regex,marker1,marker2,2)
}
def replaceCode(str: String): String ={
var input = str
val regex = new Regex("<code>(.*?)</code>")
val marker1 = HtmlCode("`")
val marker2 = HtmlCode("`")
this.getPlainText(str,regex,marker1,marker2,1)
}
def replaceBlockquote(str: String): String ={
var input = str
val regex = new Regex("(?s)<blockquote>(.*?)</blockquote>")
val marker1 = HtmlCode(">")
val marker2 = "\n"
this.getPlainText(str,regex,marker1,marker2,1)
}
def replaceParagraph(str: String): String ={
var input = str
val regex = new Regex("(?s)<p\\s?(?:.*?)>(.*?)</p>")
val marker1 = ""
val marker2 = "\n"
this.getPlainText(str,regex,marker1,marker2,1)
}
def replaceHeading(str: String): String ={
var input = str
val regex = new Regex("<(h[1-6])\\s?(?:.*?)>(.*?)</h[1-6]>")
regex.findAllIn(str).matchData.foreach(m=>{
if(m.group(2).length != 0) {
m.group(1) match {
case "h1" => input = input.replaceFirst(regex.toString(), s"${HtmlCode("#")} ${m.group(2)}\n")
case "h2" => input = input.replaceFirst(regex.toString(), s"${HtmlCode("##")} ${m.group(2)}\n")
case "h3" => input = input.replaceFirst(regex.toString(), s"${HtmlCode("###")} ${m.group(2)}\n")
case "h4" => input = input.replaceFirst(regex.toString(), s"${HtmlCode("####")} ${m.group(2)}\n")
case "h5" => input = input.replaceFirst(regex.toString(), s"${HtmlCode("#####")} ${m.group(2)}\n")
case "h6" => input = input.replaceFirst(regex.toString(), s"${HtmlCode("######")} ${m.group(2)}\n")
}
}else{
input = input.replaceFirst(regex.toString(), "")
}
})
input
}
def replaceHR(str: String): String ={
var input = str
var regex = new Regex("<hr(|\\s?/)>")
regex.replaceAllIn(input,HtmlCode("----"))
}
def replaceBR(str: String): String ={
var input = str
val regex = new Regex("<br(|\\s?/)>")
regex.replaceAllIn(input,"")
}
def replaceOptionalTags(str: String): String ={
var input = str
val regex = new Regex("<.*?>")
regex.replaceAllIn(input,"")
}
def getImgSrc(str: String): String ={
var input = str
val regex = new Regex(" src\\s?=\\s?[\"'](.*?)[\"']")
val src = if(!regex.findFirstMatchIn(input).isEmpty) {
regex.findFirstMatchIn(input).get.group(1)
}else {
""
}
src
}
def getImgAlt(str: String): String ={
var input = str
val regex = new Regex(" alt\\s?=\\s?[\"'](.*?)[\"']")
val alt = if(!regex.findFirstMatchIn(input).isEmpty) {
regex.findFirstMatchIn(input).get.group(1)
}else {
""
}
alt
}
def replaceImage(str: String): String ={
var input = str
val regex = new Regex("<img(.*?)\\s?/?>")
regex.findAllIn(input).matchData.foreach( (m) => {
if(m.group(1).length != 0) {
val syntax = s"${HtmlCode("!")}${HtmlCode("[")}${this.getImgAlt(m.group(1))}${HtmlCode("]")}${HtmlCode("(")}${this.getImgSrc(m.group(1))}${HtmlCode(")")}"
input = input.replaceFirst(regex.toString(), syntax)
}else{
input = input.replaceFirst(regex.toString(), "")
}
})
input
}
def getHref(str: String): String ={
var input = str
val regex = new Regex("href\\s?=\\s?[\"'](.*?)[\"']")
val href = if(!regex.findFirstMatchIn(input).isEmpty) {
regex.findFirstMatchIn(input).get.group(1)
}else {
""
}
href
}
def replaceAnchor(str: String): String ={
var input = str
val regex = new Regex("(?s)<a (.*?)\\s?>(.*?)</a>")
regex.findAllIn(input).matchData.foreach((m) => {
if(m.group(2).length != 0) {
val syntax = s"${HtmlCode("[")}${this.getHref(m.group(1))}${HtmlCode("]")}${HtmlCode("(")}${m.group(2)}${HtmlCode(")")}"
input = input.replaceFirst(regex.toString(), syntax)
}else{
input = input.replaceFirst(regex.toString(), "")
}
})
input
}
def replaceMail(str: String): String ={
var input = str
val regex = new Regex("([a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\\.[a-zA-Z]+)")
regex.findAllIn(input).matchData.foreach((m) => {
val syntax = s"${HtmlCode("[")}${m.group(0)}${HtmlCode("]")}${HtmlCode("(")}${m.group(0)}${HtmlCode(")")}"
input = input.replaceFirst(m.group(0), syntax)
})
input
}
def replaceUrl(str: String): String ={
var input = str
val regex = new Regex("((https?://)?(www.)[^\"<\\s]+)(?![^<>]*>|[^\"]*?<\\/a)")
regex.findAllIn(input).matchData.foreach( (m) => {
val syntax = s"${HtmlCode("[")}${m.group(0)}${HtmlCode("]")}${HtmlCode("(")}${m.group(0)}${HtmlCode(")")}"
input = input.replaceFirst(m.group(0), syntax)
})
input
}
def replaceList(str: String): String ={
var input = str
val regex = new Regex("(?s)<(u|o)l\\s?(?:.*?)>(.*?)</(?:u|o)l>")
regex.findAllIn(input).matchData.foreach((m) => {
if(m.group(2).length != 0) {
input = input.replaceFirst(regex.toString(),replaceLi(m.group(1),m.group(2))+"\n")
} else {
input = input.replaceFirst(regex.toString(), "")
}
})
input
}
def replaceLi(listType: String, str:String ): String ={
var input = str
var index = 0
val regex = new Regex("(?s)<li\\s?(?:.*?)>(.*?)</li>")
regex.findAllIn(input).matchData.foreach(m=>{
if(m.group(1).length != 0) {
if(listType.equals("u")){
input = input.replaceFirst(regex.toString(), s"${HtmlCode("*")} ${m.group(1)}\n")
}else if(listType.equals("o")){
index += 1
input = input.replaceFirst(regex.toString(), s"$index. ${m.group(1)}\n")
}
}else{
input = input.replaceFirst(regex.toString(), "")
}
})
input
}
def Dummy(str: String): String ={
var input = str
val regex = new Regex("")
regex.findAllIn(str).matchData.foreach(m=>{
if(m.group(1).length != 0) {
input = input.replaceFirst(regex.toString(), s">${m.group(1)}")
}else{
input = input.replaceFirst(regex.toString(), "")
}
})
input
}
}
object Test1{
def main(str:Array[String]): Unit ={
val testm = new TestMarkdown()
val input = Source.fromFile("/home/test/Desktop/testRegex.html").mkString
val writer = new PrintWriter(new File("/home/test/Desktop/result.html"))
writer.write(testm.getMarkdown(input))
writer.close()
print(testm.getMarkdown(input))
// scalaLMarkdown.html
// TestMark.html
// Markdown.html
// backstageMark.html
}
}