11$ ( function ( ) {
2+ // convert a string like "1,3-5,7" into an array [1,3,4,5,7]
23 function parseLines ( lines ) {
34 lines = lines . split ( / \s * , \s * / ) ;
45 var all_lines = [ ] ;
@@ -63,6 +64,8 @@ $(function () {
6364
6465 var getCodeLinesHtml = SyntaxHighlighter . Highlighter . prototype . getCodeLinesHtml ;
6566 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.
6669 html = html . replace ( / ^ / , " " ) ;
6770 html = html . replace ( / ^ \t / , "	" ) ;
6871 html = getCodeLinesHtml . call ( this , html , lineNumbers ) ;
@@ -74,9 +77,12 @@ $(function () {
7477 if ( source . length ) {
7578 var lineMatch ;
7679 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
7782 if ( source . html ( ) . length > 500000 ) {
7883 source . children ( 'code' ) . removeClass ( ) ;
7984 }
85+ // save highlighted lines in an attribute, to be used later
8086 else if ( lineMatch = document . location . hash . match ( hashLines ) ) {
8187 source . attr ( 'data-line' , lineMatch [ 1 ] ) ;
8288 }
@@ -99,7 +105,7 @@ $(function () {
99105 }
100106 }
101107
102- /* set perl as the default type in pod */
108+ // on pod pages, set the language to perl if no other language is set
103109 $ ( ".pod pre > code" ) . each ( function ( index , code ) {
104110 var have_lang ;
105111 if ( code . className && code . className . match ( / (?: \s | ^ ) l a n g u a g e - \S + / ) ) {
@@ -130,10 +136,12 @@ $(function () {
130136 if ( pre . hasClass ( 'line-numbers' ) ) {
131137 config . gutter = true ;
132138 }
139+ // starting line number can be provided by an attribute
133140 var first_line = pre . attr ( 'data-start' ) ;
134141 if ( first_line ) {
135142 config [ 'first-line' ] = first_line ;
136143 }
144+ // highlighted lines can be provided by an attribute
137145 var lines = pre . attr ( 'data-line' ) ;
138146 if ( lines ) {
139147 config . highlight = parseLines ( lines ) ;
@@ -148,6 +156,7 @@ $(function () {
148156 } ) ;
149157
150158 if ( source . length ) {
159+ // on the source page, make line numbers into links
151160 source . find ( '.syntaxhighlighter .gutter .line' ) . each ( function ( i , el ) {
152161 var line = $ ( el ) ;
153162 var res ;
@@ -157,6 +166,9 @@ $(function () {
157166 line . contents ( ) . wrap ( '<a href="#' + id + '" id="' + id + '"></a>' ) ;
158167 var link = line . children ( 'a' ) ;
159168 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
160172 e . preventDefault ( ) ;
161173 link . removeAttr ( 'id' ) ;
162174 document . location . hash = '#' + id ;
@@ -165,12 +177,16 @@ $(function () {
165177 }
166178 } ) ;
167179
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.
168183 var res ;
169184 if ( res = document . location . hash . match ( / ^ ( # L \d + ) ( - | , | $ ) / ) ) {
170185 var el = $ ( res [ 1 ] ) ;
171186 $ ( 'html, body' ) . scrollTop ( el . offset ( ) . top ) ;
172187 }
173188
189+ // if someone changes the url hash manually, update the highlighted lines
174190 $ ( window ) . on ( 'hashchange' , function ( ) {
175191 var lineMatch ;
176192 if ( lineMatch = document . location . hash . match ( hashLines ) ) {
0 commit comments