UNPKG

2.06 kBtext/coffeescriptView Raw
1hl = require("highlight").Highlight
2
3replace_scripts = (l) ->
4 script_regex = /\[script\]\(([\w|#!:.?+=&%@!\-\/]*)\)/gi
5 matches = l.match(script_regex)
6
7 scripts = []
8 if matches
9 matches.each ->
10 m = script_regex.exec(l)
11 scripts.push
12 short_form: m[0]
13 src: m[1]
14
15 scripts.each (s) ->
16 l = l.replace(s.short_form, """<script src="#{s.src}" type="text/javascript"></script>""")
17 l
18
19replace_links = (l) ->
20 links_regex = /\[([\w|\s|#!:.?+=&%@!\-\/]*)\]\(([\w|#!:.?+=&%@!\-\/]*) "([\w|\s|#!:.?+=&%@!\-\/]*)"\)/gi
21 matches = l.match(links_regex)
22
23 links = []
24 if matches
25 matches.each ->
26 m = links_regex.exec(l)
27 links.push
28 short_form: m[0]
29 href: m[2]
30 title: m[3]
31 element: m[1]
32
33 links.each (a) ->
34 l = l.replace(a.short_form, """<a href="#{a.href}" title="#{a.title}">#{a.element}</a>""")
35 l
36
37mark_bold_italic = (l) ->
38 tokens = l.match(/\*\*\*/g)
39 if tokens
40 for i in [1..(tokens.length)]
41 do ->
42 tag = if i % 2 then '<b><i>' else '</i></b>'
43 l = l.replace('***', tag, 1)
44 l
45
46
47mark_bold = (l) ->
48 tokens = l.match(/\*\*/g)
49 if tokens
50 for i in [1..(tokens.length)]
51 do ->
52 tag = if i % 2 then '<b>' else '</b>'
53 l = l.replace('**', tag, 1)
54 l
55
56
57mark_italic = (l) ->
58 tokens = l.match(/\*/g)
59 if tokens
60 for i in [1..(tokens.length)]
61 do ->
62 tag = if i % 2 then '<i>' else '</i>'
63 l = l.replace('*', tag, 1)
64 l
65
66
67module.exports =
68 html: (text) ->
69 lines = text.split('\n')
70
71 html = ""
72 lines.each (l) ->
73 unless l == ""
74 l = mark_bold_italic(l)
75 l = mark_bold(l)
76 l = mark_italic(l)
77
78 l = replace_links(l)
79 l = replace_scripts(l)
80
81 if l.startsWith('#')
82 if l.endsWith('<code>')
83 html += """#{l.substring(1)}"""
84 else
85 html += """#{l.substring(1)}\n"""
86 else
87 html += """<p>#{ l }</p>"""
88
89 html = hl(html, false, true)