/
page.js
258 lines (221 loc) · 10.3 KB
/
page.js
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
define([
'jquery',
'underscore',
'backbone',
'libs/marked/marked',
'text!templates/style/page.html',
'config',
'jscssp',
'pagedown',
'libs/highlight/highlight',
'libs/parseuri/parseuri',
'libs/waypoints/waypoints',
'libs/less/less-1.3.3.min'
],
function($, _, Backbone, marked, stylePageTemplate, config, jscssp, Pagedown, hljs, parseuri, waypoints){
var that = null;
var StylePage = Backbone.View.extend({
el: '.kalei-style-page',
render: function () {
that = this;
$('a.kalei-styleguide-menu-link').removeClass('active');
if(window.location.hash === '') {
$('.js-kalei-home').addClass('active');
} else {
$('[href="' + window.location.hash + '"]').addClass('active');
}
// $("head").find("link").attr("href", function (i, value) {
// if (value != config.css_path){
// $('head').append('<link rel="stylesheet" href="' + config.css_path + '"" type="text/css" />');
// }
// });
var styleUrl;
var configDir;
if(this.options.style === null) {
this.options.style = config.css_path.substr(config.css_path.lastIndexOf('/')+1);
}
if(this.options.style.substr(0,1) === '/') {
// non relative
configDir = config.css_path.substr(0, config.css_path.lastIndexOf('/'));
var pUrl = parseuri(configDir);
styleUrl = pUrl.protocol + '://' + pUrl.host + (pUrl.port === '' ? '' : ':'+ pUrl) + this.options.style;
} else {
configDir = config.css_path.substr(0, config.css_path.lastIndexOf('/'));
styleUrl = configDir + '/' + this.options.style;
}
console.log('try', configDir);
console.log('try', styleUrl);
require(['text!'+ styleUrl], function (stylesheet) {
var parser = null;
var regex = /(?:.*\/)(.*)\.(css|less|sass)$/gi;
var result = regex.exec(styleUrl);
//result[0] Original Input
//result[1] Filename
//result[2] Extension
var page = {blocks:[]};
switch (result[2]) {
case 'css':
parser = new jscssp();
stylesheet = parser.parse(stylesheet, false, true);
page = that.compute_css(stylesheet);
break;
case 'less':
parser = new(less.Parser)({
paths: [configDir + '/'], // Specify search paths for @import directives
});
parser.parse(stylesheet, function (err, tree) {
stylesheet = tree;
});
page = that.compute_less(stylesheet);
break;
case 'sass':
console.log("Error", "Unsupported style type.");
/*require(['sass'], function (sass){
//parse
});*/
break;
}
console.log((new Date()).getTime() + " bottom", page)
$('.kalei-sheet-submenu').slideUp(200);
var submenu = $('<ul>');
////////////NEEDS TO BE EXPORTED TO Menu.js
_.each(page.blocks, function (block) {
if (block.heading != "") {
submenu.append($('<li>').text(block.heading));
}
});
$('li:first-child', submenu).addClass('active');
$('.kalei-sheet-submenu', $('[data-sheet="' + that.options.style + '"]')).html(submenu).slideDown(200);
////////////NEEDS TO BE EXPORTED TO Menu.js
$(that.el).html(_.template(stylePageTemplate, {_:_, page: page, config: config}));
//Colour Coding in code Block
$(' code').each(function(i, e) {hljs.highlightBlock(e); });
$('.kalei-comments-container > .kalei-comments > h2, .kalei-comments-container > .kalei-comments > h1').waypoint(function(ev) {
$('.kalei-sheet-submenu li').removeClass('active');
$('.kalei-sheet-submenu li:contains('+$(ev.currentTarget).text()+')').addClass('active');
}, {
offset: 20 // middle of the page
});
$(".kalei-sheet-submenu li").on('click', function(ev) {
$('html, body').animate({
scrollTop: $(".kalei-comments h2:contains('"+$(ev.currentTarget).text()+"'),.kalei-comments h1:contains('"+$(ev.currentTarget).text()+"')").offset().top - 20
}, 200);
});
fixie.init();
});
},
compute_css: function(stylesheet) {
console.log("compute_css()")
var page = {
blocks:[],
css:"",
stylesheets: []
};
console.log(stylesheet)
_.each(stylesheet.cssRules, function(rule) {
switch (rule.type) {
case 1: //Standard rule?
break;
case 3: //Import Rule (@import)
//we need to import jsscp doesnt compile imports
stylesheet.deleteRule(rule);
break;
case 101: //Comment Block
page.blocks = page.blocks.concat(that.parse_commentblock(rule.parsedCssText))
break;
}
});
page.css = stylesheet.cssText()
var parser = new(less.Parser);
var stylesheet
page.css = ".kalei-style-page{" + page.css + "}"
parser.parse(page.css, function (err, tree) {
stylesheet = tree;
});
page.css = stylesheet.toCSS({ compress: true });
return page;
},
compute_less: function(stylesheet) {
console.log("compute_less()", stylesheet)
var page = {
blocks:[],
css:"",
stylesheets: []
};
_.each(stylesheet.rules, function(rule) {
if (rule.silent != null){ //Comment block
page.blocks = page.blocks.concat(that.parse_commentblock(rule.value))
//page.blocks.push();
} else if (rule.rules != null) { //Standard Rule
} else if (rule.path != null) { //Import Rule
// var previous_heading = page.blocks.length - 1;
// if (typeof page.blocks[previous_heading].import_rule == "undefined") {
// page.blocks[previous_heading].import_rule = []
// }
// page.blocks[previous_heading].import_rule.push(rule.path)
}
});
page.css = stylesheet.toCSS({ compress: true });
page.css = ".kalei-style-page{" + page.css + "}";
page.css = stylesheet.toCSS({ compress: true });
return page;
},
parse_commentblock: function (comment_block_text) {
//Remove /* & */
comment_block_text = comment_block_text.replace(/(?:\/\*)|(?:\*\/)/gi, '');
marked.setOptions(_.extend({ sanitize: false, gfm: true }, config.marked_options || {}));
var lexedCommentblock = marked.lexer(comment_block_text);
var lexerLinks = lexedCommentblock.links || {}; // lexer appends definition links to returned token object
var return_val = [];
var block_def = {
content: [],
heading: "",
};
var block = _.clone(block_def);
_.each(lexedCommentblock, function (comment) {
switch (comment.type) {
case "code":
//Push the code for an example
block.content.push({
type: 'html',
text: '<div class="codedemo">' + comment.text + '<div style="clear: both;"></div></div>'
});
//Push the code section so marked can parse it as a <pre><code> block
comment.text = comment.text.replace(/class=([""'])fixie\1|(?![""' ])fixie(?=[""' ])/g, "")
block.content.push(comment);
break;
case "heading":
if (block.heading != "") { //Multiple headings in one comment block
//We want to break them up
//Parse the content blocks and return the HTML to display
block.content.links = lexerLinks
block.content = marked.parser(block.content)
return_val.push(block);
block = _.clone(block_def);
}
if (comment.depth <= 2) {
block.heading = comment.text;
block.content.push(comment);
} else if (comment.depth == 3) { //Import statement title
block.stylesheet = comment.text;
//block.heading = "Stylesheets"
//this is an import statement
//if ($.inArray("Stylesheets", ))
//console.log("else", comment)
}
break;
default:
//Push everything else
block.content.push(comment);
break;
} //Switch
});
//Parse the content blocks and return the HTML to display
block.content.links = lexerLinks
block.content = marked.parser(block.content)
return_val.push(block);
return return_val;
},
});
return StylePage;
});