1 | (function() {
|
2 | var hl, mark_bold, mark_bold_italic, mark_italic, replace_links, replace_scripts;
|
3 | hl = require("highlight").Highlight;
|
4 | replace_scripts = function(l) {
|
5 | var matches, script_regex, scripts;
|
6 | script_regex = /\[script\]\(([\w|#!:.?+=&%@!\-\/]*)\)/gi;
|
7 | matches = l.match(script_regex);
|
8 | scripts = [];
|
9 | if (matches) {
|
10 | matches.each(function() {
|
11 | var m;
|
12 | m = script_regex.exec(l);
|
13 | return scripts.push({
|
14 | short_form: m[0],
|
15 | src: m[1]
|
16 | });
|
17 | });
|
18 | }
|
19 | scripts.each(function(s) {
|
20 | return l = l.replace(s.short_form, "<script src=\"" + s.src + "\" type=\"text/javascript\"></script>");
|
21 | });
|
22 | return l;
|
23 | };
|
24 | replace_links = function(l) {
|
25 | var links, links_regex, matches;
|
26 | links_regex = /\[([\w|\s|#!:.?+=&%@!\-\/]*)\]\(([\w|#!:.?+=&%@!\-\/]*) "([\w|\s|#!:.?+=&%@!\-\/]*)"\)/gi;
|
27 | matches = l.match(links_regex);
|
28 | links = [];
|
29 | if (matches) {
|
30 | matches.each(function() {
|
31 | var m;
|
32 | m = links_regex.exec(l);
|
33 | return links.push({
|
34 | short_form: m[0],
|
35 | href: m[2],
|
36 | title: m[3],
|
37 | element: m[1]
|
38 | });
|
39 | });
|
40 | }
|
41 | links.each(function(a) {
|
42 | return l = l.replace(a.short_form, "<a href=\"" + a.href + "\" title=\"" + a.title + "\">" + a.element + "</a>");
|
43 | });
|
44 | return l;
|
45 | };
|
46 | mark_bold_italic = function(l) {
|
47 | var i, tokens, _fn, _ref;
|
48 | tokens = l.match(/\*\*\*/g);
|
49 | if (tokens) {
|
50 | _fn = function() {
|
51 | var tag;
|
52 | tag = i % 2 ? '<b><i>' : '</i></b>';
|
53 | return l = l.replace('***', tag, 1);
|
54 | };
|
55 | for (i = 1, _ref = tokens.length; 1 <= _ref ? i <= _ref : i >= _ref; 1 <= _ref ? i++ : i--) {
|
56 | _fn();
|
57 | }
|
58 | }
|
59 | return l;
|
60 | };
|
61 | mark_bold = function(l) {
|
62 | var i, tokens, _fn, _ref;
|
63 | tokens = l.match(/\*\*/g);
|
64 | if (tokens) {
|
65 | _fn = function() {
|
66 | var tag;
|
67 | tag = i % 2 ? '<b>' : '</b>';
|
68 | return l = l.replace('**', tag, 1);
|
69 | };
|
70 | for (i = 1, _ref = tokens.length; 1 <= _ref ? i <= _ref : i >= _ref; 1 <= _ref ? i++ : i--) {
|
71 | _fn();
|
72 | }
|
73 | }
|
74 | return l;
|
75 | };
|
76 | mark_italic = function(l) {
|
77 | var i, tokens, _fn, _ref;
|
78 | tokens = l.match(/\*/g);
|
79 | if (tokens) {
|
80 | _fn = function() {
|
81 | var tag;
|
82 | tag = i % 2 ? '<i>' : '</i>';
|
83 | return l = l.replace('*', tag, 1);
|
84 | };
|
85 | for (i = 1, _ref = tokens.length; 1 <= _ref ? i <= _ref : i >= _ref; 1 <= _ref ? i++ : i--) {
|
86 | _fn();
|
87 | }
|
88 | }
|
89 | return l;
|
90 | };
|
91 | module.exports = {
|
92 | html: function(text) {
|
93 | var html, lines;
|
94 | lines = text.split('\n');
|
95 | html = "";
|
96 | lines.each(function(l) {
|
97 | if (l !== "") {
|
98 | l = mark_bold_italic(l);
|
99 | l = mark_bold(l);
|
100 | l = mark_italic(l);
|
101 | l = replace_links(l);
|
102 | l = replace_scripts(l);
|
103 | if (l.startsWith('#')) {
|
104 | if (l.endsWith('<code>')) {
|
105 | return html += "" + (l.substring(1));
|
106 | } else {
|
107 | return html += "" + (l.substring(1)) + "\n";
|
108 | }
|
109 | } else {
|
110 | return html += "<p>" + l + "</p>";
|
111 | }
|
112 | }
|
113 | });
|
114 | return html = hl(html, false, true);
|
115 | }
|
116 | };
|
117 | }).call(this);
|