Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocReview/ComparisonReport.swift3// Description: Comparison reports: Markdown text and a formatted ORIX document (headings, summary table, change list) ready for PDF export.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import DocModel910extension DocumentChange.Kind {11 /// Display name used in reports.12 public var label: String {13 switch self {14 case .inserted: return "Inserted"15 case .deleted: return "Deleted"16 case .modified: return "Modified"17 case .moved: return "Moved"18 case .formatting: return "Formatting"19 case .style: return "Style"20 case .structure: return "Structure"21 }22 }23}2425extension ComparisonResult {26 private func location(_ c: DocumentChange) -> String {27 var where_: String28 switch (c.originalBlock, c.revisedBlock) {29 case let (o?, r?): where_ = o == r ? "block \(o + 1)" : "block \(o + 1) → \(r + 1)"30 case let (o?, nil): where_ = "original block \(o + 1)"31 case let (nil, r?): where_ = "revised block \(r + 1)"32 default: where_ = "document"33 }34 switch c.story {35 case .body: break36 case .header: where_ = "header, " + where_37 case .footer: where_ = "footer, " + where_38 case .note: where_ = "note, " + where_39 }40 return where_41 }4243 private var summaryRows: [[String]] {44 var rows: [[String]] = [["Measure", "Value"]]45 rows.append(["Changes", String(summary.totalChanges)])46 for k in DocumentChange.Kind.allCases where summary.count(k) > 0 { rows.append([k.label, String(summary.count(k))]) }47 rows.append(["Words inserted", String(summary.wordsInserted)])48 rows.append(["Words deleted", String(summary.wordsDeleted)])49 if summary.wordsMoved > 0 { rows.append(["Words moved", String(summary.wordsMoved)]) }50 rows.append(["Words (original → revised)", "\(summary.wordsOriginal) → \(summary.wordsRevised)"])51 rows.append(["Blocks (original → revised)", "\(summary.blocksOriginal) → \(summary.blocksRevised)"])52 rows.append(["Similarity", String(format: "%.1f %%", summary.similarity * 100)])53 return rows54 }5556 private static func escape(_ s: String) -> String {57 s.replacingOccurrences(of: "|", with: "\\|").replacingOccurrences(of: "\n", with: " ")58 }5960 /// A Markdown report: summary table, then every change with its location, detail and before/after excerpts.61 public func reportMarkdown(originalName: String, revisedName: String) -> String {62 var md = "# Comparison report\n\n"63 md += "- **Original:** \(originalName)\n- **Revised:** \(revisedName)\n\n"64 md += "## Summary\n\n"65 let rows = summaryRows66 md += "| \(rows[0][0]) | \(rows[0][1]) |\n|---|---|\n"67 for r in rows.dropFirst() { md += "| \(Self.escape(r[0])) | \(Self.escape(r[1])) |\n" }68 md += "\n## Changes\n\n"69 if changes.isEmpty { md += "The documents are identical.\n"; return md }70 for (n, c) in changes.enumerated() {71 md += "\(n + 1). **\(c.kind.label)** (\(location(c)))"72 if !c.detail.isEmpty { md += ": \(c.detail)" }73 md += "\n"74 if !c.before.isEmpty { md += " - Before: \(c.before.replacingOccurrences(of: "\n", with: " "))\n" }75 if !c.after.isEmpty && c.after != c.before { md += " - After: \(c.after.replacingOccurrences(of: "\n", with: " "))\n" }76 }77 return md78 }7980 /// The same report as a formatted document: title, summary table, one entry per change.81 public func reportDocument(originalName: String, revisedName: String) -> DocumentModel {82 var doc = DocumentModel(meta: DocumentMetadata(title: "Comparison report", author: merged.meta.author, subject: "\(originalName) → \(revisedName)",83 language: "en"),84 pageSetup: PageSetup(paper: merged.pageSetup.paper))85 var body: [Block] = []86 body.append(.paragraph(Paragraph("Comparison report", style: StyleID.title)))87 body.append(.paragraph(Paragraph("\(originalName) → \(revisedName)", style: StyleID.subtitle)))88 body.append(.paragraph(Paragraph("Summary", style: StyleID.heading(1))))89 body.append(.table(Table(strings: summaryRows, width: doc.pageSetup.contentWidth, style: .lined)))90 body.append(.paragraph(Paragraph("Changes", style: StyleID.heading(1))))91 if changes.isEmpty { body.append(.paragraph(Paragraph("The documents are identical."))) }92 let red = RGBA(hex: 0xB42318), green = RGBA(hex: 0x067647), grey = RGBA(hex: 0x5B6170)93 for (n, c) in changes.enumerated() {94 var head = Paragraph(style: StyleID.normal, props: ParaProps(spaceBefore: 8, spaceAfter: 2, keepWithNext: true))95 head.runs = [Run("\(n + 1). \(c.kind.label)", props: CharProps(bold: true)), Run(" \(location(c))", props: CharProps(color: grey))]96 body.append(.paragraph(head))97 if !c.detail.isEmpty { body.append(.paragraph(Paragraph(c.detail, props: ParaProps(indentLeft: 18, spaceAfter: 2)))) }98 if !c.before.isEmpty {99 var p = Paragraph(style: StyleID.normal, props: ParaProps(indentLeft: 18, spaceAfter: 2))100 p.runs = [Run("Before ", props: CharProps(bold: true, color: grey)), Run(c.before, props: CharProps(color: c.after == c.before ? nil : red))]101 body.append(.paragraph(p))102 }103 if !c.after.isEmpty && c.after != c.before {104 var p = Paragraph(style: StyleID.normal, props: ParaProps(indentLeft: 18, spaceAfter: 2))105 p.runs = [Run("After ", props: CharProps(bold: true, color: grey)), Run(c.after, props: CharProps(color: green))]106 body.append(.paragraph(p))107 }108 }109 doc.body = body110 var footer = Paragraph(style: StyleID.footer, props: ParaProps(alignment: .center))111 footer.runs = [Run(inline: .field(.pageNumber)), Run(" / "), Run(inline: .field(.pageCount))]112 doc.pageSetup.footer.default = [footer]113 return doc114 }115}116