Skip to content

Commit

Permalink
Merge pull request #36 from colinc86/develop
Browse files Browse the repository at this point in the history
Add batch input methods.
  • Loading branch information
colinc86 authored Jul 19, 2023
2 parents 01797ab + 2eb1388 commit e23d6ea
Show file tree
Hide file tree
Showing 24 changed files with 953 additions and 239 deletions.
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
- [Installation](#📦-installation)
- [Usage](#🎛️-usage)
- [Available Methods](#🧰-available-methods)
- [Threading and Memory](#🧵-threading-and-memory)
- [Initializing and Converting](#initializing-and-converting)
- [Batch Converting](#batch-converting)
- [Threading](#threading)
- [Preferred Output Formats](#preferred-output-formats)
- [Options](#⚙️-options)
- [Document Options](#document-options)
Expand All @@ -32,7 +34,7 @@
Add the dependency to your package manifest file.

```swift
.package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.3.0")
.package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.4.0")
```

## 🎛️ Usage
Expand Down Expand Up @@ -69,7 +71,7 @@ MathJaxSwift implements the following methods to convert [TeX](https://tug.org),
| `am2chtml` | AsciiMath | cHTML |
| `am2mml` | AsciiMath | MathML |

### 🧵 Threading and Memory
#### Initializing and Converting

Initializing an instance of `MathJax` should not be performed on the main queue to prevent blocking of the UI. You should also attempt to keep a single reference to an instance and submit your function calls to it instead of creating a new `MathJax` instance each time you need to convert.

Expand All @@ -89,6 +91,36 @@ class MyModel {
}
```

#### Batch Converting

You can submit more than a single input string for conversion.

```swift
do {
// Some input array of TeX strings
let input: [String] = [ ... ]

// Convert each string in the input array
let responses = try mathjax.tex2svg(input)

for response in responses {
if let error = response.error {
print("Error converting input value: \(error)")
}
else {
print("Got response value: \(response.value)")
}
}
}
catch {
print("MathJax error: \(error)")
}
```

The `MathJax` instance will return an array of `Response` types with errors parsed from the response's `value` and set on the `error` property.

#### Threading

Each of the methods are also available with an `async` implementation.

```swift
Expand Down Expand Up @@ -260,7 +292,7 @@ To use the class you could do something like:

```swift
let renderer = try EquationRenderer()
let svg = try await renderer.render("$\\text{Hello, }\\TeX$!")
let svg = try await renderer.convert("$\\text{Hello, }\\TeX$!")
```

## 📘 Documentation
Expand Down
108 changes: 90 additions & 18 deletions Sources/MathJaxSwift/Conversions/AM/AM2CHTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,74 @@ import Foundation

extension MathJax {

/// Converts ASCIIMath input strings to CHTML.
///
/// - Parameters:
/// - input: The input strings containing ASCIIMath.
/// - css: Whether the document's CSS should be output.
/// - assistiveMml: Whether the include assistive MathML output.
/// - conversionOptions: The MathJax conversion options.
/// - documentOptions: The math document options.
/// - inputOptions: The ASCIIMath input processor options.
/// - outputOptions: The CHTML output processor options.
/// - queue: The queue to execute the conversion on.
/// - Returns: CHTML formatted output.
public func am2chtml(
_ input: [String],
css: Bool = false,
assistiveMml: Bool = false,
conversionOptions: ConversionOptions = ConversionOptions(),
documentOptions: DocumentOptions = DocumentOptions(),
inputOptions: AMInputProcessorOptions = AMInputProcessorOptions(),
outputOptions: CHTMLOutputProcessorOptions = CHTMLOutputProcessorOptions(),
queue: DispatchQueue = .global()
) async throws -> [Response] {
return try await perform(on: queue) { mathjax in
try mathjax.am2chtml(
input,
css: css,
assistiveMml: assistiveMml,
conversionOptions: conversionOptions,
documentOptions: documentOptions,
inputOptions: inputOptions,
outputOptions: outputOptions
)
}
}

/// Converts ASCIIMath input strings to CHTML.
///
/// - Parameters:
/// - input: The input strings containing ASCIIMath.
/// - css: Whether the document's CSS should be output.
/// - assistiveMml: Whether the include assistive MathML output.
/// - conversionOptions: The MathJax conversion options.
/// - documentOptions: The math document options.
/// - inputOptions: The ASCIIMath input processor options.
/// - outputOptions: The CHTML output processor options.
/// - Returns: CHTML formatted output.
public func am2chtml(
_ input: [String],
css: Bool = false,
assistiveMml: Bool = false,
conversionOptions: ConversionOptions = ConversionOptions(),
documentOptions: DocumentOptions = DocumentOptions(),
inputOptions: AMInputProcessorOptions = AMInputProcessorOptions(),
outputOptions: CHTMLOutputProcessorOptions = CHTMLOutputProcessorOptions()
) throws -> [Response] {
return try callFunctionAndValidate(
.am2chtml,
input: input,
arguments: [
css,
assistiveMml,
conversionOptions,
documentOptions,
inputOptions,
outputOptions
])
}

/// Converts an ASCIIMath input string to CHTML.
///
/// - Parameters:
Expand Down Expand Up @@ -82,15 +150,17 @@ extension MathJax {
inputOptions: AMInputProcessorOptions = AMInputProcessorOptions(),
outputOptions: CHTMLOutputProcessorOptions = CHTMLOutputProcessorOptions()
) throws -> String {
return try callFunction(.am2chtml, with: [
input,
css,
assistiveMml,
conversionOptions,
documentOptions,
inputOptions,
outputOptions
])
return try callFunctionAndValidate(
.am2chtml,
input: input,
arguments: [
css,
assistiveMml,
conversionOptions,
documentOptions,
inputOptions,
outputOptions
])
}

/// Converts an ASCIIMath input string to CHTML.
Expand All @@ -115,15 +185,17 @@ extension MathJax {
outputOptions: CHTMLOutputProcessorOptions = CHTMLOutputProcessorOptions(),
error: inout Error?
) -> String {
return callFunction(.am2chtml, with: [
input,
css,
assistiveMml,
conversionOptions,
documentOptions,
inputOptions,
outputOptions
], error: &error)
return callFunctionAndValidate(
.am2chtml,
input: input,
arguments: [
css,
assistiveMml,
conversionOptions,
documentOptions,
inputOptions,
outputOptions
], error: &error)
}

}
78 changes: 66 additions & 12 deletions Sources/MathJaxSwift/Conversions/AM/AM2MML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,56 @@ import Foundation

extension MathJax {

/// Converts ASCIIMath input strings to MathML.
///
/// - Parameters:
/// - input: The input strings containing ASCIIMath.
/// - conversionOptions: The MathJax conversion options.
/// - documentOptions: The math document options.
/// - inputOptions: The ASCIIMath input processor options.
/// - queue: The queue to execute the conversion on.
/// - Returns: MathML formatted output.
public func am2mml(
_ input: [String],
conversionOptions: ConversionOptions = ConversionOptions(),
documentOptions: DocumentOptions = DocumentOptions(),
inputOptions: AMInputProcessorOptions = AMInputProcessorOptions(),
queue: DispatchQueue = .global()
) async throws -> [Response] {
return try await perform(on: queue) { mathjax in
try mathjax.am2mml(
input,
conversionOptions: conversionOptions,
documentOptions: documentOptions,
inputOptions: inputOptions
)
}
}

/// Converts ASCIIMath input strings to MathML.
///
/// - Parameters:
/// - input: The input strings containing ASCIIMath.
/// - conversionOptions: The MathJax conversion options.
/// - documentOptions: The math document options.
/// - inputOptions: The ASCIIMath input processor options.
/// - Returns: MathML formatted output.
public func am2mml(
_ input: [String],
conversionOptions: ConversionOptions = ConversionOptions(),
documentOptions: DocumentOptions = DocumentOptions(),
inputOptions: AMInputProcessorOptions = AMInputProcessorOptions()
) throws -> [Response] {
return try callFunctionAndValidate(
.am2mml,
input: input,
arguments: [
conversionOptions,
documentOptions,
inputOptions
])
}

/// Converts an ASCIIMath input string to MathML.
///
/// - Parameters:
Expand Down Expand Up @@ -67,12 +117,14 @@ extension MathJax {
documentOptions: DocumentOptions = DocumentOptions(),
inputOptions: AMInputProcessorOptions = AMInputProcessorOptions()
) throws -> String {
return try callFunction(.am2mml, with: [
input,
conversionOptions,
documentOptions,
inputOptions
])
return try callFunctionAndValidate(
.am2mml,
input: input,
arguments: [
conversionOptions,
documentOptions,
inputOptions
])
}

/// Converts an ASCIIMath input string to MathML.
Expand All @@ -91,12 +143,14 @@ extension MathJax {
inputOptions: AMInputProcessorOptions = AMInputProcessorOptions(),
error: inout Error?
) -> String {
return callFunction(.am2mml, with: [
input,
conversionOptions,
documentOptions,
inputOptions
], error: &error)
return callFunctionAndValidate(
.am2mml,
input: input,
arguments: [
conversionOptions,
documentOptions,
inputOptions
], error: &error)
}

}
Loading

0 comments on commit e23d6ea

Please sign in to comment.