SPB Git

Repository Public

AuditReport.swift 5.6 KB 89 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocAudit/AuditReport.swift3// Description: The audit report as a formatted ORIX document (title, summary table, findings by severity), ready for PDF export.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import DocModel910extension DocumentAuditor {11    /// A formatted report of `findings`, in the language of `doc` (French when its language starts with "fr", English otherwise).12    public static func reportDocument(_ findings: [AuditFinding], for doc: DocumentModel) -> DocumentModel {13        let french = AuditTexts.isFrench(doc.meta.language)14        func t(_ en: String, _ fr: String) -> String { french ? fr : en }15        func severityName(_ s: AuditFinding.Severity) -> String {16            switch s {17            case .error: return t("Errors", "Erreurs")18            case .warning: return t("Warnings", "Avertissements")19            case .info: return t("Information", "Information")20            }21        }22        let name = doc.meta.title.isEmpty ? t("Untitled document", "Document sans titre") : doc.meta.title23        var report = DocumentModel(meta: DocumentMetadata(title: t("Quality report", "Rapport de qualité") + " — " + name, author: doc.meta.author,24                                                          subject: t("Document audit", "Audit du document"), language: french ? "fr-CA" : "en"),25                                   pageSetup: PageSetup(paper: doc.pageSetup.paper))26        var body: [Block] = []27        body.append(.paragraph(Paragraph(t("Quality report", "Rapport de qualité"), style: StyleID.title)))28        body.append(.paragraph(Paragraph(name, style: StyleID.subtitle)))2930        body.append(.paragraph(Paragraph(t("Summary", "Sommaire"), style: StyleID.heading(1))))31        if findings.isEmpty {32            body.append(.paragraph(Paragraph(t("No finding: every enabled check passed.", "Aucun constat : toutes les vérifications activées sont réussies."))))33        } else {34            var rows: [[String]] = [[t("Check", "Vérification"), t("Errors", "Erreurs"), t("Warnings", "Avertissements"), t("Information", "Information")]]35            for check in AuditCheck.allCases {36                let mine = findings.filter { $0.check == check }37                guard !mine.isEmpty else { continue }38                func n(_ s: AuditFinding.Severity) -> String { let c = mine.filter { $0.severity == s }.count; return c == 0 ? "" : String(c) }39                rows.append([AuditTexts.text(check, french: french).title, n(.error), n(.warning), n(.info)])40            }41            func total(_ s: AuditFinding.Severity) -> String { String(findings.filter { $0.severity == s }.count) }42            rows.append([t("Total", "Total"), total(.error), total(.warning), total(.info)])43            let width = report.pageSetup.contentWidth44            var table = Table(strings: rows, width: width, style: .lined)45            table.columnWidths = [width * 0.55, width * 0.15, width * 0.15, width * 0.15]46            body.append(.table(table))47        }4849        let colours: [AuditFinding.Severity: RGBA] = [.error: RGBA(hex: 0xB42318), .warning: RGBA(hex: 0xB54708), .info: RGBA(hex: 0x175CD3)]50        let grey = RGBA(hex: 0x5B6170)51        for severity in [AuditFinding.Severity.error, .warning, .info] {52            let group = findings.filter { $0.severity == severity }53            guard !group.isEmpty else { continue }54            body.append(.paragraph(Paragraph("\(severityName(severity)) (\(group.count))", style: StyleID.heading(1))))55            for (n, f) in group.enumerated() {56                var head = Paragraph(style: StyleID.normal, props: ParaProps(spaceBefore: 10, spaceAfter: 2, keepWithNext: true))57                head.runs = [Run("\(n + 1). \(f.title)", props: CharProps(bold: true, color: colours[severity]))]58                var whereText: [String] = []59                if let l = f.location {60                    if let page = l.page { whereText.append(t("page \(page)", "page \(page)")) }61                    switch l.story {62                    case .body: if let path = l.path { whereText.append(t("block \(path.block + 1)", "bloc \(path.block + 1)")) }63                    case .header: whereText.append(t("header", "en-tête"))64                    case .footer: whereText.append(t("footer", "pied de page"))65                    case .note: whereText.append(t("note", "note"))66                    }67                    if l.path == nil && l.page == nil { whereText = [t("document", "document")] }68                }69                if !whereText.isEmpty { head.runs.append(Run("  " + whereText.joined(separator: ", "), props: CharProps(color: grey))) }70                body.append(.paragraph(head))71                func line(_ label: String, _ text: String, last: Bool = false) {72                    guard !text.isEmpty else { return }73                    var p = Paragraph(style: StyleID.normal, props: ParaProps(indentLeft: 18, spaceAfter: last ? 6 : 2))74                    p.runs = [Run(label + "  ", props: CharProps(bold: true, color: grey)), Run(text)]75                    body.append(.paragraph(p))76                }77                line(t("Evidence", "Constat"), f.evidence)78                line(t("Why it matters", "Pourquoi"), f.explanation)79                line(t("Suggestion", "Correction suggérée"), f.suggestion, last: true)80            }81        }82        report.body = body83        var footer = Paragraph(style: StyleID.footer, props: ParaProps(alignment: .center))84        footer.runs = [Run(inline: .field(.pageNumber)), Run(" / "), Run(inline: .field(.pageCount))]85        report.pageSetup.footer.default = [footer]86        return report87    }88}89
Files main
Menu

Theme