Skip to content

Commit be34fcb

Browse files
authored
Merge pull request #15 from colinc86/develop
Develop
2 parents 383ce8f + 0a18295 commit be34fcb

File tree

6 files changed

+19
-46
lines changed

6 files changed

+19
-46
lines changed

Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ let package = Package(
1717
dependencies: [
1818
.package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.3.0"),
1919
.package(url: "https://github.com/exyte/SVGView", from: "1.0.4"),
20-
.package(url: "https://github.com/kean/Nuke", from: "12.1.0"),
2120
.package(url: "https://github.com/Kitura/swift-html-entities", from: "4.0.1")
2221
],
2322
targets: [
@@ -26,7 +25,6 @@ let package = Package(
2625
dependencies: [
2726
"MathJaxSwift",
2827
"SVGView",
29-
"Nuke",
3028
.product(name: "HTMLEntities", package: "swift-html-entities")
3129
]),
3230
.testTarget(

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ It won't
4747
Add the dependency to your package manifest file.
4848

4949
```swift
50-
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.2.0")
50+
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.2.1")
5151
```
5252

5353
## ⌨️ Usage
@@ -253,18 +253,14 @@ LaTeX(input)
253253

254254
`LaTeXSwiftUI` caches its SVG responses from MathJax and the images rendered as a result of the view's environment. If you want to control the cache, then you can access the static `dataCache` and `imageCache` properties.
255255

256-
The caches are managed automatically, but if, for example, you wanted to clear the cache manually you may do so.
257-
258256
```swift
259257
// Clear the SVG data cache.
260-
LaTeX.dataCache?.removeAll()
258+
LaTeX.dataCache.removeAllObjects()
261259

262260
// Clear the rendered image cache.
263-
LaTeX.imageCache.removeAll()
261+
LaTeX.imageCache.removeAllObjects()
264262
```
265263

266-
`LaTeXSwiftUI` uses the [caching](https://github.com/kean/Nuke/tree/master/Sources/Nuke/Caching) components of the [Nuke](https://github.com/kean/Nuke) package.
267-
268264
### 🏃‍♀️ Preloading
269265

270266
SVGs and images are rendered and cached on demand, but there may be situations where you want to preload the data so that there is minimal lag when the view appears.

Sources/LaTeXSwiftUI/LaTeX.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import HTMLEntities
2727
import MathJaxSwift
28-
import Nuke
2928
import SwiftUI
3029

3130
/// A view that can parse and render TeX and LaTeX equations that contain
@@ -104,14 +103,22 @@ public struct LaTeX: View {
104103
// MARK: Static properties
105104

106105
/// The package's shared data cache.
107-
public static var dataCache: DataCache? {
106+
public static var dataCache: NSCache<NSString, NSData> {
108107
Renderer.shared.dataCache
109108
}
110109

110+
#if os(macOS)
111111
/// The package's shared image cache.
112-
public static var imageCache: ImageCache {
112+
public static var imageCache: NSCache<NSString, NSImage> {
113113
Renderer.shared.imageCache
114114
}
115+
#else
116+
/// The package's shared image cache.
117+
public static var imageCache: NSCache<NSString, UIImage> {
118+
Renderer.shared.imageCache
119+
}
120+
#endif
121+
115122

116123
// MARK: Public properties
117124

Sources/LaTeXSwiftUI/Models/Parser.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,14 @@ extension Parser {
115115
/// - Parameter input: The input string.
116116
/// - Returns: An array of LaTeX components.
117117
static func parse(_ input: String) -> [Component] {
118-
print("parsing input \(input)")
119118
// Get the first match of each each equation type
120-
// let matches = allEquations.map({ ($0, input.firstMatch(of: $0.regex)) })
121119
let matchArrays = allEquations.map { equationComponent in
122120
let regexMatches = input.matches(of: equationComponent.regex)
123121
return regexMatches.map({ (equationComponent, $0) })
124122
}
125123

126124
let matches = matchArrays.reduce([], +)
127125

128-
for match in matches {
129-
let substring = input[match.1.range]
130-
print("match: \(substring)")
131-
}
132-
133126
// Filter the matches
134127
let filteredMatches = matches.filter { match in
135128
// We only want matches with ranges

Sources/LaTeXSwiftUI/Models/Renderer.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import CryptoKit
2727
import Foundation
2828
import MathJaxSwift
29-
import Nuke
3029
import SwiftUI
3130
import SVGView
3231

@@ -83,7 +82,6 @@ internal class Renderer {
8382
let svg: SVG
8483
let xHeight: CGFloat
8584
internal var fallbackKey: String { String(data: svg.data, encoding: .utf8) ?? "" }
86-
internal var nukeCacheKey: Nuke.ImageCacheKey { Nuke.ImageCacheKey(key: key()) }
8785
}
8886

8987
// MARK: Static properties
@@ -97,13 +95,13 @@ internal class Renderer {
9795
private let mathjax: MathJax?
9896

9997
/// The renderer's data cache.
100-
internal let dataCache: DataCache?
98+
internal let dataCache: NSCache<NSString, NSData> = NSCache()
10199

102100
/// Semaphore for thread-safe access to `dataCache`.
103101
internal let dataCacheSemaphore = DispatchSemaphore(value: 1)
104102

105103
/// The renderer's image cache.
106-
internal let imageCache: ImageCache
104+
internal let imageCache: NSCache<NSString, _Image> = NSCache()
107105

108106
/// Semaphore for thread-safe access to `imageCache`.
109107
internal let imageCacheSemaphore = DispatchSemaphore(value: 1)
@@ -112,16 +110,6 @@ internal class Renderer {
112110

113111
/// Initializes a renderer with a MathJax instance.
114112
init() {
115-
do {
116-
dataCache = try DataCache(name: "mathJaxRenderDataCache")
117-
}
118-
catch {
119-
NSLog("Error creating DataCache instance: \(error)")
120-
dataCache = nil
121-
}
122-
123-
imageCache = ImageCache()
124-
125113
do {
126114
mathjax = try MathJax(preferredOutputFormat: .svg)
127115
}
@@ -275,7 +263,7 @@ extension Renderer {
275263
private func dataCacheValue(for key: SVGCacheKey) -> Data? {
276264
dataCacheSemaphore.wait()
277265
defer { dataCacheSemaphore.signal() }
278-
return dataCache?[key.key()]
266+
return dataCache.object(forKey: key.key() as NSString) as Data?
279267
}
280268

281269
/// Safely sets the cache value.
@@ -285,7 +273,7 @@ extension Renderer {
285273
/// - key: The value's key.
286274
private func setDataCacheValue(_ value: Data, for key: SVGCacheKey) {
287275
dataCacheSemaphore.wait()
288-
dataCache?[key.key()] = value
276+
dataCache.setObject(value as NSData, forKey: key.key() as NSString)
289277
dataCacheSemaphore.signal()
290278
}
291279

@@ -296,7 +284,7 @@ extension Renderer {
296284
private func imageCacheValue(for key: ImageCacheKey) -> _Image? {
297285
imageCacheSemaphore.wait()
298286
defer { imageCacheSemaphore.signal() }
299-
return imageCache[key.nukeCacheKey]?.image
287+
return imageCache.object(forKey: key.key() as NSString)
300288
}
301289

302290
/// Safely sets the cache value.
@@ -306,7 +294,7 @@ extension Renderer {
306294
/// - key: The value's key.
307295
private func setImageCacheValue(_ value: _Image, for key: ImageCacheKey) {
308296
imageCacheSemaphore.wait()
309-
imageCache[key.nukeCacheKey] = ImageContainer(image: value)
297+
imageCache.setObject(value, forKey: key.key() as NSString)
310298
imageCacheSemaphore.signal()
311299
}
312300

0 commit comments

Comments
 (0)