@@ -23,6 +23,7 @@ struct Documentation {
2323
2424 // Remove comment markers and extra whitespace from each line.
2525 var cleanedLines = [ String] ( )
26+ var previousLineWasEmpty = false
2627 for var line in lines {
2728 // Trim whitespace first.
2829 line = line. trimmingCharacters ( in: . whitespaces)
@@ -51,10 +52,15 @@ struct Documentation {
5152 // Remove unprintable ASCII characters
5253 line = line. removingUnprintableCharacters
5354
54- // If the line isn't empty after cleaning, keep it.
55- if !line. isEmpty {
55+ // If the line is empty and the previous line wasn't empty, keep it to preserve paragraph breaks
56+ if line. isEmpty {
57+ if !previousLineWasEmpty {
58+ cleanedLines. append ( line)
59+ }
60+ } else {
5661 cleanedLines. append ( line)
5762 }
63+ previousLineWasEmpty = line. isEmpty
5864 }
5965
6066 // We'll accumulate the initial description and any parameter descriptions.
@@ -73,7 +79,8 @@ struct Documentation {
7379 func flushCurrentParameter( ) {
7480 if let paramName = currentParameterName {
7581 let fullDescription = currentParameterLines. joined ( separator: " " ) . trimmingCharacters ( in: . whitespaces)
76- parameters [ paramName] = fullDescription
82+ // Escape the description when storing it
83+ parameters [ paramName] = fullDescription. escapedForSwiftString
7784 }
7885 currentParameterName = nil
7986 currentParameterLines = [ ]
@@ -209,15 +216,45 @@ struct Documentation {
209216 // Flush any parameter still being accumulated.
210217 flushCurrentParameter ( )
211218
212- // Combine initial description lines into a single string.
213- let initialDescription = initialDescriptionLines. joined ( separator: " " ) . trimmingCharacters ( in: . whitespaces)
219+ // Combine initial description lines into a single string, preserving paragraph breaks
220+ var initialDescription = " "
221+ previousLineWasEmpty = false // Reuse the existing variable
222+ for line in initialDescriptionLines {
223+ if line. isEmpty {
224+ if !previousLineWasEmpty {
225+ initialDescription += " \n \n "
226+ }
227+ } else {
228+ if !initialDescription. isEmpty && !previousLineWasEmpty {
229+ initialDescription += " "
230+ }
231+ initialDescription += line
232+ }
233+ previousLineWasEmpty = line. isEmpty
234+ }
235+ initialDescription = initialDescription. trimmingCharacters ( in: . whitespacesAndNewlines)
214236
215- // Combine returns lines into a single string.
216- let returnsDescription = returnsLines. isEmpty ? nil : returnsLines. joined ( separator: " " ) . trimmingCharacters ( in: . whitespaces)
237+ // Combine returns lines into a single string, preserving paragraph breaks
238+ var returnsDescription = " "
239+ previousLineWasEmpty = false // Reuse the existing variable
240+ for line in returnsLines {
241+ if line. isEmpty {
242+ if !previousLineWasEmpty {
243+ returnsDescription += " \n \n "
244+ }
245+ } else {
246+ if !returnsDescription. isEmpty && !previousLineWasEmpty {
247+ returnsDescription += " "
248+ }
249+ returnsDescription += line
250+ }
251+ previousLineWasEmpty = line. isEmpty
252+ }
253+ returnsDescription = returnsDescription. trimmingCharacters ( in: . whitespacesAndNewlines)
217254
218- self . description = initialDescription
255+ self . description = initialDescription. escapedForSwiftString
219256 self . parameters = parameters
220- self . returns = returnsDescription
257+ self . returns = returnsDescription. isEmpty ? nil : returnsDescription . escapedForSwiftString
221258 }
222259}
223260
@@ -239,13 +276,3 @@ fileprivate func parseParameterLine(from line: String) -> (name: String, descrip
239276 }
240277 return nil
241278}
242-
243- extension String {
244- var removingUnprintableCharacters : String {
245- // Create a character set of printable ASCII characters (32-126) plus newline, tab, etc.
246- let printableCharacters = CharacterSet ( charactersIn: " \t \n \r " ) . union ( CharacterSet ( charactersIn: UnicodeScalar ( 32 ) ... UnicodeScalar ( 126 ) ) )
247-
248- // Filter out any characters that are not in the printable set
249- return unicodeScalars. filter { printableCharacters. contains ( $0) } . map { String ( $0) } . joined ( )
250- }
251- }
0 commit comments