Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Sources/DocDOCX/DocxImportBody.swift3// Description: WordprocessingML import of content: stories (body, headers, footers, notes), paragraphs, runs, tracked changes,4// comment ranges, bookmarks, hyperlinks, fields, tables, sections, comments and notes.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import DocModel10import DocContainer1112/// How a field's result is treated.13enum DocxFieldKind {14 case pending15 /// The field becomes an inline object; its cached result is discarded.16 case replace(Inline)17 /// HYPERLINK: the result runs are kept and carry the link.18 case link(String)19 /// TOC: the cached entries are generated content, replaced by one block.20 case toc(TOCBlock)21 /// Table formula: the result stays as text, the instruction goes to the cell.22 case formula(String)23 /// Anything else: the cached result is kept as plain text.24 case keep2526 var suppressesResult: Bool {27 switch self {28 case .replace, .toc: return true29 default: return false30 }31 }32}3334struct DocxFieldState {35 var instruction = ""36 var inResult = false37 var kind: DocxFieldKind = .pending38 var props: DocxRunProps39 var state: DocxInlineState40 var token: Int41}4243struct DocxRunProps {44 var props: CharProps = .none45 var charStyle: String?46 var formatRevision: FormatRevision?47}4849struct DocxInlineState {50 var revision: Revision?51 var link: String?52}5354/// One text flow being imported (the body, a header, a note…), with the state that spans its paragraphs.55final class DocxStory {56 enum Kind { case body, header, footer, note, comment }57 let part: String58 let rels: [String: Relationship]59 let kind: Kind60 var activeComments: [String] = []61 var commentHits: [String: Int] = [:]62 var commentRangesSeen = Set<String>()63 var pendingComments: [String] = []64 var fields: [DocxFieldState] = []65 var openBookmarks: [String: (name: String, token: Int, runIndex: Int)] = [:]66 var cellFormula: String?67 var paragraphToken = 068 var tableDepth = 069 var watermark: String?7071 init(part: String, rels: [Relationship], kind: Kind) {72 self.part = part73 self.rels = Dictionary(rels.map { ($0.id, $0) }, uniquingKeysWith: { a, _ in a })74 self.kind = kind75 }7677 var suppressed: Bool { fields.contains { !$0.inResult || $0.kind.suppressesResult } }78 var inBlockField: Bool { fields.contains { if case .toc = $0.kind { return true } else { return false } } }79 var fieldLink: String? {80 for f in fields.reversed() { if case .link(let l) = f.kind, f.inResult { return l } }81 return nil82 }83}8485struct DocxParaBuilder {86 var runs: [Run] = []87 var breaks: [(at: Int, kind: BreakBlock.Kind)] = []88 var leading: [Block] = []89 var trailing: [Block] = []90 var assignedID: String?91 var dropIfEmpty = false92 var afterNoteRef = false93 var token = 094 /// Revisions already met in this paragraph: pieces of one edit (same kind, author and time) are one revision in the model,95 /// even when Word wrote them as several elements or when another change sits between them.96 var revisions: [Revision] = []97 var formatRevisions: [FormatRevision] = []98}99100extension DocxImportContext {101102 // MARK: Body and sections103104 func importBody(root: XMLNode, body: XMLNode) throws {105 collectBookmarks(body)106 let story = DocxStory(part: mainPart, rels: mainRels, kind: .body)107 var blocks = parseBlocks(body.children, story: story, allowTables: true)108 closeStory(story)109 // The hidden paragraph that only exists because a document cannot end with a table.110 if let last = blocks.last?.paragraph, tableSeparatorIDs.contains(last.id) { blocks.removeLast() }111112 // Word stores the properties of a section in its last paragraph; the model stores them on the break that starts the NEXT section.113 let rels = story.rels114 var results: [(setup: PageSetup, kind: SectionBreak.Kind)] = []115 var previous: PageSetup?116 for node in sectPrNodes.map(Optional.some) + [body.first("w:sectPr")] {117 let r = pageSetup(node, previous: previous, rels: rels)118 results.append(r)119 previous = r.setup120 }121 if body.first("w:sectPr") == nil, results.count > 1 {122 // No final section properties: the last section repeats the previous page setup.123 var last = results[results.count - 2].setup124 last.pageNumberStart = nil125 results[results.count - 1] = (last, .nextPage)126 }127 let background = DocxColor.hex(root.first("w:background")?.attr("w:color"))128 for i in results.indices where background != nil { results[i].setup.background = background }129 doc.pageSetup = results[0].setup130 for (k, id) in sectionBreakIDs.enumerated() {131 guard let i = blocks.firstIndex(where: { $0.id == id }) else { continue }132 blocks[i] = .sectionBreak(SectionBreak(id: id, kind: results[k + 1].kind, setup: results[k + 1].setup))133 }134 doc.body = blocks135136 // Cross-reference displays depend on what the target bookmark covers, which is only known once everything is read.137 let display = bookmarkDisplay138 doc.mutateEveryParagraph { p in139 for i in p.runs.indices {140 guard case .crossRef(var ref)? = p.runs[i].inline else { continue }141 if let orix = DocxConvention.parseRefBookmark(ref.target) {142 ref.target = orix.id143 if ref.display != .page {144 switch orix.kind {145 case "L": ref.display = .labelAndNumber146 case "N": ref.display = .number147 default: break148 }149 }150 } else if ref.display == .text, let d = display[ref.target] {151 ref.display = d152 }153 p.runs[i].inline = .crossRef(ref)154 }155 }156 }157158 func collectBookmarks(_ node: XMLNode) {159 var stack = [node]160 while let n = stack.popLast() {161 if n.name == "w:bookmarkStart", let name = n.attr("w:name") { knownBookmarks.insert(name) }162 stack.append(contentsOf: n.children)163 }164 }165166 func closeStory(_ story: DocxStory) {167 if !story.fields.isEmpty {168 warn(.approximated, "Fields", "A field is not terminated; the text that follows it inside the field was not imported.")169 story.fields.removeAll()170 }171 }172173 // MARK: Blocks174175 func parseBlocks(_ nodes: [XMLNode], story: DocxStory, allowTables: Bool) -> [Block] {176 var out: [Block] = []177 for n in nodes {178 switch n.name {179 case "w:p":180 out.append(contentsOf: parseParagraph(n, story: story, previous: out.last, hasPrevious: !out.isEmpty))181 case "w:tbl":182 if story.suppressed { continue }183 if allowTables && story.tableDepth == 0 {184 // A hidden empty paragraph between two tables only keeps Word from merging them.185 if let last = out.last?.paragraph, tableSeparatorIDs.contains(last.id) { out.removeLast() }186 out.append(.table(parseTable(n, story: story)))187 } else {188 warn(.approximated, story.tableDepth > 0 ? "Nested tables" : "Tables in headers, footers and notes",189 story.tableDepth > 0 ? "A table inside a table cell is flattened into paragraphs." : "A table outside the body is flattened into paragraphs.")190 out.append(contentsOf: flattenTable(n, story: story))191 }192 case "w:sdt":193 out.append(contentsOf: parseBlockSDT(n, story: story, allowTables: allowTables))194 case "w:customXml", "w:smartTag", "w:ins", "w:del", "w:moveFrom", "w:moveTo", "w:sdtContent":195 out.append(contentsOf: parseBlocks(n.children, story: story, allowTables: allowTables))196 case "mc:AlternateContent":197 let branch = n.first("mc:Choice") ?? n.first("mc:Fallback")198 out.append(contentsOf: parseBlocks(branch?.children ?? [], story: story, allowTables: allowTables))199 case "w:altChunk":200 warn(.dropped, "Embedded documents", "Content imported from another file (altChunk) is not read.")201 case "w:sectPr", "w:bookmarkStart", "w:bookmarkEnd", "w:proofErr", "w:permStart", "w:permEnd", "w:commentRangeStart", "w:commentRangeEnd",202 "w:moveFromRangeStart", "w:moveFromRangeEnd", "w:moveToRangeStart", "w:moveToRangeEnd", "w:tcPr", "w:trPr", "w:tblPr", "w:tblGrid", "w:sdtPr", "w:sdtEndPr":203 if n.name == "w:commentRangeStart" || n.name == "w:commentRangeEnd" || n.name == "w:bookmarkStart" {204 var b = DocxParaBuilder()205 handleMarker(n, &b, story)206 // A bookmark between paragraphs has no paragraph to live in; attach it to the next one through the pending list.207 if let run = b.runs.first { pendingBlockRuns.append(run) }208 }209 default:210 reportUnknown(n, context: "block")211 let inner = n.descendants("w:p")212 for p in inner { out.append(contentsOf: parseParagraph(p, story: story, previous: out.last, hasPrevious: !out.isEmpty)) }213 }214 }215 return out216 }217218 func reportUnknown(_ n: XMLNode, context: String) {219 let known: [String: (String, String)] = [220 "w:object": ("Embedded objects", "An embedded OLE object is not imported and never opened."),221 "w:control": ("ActiveX controls", "An ActiveX control is not imported and never run."),222 "w:ruby": ("Phonetic guides", "Ruby (phonetic guide) text is imported without its annotation."),223 "w:subDoc": ("Subdocuments", "Links to subdocuments are not followed."),224 ]225 if let (f, d) = known[n.name] { warn(.dropped, f, d) } else {226 warn(.dropped, "Unsupported content", "The \(context) element <\(n.name)> is not supported; any text it contains is kept as plain text.")227 }228 }229230 func parseBlockSDT(_ sdt: XMLNode, story: DocxStory, allowTables: Bool) -> [Block] {231 let pr = sdt.first("w:sdtPr")232 let content = sdt.first("w:sdtContent")?.children ?? []233 let gallery = pr?.path("w:docPartObj", "w:docPartGallery")?.attr("w:val") ?? ""234 let tag = pr?.val("w:tag") ?? ""235 if tag == DocxConvention.bibliographyTag {236 // Written by ORIX Docs: the entries are generated content, only the block and its title come back.237 _ = content238 return [.bibliography(BibliographyBlock(title: pr?.val("w:alias")))]239 }240 var blocks = parseBlocks(content, story: story, allowTables: allowTables)241 if gallery.lowercased().contains("table of contents") || gallery.lowercased().contains("table of figures") {242 if let ti = blocks.firstIndex(where: { if case .toc = $0 { return true } else { return false } }), case .toc(var toc) = blocks[ti] {243 toc.title = nil244 if ti > 0, case .paragraph(let p) = blocks[ti - 1] {245 toc.title = p.visibleText246 blocks.remove(at: ti - 1)247 blocks[ti - 1] = .toc(toc)248 } else {249 blocks[ti] = .toc(toc)250 }251 }252 return blocks253 }254 if pr?.first("w:bibliography") != nil || gallery.lowercased().contains("bibliograph") {255 warn(.approximated, "Word bibliography", "The bibliography generated by Word is imported as ordinary text; Word's source list is not converted to ORIX references.")256 } else if pr != nil {257 noteContentControl(pr!)258 }259 return blocks260 }261262 func noteContentControl(_ pr: XMLNode) {263 let kinds = ["w:dropDownList": "drop-down list", "w:comboBox": "combo box", "w:date": "date picker", "w:picture": "picture control",264 "w14:checkbox": "check box", "w:richText": "rich text control", "w:text": "text control", "w:docPartList": "building block gallery",265 "w:citation": "citation"]266 let kind = pr.children.compactMap { kinds[$0.name] }.first ?? "content control"267 if kind == "citation" {268 warn(.approximated, "Word citations", "Citations inserted with Word's reference tools are imported as their displayed text.")269 } else {270 warn(.approximated, "Content controls", "A \(kind) is imported as its current content; the control itself is not kept.")271 }272 }273274 // MARK: Paragraphs275276 func parseParagraph(_ p: XMLNode, story: DocxStory, previous: Block?, hasPrevious: Bool) -> [Block] {277 let pPr = p.first("w:pPr")278 story.paragraphToken += 1279 var b = DocxParaBuilder()280 b.token = story.paragraphToken281 b.dropIfEmpty = story.inBlockField282 if !pendingBlockRuns.isEmpty && !story.suppressed { b.runs = pendingBlockRuns; pendingBlockRuns = [] }283284 var para = Paragraph(style: paragraphStyle(pPr?.val("w:pStyle")), props: paraProps(pPr))285 if let numPr = pPr?.first("w:numPr") {286 if let numId = numPr.val("w:numId"), numId != "0", let list = numToList[numId] {287 para.list = ListItem(listID: list, level: max(0, min(8, DocxUnit.int(numPr.val("w:ilvl")) ?? 0)))288 }289 if numPr.first("w:ins") != nil { warn(.dropped, "Tracked list changes", "Tracked changes of list numbering are not kept; the current numbering is.") }290 } else {291 // Numbering inherited from the paragraph style ("List Bullet", "List Number" …).292 for s in doc.styles.chain(para.style).reversed() {293 if let ref = styleNumbering[s.id], let list = numToList[ref.numId] { para.list = ListItem(listID: list, level: max(0, min(8, ref.level))); break }294 }295 }296 if let mark = pPr?.first("w:rPr") {297 if let d = mark.first("w:del") ?? mark.first("w:moveFrom") { para.markRevision = revision(.deletion, d) }298 else if let i = mark.first("w:ins") ?? mark.first("w:moveTo") { para.markRevision = revision(.insertion, i) }299 }300301 walkInline(p.children, DocxInlineState(), &b, story)302303 // A field that is replaced by an inline object must end in the paragraph where it starts.304 while let last = story.fields.last, last.token == b.token, case .replace = last.kind { story.fields.removeLast() }305306 if let id = b.assignedID, !usedParagraphIDs.contains(id) { para.id = id }307 usedParagraphIDs.insert(para.id)308309 var out = b.leading310 var ownParagraphEmitted = false311 if b.breaks.isEmpty {312 para.runs = b.runs313 para.normalize()314 if !(b.dropIfEmpty && para.runs.isEmpty) { out.append(.paragraph(para)); ownParagraphEmitted = true }315 } else {316 var segments: [[Run]] = []317 var start = 0318 for br in b.breaks { segments.append(Array(b.runs[start..<br.at])); start = br.at }319 segments.append(Array(b.runs[start...]))320 let nonEmpty = segments.filter { !$0.isEmpty }.count321 if nonEmpty > 1 { warn(.approximated, "Page breaks inside paragraphs", "A paragraph containing a page or column break in the middle of its text is split in two paragraphs.") }322 var emitted = 0323 for (i, seg) in segments.enumerated() {324 if !seg.isEmpty {325 var part = para326 if emitted > 0 { part.id = DocID.make(); part.props.pageBreakBefore = nil }327 if emitted < nonEmpty - 1 { part.markRevision = nil }328 part.runs = seg329 part.normalize()330 out.append(.paragraph(part))331 emitted += 1332 ownParagraphEmitted = true333 }334 if i < b.breaks.count { out.append(.pageBreak(BreakBlock(kind: b.breaks[i].kind))) }335 }336 }337338 if ownParagraphEmitted, b.runs.isEmpty, case .table? = previous, docxOnOff(pPr?.path("w:rPr", "w:vanish")) == true { tableSeparatorIDs.insert(para.id) }339340 if let sectPr = pPr?.first("w:sectPr"), story.kind == .body, story.tableDepth == 0 {341 // Word needs a paragraph to carry the section properties. When that paragraph is empty and follows something that is342 // not a paragraph, it exists only for that purpose and is not content.343 if ownParagraphEmitted, b.leading.isEmpty, b.breaks.isEmpty, case .paragraph(let own)? = out.last, own.runs.isEmpty, own.list == nil {344 var structural = !hasPrevious345 if let previous { if case .paragraph = previous { structural = false } else { structural = true } }346 if structural { out.removeLast() }347 }348 let placeholder = SectionBreak(setup: PageSetup(paper: .letter))349 sectPrNodes.append(sectPr)350 sectionBreakIDs.append(placeholder.id)351 out.append(.sectionBreak(placeholder))352 }353 out.append(contentsOf: b.trailing)354 return out355 }356357 func revision(_ kind: Revision.Kind, _ node: XMLNode) -> Revision {358 Revision(kind: kind, author: node.attr("w:author") ?? "", date: DocxDate.parse(node.attr("w:date")) ?? DocxDate.unknown)359 }360361 func revision(_ kind: Revision.Kind, _ node: XMLNode, in b: inout DocxParaBuilder) -> Revision {362 let fresh = revision(kind, node)363 if let known = b.revisions.first(where: { $0.kind == fresh.kind && $0.author == fresh.author && $0.date == fresh.date }) { return known }364 b.revisions.append(fresh)365 return fresh366 }367368 func runProps(_ rPr: XMLNode?, in b: inout DocxParaBuilder) -> DocxRunProps {369 guard let rPr else { return DocxRunProps() }370 var rp = DocxRunProps(props: charProps(rPr), charStyle: characterStyle(rPr.val("w:rStyle")))371 if let ch = rPr.first("w:rPrChange") {372 let prev = ch.first("w:rPr")373 let fr = FormatRevision(author: ch.attr("w:author") ?? "", date: DocxDate.parse(ch.attr("w:date")) ?? DocxDate.unknown,374 previous: charProps(prev), previousCharStyle: characterStyle(prev?.val("w:rStyle")))375 if let known = b.formatRevisions.first(where: { $0.author == fr.author && $0.date == fr.date && $0.previous == fr.previous && $0.previousCharStyle == fr.previousCharStyle }) {376 rp.formatRevision = known377 } else {378 b.formatRevisions.append(fr)379 rp.formatRevision = fr380 }381 }382 return rp383 }384385 // MARK: Inline content386387 func walkInline(_ nodes: [XMLNode], _ st: DocxInlineState, _ b: inout DocxParaBuilder, _ story: DocxStory) {388 for n in nodes {389 switch n.name {390 case "w:r":391 parseRun(n, st, &b, story)392 case "w:hyperlink":393 var inner = st394 inner.link = hyperlinkTarget(n, story)395 walkInline(n.children, inner, &b, story)396 case "w:ins", "w:moveTo":397 var inner = st398 if st.revision?.kind != .deletion { inner.revision = revision(.insertion, n, in: &b) }399 if n.name == "w:moveTo" { warn(.approximated, "Tracked moves", "Tracked moves are imported as a deletion and an insertion.") }400 walkInline(n.children, inner, &b, story)401 case "w:del", "w:moveFrom":402 var inner = st403 inner.revision = revision(.deletion, n, in: &b)404 if n.name == "w:moveFrom" { warn(.approximated, "Tracked moves", "Tracked moves are imported as a deletion and an insertion.") }405 walkInline(n.children, inner, &b, story)406 case "w:bookmarkStart", "w:bookmarkEnd", "w:commentRangeStart", "w:commentRangeEnd":407 handleMarker(n, &b, story)408 case "w:fldSimple":409 let rp = DocxRunProps()410 var field = DocxFieldState(instruction: n.attr("w:instr") ?? "", inResult: false, kind: .pending, props: rp, state: st, token: b.token)411 let parentSuppressed = story.suppressed412 field.kind = classifyField(field.instruction, story)413 field.inResult = true414 startField(field, parentSuppressed: parentSuppressed, &b, story)415 story.fields.append(field)416 let depth = story.fields.count417 walkInline(n.children, st, &b, story)418 if story.fields.count >= depth { story.fields.removeSubrange((depth - 1)...) }419 case "w:sdt":420 if let pr = n.first("w:sdtPr") { noteContentControl(pr) }421 walkInline(n.first("w:sdtContent")?.children ?? [], st, &b, story)422 case "w:smartTag", "w:customXml", "w:dir", "w:bdo", "w:sdtContent":423 walkInline(n.children, st, &b, story)424 case "m:oMath":425 appendEquation(n, display: false, DocxRunProps(), st, &b, story)426 case "m:oMathPara":427 appendEquation(n, display: true, DocxRunProps(), st, &b, story)428 case "mc:AlternateContent":429 let branch = n.first("mc:Choice") ?? n.first("mc:Fallback")430 walkInline(branch?.children ?? [], st, &b, story)431 case "w:pPr", "w:proofErr", "w:permStart", "w:permEnd", "w:moveFromRangeStart", "w:moveFromRangeEnd", "w:moveToRangeStart", "w:moveToRangeEnd",432 "w:customXmlInsRangeStart", "w:customXmlInsRangeEnd", "w:customXmlDelRangeStart", "w:customXmlDelRangeEnd":433 break434 case "w:subDoc":435 reportUnknown(n, context: "inline")436 default:437 reportUnknown(n, context: "inline")438 let text = n.descendants("w:t").map(\.text).joined()439 appendText(text, DocxRunProps(), st, &b, story)440 }441 }442 }443444 func hyperlinkTarget(_ n: XMLNode, _ story: DocxStory) -> String? {445 let anchor = n.attr("w:anchor").flatMap { $0.isEmpty ? nil : $0 }446 if let rid = n.attr("r:id"), let rel = story.rels[rid] {447 // The target is kept as a string and never fetched.448 let base = rel.target449 if rel.external { return anchor.map { base + "#" + $0 } ?? base }450 }451 if let anchor { return "#" + anchor }452 return nil453 }454455 func handleMarker(_ n: XMLNode, _ b: inout DocxParaBuilder, _ story: DocxStory) {456 switch n.name {457 case "w:bookmarkStart":458 guard let name = n.attr("w:name"), let wid = n.attr("w:id"), !story.suppressed else { return }459 if name == "_GoBack" || name.hasPrefix("_Toc") || name.hasPrefix("_Hlk") { return }460 if let orix = DocxConvention.parseRefBookmark(name) {461 if b.assignedID == nil { b.assignedID = orix.id }462 return463 }464 story.openBookmarks[wid] = (name, b.token, b.runs.count)465 var run = Run(inline: .bookmark(name))466 run.comments = []467 b.runs.append(run)468 case "w:bookmarkEnd":469 guard let wid = n.attr("w:id"), let open = story.openBookmarks.removeValue(forKey: wid), open.token == b.token else { return }470 // What the bookmark covers decides how a REF field that points to it reads: the whole caption, "Figure 3", or "3".471 let covered = b.runs.count > open.runIndex + 1 ? Array(b.runs[(open.runIndex + 1)...]) : []472 let isSeq: (Run) -> Bool = { if case .field(.sequence)? = $0.inline { return true } else { return false } }473 let content = covered.filter { if case .bookmark? = $0.inline { return false } else { return true } }474 guard let last = content.last, isSeq(last) else { return }475 bookmarkDisplay[open.name] = content.count == 1 ? .number : .labelAndNumber476 case "w:commentRangeStart":477 guard let wid = n.attr("w:id"), !replyCommentIDs.contains(wid), let id = commentIDs[wid] else { return }478 story.commentRangesSeen.insert(wid)479 if !story.activeComments.contains(id) { story.activeComments.append(id); story.commentHits[id] = 0 }480 case "w:commentRangeEnd":481 guard let wid = n.attr("w:id"), let id = commentIDs[wid], let i = story.activeComments.firstIndex(of: id) else { return }482 story.activeComments.remove(at: i)483 if (story.commentHits[id] ?? 0) == 0 { anchorComment(id, &b, story) }484 default: break485 }486 }487488 /// Anchors a comment that covers no text on the nearest run.489 func anchorComment(_ id: String, _ b: inout DocxParaBuilder, _ story: DocxStory) {490 guard !commentAnchored.contains(id) else { return }491 if let i = b.runs.indices.last {492 if !b.runs[i].comments.contains(id) { b.runs[i].comments.append(id) }493 commentAnchored.insert(id)494 } else if !story.pendingComments.contains(id) {495 story.pendingComments.append(id)496 }497 }498499 func takeComments(_ story: DocxStory) -> [String] {500 var ids = story.activeComments501 for p in story.pendingComments where !ids.contains(p) { ids.append(p) }502 story.pendingComments.removeAll()503 for id in ids { story.commentHits[id, default: 0] += 1; commentAnchored.insert(id) }504 return ids505 }506507 func appendText(_ text: String, _ rp: DocxRunProps, _ st: DocxInlineState, _ b: inout DocxParaBuilder, _ story: DocxStory) {508 guard !text.isEmpty, !story.suppressed else { return }509 var text = text510 if b.afterNoteRef {511 b.afterNoteRef = false512 if text.hasPrefix(" ") || text.hasPrefix("\t") { text.removeFirst() }513 if text.isEmpty { return }514 }515 var props = rp.props516 if let l = st.link ?? story.fieldLink { props.link = l }517 var run = Run(text, props: props, charStyle: rp.charStyle, revision: st.revision, comments: takeComments(story))518 run.formatRevision = rp.formatRevision519 b.runs.append(run)520 }521522 func appendInline(_ inline: Inline, _ rp: DocxRunProps, _ st: DocxInlineState, _ b: inout DocxParaBuilder, _ story: DocxStory) {523 guard !story.suppressed else { return }524 b.afterNoteRef = false525 var props = rp.props526 if let l = st.link ?? story.fieldLink { props.link = l }527 var run = Run(inline: inline, props: props, charStyle: rp.charStyle, revision: st.revision)528 run.formatRevision = rp.formatRevision529 run.comments = takeComments(story)530 b.runs.append(run)531 }532533 func appendEquation(_ n: XMLNode, display: Bool, _ rp: DocxRunProps, _ st: DocxInlineState, _ b: inout DocxParaBuilder, _ story: DocxStory) {534 let (latex, unsupported) = MathConverter.latex(fromOMMLNode: n)535 if !unsupported.isEmpty {536 warn(.approximated, "Equations", "An equation uses constructs that have no LaTeX equivalent here (\(Set(unsupported).sorted().joined(separator: ", "))); their content is kept as text inside the equation.")537 }538 if latex.trimmingCharacters(in: .whitespaces).isEmpty { return }539 appendInline(.equation(Equation(latex: latex, display: display)), rp, st, &b, story)540 }541542 func parseRun(_ r: XMLNode, _ st: DocxInlineState, _ b: inout DocxParaBuilder, _ story: DocxStory) {543 let rp = runProps(r.first("w:rPr"), in: &b)544 for c in r.children {545 switch c.name {546 case "w:rPr": break547 case "w:t":548 appendText(c.text, rp, st, &b, story)549 case "w:delText":550 appendText(c.text, rp, st, &b, story)551 case "w:tab", "w:ptab":552 appendText("\t", rp, st, &b, story)553 case "w:br", "w:cr":554 switch c.attr("w:type") ?? "textWrapping" {555 case "page": if !story.suppressed { b.breaks.append((b.runs.count, .page)) }556 case "column": if !story.suppressed { b.breaks.append((b.runs.count, .column)) }557 default: appendText(lineSeparatorCharacter, rp, st, &b, story)558 }559 case "w:noBreakHyphen": appendText("\u{2011}", rp, st, &b, story)560 case "w:softHyphen": appendText("\u{00AD}", rp, st, &b, story)561 case "w:sym":562 var ch = "\u{25A1}"563 if let hex = c.attr("w:char"), let v = UInt32(hex, radix: 16) {564 if let g = DocxImportContext.bulletGlyphs[v] { ch = g }565 else if let u = Unicode.Scalar(v >= 0xF000 && v <= 0xF0FF ? v - 0xF000 : v), XMLEscape.isValid(u) { ch = String(Character(u)) }566 }567 warn(.approximated, "Symbol characters", "Characters inserted from a symbol font (\(c.attr("w:font") ?? "unknown")) are mapped to the nearest Unicode character.")568 appendText(ch, rp, st, &b, story)569 case "w:drawing":570 if !story.suppressed { parseDrawing(c, rp, st, &b, story) }571 case "w:pict":572 if !story.suppressed { parseVML(c, isObject: false, rp, st, &b, story) }573 case "w:object":574 if !story.suppressed { parseVML(c, isObject: true, rp, st, &b, story) }575 case "w:footnoteReference", "w:endnoteReference":576 let kind: NoteRef.Kind = c.name == "w:footnoteReference" ? .footnote : .endnote577 guard !story.suppressed, let wid = c.attr("w:id") else { break }578 if story.kind == .note { warn(.dropped, "Notes", "A note reference inside a note is not supported."); break }579 if let id = importNote(kind, wid) {580 var noteProps = rp581 // The mark is drawn by the layout; the superscript and the reference style Word puts on it are not formatting to keep.582 if noteProps.props.vertical == .superscript { noteProps.props.vertical = nil }583 if let cs = noteProps.charStyle, ["FootnoteReference", "EndnoteReference", "FootnoteAnchor", "EndnoteAnchor", "FootnoteCharacters", "EndnoteCharacters"].contains(cs) { noteProps.charStyle = nil }584 appendInline(.note(NoteRef(kind: kind, id: id)), noteProps, st, &b, story)585 if docxBool(c.attr("w:customMarkFollows")) { warn(.approximated, "Custom note marks", "Custom footnote marks are replaced by automatic numbers.") }586 }587 case "w:footnoteRef", "w:endnoteRef":588 b.afterNoteRef = true589 case "w:fldChar":590 handleFieldChar(c, rp, st, &b, story)591 case "w:instrText", "w:delInstrText":592 if let i = story.fields.indices.last, !story.fields[i].inResult { story.fields[i].instruction += c.text }593 case "w:commentReference":594 if let wid = c.attr("w:id"), !replyCommentIDs.contains(wid), let id = commentIDs[wid], !story.commentRangesSeen.contains(wid) {595 anchorComment(id, &b, story)596 }597 case "w:pgNum":598 appendInline(.field(.pageNumber), rp, st, &b, story)599 case "mc:AlternateContent":600 let branch = c.first("mc:Choice") ?? c.first("mc:Fallback")601 let wrapper = XMLNode(name: "w:r")602 if let rPr = r.first("w:rPr") { wrapper.children.append(rPr) }603 wrapper.children.append(contentsOf: branch?.children ?? [])604 parseRun(wrapper, st, &b, story)605 case "w:annotationRef", "w:lastRenderedPageBreak", "w:separator", "w:continuationSeparator", "w:dayShort", "w:dayLong", "w:monthShort",606 "w:monthLong", "w:yearShort", "w:yearLong":607 break608 case "w:ruby":609 reportUnknown(c, context: "inline")610 appendText(c.first("w:rubyBase")?.descendants("w:t").map(\.text).joined() ?? "", rp, st, &b, story)611 default:612 reportUnknown(c, context: "run")613 appendText(c.descendants("w:t").map(\.text).joined(), rp, st, &b, story)614 }615 }616 }617618 // MARK: Fields619620 func handleFieldChar(_ c: XMLNode, _ rp: DocxRunProps, _ st: DocxInlineState, _ b: inout DocxParaBuilder, _ story: DocxStory) {621 switch c.attr("w:fldCharType") ?? "" {622 case "begin":623 guard story.fields.count < 64 else { return }624 story.fields.append(DocxFieldState(props: rp, state: st, token: b.token))625 if c.first("w:ffData") != nil { warn(.approximated, "Form fields", "Legacy form fields are imported as their current text.") }626 case "separate":627 guard var f = story.fields.popLast() else { return }628 let parentSuppressed = story.suppressed629 f.kind = classifyField(f.instruction, story)630 f.inResult = true631 startField(f, parentSuppressed: parentSuppressed, &b, story)632 story.fields.append(f)633 case "end":634 guard var f = story.fields.popLast() else { return }635 if !f.inResult {636 // No cached result at all.637 let parentSuppressed = story.suppressed638 f.kind = classifyField(f.instruction, story)639 startField(f, parentSuppressed: parentSuppressed, &b, story)640 }641 default: break642 }643 }644645 func startField(_ f: DocxFieldState, parentSuppressed: Bool, _ b: inout DocxParaBuilder, _ story: DocxStory) {646 guard !parentSuppressed else { return }647 switch f.kind {648 case .replace(let inline):649 appendInline(inline, f.props, f.state, &b, story)650 case .toc(let block):651 b.leading.append(.toc(block))652 b.dropIfEmpty = true653 case .formula(let formula):654 story.cellFormula = formula655 default: break656 }657 }658659 static func fieldTokens(_ instr: String) -> [String] {660 var out: [String] = []661 var cur = ""662 var quoted = false663 var has = false664 var it = instr.makeIterator()665 while let ch = it.next() {666 if quoted {667 if ch == "\\" { if let n = it.next() { cur.append(n) } }668 else if ch == "\"" { quoted = false }669 else { cur.append(ch) }670 } else if ch == "\"" {671 quoted = true; has = true672 } else if ch.isWhitespace {673 if has || !cur.isEmpty { out.append(cur); cur = ""; has = false }674 } else {675 cur.append(ch)676 }677 }678 if has || !cur.isEmpty { out.append(cur) }679 return out680 }681682 /// Word date picture → Unicode date pattern.683 static func unicodeDatePattern(_ word: String) -> String {684 var out = ""685 var i = word.startIndex686 var quoted = false687 while i < word.endIndex {688 let rest = word[i...]689 if rest.hasPrefix("'") { quoted.toggle(); out.append("'"); i = word.index(after: i); continue }690 if !quoted {691 if rest.hasPrefix("dddd") { out += "EEEE"; i = word.index(i, offsetBy: 4); continue }692 if rest.hasPrefix("ddd") { out += "EEE"; i = word.index(i, offsetBy: 3); continue }693 if rest.lowercased().hasPrefix("am/pm") { out += "a"; i = word.index(i, offsetBy: 5); continue }694 }695 out.append(word[i]); i = word.index(after: i)696 }697 return out698 }699700 func classifyField(_ instruction: String, _ story: DocxStory) -> DocxFieldKind {701 let trimmed = instruction.trimmingCharacters(in: .whitespacesAndNewlines)702 if trimmed.hasPrefix(DocxConvention.citationInstruction) {703 let json = trimmed.dropFirst(DocxConvention.citationInstruction.count).trimmingCharacters(in: .whitespaces)704 if json.utf8.count < 1_000_000, let c = try? JSONDecoder().decode(Citation.self, from: Data(json.utf8)), !c.items.isEmpty {705 return .replace(.citation(c))706 }707 warn(.approximated, "Citations", "An ORIX citation field could not be decoded; its displayed text is kept.")708 return .keep709 }710 if trimmed.hasPrefix("=") {711 if story.tableDepth > 0 { return .formula(trimmed) }712 warn(.approximated, "Fields", "A formula field outside a table is imported as its last displayed text.")713 return .keep714 }715 let tokens = DocxImportContext.fieldTokens(trimmed)716 guard let first = tokens.first else { return .keep }717 let name = first.uppercased()718 let args = Array(tokens.dropFirst())719 let switches = Set(args.filter { $0.hasPrefix("\\") }.map { $0.lowercased() })720 func value(after sw: String) -> String? {721 guard let i = args.firstIndex(where: { $0.lowercased() == sw }), i + 1 < args.count else { return nil }722 return args[i + 1]723 }724 let firstArg = args.first { !$0.hasPrefix("\\") }725 switch name {726 case "PAGE": return .replace(.field(.pageNumber))727 case "NUMPAGES": return .replace(.field(.pageCount))728 case "SECTIONPAGES":729 warn(.approximated, "Fields", "The section page count field is imported as the document page count.")730 return .replace(.field(.pageCount))731 case "DATE", "TIME":732 let picture = value(after: "\\@")733 if name == "TIME" && picture == nil { return .replace(.field(.date(format: "HH:mm"))) }734 return .replace(.field(.date(format: picture.map(DocxImportContext.unicodeDatePattern) ?? "d MMMM yyyy")))735 case "TITLE": return .replace(.field(.title))736 case "AUTHOR": return .replace(.field(.author))737 case "SEQ":738 guard let seq = firstArg else { return .keep }739 if switches.contains("\\c") || switches.contains("\\h") {740 warn(.approximated, "Fields", "A sequence field that repeats or hides its number is imported as static text.")741 return .keep742 }743 return .replace(.field(.sequence(seq)))744 case "REF", "PAGEREF":745 guard let target = firstArg, knownBookmarks.contains(target) else {746 warn(.approximated, "Cross-references", "A cross-reference whose target is missing is imported as its last displayed text.")747 return .keep748 }749 var display: CrossRef.Display = .text750 if name == "PAGEREF" { display = .page }751 else if switches.contains("\\w") { display = .labelAndNumber }752 else if switches.contains("\\r") || switches.contains("\\n") { display = .number }753 if switches.contains("\\p") { warn(.approximated, "Cross-references", "\"above/below\" cross-references are imported as text references.") }754 return .replace(.crossRef(CrossRef(target: target, display: display)))755 case "STYLEREF":756 if let a = firstArg {757 if let n = Int(a), (1...9).contains(n) { return .replace(.field(.headingText(level: n))) }758 let mapped = DocxImportContext.builtInByName[a.lowercased()] ?? styleIDMap[a] ?? a759 for n in 1...9 where mapped == StyleID.heading(n) { return .replace(.field(.headingText(level: n))) }760 }761 warn(.approximated, "Fields", "A STYLEREF field that does not point to a heading level is imported as its last displayed text.")762 return .keep763 case "HYPERLINK":764 let anchor = value(after: "\\l")765 let urlToken = args.first { !$0.hasPrefix("\\") && $0 != anchor } ?? (anchor == nil ? firstArg : nil)766 if let url = urlToken, !url.isEmpty, url != anchor { return .link(anchor.map { url + "#" + $0 } ?? url) }767 if let anchor { return .link("#" + anchor) }768 return .keep769 case "TOC":770 var toc = TOCBlock(kind: .contents, title: nil, maxLevel: 3)771 if let range = value(after: "\\o"), let hi = range.split(separator: "-").last.flatMap({ Int($0) }), (1...9).contains(hi) { toc.maxLevel = hi }772 else if switches.contains("\\o") || !switches.contains("\\c") && value(after: "\\t") == nil { toc.maxLevel = switches.contains("\\o") ? 9 : 3 }773 if let caption = value(after: "\\c") {774 let lower = caption.lowercased()775 toc.kind = lower.hasPrefix("tab") ? .tables : .figures776 if !lower.hasPrefix("tab") && !lower.hasPrefix("fig") {777 warn(.approximated, "Tables of captions", "A list of \"\(caption)\" captions is imported as a list of figures.")778 }779 }780 if value(after: "\\t") != nil { warn(.approximated, "Table of contents", "A table of contents built from custom styles is imported as a table of contents built from headings.") }781 return .toc(toc)782 case "CITATION", "BIBLIOGRAPHY":783 warn(.approximated, "Word citations", "Citations and bibliographies inserted with Word's reference tools are imported as their displayed text.")784 return .keep785 case "ADDIN":786 warn(.approximated, "Add-in fields", "A field managed by an add-in (for example a reference manager) is imported as its displayed text.")787 return .keep788 case "INCLUDEPICTURE", "INCLUDETEXT", "LINK", "DDE", "DDEAUTO", "IMPORT":789 warn(.dropped, "External links", "A field that links to external content (\(name)) is not followed; its last displayed text is kept.")790 return .keep791 case "FORMTEXT", "FORMCHECKBOX", "FORMDROPDOWN":792 return .keep793 default:794 warn(.approximated, "Fields", "The \(name) field is imported as its last displayed text.")795 return .keep796 }797 }798799 // MARK: Notes and comments800801 func importNote(_ kind: NoteRef.Kind, _ wid: String) -> String? {802 let key = (kind == .footnote ? "f" : "e") + wid803 if let id = noteIDs[key] { return id }804 guard let node = (kind == .footnote ? footnoteNodes : endnoteNodes)[wid], let part = notePart[kind] else {805 warn(.dropped, "Notes", "A note reference points to a note that does not exist in the file.")806 return nil807 }808 let story = DocxStory(part: part, rels: Array((kind == .footnote ? footnoteRels : endnoteRels).values), kind: .note)809 let blocks = parseBlocks(node.children, story: story, allowTables: false)810 closeStory(story)811 var paragraphs = blocks.compactMap(\.paragraph)812 if blocks.contains(where: { $0.paragraph == nil }) { warn(.dropped, "Notes", "Breaks and generated lists inside notes are not supported.") }813 if paragraphs.isEmpty { paragraphs = [Paragraph(style: doc.styles.style(StyleID.footnoteText) != nil ? StyleID.footnoteText : defaultParaStyle)] }814 let note = Note(kind: kind, paragraphs: paragraphs)815 doc.notes[note.id] = note816 noteIDs[key] = note.id817 return note.id818 }819820 /// Text of a paragraph element without any formatting (used for comments).821 func plainText(of node: XMLNode) -> String {822 var out = ""823 func visit(_ n: XMLNode) {824 switch n.name {825 case "w:t": out += n.text826 case "w:tab": out += "\t"827 case "w:br", "w:cr": out += "\n"828 case "w:delText", "w:instrText", "w:pPr", "w:rPr": break829 default: for c in n.children { visit(c) }830 }831 }832 visit(node)833 return out834 }835836 func importComments(_ root: XMLNode, extended: XMLNode?) {837 var ex: [String: (parent: String?, done: Bool)] = [:]838 for e in extended?.all("w15:commentEx") ?? [] {839 guard let pid = e.attr("w15:paraId") else { continue }840 ex[pid.uppercased()] = (e.attr("w15:paraIdParent")?.uppercased(), docxBool(e.attr("w15:done")))841 }842 struct Raw { var wid: String; var author: String; var date: Date; var text: String; var paraID: String? }843 var raws: [Raw] = []844 for c in root.all("w:comment") {845 guard let wid = c.attr("w:id") else { continue }846 let paragraphs = c.descendants("w:p")847 raws.append(Raw(wid: wid, author: c.attr("w:author") ?? "", date: DocxDate.parse(c.attr("w:date")) ?? DocxDate.unknown,848 text: paragraphs.map { plainText(of: $0) }.joined(separator: "\n"), paraID: paragraphs.last?.attr("w14:paraId")?.uppercased()))849 }850 let byPara = Dictionary(raws.compactMap { r in r.paraID.map { ($0, r.wid) } }, uniquingKeysWith: { a, _ in a })851 func rootWid(_ r: Raw) -> String? {852 var cur = r.paraID, seen = Set<String>(), top: String?853 while let c = cur, !seen.contains(c), let parent = ex[c]?.parent, let w = byPara[parent] { seen.insert(c); top = w; cur = parent }854 return top == r.wid ? nil : top855 }856 var replies: [String: [CommentReply]] = [:]857 for r in raws {858 if let parent = rootWid(r) {859 replies[parent, default: []].append(CommentReply(author: r.author, date: r.date, text: r.text))860 replyCommentIDs.insert(r.wid)861 }862 }863 for r in raws where !replyCommentIDs.contains(r.wid) {864 let c = Comment(author: r.author, date: r.date, text: r.text, resolved: r.paraID.flatMap { ex[$0]?.done } ?? false, replies: replies[r.wid] ?? [])865 doc.comments.append(c)866 commentIDs[r.wid] = c.id867 }868 }869870 // MARK: Headers and footers871872 func importHeaderFooter(part: String) throws -> [Paragraph] {873 if let cached = headerCache[part] { return cached.map { var p = $0; p.id = DocID.make(); return p } }874 guard zip.contains(part), let root = try xml(part) else { return [] }875 consumed.insert(part)876 let story = DocxStory(part: part, rels: try relationships(of: part), kind: root.name == "w:ftr" ? .footer : .header)877 let blocks = parseBlocks(root.children, story: story, allowTables: false)878 closeStory(story)879 var paragraphs = blocks.compactMap(\.paragraph)880 if blocks.contains(where: { $0.paragraph == nil }) { warn(.dropped, "Headers and footers", "Breaks and generated lists inside headers and footers are not supported.") }881 if paragraphs.count == 1, paragraphs[0].runs.isEmpty { paragraphs = [] }882 headerCache[part] = paragraphs883 if let w = story.watermark { watermarkByPart[part] = w }884 return paragraphs885 }886}887