Skip to content

Commit

Permalink
FileConventions: fix WrapText function
Browse files Browse the repository at this point in the history
Ignore codeblocks while splitting the text into paragraphs
because there might be a codeblock with multiple paragraphs.

Fixes nblockchain#117
  • Loading branch information
parhamsaremi committed Aug 15, 2023
1 parent 1fa1684 commit 0e9b157
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,61 @@ let private WrapParagraph (text: string) (maxCharsPerLine: int) : string =

processWords String.Empty String.Empty words

let ExtractParagraphs(text: string) =
let lines = text.Split('\n') |> Seq.toList

let rec processLines
(remainingLines: List<string>)
(currentParagraph: List<string>)
(paragraphs: List<string>)
(insideCodeBlock: bool)
=
match remainingLines with
| [] ->
if currentParagraph <> [] then
List.singleton(String.Join("\n", List.rev currentParagraph))
@ paragraphs
else
paragraphs
| line :: rest ->
if String.IsNullOrWhiteSpace(line) then
if insideCodeBlock then
processLines
rest
(line :: currentParagraph)
paragraphs
insideCodeBlock
else if currentParagraph <> [] then
let newParagraph =
String.Join("\n", List.rev currentParagraph)

processLines
rest
[]
(newParagraph :: paragraphs)
insideCodeBlock
else
processLines rest [] paragraphs insideCodeBlock
elif line.Trim().StartsWith("```") then
let newInsideCodeBlock = not insideCodeBlock

processLines
rest
(line :: currentParagraph)
paragraphs
newInsideCodeBlock
else
processLines
rest
(line :: currentParagraph)
paragraphs
insideCodeBlock

List.rev <| processLines lines [] [] false

let WrapText (text: string) (maxCharsPerLine: int) : string =
let wrappedParagraphs =
text.Split $"{Environment.NewLine}{Environment.NewLine}"
ExtractParagraphs text
|> Seq.map(fun paragraph -> WrapParagraph paragraph maxCharsPerLine)

String.Join(
Expand Down

0 comments on commit 0e9b157

Please sign in to comment.