From 103e84d847262830bbaa550b37218e9ca8b317d3 Mon Sep 17 00:00:00 2001 From: Himanshu Sardana Date: Thu, 26 Mar 2026 21:26:35 +0000 Subject: refactor: split into cmd, pkg --- pkg/config/config.go | 29 ++++++++++++++++++++++ pkg/content/content.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/themes/themes.go | 40 ++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 pkg/config/config.go create mode 100644 pkg/content/content.go create mode 100644 pkg/themes/themes.go (limited to 'pkg') diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..56f40aa --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,29 @@ +package config + +import ( + "os" + + "gopkg.in/yaml.v2" +) + +type Config struct { + SiteTitle string `yaml:"siteTitle"` + AuthorName string `yaml:"authorName"` + AuthorRole string `yaml:"authorRole"` + AuthorBio string `yaml:"authorBio"` + DefaultTheme string `yaml:"defaultTheme"` +} + +func Load(path string) (*Config, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + + var cfg Config + if err := yaml.Unmarshal(data, &cfg); err != nil { + return nil, err + } + + return &cfg, nil +} diff --git a/pkg/content/content.go b/pkg/content/content.go new file mode 100644 index 0000000..934b2ee --- /dev/null +++ b/pkg/content/content.go @@ -0,0 +1,66 @@ +package content + +import ( + "io/fs" + "path/filepath" + "strings" +) + +type Frontmatter struct { + Title string `yaml:"title"` + Date string `yaml:"date"` + Tags []string `yaml:"tags"` +} + +type PostSummary struct { + Title string + Slug string + Date string + Tags []string +} + +type ContentFile struct { + Path string + Slug string + Frontmatter Frontmatter +} + +func ListContentFiles(contentDir string) ([]ContentFile, error) { + var files []ContentFile + + err := filepath.WalkDir(contentDir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if d.IsDir() { + return nil + } + + if strings.HasSuffix(d.Name(), ".md") { + slug := strings.TrimSuffix(d.Name(), ".md") + files = append(files, ContentFile{ + Path: path, + Slug: slug, + }) + } + + return nil + }) + + if err != nil { + return nil, err + } + + return files, nil +} + +func GetOutputPath(contentDir, contentPath, outputDir string) (string, error) { + relPath, err := filepath.Rel(contentDir, contentPath) + if err != nil { + return "", err + } + + outputFilePath := filepath.Join(outputDir, strings.Replace(relPath, ".md", "/index.html", 1)) + return outputFilePath, nil +} diff --git a/pkg/themes/themes.go b/pkg/themes/themes.go new file mode 100644 index 0000000..a8af863 --- /dev/null +++ b/pkg/themes/themes.go @@ -0,0 +1,40 @@ +package themes + +import ( + "os" + "path/filepath" +) + +type Theme struct { + Name string + Path string +} + +func List(themesDir string) ([]Theme, error) { + entries, err := os.ReadDir(themesDir) + if err != nil { + return nil, err + } + + var themes []Theme + for _, entry := range entries { + if entry.IsDir() { + themes = append(themes, Theme{ + Name: entry.Name(), + Path: filepath.Join(themesDir, entry.Name()), + }) + } + } + + return themes, nil +} + +func GetThemePath(themesDir, themeName string) string { + return filepath.Join(themesDir, themeName) +} + +func ThemeExists(themesDir, themeName string) bool { + path := GetThemePath(themesDir, themeName) + info, err := os.Stat(path) + return err == nil && info.IsDir() +} -- cgit v1.3.1