Skip to content

Commit

Permalink
task: back to single internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed May 27, 2024
1 parent b43a7d5 commit 84c90f1
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 107 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (

require (
github.com/kr/pretty v0.3.1 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/sys v0.16.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=
Expand Down
17 changes: 11 additions & 6 deletions internal/config/config.go → internal/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package config
package internal

import (
"bytes"
"io"
"os"
"path/filepath"

"github.com/paganotoni/doco/internal/markdown"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
)

const (
Expand All @@ -15,7 +17,7 @@ const (
)

// config of the general elements of the site.
type Site struct {
type siteConfig struct {
Name string
Favicon string
Description string
Expand All @@ -41,7 +43,7 @@ type Link struct {
// Read parses the _meta.md file and returns the config
// for the site.
// TODO: change this to receive the file access (fs package?) instead of the folder.
func Read(folder string) (c Site, err error) {
func readConfig(folder string) (c siteConfig, err error) {
file, err := os.Open(filepath.Join(folder, metafile))
if err != nil {
return c, err
Expand All @@ -54,11 +56,14 @@ func Read(folder string) (c Site, err error) {
return c, err
}

meta, err := markdown.ReadMetadata(content)
if err != nil {
// Parse the metadata and apply it to the document
var buf bytes.Buffer
context := parser.NewContext()
if err := mparser.Convert(content, &buf, parser.WithContext(context)); err != nil {
return c, err
}

meta := meta.Get(context)
def := func(val any, defs string) string {
v, ok := val.(string)
if !ok || v == "" {
Expand Down
6 changes: 3 additions & 3 deletions internal/config/config_test.go → internal/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package internal

import (
_ "embed"
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestReadConfig(t *testing.T) {
t.Fatal(err)
}

_, err = Read(tmpdir)
_, err = readConfig(tmpdir)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestReadConfig(t *testing.T) {
t.Fatal(err)
}

_, err = Read(tmpdir)
_, err = readConfig(tmpdir)
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 8 additions & 4 deletions internal/document.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package internal

import (
"bytes"
"fmt"
"path"
"path/filepath"
"strings"

"github.com/paganotoni/doco/internal/markdown"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand All @@ -15,11 +17,13 @@ import (
// and returns a parsed document with the metadata applied.
func NewDocument(path string, content []byte) (document, error) {
// Parse the metadata and apply it to the document
meta, err := markdown.ReadMetadata(content)
if err != nil {
return document{}, fmt.Errorf("error parsing metadata for %v: %w", path, err)
var buf bytes.Buffer
context := parser.NewContext()
if err := mparser.Convert(content, &buf, parser.WithContext(context)); err != nil {
return document{}, err
}

meta := meta.Get(context)
title, ok := meta["title"].(string)
if !ok {
// Humanizing the filename into a title
Expand Down
55 changes: 41 additions & 14 deletions internal/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
"os"
"path/filepath"

"github.com/paganotoni/doco/internal/config"
"github.com/paganotoni/doco/internal/markdown"
"github.com/yuin/goldmark"

meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
"go.abhg.dev/goldmark/anchor"
)

var (
Expand All @@ -25,18 +28,30 @@ var (
pageHTML string
pageTmpl = template.Must(
template.New("page").Funcs(template.FuncMap{
// htmlFrom generates html from markdown
// this is useful for the content of the page
// to be generated.
"htmlFrom": func(m []byte) template.HTML {
c, err := markdown.HTMLFrom(m)
if err != nil {
return ""
}
return template.HTML(c)
},
"htmlFrom": htmlFromMarkdown,
}).Parse(pageHTML),
)

// mparser is the markdown parser used to parse the content
mparser = goldmark.New(
goldmark.WithParserOptions(
parser.WithAutoHeadingID(), // read note
),

goldmark.WithExtensions(
meta.Meta,

// anchor is used to generate anchor links for headings
// in the markdown file.
&anchor.Extender{
Texter: anchor.Text("#"),
Attributer: anchor.Attributes{
"class": "heading-anchor",
"alt": "Link to this section",
},
},
),
)
)

// Generates the static html files for the site
Expand All @@ -48,7 +63,7 @@ func Generate(srcFolder, destination string, s *site) error {
return err
}

conf, err := config.Read(srcFolder)
conf, err := readConfig(srcFolder)
if err != nil {
return err
}
Expand All @@ -62,7 +77,7 @@ func Generate(srcFolder, destination string, s *site) error {
}

type docData struct {
Config config.Site
Config siteConfig
Site *site

Title string
Expand Down Expand Up @@ -172,3 +187,15 @@ func copyDir(src string, dst string) error {
return os.Chmod(dstPath, info.Mode())
})
}

// htmlFromMarkdown generates html from markdown
// this is useful for the content of the page
// to be generated.
func htmlFromMarkdown(m []byte) template.HTML {
var buf bytes.Buffer
if err := mparser.Convert(m, &buf); err != nil {
return template.HTML("")
}

return template.HTML(buf.String())
}
16 changes: 0 additions & 16 deletions internal/markdown/html.go

This file was deleted.

32 changes: 0 additions & 32 deletions internal/markdown/markdown.go

This file was deleted.

20 changes: 0 additions & 20 deletions internal/markdown/meta.go

This file was deleted.

25 changes: 16 additions & 9 deletions internal/site.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package internal

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"

"github.com/paganotoni/doco/internal/markdown"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -61,14 +63,19 @@ func NewSite(folder string) (*site, error) {
continue
}

meta, err := markdown.ReadMetadata(bb)
if err == nil {
var ok bool
site.Sections[i].index, ok = meta["index"].(int)
if !ok {
// 10 million to make sure it is the last one by default
v.index = 10_000_000
}
// Parse the metadata and apply it to the document
var buf bytes.Buffer
context := parser.NewContext()
if err := mparser.Convert(bb, &buf, parser.WithContext(context)); err != nil {
continue
}

meta := meta.Get(context)
var ok bool
site.Sections[i].index, ok = meta["index"].(int)
if !ok {
// 10 million to make sure it is the last one by default
v.index = 10_000_000
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 84c90f1

Please sign in to comment.