Read CRLF line ends in LaTeX export as Emacs does !429
merged
merged by cmc on 2026-11-21 21:26 UTC
· krz/orgstar:crlf-latex into main
Conversation
Commits 1
Files changed 4
4 files changed, +41 −9
Layout: unified · split
Sources/OrgCore/Export/ExportOptions.swift
+11 −1
@@ -116,6 +116,16 @@ public struct ExportOptions: Sendable, Equatable {
116 116 // / W h a t h a p p e n s t o t h e t e x t b e f o r e i t ' s p a r s e d f o r e x p o r t : ` # + I N C L U D E ` ( o x . e l ' s
117 117 // / ` o r g - e x p o r t - e x p a n d - i n c l u d e - k e y w o r d ` ) a n d m a c r o s ( ` o r g - m a c r o - r e p l a c e - a l l ` ) .
118 118 public enum ExportPreprocess {
119 // / ` t e x t ` w i t h l i n e e n d s a s E m a c s d e c o d e s t h e f i l e : C R L F b e c o m e s L F w h e n n o l i n e e n d s i n a
120 // / b a r e L F ( d o s ) , C R b e c o m e s L F w h e n t h e r e i s n o L F ( m a c ) ; o t h e r w i s e e v e r y C R s t a y s ( u n i x ) .
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 129 // / ` # + I N C L U D E : " f i l e " [ e x a m p l e | e x p o r t F O R M A T | s r c L A N G ] [ : l i n e s " A - B " ] [ : m i n l e v e l N ] ` , w i t h
120 130 // / ` f i l e : : * H e a d i n g ` o r ` f i l e : : # c u s t o m - i d ` f o r o n e s u b t r e e . R e l a t i v e t o ` d i r e c t o r y ` .
121 131 // / ` i n c l u d e r ` i s t h e e x p o r t e d f i l e ' s d i r e c t o r y , ` d i r e c t o r y ` a t t h e t o p .
@@ -137,7 +147,7 @@ public enum ExportPreprocess {
137 147 target = String ( spec [ r . upperBound . . . ] )
138 148 }
139 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 151 lines . append ( " " )
142 152 continue
143 153 }
Sources/OrgCore/Export/LaTeXExport.swift
+10 −8
@@ -44,7 +44,7 @@ public enum LaTeXExport {
44 44 _ text : String , settings : OrgSettings = . default , path : String ? = nil , now : Date = Date ( ) , bodyOnly : Bool = false ,
45 45 author : String = " " , language : String = " en " , timeZone : TimeZone = . current , file : Bool = false
46 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 48 let tree = OrgParser . parse ( text , defaults : settings )
49 49 let keywords = settings . setupKeywords . split ( separator : " \n " ) . map ( String . init )
50 50 + tree . root . descendants ( ) . filter { $0 . kind = = . keyword } . map { $0 . text . trimmingCharacters ( in : . newlines ) }
@@ -269,20 +269,22 @@ final class LaTeXRenderer {
269 269 // / ` o r g - e l e m e n t - n o r m a l i z e - s t r i n g ` : o n e f i n a l n e w l i n e , n o n e f o r t h e e m p t y s t r i n g .
270 270 static func normalize ( _ s : String ) -> String {
271 271 guard ! s . isEmpty else { return " " }
272 var end = s . endIndex
273 var cut = s . endIndex
274 while end > s . startIndex {
275 let before = s . index ( before : end )
276 if s [ before ] = = " " || s [ before ] = = " \t " {
272 // B y s c a l a r : a C R t h e f i l e k e p t s t a y s , a s i n E m a c s , w h e r e " \ r \ n " i s n ' t o n e c h a r a c t e r .
273 let scalars = s . unicodeScalars
274 var end = scalars . endIndex
275 var cut = scalars . endIndex
276 while end > scalars . startIndex {
277 let before = scalars . index ( before : end )
278 if scalars [ before ] = = " " || scalars [ before ] = = " \t " {
277 279 end = before
278 } else if s [ before ] = = " \n " {
280 } else if scalars [ before ] = = " \n " {
279 281 end = before
280 282 cut = before
281 283 } else {
282 284 break
283 285 }
284 286 }
285 return String ( s [ . . < cut ] ) + " \n "
287 return String ( scalars [ . . < cut ] ) + " \n "
286 288 }
287 289
288 290 // / ` o r g - t r i m ` .
Tests/OrgCoreTests/LaTeXExportTests.swift
+19
@@ -335,6 +335,25 @@ struct LaTeXExportTests {
335 335 # expect ( body . hasPrefix ( " Text \\ url{elisp:(+ 1 2)} and \\ url{https://a.b/c d} ok. " ) )
336 336 }
337 337
338 // / L i n e e n d s a s E m a c s d e c o d e s t h e f i l e : C R L F t h r o u g h o u t ( d o s ) , C R o n l y ( m a c ) , a n d m i x e d ( u n i x , C R s k e p t ) .
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 \n Text ok. \r \n \r \n Second *b* paragraph. \r \n | a | b | \r \n |---+---| \r \n | 1 | 2 | \r \n " ,
347 " Text ok. \r \n No final newline " ,
348 " Mixed \r \n ends. \n here \r \n " ,
349 " Old \r Mac \r \r ends \r " ,
350 " Lone \r CR \r \n in dos. \r \n " ,
351 " #+INCLUDE: \" \( folder . path ) /inc.org \" \r \n After. \r \n " ,
352 " #+INCLUDE: \" \( folder . path ) /inc.org \" \n After. \n " ,
353 ]
354 try compare ( docs . map { LaTeXOracle . Case ( text : $0 , bodyOnly : true ) } , names : docs . indices . map { " line ends \( $0 ) " } )
355 }
356
338 357 // / W h a t E m a c s e x p o r t s d i f f e r e n t l y o r r e f u s e s i s r e f u s e d , n a m i n g i t .
339 358 @ Test func refusesWhatItCannotMatch ( ) {
340 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 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 306 - A file with a UTF-8 byte order mark keeps it when saved; the modeline shows =BOM= .
307 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 310 orgstar does not convert between encodings or line endings.
310 311
Checks
No checks reported
Revisions
1. e1d0c11c63 2026-11-21 21:26 UTC
Source and target
krz/orgstar:crlf-latex into main
merged at e1d0c11c63 · base 9eb6f4c04a · branch deleted