From 47e056f253fdefc244380529ab5990cff890ddad Mon Sep 17 00:00:00 2001 From: Timur Demin Date: Sat, 12 Dec 2020 14:03:29 +0500 Subject: [PATCH] Refactor RenderMarkdown signature This makes the function accept a bitmask instead of a single flag. This is a breaking change. --- cmd/gmnhg/main.go | 8 +++----- render.go | 23 ++++++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/cmd/gmnhg/main.go b/cmd/gmnhg/main.go index 0f51589..170c991 100644 --- a/cmd/gmnhg/main.go +++ b/cmd/gmnhg/main.go @@ -98,8 +98,6 @@ var ( topLevelPostRegex = regexp.MustCompile(contentBase + `([\w-_ ]+)/([\w-_ ]+)\.md`) ) -// TODO: more meaningful errors - type post struct { Post []byte Metadata gemini.HugoMetadata @@ -205,7 +203,7 @@ func main() { if err != nil { return err } - gemText, metadata, err := gemini.RenderMarkdown(fileContent, gemini.WithoutMetadata) + gemText, metadata, err := gemini.RenderMarkdown(fileContent, gemini.Defaults) // skip drafts from rendering if errors.Is(err, gemini.ErrPostIsDraft) { return nil @@ -277,7 +275,7 @@ func main() { // skip unreadable index files continue } - gemtext, _, err := gemini.RenderMarkdown(content, gemini.WithoutMetadata) + gemtext, _, err := gemini.RenderMarkdown(content, gemini.Defaults) if errors.Is(err, gemini.ErrPostIsDraft) { continue } else if err != nil { @@ -305,7 +303,7 @@ func main() { if err != nil { panic(err) } - gemtext, _, err := gemini.RenderMarkdown(indexContent, gemini.WithoutMetadata) + gemtext, _, err := gemini.RenderMarkdown(indexContent, gemini.Defaults) if err != nil && !errors.Is(err, gemini.ErrPostIsDraft) { panic(err) } diff --git a/render.go b/render.go index bd07869..7008316 100644 --- a/render.go +++ b/render.go @@ -54,15 +54,20 @@ var yamlDelimiter = []byte("---\n") // supposed to be rendered. var ErrPostIsDraft = errors.New("post is draft") -// MetadataSetting defines whether or not metadata is included in the -// rendered text. -type MetadataSetting int +// Settings is a bitmask for renderer preferences. +type Settings uint + +// Has uses AND to check whether a flag is set. +func (s Settings) Has(setting Settings) bool { + return (s & setting) != 0 +} -// Metadata settings control the inclusion of metadata in the rendered -// text. const ( - WithMetadata MetadataSetting = iota - WithoutMetadata + // Defaults simply renders the document. + Defaults Settings = 0b0 + // WithMetadata indicates that the metadata should be included in + // the text produced by the renderer. + WithMetadata Settings = 0b1 ) // RenderMarkdown converts Markdown text to text/gemini using @@ -74,7 +79,7 @@ const ( // // Draft posts are still rendered, but with an error of type // ErrPostIsDraft. -func RenderMarkdown(md []byte, metadataSetting MetadataSetting) (geminiText []byte, metadata HugoMetadata, err error) { +func RenderMarkdown(md []byte, settings Settings) (geminiText []byte, metadata HugoMetadata, err error) { var ( blockEnd int yamlContent []byte @@ -95,7 +100,7 @@ func RenderMarkdown(md []byte, metadataSetting MetadataSetting) (geminiText []by parse: ast := markdown.Parse(md, parser.NewWithExtensions(parser.CommonExtensions)) var geminiContent []byte - if metadataSetting == WithMetadata && metadata.PostTitle != "" { + if settings.Has(WithMetadata) && metadata.PostTitle != "" { geminiContent = markdown.Render(ast, gemini.NewRendererWithMetadata(metadata)) } else { geminiContent = markdown.Render(ast, gemini.NewRenderer())