forked from slowe/VirtualSky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatexamples.js
64 lines (47 loc) · 2.42 KB
/
formatexamples.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
// Update anchor now that we've updated the page
if(location.hash) document.location = location.hash;
var d = new Date(document.lastModified);
str = d.toUTCString().replace(/[^\,]+\, /,"").replace(/ [0-9]{2}\:[0-9]{2}\:[0-9]{2} [^\s]+/,"").replace(/^0/,"");
S('.lastupdated').html(str);
S('.lastupdatedyear').html(d.getUTCFullYear());
// Format examples nicely to show how they were done
var examples = S('.example-code');
function tidy(t){ return t.replace(/===NEWLINE===/g,"\n").replace(/\n*$/,"").replace(/^\n*/,""); }
function sanitise(t){ return t.replace(/\</g,"<").replace(/\>/g,">"); }
function deindent(t){ var indent = ""; t.replace(/^([\s\t]+)/,function(m,p1,p2){ indent = m; }); return t.replace(new RegExp(indent,'g'),"").replace(/[\s\t]+$/,""); }
function highlight(el){
var els = S(examples[el]).find('.prettyprint');
for(var i = 0; i < els.length; i++){
// Add styling to code blocks
hljs.highlightBlock(els[i]);
// Get the final markup
markup = S(els[i]).html()
// Make JSON files into links
markup = markup.replace(/([\"\'])([^\"\']*\.json)([\"\'])/g,function(m,p1,p2,p3){ return p1+'<a href="'+p2+'">'+p2+'</a>'+p3; });
// Add back to the document
S(els[i]).html(markup);
}
}
for(var i = 0; i < examples.length; i++){
html = examples[i].innerHTML;
css = "";
js = "";
json = "";
temp = html.replace(/\n/g,"===NEWLINE===").replace(/<!-- HTML -->/i,"");
temp = temp.replace(/<!-- Javascript -->/i,"").replace(/<script>(.*)<\/script>/,function(m,p1){
js = js+tidy(p1);
return "";
})
temp = temp.replace(/<!-- CSS -->/i,"").replace(/<style>(.*)<\/style>/,function(m,p1){
css = tidy(p1);
return "";
});
code = sanitise(tidy(temp));
code = code.replace(/(src|href)=\"([^\"]+)\"/g,function(m,p1,p2){ return p1+'="<a href="'+p2+'">'+p2+'</a>"'; });
showtitle = true;
if(S(examples[i]).attr('data-title')=="false") showtitle = false;
code = deindent(code);
// Append the 'How to do it' content
S(examples[i]).prepend((showtitle ? '<h3>How to do it</h3>':'')+(css ? (showtitle ? '<h4>CSS</h4>':'')+'<pre class="prettyprint lang-css">'+deindent(sanitise(css))+'</pre>':'')+(js ? (showtitle ? '<h4>Javascript</h4>':'')+'<pre class="prettyprint lang-js">'+deindent(sanitise(js))+'</pre>':'')+(code && showtitle ? '<h4>HTML</h4>':'')+(code ? '<pre class="prettyprint lang-html">'+deindent(code)+'</pre>':'')+'<h3>Result</h3>')
highlight(i);
}