-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdown.scala
More file actions
115 lines (84 loc) · 2.46 KB
/
Markdown.scala
File metadata and controls
115 lines (84 loc) · 2.46 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
import scala.io.Source
import scala.util.matching.Regex
class Markdown{
val tags = Map(
"<b>" -> "*",
"</b>" -> "*",
"<strong>" -> "*",
"</strong>" -> "*",
"<em>" -> "-",
"</em>" -> "-",
"<italic>" -> "-",
"</italic>" -> "-",
"<u>" -> "_",
"</u>" -> "_",
"<strike>" -> "~",
"</strike>" -> "~",
"<del>" -> "~",
"<del>" -> "~",
"<code>" -> "```",
"</code>" -> "```",
"<p>" -> "",
"</p>" ->"\n",
"<h1>" -> "#",
"</h1>" -> "\n",
"<h2>" -> "##",
"</h2>" -> "\n",
"<h3>" -> "###",
"</h3>" -> "\n",
"<h4>" -> "####",
"</h4>" -> "\n",
"<h5>" -> "#####",
"</h5>" -> "\n",
"<h6>" -> "######",
"</h6>" -> "\n",
"<blockquote>" -> ">",
"</blockquote>" -> "\n",
"<hr>" -> "---"
)
def getMarkdown(str: String): String= {
var input = str
input = this.getTag(input)
input = this.getEmail(input)
input = this.scrapUrl(input)
input = this.scrapAnchor(input)
return input
}
def getTag(str: String): String ={
var input = str
this.tags.keys.foreach{ key =>
input = (key.r replaceFirstIn(input, this.tags(key)))
}
return input
}
def getEmail(str: String): String= {
val regex = new Regex("([a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\\.[a-zA-Z]+)")
return regex.replaceFirstIn(str,"mailto:$1")
}
def getUrl(str: String): String ={
val regex = new Regex("((https?://)?(www.)[^\"<\\s]+)(?![^<>]*>|[^\"]*?<\\/a)")
return regex.replaceFirstIn(str,"$1")
}
def scrapAnchor(str: String): String ={
val regex = new Regex("<a href=\"(.+)\">(.+)</a>")
return regex.replaceFirstIn(str,"[$2]($1)")
}
def scrapUrl(str: String): String ={
val regex = new Regex("((https?://)?(www.)[^\"<\\s]+)(?![^<>]*>|[^\"]*?<\\/a)")
return regex.replaceFirstIn(str,"[$1]($1)")
}
}
object Test{
def main(args: Array[String]):Unit = {
val test = new Markdown()
var input = Source.fromFile("/home/test/Markdown.html").mkString
// var input= "<b>Bold</b><em>EM</em><strike>Strike</strike><code><strong>Hi</strong> I am <h1>code</h1> block</code>\n" +
// "rajk@gmail.com \n " +
// "<a href=\"www.youtube.com\">Youtube</a> \n" +
// "http://www.eventz.com"
println(test.getMarkdown(input))
// test.isEmail()
// test.scrapUrl("www.goodfgle.com")
//test.scrapAnchor("<a href=\"www.google.com\">goole</a>")
}
}