Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow subsections and section less items #22

Merged
merged 8 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/Ionide.KeepAChangelog.Tasks/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ module Util =
item.ItemSpec <- "Unreleased"
item

let stitch items =
String.concat System.Environment.NewLine items

let mapChangelogData (data: ChangelogData) (item: ITaskItem) : ITaskItem =
item.SetMetadata("Added", stitch data.Added)
item.SetMetadata("Changed", stitch data.Changed)
item.SetMetadata("Deprecated", stitch data.Deprecated)
item.SetMetadata("Removed", stitch data.Removed)
item.SetMetadata("Fixed", stitch data.Fixed)
item.SetMetadata("Security", stitch data.Security)
item.SetMetadata("Added", data.Added)
item.SetMetadata("Changed", data.Changed)
item.SetMetadata("Deprecated", data.Deprecated)
item.SetMetadata("Removed", data.Removed)
item.SetMetadata("Fixed", data.Fixed)
item.SetMetadata("Security", data.Security)
for (KeyValue(heading, lines)) in data.Custom do
item.SetMetadata(heading, stitch lines)
item.SetMetadata(heading, lines)
item

type ParseChangelogs() =
Expand Down
98 changes: 51 additions & 47 deletions src/Ionide.KeepAChangelog/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,40 @@ module Domain =
open SemVersion
open System

// TODO: a changelog entry may have a description?
type ChangelogData =
{ Added: string list
Changed: string list
Deprecated: string list
Removed: string list
Fixed: string list
Security: string list
Custom: Map<string, string list>}
{
Added: string
Changed: string
Deprecated: string
Removed: string
Fixed: string
Security: string
Custom: Map<string, string>
}
static member Default =
{ Added = []
Changed = []
Deprecated = []
Removed = []
Fixed = []
Security = []
{ Added = String.Empty
Changed = String.Empty
Deprecated = String.Empty
Removed = String.Empty
Fixed = String.Empty
Security = String.Empty
Custom = Map.empty }

member this.ToMarkdown () =

let renderItems (items : string list) =
items
|> List.map (fun item ->
"* " + item
)
|> String.concat Environment.NewLine

let section name items =
match items with
| [] -> []
| items ->
$"### {name}"
+ Environment.NewLine
+ Environment.NewLine
+ (renderItems items)
+ Environment.NewLine
|> List.singleton
let section name (body: string) =
baronfel marked this conversation as resolved.
Show resolved Hide resolved
$"### {name}%s{Environment.NewLine}%s{Environment.NewLine}%s{body}"

String.concat
Environment.NewLine
[
yield! section "Added" this.Added
yield! section "Changed" this.Changed
yield! section "Deprecated" this.Deprecated
yield! section "Removed" this.Removed
yield! section "Fixed" this.Fixed
yield! section "Security" this.Security
section "Added" this.Added
section "Changed" this.Changed
section "Deprecated" this.Deprecated
section "Removed" this.Removed
section "Fixed" this.Fixed
section "Security" this.Security
for KeyValue(heading, lines) in this.Custom do
yield! section heading lines
section heading lines
]

type Changelogs =
Expand Down Expand Up @@ -130,23 +115,31 @@ module Parser =

pipe2 bullet content (fun bullet text -> $"{bullet} {text}")

let pCustomSection: Parser<string * string list> =
let pSectionBody sectionName : Parser<string> =
let nextHeader = (newline >>. regex @"[#]{1,3}\s\S" |>> ignore)
let endOfSection = choice [ eof; nextHeader ]
manyTill anyChar (lookAhead endOfSection)
|>> System.String.Concat
<?> $"{sectionName} section body"

let pCustomSection: Parser<string * string> =
let sectionName =
skipString "###"
>>. spaces1
>>. restOfLine true // TODO: maybe not the whole line?
<?> $"custom section header"
sectionName
.>>. (many pEntry <?> $"{sectionName} entries")
.>> attempt (opt newline)
.>>. (pSectionBody sectionName)
.>> attempt (opt newline)

let pSection sectionName : Parser<string list> =
(skipString "###"
let pSection sectionName : Parser<string> =
((skipString "###"
>>. spaces1
>>. skipString sectionName)
<?> $"{sectionName} section header"
<?> $"{sectionName} section header")
>>. many1 newline
>>. (many pEntry <?> $"{sectionName} entries")
>>. pSectionBody sectionName
.>> attempt (opt newline)

let pAdded = pSection "Added"
Expand All @@ -156,6 +149,9 @@ module Parser =
let pFixed = pSection "Fixed"
let pSecurity = pSection "Security"
let pOrEmptyList p = opt (attempt p)
let pSectionLessItems =
many1 pEntry
.>> attempt (opt newline)

let pSections: Parser<ChangelogData -> ChangelogData> =
choice [
Expand All @@ -172,6 +168,13 @@ module Parser =
many1 pSections
|>> List.fold (fun x f -> f x) ChangelogData.Default

let pNonStructuredData : Parser<ChangelogData, unit> =
let nextHeader = (newline >>. regex @"[#]{1,2}\s\S" |>> ignore)
let endOfSection = choice [ eof; nextHeader ]
(manyTill anyChar (lookAhead endOfSection) .>> attempt (opt newline))
|>> (fun _content -> ChangelogData.Default)
<?> "release body"

let pHeader: Parser<unit> =
(skipString "# Changelog" >>. skipNewline
.>> skipTillStringOrEof "##")
Expand Down Expand Up @@ -242,8 +245,9 @@ module Parser =
let vPart = skipString "##" >>. spaces1 >>. pVersion
let middle = spaces1 .>> pchar '-' .>> spaces1
let date = pDate .>> skipRestOfLine true
let content = choice [ pData; pNonStructuredData ]

pipe5 vPart middle date (opt (many newline)) (opt pData) (fun v _ date _ data -> v, date, data)
pipe5 vPart middle date (opt (many newline)) (opt content) (fun v _ date _ data -> v, date, data)

let pChangeLogs: Parser<Changelogs, unit> =
let unreleased =
Expand All @@ -257,7 +261,7 @@ module Parser =
pHeader
(attempt (opt unreleased))
(attempt (many pRelease))
(fun header unreleased releases ->
(fun _header unreleased releases ->
{ Unreleased = defaultArg unreleased None
Releases = releases })

Expand Down
Loading
Loading