larscorneliussen / beautyOfCode (http://startbigthinksmall.wordpress.com/2009/05/28/beautyofcode-jquery-plugin-for-syntax-highlighter-2-0-by-alex-gorbatchev/)
Provides a jQuery-like wrapper for the great syntaxhighlighter by Alex Gorbatchev.
Clone this repository (size: 24.3 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/larscorneliussen/beautyofcode/
| commit 0: | 181fe945d729 |
| branch: | default |
Initial version from http://startDbigthinksmall.wordpress.com/2008/10/30/beautyofcode-jquery-plugin-for-syntax-highlighting/. No samples yet.
10 months ago
Changed (Δ3.5 KB):
raw changeset »
Scripts/jquery.beautyOfCode.js (149 lines added, 0 lines removed)
Up to file-list Scripts/jquery.beautyOfCode.js:
1 |
jQuery.beautyOfCode = { |
|
2 |
initialized: false, |
|
3 |
||
4 |
settings: { |
|
5 |
||
6 |
// hide line numbers? |
|
7 |
noGutter: false, |
|
8 |
||
9 |
// show copy, plain, ... links |
|
10 |
addControls: true, |
|
11 |
||
12 |
// collapse to control bar. cant be used |
|
13 |
// with addControls set to false |
|
14 |
collapse: false, |
|
15 |
||
16 |
// show column numbers |
|
17 |
showColumns: false, |
|
18 |
||
19 |
// start with another line number? |
|
20 |
firstLine: 1 |
|
21 |
}, |
|
22 |
||
23 |
brushByAlias: {}, |
|
24 |
||
25 |
init: function (clipboardSwf, settings) { |
|
26 |
dp.SyntaxHighlighter.ClipboardSwf = clipboardSwf; |
|
27 |
||
28 |
if (settings) |
|
29 |
jQuery.extend(jQuery.beautyOfCode.settings, settings); |
|
30 |
||
31 |
if (jQuery.beautyOfCode.isInitialized) |
|
32 |
return; |
|
33 |
||
34 |
// creates a map of each registered brush by alias |
|
35 |
jQuery.each(dp.sh.Brushes, function (i, brush) { |
|
36 |
var aliases = brush.Aliases; |
|
37 |
||
38 |
if(aliases == null) |
|
39 |
return; |
|
40 |
||
41 |
jQuery.each(aliases, function (ii, alias) { |
|
42 |
jQuery.beautyOfCode.brushByAlias[alias] = brush; |
|
43 |
}); |
|
44 |
}); |
|
45 |
||
46 |
jQuery.beautyOfCode.isInitialized = true; |
|
47 |
}, |
|
48 |
||
49 |
addCssForBrush: function (brush, highlighter) { |
|
50 |
if (brush.isCssInitialized) |
|
51 |
return; |
|
52 |
||
53 |
var headNode = $("head")[0]; |
|
54 |
if(highlighter.Style && headNode) |
|
55 |
{ |
|
56 |
var styleNode = document.createElement('style'); |
|
57 |
styleNode.setAttribute('type', 'text/css'); |
|
58 |
||
59 |
if(styleNode.styleSheet) // for IE |
|
60 |
styleNode.styleSheet.cssText = highlighter.Style; |
|
61 |
else // for everyone else |
|
62 |
$(styleNode).text(highlighter.Style); |
|
63 |
||
64 |
headNode.appendChild(styleNode); |
|
65 |
} |
|
66 |
||
67 |
brush.isCssInitialized = true; |
|
68 |
}, |
|
69 |
||
70 |
beautifyAll: function() { |
|
71 |
jQuery("pre.code:has(code[class])").each(function (i, item) { |
|
72 |
||
73 |
function getOptionValue(name, list) |
|
74 |
{ |
|
75 |
var regex = new RegExp('^' + name + '\\[(\\w+)\\]$', 'gi'); |
|
76 |
var matches = null; |
|
77 |
||
78 |
for(var i = 0; i < list.length; i++) |
|
79 |
if((matches = regex.exec(list[i])) != null) |
|
80 |
return matches[1]; |
|
81 |
||
82 |
return null; |
|
83 |
} |
|
84 |
||
85 |
var $item = jQuery(item); |
|
86 |
var $code = $item.children("code"); |
|
87 |
var code = $code[0]; |
|
88 |
||
89 |
var options = code.className.split(" "); |
|
90 |
var language = options[0]; |
|
91 |
||
92 |
var settings = {}; |
|
93 |
||
94 |
if ($code.hasClass("boc-nogutter")) |
|
95 |
settings.noGutter = true; |
|
96 |
||
97 |
if ($code.hasClass("boc-nocontrols")) |
|
98 |
settings.addControls = false; |
|
99 |
||
100 |
if ($code.hasClass("boc-showcolumns")) |
|
101 |
settings.showColumns = true; |
|
102 |
||
103 |
if ($code.hasClass("boc-collapse")) |
|
104 |
settings.collapse = true; |
|
105 |
||
106 |
var firstLine = getOptionValue("boc-firstline", options, 1); |
|
107 |
if (firstLine) |
|
108 |
settings.firstLine = firstLine; |
|
109 |
||
110 |
$item.beautifyCode(language, settings); |
|
111 |
}); |
|
112 |
} |
|
113 |
}; |
|
114 |
||
115 |
jQuery.fn.beautifyCode = function (language, settings) { |
|
116 |
||
117 |
var saveLanguage = language; |
|
118 |
var saveSettings = settings; |
|
119 |
||
120 |
// iterate all elements |
|
121 |
this.each( function (i, item) { |
|
122 |
var $item = jQuery(item); |
|
123 |
||
124 |
var settings = jQuery.extend({}, jQuery.beautyOfCode.settings, saveSettings); |
|
125 |
||
126 |
var brush = jQuery.beautyOfCode.brushByAlias[saveLanguage]; |
|
127 |
||
128 |
if (!brush) |
|
129 |
return; |
|
130 |
||
131 |
// instantiate brush |
|
132 |
highlighter = new brush(); |
|
133 |
||
134 |
// set brush options |
|
135 |
jQuery.extend(highlighter, settings); |
|
136 |
||
137 |
jQuery.beautyOfCode.addCssForBrush(brush, highlighter); |
|
138 |
||
139 |
// IE Bug?: code in pre has to be skipped |
|
140 |
// in order to preserver line breaks. |
|
141 |
if ($item.is("pre") && ($code = $item.children("code"))) |
|
142 |
$item.text($code.text()); |
|
143 |
||
144 |
highlighter.Highlight($item.html()); |
|
145 |
highlighter.source = item; |
|
146 |
||
147 |
$item.replaceWith(highlighter.div); |
|
148 |
}); |
|
149 |
} |
