Skip to content

Commit 209e3bd

Browse files
committed
Merge branch 'develop'
2 parents ad8bb2a + c8a217b commit 209e3bd

File tree

7 files changed

+41
-91
lines changed

7 files changed

+41
-91
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
@@ -18,7 +18,6 @@ let package = Package(
1818
.package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.2.2"),
1919
.package(url: "https://github.com/exyte/SVGView", from: "1.0.4"),
2020
.package(url: "https://github.com/kean/Nuke", from: "11.3.1"),
21-
.package(url: "https://github.com/apple/swift-log", from: "1.4.4"),
2221
.package(url: "https://github.com/Kitura/swift-html-entities", from: "4.0.1")
2322
],
2423
targets: [
@@ -28,7 +27,6 @@ let package = Package(
2827
"MathJaxSwift",
2928
"SVGView",
3029
"Nuke",
31-
.product(name: "Logging", package: "swift-log"),
3230
.product(name: "HTMLEntities", package: "swift-html-entities")
3331
]),
3432
.testTarget(

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ A SwiftUI view that renders LaTeX.
2525
Add the dependency to your package manifest file.
2626

2727
```swift
28-
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.0.0")
28+
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.0.3")
2929
```
3030

3131
## ⌨️ Usage

Sources/LaTeXSwiftUI/LaTeX.swift

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,35 @@ extension LaTeX {
133133

134134
/// Preloads the view's components.
135135
///
136+
/// - Note: You must set this method's parameters the same as its environment
137+
/// or call it is ineffective and adds additional computational overhead.
138+
///
136139
/// - Returns: A LaTeX view whose components have been preloaded.
137-
@MainActor public func preload() -> LaTeX {
138-
switch blockMode {
139-
case .alwaysInline:
140-
blocksAsText(blocks, forceInline: true)
141-
case .blockText:
142-
blocksAsText(blocks)
143-
case .blockViews:
144-
blocksAsStack(blocks)
140+
@MainActor public func preload(
141+
unencodeHTML: Bool = false,
142+
parsingMode: ParsingMode = .onlyEquations,
143+
imageRenderingMode: Image.TemplateRenderingMode = .template,
144+
font: Font = .body,
145+
displayScale: CGFloat = 1.0,
146+
texOptions: TeXInputProcessorOptions = TeXInputProcessorOptions(loadPackages: TeXInputProcessorOptions.Packages.all)
147+
) -> LaTeX {
148+
// Render the blocks
149+
let preloadedBlocks = Renderer.shared.render(
150+
blocks: Parser.parse(unencodeHTML ? latex.htmlUnescape() : latex, mode: parsingMode),
151+
font: font,
152+
displayScale: displayScale,
153+
texOptions: texOptions)
154+
155+
// Render the images
156+
for block in preloadedBlocks {
157+
for component in block.components where component.type.isEquation {
158+
guard let svg = component.svg else { continue }
159+
_ = Renderer.shared.convertToImage(
160+
svg: svg,
161+
font: font,
162+
displayScale: displayScale,
163+
renderingMode: imageRenderingMode)
164+
}
145165
}
146166
return self
147167
}

Sources/LaTeXSwiftUI/Models/Logger.swift

Lines changed: 0 additions & 40 deletions
This file was deleted.

Sources/LaTeXSwiftUI/Models/Parser.swift

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal struct Parser {
1313
// MARK: Types
1414

1515
/// An equation component.
16-
private struct EquationComponent<T, U> {
16+
struct EquationComponent<T, U> {
1717
let regex: Regex<T>
1818
let terminatingRegex: Regex<U>
1919
let equation: Component.ComponentType
@@ -23,42 +23,42 @@ internal struct Parser {
2323
// MARK: Private properties
2424

2525
/// An inline equation component.
26-
private static let inline = EquationComponent(
26+
static let inline = EquationComponent(
2727
regex: #/\$(.|\s)*?\$/#,
2828
terminatingRegex: #/\$/#,
2929
equation: .inlineEquation,
3030
supportsRecursion: false)
3131

3232
/// An TeX-style block equation component.
33-
private static let tex = EquationComponent(
33+
static let tex = EquationComponent(
3434
regex: #/\$\$(.|\s)*?\$\$/#,
3535
terminatingRegex: #/\$\$/#,
3636
equation: .texEquation,
3737
supportsRecursion: false)
3838

3939
/// A block equation.
40-
private static let block = EquationComponent(
40+
static let block = EquationComponent(
4141
regex: #/\\\[(.|\s)*?\\\]/#,
4242
terminatingRegex: #/\\\]/#,
4343
equation: .blockEquation,
4444
supportsRecursion: false)
4545

4646
/// A named equation component.
47-
private static let named = EquationComponent(
48-
regex: #/\\begin{equation}(.|\s)*\\end{equation}/#,
47+
static let named = EquationComponent(
48+
regex: #/\\begin{equation}(.|\s)*?\\end{equation}/#,
4949
terminatingRegex: #/\\end{equation}/#,
5050
equation: .namedEquation,
5151
supportsRecursion: true)
5252

5353
/// A named no number equation component.
54-
private static let namedNoNumber = EquationComponent(
55-
regex: #/\\begin{equation\*}(.|\s)*\\end{equation\*}/#,
54+
static let namedNoNumber = EquationComponent(
55+
regex: #/\\begin{equation\*}(.|\s)*?\\end{equation\*}/#,
5656
terminatingRegex: #/\\end{equation\*}/#,
5757
equation: .namedNoNumberEquation,
5858
supportsRecursion: true)
5959

6060
// Order matters
61-
private static let allEquations: [EquationComponent] = [
61+
static let allEquations: [EquationComponent] = [
6262
inline,
6363
tex,
6464
block,
@@ -185,22 +185,3 @@ extension Parser {
185185
}
186186

187187
}
188-
189-
// MARK: Private static methods
190-
191-
extension Parser {
192-
193-
/// Determines if an index is smaller than all of the indexes in another
194-
/// array.
195-
///
196-
/// - Parameters:
197-
/// - index: The index to compare.
198-
/// - indexes: The indexes. The value `index` should not be present in this.
199-
/// - Returns: A boolean.
200-
private static func isSmallest(_ index: String.Index?, outOf indexes: [String.Index?]) -> Bool {
201-
guard let index = index else { return false }
202-
let indexes = indexes.filter({ $0 != nil }).map({ $0! }) as! [String.Index]
203-
return indexes.first(where: { $0 < index }) == nil
204-
}
205-
206-
}

Sources/LaTeXSwiftUI/Models/Renderer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal class Renderer {
9191
dataCache = try DataCache(name: "mathJaxRenderDataCache")
9292
}
9393
catch {
94-
logError("Error creating DataCache instance: \(error)")
94+
NSLog("Error creating DataCache instance: \(error)")
9595
dataCache = nil
9696
}
9797

@@ -101,7 +101,7 @@ internal class Renderer {
101101
mathjax = try MathJax(preferredOutputFormat: .svg)
102102
}
103103
catch {
104-
logError("Error creating MathJax instance: \(error)")
104+
NSLog("Error creating MathJax instance: \(error)")
105105
mathjax = nil
106106
}
107107
}
@@ -139,7 +139,7 @@ extension Renderer {
139139
newBlocks.append(ComponentBlock(components: newComponents))
140140
}
141141
catch {
142-
logError("Error rendering block: \(error)")
142+
NSLog("Error rendering block: \(error)")
143143
newBlocks.append(block)
144144
continue
145145
}

0 commit comments

Comments
 (0)