Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
sters committed Dec 13, 2021
1 parent 2355db0 commit 6e42f8d
Show file tree
Hide file tree
Showing 13 changed files with 867 additions and 17 deletions.
32 changes: 28 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
run:
skip-dirs:
- bin
- .github
- tools

linters-settings:
golint:
min-confidence: 0
govet:
check-shadowing: true

linters:
enable:
- deadcode
- errcheck
- errorlint
- exportloopref
- forcetypeassert
- goconst
- gocritic
- godot
- gofmt
- gofumpt
- goimports
- golint
- gomnd
- gosec
- gosimple
- govet
- ifshort
- ineffassign
- misspell
- scopelint
- noctx
- prealloc
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- wastedassign

issues:
exclude-rules:
Expand Down
31 changes: 31 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
builds:
- main: ./cmd/publish-reserved-content/main.go
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- 386
- amd64
- main: ./cmd/find-often-tag-with/main.go
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- 386
- amd64
- main: ./cmd/find-similar-tags/main.go
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- 386
- amd64
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ bootstrap-tools:

.PHONY: lint
lint:
golangci-lint run -v ./...
go-consistent -v ./...
$(GOBIN)/golangci-lint run -v ./...

.PHONY: lint-fix
lint-fix:
golangci-lint run --fix -v ./...
$(GOBIN)/golangci-lint run --fix -v ./...

.PHONY: test
test:
Expand Down
8 changes: 5 additions & 3 deletions cmd/find-similar-tags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"

"github.com/agnivade/levenshtein"
"github.com/sters/hugo-utilities/content"
"github.com/sters/hugo-utilities/util"
hugocontent "github.com/sters/simple-hugo-content-parse"
)

