@@ -79,73 +79,6 @@ Syntax.extractElementMatches = function (elems, offset) {
79
79
return matches ;
80
80
}
81
81
82
- // Basic layout doesn't do anything e.g. identity layout.
83
- Syntax . layouts . preformatted = function ( options , html , container ) {
84
- return html ;
85
- } ;
86
-
87
-
88
- // This function converts from a compressed set of offsets of the form:
89
- // [
90
- // [offset, width, totalOffset],
91
- // ...
92
- // ]
93
- // This means that at a $offset, a tab (single character) was expanded to $width
94
- // single space characters.
95
- // This function produces a lookup table of offsets, where a given character offset
96
- // is mapped to how far the character has been offset.
97
- Syntax . convertToLinearOffsets = function ( offsets , length ) {
98
- var current = 0 , changes = [ ] ;
99
-
100
- // Anything with offset after offset[current][0] but smaller than offset[current+1][0]
101
- // has been shifted right by offset[current][2]
102
- for ( var i = 0 ; i < length ; i ++ ) {
103
- if ( offsets [ current ] && i > offsets [ current ] [ 0 ] ) {
104
- // Is there a next offset?
105
- if ( offsets [ current + 1 ] ) {
106
- // Is the index less than the start of the next offset?
107
- if ( i <= offsets [ current + 1 ] [ 0 ] ) {
108
- changes . push ( offsets [ current ] [ 2 ] ) ;
109
- } else {
110
- // If so, move to the next offset.
111
- current += 1 ;
112
- i -= 1 ;
113
- }
114
- } else {
115
- // If there is no next offset we assume this one to the end.
116
- changes . push ( offsets [ current ] [ 2 ] ) ;
117
- }
118
- } else {
119
- changes . push ( changes [ changes . length - 1 ] || 0 ) ;
120
- }
121
- }
122
-
123
- return changes ;
124
- }
125
-
126
- // Used for tab expansion process, by shifting matches when tab charaters were converted to
127
- // spaces.
128
- Syntax . updateMatchesWithOffsets = function ( matches , linearOffsets , text ) {
129
- ( function ( matches ) {
130
- for ( var i = 0 ; i < matches . length ; i ++ ) {
131
- var match = matches [ i ] ;
132
-
133
- // Calculate the new start and end points
134
- var offset = match . offset + linearOffsets [ match . offset ] ;
135
- var end = match . offset + match . length ;
136
- end += linearOffsets [ end ] ;
137
-
138
- // Start, Length, Text
139
- match . adjust ( linearOffsets [ match . offset ] , end - offset , text ) ;
140
-
141
- if ( match . children . length > 0 )
142
- arguments . callee ( match . children ) ;
143
- }
144
- } ) ( matches ) ;
145
-
146
- return matches ;
147
- } ;
148
-
149
82
// A helper function which automatically matches expressions with capture groups from the regular expression match.
150
83
// Each argument position corresponds to the same index regular expression group.
151
84
// Or, override by providing rule.index
@@ -314,6 +247,11 @@ Syntax.Match.prototype.reduce = function (append, process) {
314
247
container . className += this . expression . klass ;
315
248
}
316
249
250
+ if ( this . className ) {
251
+ container . className += ' ' ;
252
+ container . className += this . className ;
253
+ }
254
+
317
255
for ( var i = 0 ; i < this . children . length ; i += 1 ) {
318
256
var child = this . children [ i ] , end = child . offset ;
319
257
@@ -773,6 +711,31 @@ Syntax.Match.prototype.split = function(pattern) {
773
711
} ) ;
774
712
} ;
775
713
714
+ Syntax . Match . prototype . splitLines = function ( ) {
715
+ var lines = this . split ( / \n / g) ;
716
+
717
+ for ( var i = 0 ; i < lines . length ; i += 1 ) {
718
+ var line = lines [ i ] ;
719
+ var indentOffset = line . value . search ( / \S / ) ;
720
+
721
+ var top = new Syntax . Match ( line . offset , line . length , line . expression , line . value ) ;
722
+
723
+ if ( indentOffset > 0 ) {
724
+ var parts = line . bisectAtOffsets ( [ line . offset + indentOffset ] ) ;
725
+ top . children = parts ;
726
+ parts [ 0 ] . expression = { klass : 'indent' } ;
727
+ parts [ 1 ] . expression = { klass : 'text' } ;
728
+ } else {
729
+ line . expression = { klass : 'text' } ;
730
+ top . children = [ line ] ;
731
+ }
732
+
733
+ lines [ i ] = top ;
734
+ }
735
+
736
+ return lines ;
737
+ }
738
+
776
739
Syntax . Brush = function ( ) {
777
740
// The primary class of this brush. Must be unique.
778
741
this . klass = null ;
@@ -795,7 +758,7 @@ Syntax.Brush.prototype.derives = function (name) {
795
758
return Syntax . brushes [ name ] . getMatches ( text ) ;
796
759
}
797
760
} ) ;
798
- }
761
+ } ;
799
762
800
763
// Return an array of all classes that the brush consists of.
801
764
// A derivied brush is its own klass + the klass of any and all parents.
@@ -1003,7 +966,7 @@ Syntax.Brush.prototype.buildTree = function(text, offset, additionalMatches) {
1003
966
Syntax . Brush . prototype . process = function ( text , matches , options ) {
1004
967
var top = this . buildTree ( text , 0 , matches ) ;
1005
968
1006
- var lines = top . split ( / \n / g ) ;
969
+ var lines = top . splitLines ( ) ;
1007
970
1008
971
var html = document . createElement ( 'code' ) ;
1009
972
html . className = 'syntax' ;
@@ -1102,37 +1065,31 @@ Syntax.highlight = function (elements, options, callback) {
1102
1065
}
1103
1066
1104
1067
Syntax . highlightText ( text , brush , matches , options , function ( html , brush /*, text, options*/ ) {
1105
- Syntax . layouts . get ( options . layout , function ( layout ) {
1106
- if ( layout ) {
1107
- html = layout ( options , jQuery ( html ) , container ) ;
1108
- } else {
1109
- html = jQuery ( html ) ;
1110
- }
1111
-
1112
- // If there is a theme specified, ensure it is added to the top level class.
1113
- if ( options . theme ) {
1114
- // Load dependencies
1115
- var themes = Syntax . themes [ options . theme ] ;
1116
- for ( var i = 0 ; i < themes . length ; i += 1 ) {
1117
- html . addClass ( "syntax-theme-" + themes [ i ] ) ;
1118
- }
1068
+ html = jQuery ( html ) ;
1119
1069
1120
- // Add the base theme
1121
- html . addClass ( "syntax-theme-" + options . theme ) ;
1070
+ // If there is a theme specified, ensure it is added to the top level class.
1071
+ if ( options . theme ) {
1072
+ // Load dependencies
1073
+ var themes = Syntax . themes [ options . theme ] ;
1074
+ for ( var i = 0 ; i < themes . length ; i += 1 ) {
1075
+ html . addClass ( "syntax-theme-" + themes [ i ] ) ;
1122
1076
}
1123
1077
1124
- if ( brush . postprocess ) {
1125
- html = brush . postprocess ( options , html , container ) ;
1126
- }
1078
+ // Add the base theme
1079
+ html . addClass ( "syntax-theme-" + options . theme ) ;
1080
+ }
1127
1081
1128
- if ( callback ) {
1129
- html = callback ( options , html , container ) ;
1130
- }
1082
+ if ( brush . postprocess ) {
1083
+ html = brush . postprocess ( options , html , container ) ;
1084
+ }
1131
1085
1132
- if ( html && options . replace === true ) {
1133
- container . replaceWith ( html ) ;
1134
- }
1135
- } ) ;
1086
+ if ( callback ) {
1087
+ html = callback ( options , html , container ) ;
1088
+ }
1089
+
1090
+ if ( html && options . replace === true ) {
1091
+ container . replaceWith ( html ) ;
1092
+ }
1136
1093
} ) ;
1137
1094
} ) ;
1138
1095
} ;
0 commit comments