Skip to content

Commit

Permalink
FileConventions: fix WrapText function
Browse files Browse the repository at this point in the history
Split paragraph based on codeblock positions so that codeblocks
with two lines, are not broken into multiple paragraphs.

Fixes nblockchain#117
  • Loading branch information
parhamsaremi committed Aug 9, 2023
1 parent 4f76c3d commit 795b515
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ open System.IO
open System.Linq
open System.Text.RegularExpressions

let codeBlockRegex = "\s*(```[\s\S]*```)\s*"

let HasCorrectShebang(fileInfo: FileInfo) =
let fileText = File.ReadLines fileInfo.FullName

Expand Down Expand Up @@ -148,8 +150,6 @@ type Text =
}

let SplitIntoWords(text: string) =
let codeBlockRegex = "\s*(```[\s\S]*```)\s*"

let words =
Regex.Split(text, codeBlockRegex)
|> Seq.filter(fun item -> not(String.IsNullOrEmpty item))
Expand Down Expand Up @@ -241,9 +241,27 @@ let private WrapParagraph (text: string) (maxCharsPerLine: int) : string =

processWords String.Empty String.Empty words

let rec ExtractParagraphs (text: string) : List<string> =
let codeBlockParagraphRegex = "(?!\n\n)\s*(```[\s\S]*```)\s*"
let matchedRegex = Regex.Match(text, codeBlockParagraphRegex)

if matchedRegex.Success then
let beforeMatch = text.Substring(0, matchedRegex.Index)
let afterMatch = text.Substring(matchedRegex.Index + matchedRegex.Length)

let paragraphsBeforeMatch = ExtractParagraphs beforeMatch
let paragraphsAfterMatch = ExtractParagraphs afterMatch
paragraphsBeforeMatch @ (List.singleton matchedRegex.Value) @ paragraphsAfterMatch
elif String.IsNullOrEmpty(text) then
List.Empty
else
text.Split $"{Environment.NewLine}{Environment.NewLine}"
|> Seq.toList

let WrapText (text: string) (maxCharsPerLine: int) : string =
Console.WriteLine (sprintf "%A" (ExtractParagraphs text))
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 795b515

Please sign in to comment.