Gemini/GeminiIntents/GemtextToHTMLIntentHandler....

31 lines
1.1 KiB
Swift
Raw Normal View History

2021-10-02 14:53:05 +00:00
//
// GemtextToHTMLIntentHandler.swift
// GeminiIntents
//
// Created by Shadowfacts on 10/1/21.
//
import Intents
import GeminiFormat
class GemtextToHTMLIntentHandler: NSObject, GemtextToHTMLIntentHandling {
func handle(intent: GemtextToHTMLIntent, completion: @escaping (GemtextToHTMLIntentResponse) -> Void) {
guard let response = intent.response,
let text = response.body,
let url = response.url else {
completion(GemtextToHTMLIntentResponse(code: .failure, userActivity: nil))
return
}
let doc = GeminiParser.parse(text: text, baseURL: url)
let renderer = GeminiHTMLRenderer()
renderer.addLinkClass = false
renderer.addHeadingLineIDs = false
let html = renderer.renderDocumentToHTML(doc)
let intentResp = GemtextToHTMLIntentResponse(code: .success, userActivity: nil)
intentResp.html = INFile(data: html.data(using: .utf8)!, filename: "converted_gemtext.html", typeIdentifier: "public.html")
completion(intentResp)
}
}