Skip to content

Commit 22cf48c

Browse files
committed
Fallback to highlightAuto if highlighting fails
Same as #96 but without the CSS changes. Some change in 2.2.0 made this issue resurface.
1 parent fa483d3 commit 22cf48c

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

Pod/Classes/Highlightr.swift

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ open class Highlightr
2424
themeChanged?(theme)
2525
}
2626
}
27-
27+
2828
/// This block will be called every time the theme changes.
2929
open var themeChanged : ((Theme) -> Void)?
3030

@@ -39,7 +39,7 @@ open class Highlightr
3939
private let spanStartClose = "\">"
4040
private let spanEnd = "/span>"
4141
private let htmlEscape = try! NSRegularExpression(pattern: "&#?[a-zA-Z0-9]+?;", options: .caseInsensitive)
42-
42+
4343
/**
4444
Default init method.
4545

@@ -62,25 +62,25 @@ open class Highlightr
6262
{
6363
return nil
6464
}
65-
65+
6666
let hgJs = try! String.init(contentsOfFile: hgPath)
6767
let value = jsContext.evaluateScript(hgJs)
6868
guard let hljs = jsContext.objectForKeyedSubscript("hljs") else { return nil }
6969

7070
self.hljs = hljs
71-
71+
7272
guard setTheme(to: "pojoaque") else
7373
{
7474
return nil
7575
}
76-
76+
7777
}
78-
78+
7979
/**
8080
Set the theme to use for highlighting.
81-
81+
8282
- parameter to: Theme name
83-
83+
8484
- returns: true if it was possible to set the given theme, false otherwise
8585
*/
8686
@discardableResult
@@ -93,27 +93,31 @@ open class Highlightr
9393
let themeString = try! String.init(contentsOfFile: defTheme)
9494
theme = Theme(themeString: themeString)
9595

96-
96+
9797
return true
9898
}
99-
99+
100100
/**
101101
Takes a String and returns a NSAttributedString with the given language highlighted.
102-
102+
103103
- parameter code: Code to highlight.
104104
- parameter languageName: Language name or alias. Set to `nil` to use auto detection.
105105
- parameter fastRender: Defaults to true - When *true* will use the custom made html parser rather than Apple's solution.
106-
106+
107107
- returns: NSAttributedString with the detected code highlighted.
108108
*/
109109
open func highlight(_ code: String, as languageName: String? = nil, fastRender: Bool = true) -> NSAttributedString?
110110
{
111111
let ret: JSValue?
112112
if let languageName = languageName
113113
{
114-
ret = hljs.invokeMethod("highlight", withArguments: [code, ["language": languageName, "ignoreIllegals": ignoreIllegals]])
115-
116-
114+
let result: JSValue = hljs.invokeMethod("highlight", withArguments: [languageName, code, ignoreIllegals])
115+
if result.isUndefined {
116+
// If highlighting failed, use highlightAuto
117+
ret = hljs.invokeMethod("highlightAuto", withArguments: [code])
118+
} else {
119+
ret = result
120+
}
117121
}else
118122
{
119123
// language auto detection
@@ -124,7 +128,7 @@ open class Highlightr
124128
{
125129
return nil
126130
}
127-
131+
128132
var returnString : NSAttributedString?
129133
if(fastRender)
130134
{
@@ -136,20 +140,20 @@ open class Highlightr
136140
.documentType: NSAttributedString.DocumentType.html,
137141
.characterEncoding: String.Encoding.utf8.rawValue
138142
]
139-
143+
140144
let data = string.data(using: String.Encoding.utf8)!
141145
safeMainSync
142146
{
143147
returnString = try? NSMutableAttributedString(data:data, options: opt, documentAttributes:nil)
144148
}
145149
}
146-
150+
147151
return returnString
148152
}
149-
153+
150154
/**
151155
Returns a list of all the available themes.
152-
156+
153157
- returns: Array of Strings
154158
*/
155159
open func availableThemes() -> [String]
@@ -159,21 +163,21 @@ open class Highlightr
159163
for path in paths {
160164
result.append(path.lastPathComponent.replacingOccurrences(of: ".min.css", with: ""))
161165
}
162-
166+
163167
return result
164168
}
165-
169+
166170
/**
167171
Returns a list of all supported languages.
168-
172+
169173
- returns: Array of Strings
170174
*/
171175
open func supportedLanguages() -> [String]
172176
{
173177
let res = hljs.invokeMethod("listLanguages", withArguments: [])
174178
return res!.toArray() as! [String]
175179
}
176-
180+
177181
/**
178182
Execute the provided block in the main thread synchronously.
179183
*/
@@ -187,15 +191,15 @@ open class Highlightr
187191
DispatchQueue.main.sync { block() }
188192
}
189193
}
190-
194+
191195
private func processHTMLString(_ string: String) -> NSAttributedString?
192196
{
193197
let scanner = Scanner(string: string)
194198
scanner.charactersToBeSkipped = nil
195199
var scannedString: NSString?
196200
let resultString = NSMutableAttributedString(string: "")
197201
var propStack = ["hljs"]
198-
202+
199203
while !scanner.isAtEnd
200204
{
201205
var ended = false
@@ -206,7 +210,7 @@ open class Highlightr
206210
ended = true
207211
}
208212
}
209-
213+
210214
if scannedString != nil && scannedString!.length > 0 {
211215
let attrScannedString = theme.applyStyleToString(scannedString! as String, styleList: propStack)
212216
resultString.append(attrScannedString)
@@ -215,9 +219,9 @@ open class Highlightr
215219
continue
216220
}
217221
}
218-
222+
219223
scanner.scanLocation += 1
220-
224+
221225
let string = scanner.string as NSString
222226
let nextChar = string.substring(with: NSMakeRange(scanner.scanLocation, 1))
223227
if(nextChar == "s")
@@ -237,10 +241,10 @@ open class Highlightr
237241
resultString.append(attrScannedString)
238242
scanner.scanLocation += 1
239243
}
240-
244+
241245
scannedString = nil
242246
}
243-
247+
244248
let results = htmlEscape.matches(in: resultString.string,
245249
options: [.reportCompletion],
246250
range: NSMakeRange(0, resultString.length))
@@ -254,11 +258,11 @@ open class Highlightr
254258
resultString.replaceCharacters(in: fixedRange, with: String(decodedEntity))
255259
locOffset += result.range.length-1;
256260
}
257-
261+
258262

259263
}
260264

261265
return resultString
262266
}
263-
267+
264268
}

0 commit comments

Comments
 (0)