Skip to content

Commit

Permalink
quoting and unquoting attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
viktomas committed Sep 24, 2022
1 parent 96fbcbc commit 1fc9c5f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/viktomas/logseq-extractor

go 1.19

require golang.org/x/exp v0.0.0-20220921164117-439092de6870

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/exp v0.0.0-20220921164117-439092de6870 h1:j8b6j9gzSigH28O5SjSpQSSh9lFd6f5D/q0aHjNTulc=
golang.org/x/exp v0.0.0-20220921164117-439092de6870/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path/filepath"
"strings"
"time"

"golang.org/x/exp/slices"
)

type page struct {
Expand Down Expand Up @@ -84,17 +86,21 @@ func main() {
page := parsePage(name, srcContent)
result := transformPage(page)
dest := filepath.Join(exportFolder, result.filename)
err = writeStringToFile(dest, render(result))
err = writeStringToFile(dest, render(result, []string{"date", "slug"}))
if err != nil {
log.Fatalf("Error when copying file %q: %v", dest, err)
}
}
}

func render(p page) string {
func render(p page, dontQuote []string) string {
attributeBuilder := strings.Builder{}
for name, value := range p.attributes {
attributeBuilder.WriteString(fmt.Sprintf("%s: %s\n", name, value))
if slices.Contains(dontQuote, name) {
attributeBuilder.WriteString(fmt.Sprintf("%s: %s\n", name, value))
} else {
attributeBuilder.WriteString(fmt.Sprintf("%s: %q\n", name, value))
}
}
return fmt.Sprintf("---\n%s---\n%s", attributeBuilder.String(), p.text)
}
Expand Down
38 changes: 28 additions & 10 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@ import (
)

func TestRender(t *testing.T) {
testPage := page{
filename: "",
attributes: map[string]string{
"first": "1",
"second": "2",
},
text: "page text",
}
result := render(testPage)
require.Equal(t, `---
t.Run("it renders attributes as quoted strings", func(t *testing.T) {
testPage := page{
filename: "",
attributes: map[string]string{
"first": "1",
"second": "2",
},
text: "page text",
}
result := render(testPage, []string{})
require.Equal(t, `---
first: "1"
second: "2"
---
page text`, result)
})
t.Run("it renders attributes without quotes", func(t *testing.T) {
testPage := page{
filename: "",
attributes: map[string]string{
"first": "1",
"second": "2",
},
text: "page text",
}
result := render(testPage, []string{"first", "second"})
require.Equal(t, `---
first: 1
second: 2
---
page text`, result)
})
}

0 comments on commit 1fc9c5f

Please sign in to comment.