Saving colorful JavaScript code samples
Introduction
There are times when a coder really needs a sample of code to be pretty for blogs, presentations, or to supplement other content. For these raw code in a text format is not good enough not matter how well beautified. Pretty Diff allows this feature by providing the jsscope parameter to JSPretty with the value html or by selecting the option HTML output under the option heading Scope Analysis (JavaScript Only) from the web tool. See multiple examples on one page or see the required JavaScript below for another example.
This feature works using the jsscope feature of the JSPretty library. The jsscope feature is intended to produce code that comprises an entire HTML document. The jsscope option is extended to allow immediate access for only enough HTML for formatting and description of the supplied code sample. As a result the necessary CSS and JavaScript code must be included separately.
Required code assets
JavaScript
- 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
- var pd = {};
- pd.beaufold = function dom__beaufold() {
- 'use strict';
- var self = this,
- title = self.getAttribute('title').split('line '),
- min = Number(title[1].substr(0, title[1].indexOf(' '))),
- max = Number(title[2]),
- a = 0,
- b = '',
- list = [
- self.parentNode.getElementsByTagName('li'), self.parentNode.nextSibling.getElementsByTagName('li')
- ];
- if (self.innerHTML.charAt(0) === '-') {
- for (a = min; a < max; a += 1) {
- list[0][a].style.display = 'none';
- list[1][a].style.display = 'none';
- }
- self.innerHTML = '+' + self.innerHTML.substr(1);
- } else {
- for (a = min; a < max; a += 1) {
- list[0][a].style.display = 'block';
- list[1][a].style.display = 'block';
- if (list[0][a].getAttribute('class') === 'fold' && list[0][a].innerHTML.charAt(0) === '+') {
- b = list[0][a].getAttribute('title');
- b = b.substring(b.indexOf('to line ') + 1);
- a = Number(b) - 1;
- }
- }
- self.innerHTML = '-' + self.innerHTML.substr(1);
- }
- };
- (function () {
- 'use strict';
- var lists = document.getElementsByTagName('ol'),
- listslen = lists.length,
- list = [],
- listlen = 0,
- a = 0,
- b = 0;
- for (a = 0; a < listslen; a += 1) {
- if (lists[a].getAttribute('class') === 'count' && lists[a].parentNode.getAttribute('class') === 'beautify') {
- list = lists[a].getElementsByTagName('li');
- listlen = list.length;
- for (b = 0; b < listlen; b += 1) {
- if (list[b].getAttribute('class') === 'fold') {
- list[b].onmousedown = pd.beaufold;
- }
- }
- }
- }
- }());