1
1
$ ( function ( ) {
2
+ // convert a string like "1,3-5,7" into an array [1,3,4,5,7]
2
3
function parseLines ( lines ) {
3
4
lines = lines . split ( / \s * , \s * / ) ;
4
5
var all_lines = [ ] ;
@@ -63,6 +64,8 @@ $(function () {
63
64
64
65
var getCodeLinesHtml = SyntaxHighlighter . Highlighter . prototype . getCodeLinesHtml ;
65
66
SyntaxHighlighter . Highlighter . prototype . getCodeLinesHtml = function ( html , lineNumbers ) {
67
+ // the syntax highlighter has a bug that strips spaces from the first line.
68
+ // replace any leading whitespace with an entity, preventing that.
66
69
html = html . replace ( / ^ / , " " ) ;
67
70
html = html . replace ( / ^ \t / , "	" ) ;
68
71
html = getCodeLinesHtml . call ( this , html , lineNumbers ) ;
@@ -74,9 +77,12 @@ $(function () {
74
77
if ( source . length ) {
75
78
var lineMatch ;
76
79
var packageMatch ;
80
+ // avoid highlighting excessively large blocks of code as they will take
81
+ // too long, causing browsers to lag and offer to kill the script
77
82
if ( source . html ( ) . length > 500000 ) {
78
83
source . children ( 'code' ) . removeClass ( ) ;
79
84
}
85
+ // save highlighted lines in an attribute, to be used later
80
86
else if ( lineMatch = document . location . hash . match ( hashLines ) ) {
81
87
source . attr ( 'data-line' , lineMatch [ 1 ] ) ;
82
88
}
@@ -99,7 +105,7 @@ $(function () {
99
105
}
100
106
}
101
107
102
- /* set perl as the default type in pod */
108
+ // on pod pages, set the language to perl if no other language is set
103
109
$ ( ".pod pre > code" ) . each ( function ( index , code ) {
104
110
var have_lang ;
105
111
if ( code . className && code . className . match ( / (?: \s | ^ ) l a n g u a g e - \S + / ) ) {
@@ -130,10 +136,12 @@ $(function () {
130
136
if ( pre . hasClass ( 'line-numbers' ) ) {
131
137
config . gutter = true ;
132
138
}
139
+ // starting line number can be provided by an attribute
133
140
var first_line = pre . attr ( 'data-start' ) ;
134
141
if ( first_line ) {
135
142
config [ 'first-line' ] = first_line ;
136
143
}
144
+ // highlighted lines can be provided by an attribute
137
145
var lines = pre . attr ( 'data-line' ) ;
138
146
if ( lines ) {
139
147
config . highlight = parseLines ( lines ) ;
@@ -148,6 +156,7 @@ $(function () {
148
156
} ) ;
149
157
150
158
if ( source . length ) {
159
+ // on the source page, make line numbers into links
151
160
source . find ( '.syntaxhighlighter .gutter .line' ) . each ( function ( i , el ) {
152
161
var line = $ ( el ) ;
153
162
var res ;
@@ -157,6 +166,9 @@ $(function () {
157
166
line . contents ( ) . wrap ( '<a href="#' + id + '" id="' + id + '"></a>' ) ;
158
167
var link = line . children ( 'a' ) ;
159
168
link . click ( function ( e ) {
169
+ // normally the browser would update the url and scroll to
170
+ // the the link. instead, update the hash ourselves, but
171
+ // unset the id first so it doesn't scroll
160
172
e . preventDefault ( ) ;
161
173
link . removeAttr ( 'id' ) ;
162
174
document . location . hash = '#' + id ;
@@ -165,12 +177,16 @@ $(function () {
165
177
}
166
178
} ) ;
167
179
180
+ // the line ids are added by javascript, so the browser won't have
181
+ // scrolled to it. also, highlight ranges don't correspond to exact
182
+ // ids. do the initial scroll ourselves.
168
183
var res ;
169
184
if ( res = document . location . hash . match ( / ^ ( # L \d + ) ( - | , | $ ) / ) ) {
170
185
var el = $ ( res [ 1 ] ) ;
171
186
$ ( 'html, body' ) . scrollTop ( el . offset ( ) . top ) ;
172
187
}
173
188
189
+ // if someone changes the url hash manually, update the highlighted lines
174
190
$ ( window ) . on ( 'hashchange' , function ( ) {
175
191
var lineMatch ;
176
192
if ( lineMatch = document . location . hash . match ( hashLines ) ) {
0 commit comments