Commit e1d0c11c63
Verified · cmc
Layout: unified · split
Sources/OrgCore/Export/ExportOptions.swift +11 −1
| @@ -116,6 +116,16 @@ public struct ExportOptions: Sendable, Equatable { | |||
| 116 | /// What happens to the text before it's parsed for export: `#+INCLUDE` (ox.el's | 116 | /// What happens to the text before it's parsed for export: `#+INCLUDE` (ox.el's |
| 117 | /// `org-export-expand-include-keyword`) and macros (`org-macro-replace-all`). | 117 | /// `org-export-expand-include-keyword`) and macros (`org-macro-replace-all`). |
| 118 | public enum ExportPreprocess { | 118 | public enum ExportPreprocess { |
| 119 | /// `text` with line ends as Emacs decodes the file: CRLF becomes LF when no line ends in a | ||
| 120 | /// bare LF (dos), CR becomes LF when there is no LF (mac); otherwise every CR stays (unix). | ||
| 121 | public static func decodedLineEnds(_ text: String) -> String { | ||
| 122 | let scalars = Array(text.unicodeScalars) | ||
| 123 | guard scalars.contains("\r") else { return text } | ||
| 124 | guard scalars.contains("\n") else { return text.replacingOccurrences(of: "\r", with: "\n") } | ||
| 125 | for (i, s) in scalars.enumerated() where s == "\n" && (i == 0 || scalars[i - 1] != "\r") { return text } | ||
| 126 | return text.replacingOccurrences(of: "\r\n", with: "\n") | ||
| 127 | } | ||
| 128 | |||
| 119 | /// `#+INCLUDE: "file" [example|export FORMAT|src LANG] [:lines "A-B"] [:minlevel N]`, with | 129 | /// `#+INCLUDE: "file" [example|export FORMAT|src LANG] [:lines "A-B"] [:minlevel N]`, with |
| 120 | /// `file::*Heading` or `file::#custom-id` for one subtree. Relative to `directory`. | 130 | /// `file::*Heading` or `file::#custom-id` for one subtree. Relative to `directory`. |
| 121 | /// `includer` is the exported file's directory, `directory` at the top. | 131 | /// `includer` is the exported file's directory, `directory` at the top. |
| @@ -137,7 +147,7 @@ public enum ExportPreprocess { | |||
| 137 | target = String(spec[r.upperBound...]) | 147 | target = String(spec[r.upperBound...]) |
| 138 | } | 148 | } |
| 139 | let url = includeURL(file, directory: directory) | 149 | let url = includeURL(file, directory: directory) |
| 140 | guard var included = try? String(contentsOf: url, encoding: .utf8) else { | 150 | guard var included = (try? String(contentsOf: url, encoding: .utf8)).map(decodedLineEnds) else { |
| 141 | lines.append("") | 151 | lines.append("") |
| 142 | continue | 152 | continue |
| 143 | } | 153 | } |
Sources/OrgCore/Export/LaTeXExport.swift +10 −8
| @@ -44,7 +44,7 @@ public enum LaTeXExport { | |||
| 44 | _ text: String, settings: OrgSettings = .default, path: String? = nil, now: Date = Date(), bodyOnly: Bool = false, | 44 | _ text: String, settings: OrgSettings = .default, path: String? = nil, now: Date = Date(), bodyOnly: Bool = false, |
| 45 | author: String = "", language: String = "en", timeZone: TimeZone = .current, file: Bool = false | 45 | author: String = "", language: String = "en", timeZone: TimeZone = .current, file: Bool = false |
| 46 | ) throws -> String { | 46 | ) throws -> String { |
| 47 | let text = try expanded(text, settings: settings, path: path, now: now) | 47 | let text = try expanded(ExportPreprocess.decodedLineEnds(text), settings: settings, path: path, now: now) |
| 48 | let tree = OrgParser.parse(text, defaults: settings) | 48 | let tree = OrgParser.parse(text, defaults: settings) |
| 49 | let keywords = settings.setupKeywords.split(separator: "\n").map(String.init) | 49 | let keywords = settings.setupKeywords.split(separator: "\n").map(String.init) |
| 50 | + tree.root.descendants().filter { $0.kind == .keyword }.map { $0.text.trimmingCharacters(in: .newlines) } | 50 | + tree.root.descendants().filter { $0.kind == .keyword }.map { $0.text.trimmingCharacters(in: .newlines) } |
| @@ -269,20 +269,22 @@ final class LaTeXRenderer { | |||
| 269 | /// `org-element-normalize-string`: one final newline, none for the empty string. | 269 | /// `org-element-normalize-string`: one final newline, none for the empty string. |
| 270 | static func normalize(_ s: String) -> String { | 270 | static func normalize(_ s: String) -> String { |
| 271 | guard !s.isEmpty else { return "" } | 271 | guard !s.isEmpty else { return "" } |
| 272 | var end = s.endIndex | 272 | // By scalar: a CR the file kept stays, as in Emacs, where "\r\n" isn't one character. |
| 273 | var cut = s.endIndex | 273 | let scalars = s.unicodeScalars |
| 274 | while end > s.startIndex { | 274 | var end = scalars.endIndex |
| 275 | let before = s.index(before: end) | 275 | var cut = scalars.endIndex |
| 276 | if s[before] == " " || s[before] == "\t" { | 276 | while end > scalars.startIndex { |
| 277 | let before = scalars.index(before: end) | ||
| 278 | if scalars[before] == " " || scalars[before] == "\t" { | ||
| 277 | end = before | 279 | end = before |
| 278 | } else if s[before] == "\n" { | 280 | } else if scalars[before] == "\n" { |
| 279 | end = before | 281 | end = before |
| 280 | cut = before | 282 | cut = before |
| 281 | } else { | 283 | } else { |
| 282 | break | 284 | break |
| 283 | } | 285 | } |
| 284 | } | 286 | } |
| 285 | return String(s[..<cut]) + "\n" | 287 | return String(scalars[..<cut]) + "\n" |
| 286 | } | 288 | } |
| 287 | 289 | ||
| 288 | /// `org-trim`. | 290 | /// `org-trim`. |
Tests/OrgCoreTests/LaTeXExportTests.swift +19
| @@ -335,6 +335,25 @@ struct LaTeXExportTests { | |||
| 335 | #expect(body.hasPrefix("Text \\url{elisp:(+ 1 2)} and \\url{https://a.b/c d} ok.")) | 335 | #expect(body.hasPrefix("Text \\url{elisp:(+ 1 2)} and \\url{https://a.b/c d} ok.")) |
| 336 | } | 336 | } |
| 337 | 337 | ||
| 338 | /// Line ends as Emacs decodes the file: CRLF throughout (dos), CR only (mac), and mixed (unix, CRs kept). | ||
| 339 | @Test(.enabled(if: oracle)) | ||
| 340 | func lineEndsMatchEmacs() throws { | ||
| 341 | let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-crlf-\(UUID().uuidString)") | ||
| 342 | try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) | ||
| 343 | defer { try? FileManager.default.removeItem(at: folder) } | ||
| 344 | try "Included line.\r\n\r\n- item\r\n".write(to: folder.appendingPathComponent("inc.org"), atomically: true, encoding: .utf8) | ||
| 345 | let docs = [ | ||
| 346 | "#+TITLE: T\r\n* H\r\nText ok.\r\n\r\nSecond *b* paragraph.\r\n| a | b |\r\n|---+---|\r\n| 1 | 2 |\r\n", | ||
| 347 | "Text ok.\r\nNo final newline", | ||
| 348 | "Mixed\r\nends.\nhere\r\n", | ||
| 349 | "Old\rMac\r\rends\r", | ||
| 350 | "Lone\rCR\r\nin dos.\r\n", | ||
| 351 | "#+INCLUDE: \"\(folder.path)/inc.org\"\r\nAfter.\r\n", | ||
| 352 | "#+INCLUDE: \"\(folder.path)/inc.org\"\nAfter.\n", | ||
| 353 | ] | ||
| 354 | try compare(docs.map { LaTeXOracle.Case(text: $0, bodyOnly: true) }, names: docs.indices.map { "line ends \($0)" }) | ||
| 355 | } | ||
| 356 | |||
| 338 | /// What Emacs exports differently or refuses is refused, naming it. | 357 | /// What Emacs exports differently or refuses is refused, naming it. |
| 339 | @Test func refusesWhatItCannotMatch() { | 358 | @Test func refusesWhatItCannotMatch() { |
| 340 | func reason(_ org: String) -> String? { | 359 | func reason(_ org: String) -> String? { |
docs/manual/guide/01-files-and-folders.org +1
| @@ -305,6 +305,7 @@ orgstar edits UTF-8 files, with or without a byte order mark. | |||
| 305 | - A file that is not valid UTF-8 opens read-only. Its text shows with replacement characters where bytes don't decode, the window subtitle reads =Read-only: not UTF-8=, the modeline shows =Read-only=, and commands that would change it report =This file is read-only.= orgstar never writes such a file, and refiling or archiving into it is refused. | 305 | - A file that is not valid UTF-8 opens read-only. Its text shows with replacement characters where bytes don't decode, the window subtitle reads =Read-only: not UTF-8=, the modeline shows =Read-only=, and commands that would change it report =This file is read-only.= orgstar never writes such a file, and refiling or archiving into it is refused. |
| 306 | - A file with a UTF-8 byte order mark keeps it when saved; the modeline shows =BOM=. | 306 | - A file with a UTF-8 byte order mark keeps it when saved; the modeline shows =BOM=. |
| 307 | - A file containing CRLF line endings shows =CRLF= in the modeline. Existing line endings are kept. | 307 | - A file containing CRLF line endings shows =CRLF= in the modeline. Existing line endings are kept. |
| 308 | - LaTeX export, and =#+INCLUDE= in every export, read line endings as Emacs does: a file whose lines all end in CRLF, or all in CR, is read as LF lines; in a file with mixed endings the CRs stay in the text. | ||
| 308 | 309 | ||
| 309 | orgstar does not convert between encodings or line endings. | 310 | orgstar does not convert between encodings or line endings. |
| 310 | 311 | ||