type similarTagRelation struct {
Expand Down Expand Up @@ -40,7 +40,7 @@ func (s similarTagRelations) appendWithoutDuplecation(r *similarTagRelation) {
s[r.Hash()] = r
}

func getAllTags(contents []*hugocontent.MarkdownContent, field string) []string {
func getAllTags(contents []*content.MarkdownContent, field string) []string {
tags := make([]string, 0, len(contents))
check := map[string]struct{}{}
for _, c := range contents {
Expand All @@ -58,7 +58,9 @@ func getAllTags(contents []*hugocontent.MarkdownContent, field string) []string
}

func main() {
t := flag.Float64("threshold", 0.7, "")
const defaultThreshold = 0.7

t := flag.Float64("threshold", defaultThreshold, "")
ff := flag.String("field", "tags", "")
basicFlags := util.ParseFlag()
flags := struct {
Expand Down
103 changes: 103 additions & 0 deletions cmd/publish-reserved-content/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/morikuni/failure"
"github.com/sters/hugo-utilities/publish"
)

func abs(p string) string {
f, _ := filepath.Abs(p)
return f
}

func dirwalk(dir string) ([]string, error) {
dir = abs(dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, failure.Wrap(err)
}

paths := make([]string, 0, len(files))
for _, file := range files {
if file.IsDir() {
child, err := dirwalk(filepath.Join(dir, file.Name()))
if err != nil {
return nil, failure.Wrap(err)
}
paths = append(paths, child...)
continue
}

p, err := filepath.Abs(filepath.Join(dir, file.Name()))
if err != nil {
fmt.Fprintf(os.Stderr, "%+v", err)
continue
}
paths = append(paths, p)
}

return paths, nil
}

func main() {
var (
reservedKey string
draftKey string
basePath string
imageReplaceFormat string
)
flag.StringVar(&reservedKey, "reservedKey", "reserved", "hugo content's reservation bool key")
flag.StringVar(&draftKey, "draftKey", "draft", "hugo content's draft bool key")
flag.StringVar(&basePath, "basePath", "", "hugo content's root directory")
flag.StringVar(&imageReplaceFormat, "imageReplaceFormat", "![]($1)", "how to replace image url")
flag.Parse()

if reservedKey == "" {
fmt.Fprintf(os.Stderr, "reservedKey is required.\n")
os.Exit(1)
}
if draftKey == "" {
fmt.Fprintf(os.Stderr, "draftKey is required.\n")
os.Exit(1)
}
if basePath == "" {
fmt.Fprintf(os.Stderr, "basePath is required.\n")
os.Exit(1)
}
if imageReplaceFormat == "" {
fmt.Fprintf(os.Stderr, "imageReplaceFormat is required.\n")
os.Exit(1)
}

dirs, err := dirwalk(basePath)
if err != nil {
log.Fatal(err)
}

p := publish.New(reservedKey, draftKey, imageReplaceFormat)
for _, filepath := range dirs {
err := p.CheckReservedAndPublish(filepath)
c, ok := failure.CodeOf(err)
if !ok {
// = no error
fmt.Fprintf(os.Stdout, "%s is published.\n", filepath)
continue
}

switch c {
case publish.ErrContentIsReservedButNotDraft:
fmt.Fprintf(os.Stderr, "%s is reserved but not draft.\n", filepath)
case publish.ErrFileContentMismatch:
fmt.Fprintf(os.Stderr, "%s is maybe breaking content.\n", filepath)
case publish.ErrContentIsNotTheTimeYet:
fmt.Fprintf(os.Stderr, "%s is still waiting.\n", filepath)
}
}
}
117 changes: 117 additions & 0 deletions content/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package content

import (
"fmt"
"io"
"io/ioutil"
"strings"

yaml "github.com/goccy/go-yaml"
"github.com/morikuni/failure"
)

// FrontMatter is metadata for MarkdownContent.
type FrontMatter map[string]interface{}

func (f FrontMatter) getField(fieldname string) interface{} {
field, ok := f[fieldname]
if !ok {
return nil
}
return field
}

func (f FrontMatter) GetInt(fieldname string) int {
r, ok := f.getField(fieldname).(int)
if !ok {
return 0
}

return r
}

func (f FrontMatter) GetString(fieldname string) string {
r, ok := f.getField(fieldname).(string)
if !ok {
return ""
}

return r
}

func (f FrontMatter) GetStrings(fieldname string) []string {
ss, ok := f.getField(fieldname).([]string)
if ok {
return ss
}

si, ok := f.getField(fieldname).([]interface{})
if !ok {
return nil
}

result := make([]string, 0, len(si))
for _, s := range si {
r, ok := s.(string)
if !ok {
continue
}

result = append(result, r)
}

return result
}

// MarkdownContent for hugo.
// https://gohugo.io/content-management/formats/
type MarkdownContent struct {
// FrontMatter is metadata for this content
FrontMatter FrontMatter
// Body for this content
Body string
}

// Dump to string from this content.
func (m *MarkdownContent) Dump() (string, error) {
meta, err := yaml.Marshal(m.FrontMatter)
if err != nil {
return "", failure.Wrap(err, failure.WithCode(ErrFileContentMismatch))
}

return fmt.Sprintf(`%s%s
%s%s`,
hugoSeparator, strings.TrimSpace(string(meta)), hugoSeparator, m.Body), nil
}

// see https://gohugo.io/content-management/front-matter/#front-matter-formats
const (
hugoSeparator = "---\n"
hugoContentLength = 3
)

// ErrFileContentMismatch on specified filepath.
var ErrFileContentMismatch = failure.StringCode("file content mismatch")

// ParseMarkdownWithYaml from any reader to make MarkdownContent struct.
func ParseMarkdownWithYaml(r io.Reader) (*MarkdownContent, error) {
raw, err := ioutil.ReadAll(r)
if err != nil {
return nil, failure.Wrap(err)
}

content := strings.Split(string(raw), hugoSeparator)
if len(content) < hugoContentLength {
return nil, failure.New(ErrFileContentMismatch)
}

c := &MarkdownContent{
Body: strings.Join(content[2:], hugoSeparator),
}

if err := yaml.Unmarshal([]byte(content[1]), &c.FrontMatter); err != nil {
return nil, failure.Wrap(err, failure.WithCode(ErrFileContentMismatch))
}

return c, nil
}
Loading

0 comments on commit 6e42f8d

Please sign in to comment.