SPB Git

Repository Public

DocumentIO.swift 3.4 KB 60 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocFormats/DocumentIO.swift3// Description: One entry point for every format of this module: import, export and the loss report shown before a lossy export.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import DocModel9import DocContainer1011public enum DocumentIO {12    /// Formats handled here for import.13    public static let importFormats: [DocFormat] = [.orixdoc, .markdown, .html, .rtf, .txt, .pdf]14    /// Formats handled here for export (PDF is produced by the layout engine, DOCX by DocDOCX).15    public static let exportFormats: [DocFormat] = [.orixdoc, .markdown, .html, .rtf, .txt]1617    /// Formats handled here: .orixdoc, .markdown, .html, .rtf, .txt, .pdf (import only). DOCX lives in DocDOCX.18    /// `resourceLoader` resolves relative image paths of Markdown and HTML sources; remote URLs are never fetched.19    public static func importDocument(_ data: Data, format: DocFormat, resourceLoader: ((String) -> Data?)? = nil) throws -> ImportResult {20        switch format {21        case .orixdoc: return try NativeContainer.read(data)22        case .markdown, .html:23            guard data.count <= FormatLimits.maxTextBytes else { throw FormatError.tooLarge("the file is larger than \(FormatLimits.maxTextBytes) bytes") }24            let decoded = TextDecoder.decode(data)25            var result = format == .markdown ? try MarkdownImporter.read(decoded.text, resourceLoader: resourceLoader)26                                             : try HTMLImporter.read(decoded.text, resourceLoader: resourceLoader)27            var encoding = WarningCollector()28            ImportFailure.reportEncoding(decoded, &encoding)29            result.warnings = encoding.warnings + result.warnings30            return result31        case .rtf: return try RTFImporter.read(data)32        case .txt: return try PlainText.read(data)33        case .pdf: return try PDFTextImporter.read(data)34        case .docx: throw FormatError.unreadable("DOCX documents are read by the DocDOCX module")35        }36    }3738    public static func export(_ doc: DocumentModel, format: DocFormat, options: ExportOptions = .init()) throws -> ExportResult {39        switch format {40        case .orixdoc: return ExportResult(data: try NativeContainer.write(doc))41        case .markdown: return MarkdownExporter.export(doc, options: options)42        case .html: return HTMLExporter.export(doc, options: options)43        case .rtf: return RTFExporter.export(doc, options: options)44        case .txt: return PlainText.export(doc, options: options)45        case .docx: throw FormatError.unreadable("DOCX documents are written by the DocDOCX module")46        case .pdf: throw FormatError.unreadable("PDF documents are produced by the layout engine (DocLayout); this module only imports PDF text")47        }48    }4950    /// What exporting `doc` to `format` would lose, computed from the document alone, BEFORE exporting.51    public static func preflight(_ doc: DocumentModel, format: DocFormat) -> [FormatWarning] {52        preflight(doc, format: format, options: ExportOptions())53    }5455    /// Same, taking the export options into account (a citation formatter or a MathML converter removes the matching warnings).56    public static func preflight(_ doc: DocumentModel, format: DocFormat, options: ExportOptions) -> [FormatWarning] {57        LossReport.warnings(for: doc, format: format, options: options)58    }59}60
Files main
Menu

Theme