Skip to content

Commit

Permalink
fix: fixed wizard
Browse files Browse the repository at this point in the history
Co-authored-by: Aditya Hegde <[email protected]>

- switch config files to json format
- fixed collection and tags subpage sorts
  • Loading branch information
anirudhsudhir committed Jul 5, 2024
1 parent c1ffca9 commit 91fc217
Show file tree
Hide file tree
Showing 17 changed files with 414 additions and 358 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ serve:
@echo "anna: serving site"
@go build
@./anna -s "site/"
wizard:
@echo "anna: running wizard"
@go build
@./anna -w
tests:
@echo "anna: running all tests"
@go test ./...
Expand Down
5 changes: 5 additions & 0 deletions anna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"siteDataPaths": {
"site": "site/"
}
}
2 changes: 0 additions & 2 deletions anna.yml

This file was deleted.

64 changes: 28 additions & 36 deletions cmd/anna/anna.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package anna

import (
"encoding/json"
"html/template"
"log"
"os"
Expand All @@ -9,7 +10,6 @@ import (
"github.com/anna-ssg/anna/v2/pkg/engine"
"github.com/anna-ssg/anna/v2/pkg/helpers"
"github.com/anna-ssg/anna/v2/pkg/parser"
"gopkg.in/yaml.v3"
)

type Cmd struct {
Expand All @@ -25,29 +25,29 @@ type Cmd struct {
}

type AnnaConfig struct {
SiteDataPaths []map[string]string `yaml:"siteDataPaths"`
SiteDataPaths map[string]string `json:"siteDataPaths"`
}

func (cmd *Cmd) VanillaRenderManager() {

// Check if the configuration file exists
// If it does not, render only the site/ directory

_, err := os.Stat("anna.yml")
_, err := os.Stat("anna.json")
if os.IsNotExist(err) {
cmd.VanillaRender("site/")
return
}

// Read and parse the configuration file
annaConfigFile, err := os.ReadFile("anna.yml")
annaConfigFile, err := os.ReadFile("anna.json")
if err != nil {
cmd.ErrorLogger.Fatal(err)
}

var annaConfig AnnaConfig

err = yaml.Unmarshal(annaConfigFile, &annaConfig)
err = json.Unmarshal(annaConfigFile, &annaConfig)
if err != nil {
cmd.ErrorLogger.Fatal(err)
}
Expand All @@ -56,13 +56,11 @@ func (cmd *Cmd) VanillaRenderManager() {
if cmd.RenderSpecificSite == "" {
siteRendered := false

for _, sites := range annaConfig.SiteDataPaths {
for _, path := range sites {
if !siteRendered {
siteRendered = true
}
cmd.VanillaRender(path)
for _, path := range annaConfig.SiteDataPaths {
if !siteRendered {
siteRendered = true
}
cmd.VanillaRender(path)
}

// If no site has been rendered due to empty "anna.yml", render the default "site/" path
Expand All @@ -72,12 +70,10 @@ func (cmd *Cmd) VanillaRenderManager() {
} else {
siteRendered := false

for _, sites := range annaConfig.SiteDataPaths {
for _, sitePath := range sites {
if strings.Compare(cmd.RenderSpecificSite, sitePath) == 0 {
cmd.VanillaRender(sitePath)
siteRendered = true
}
for _, sitePath := range annaConfig.SiteDataPaths {
if strings.Compare(cmd.RenderSpecificSite, sitePath) == 0 {
cmd.VanillaRender(sitePath)
siteRendered = true
}
}

Expand All @@ -96,34 +92,32 @@ func (cmd *Cmd) ValidateHTMLManager() {
// Check if the configuration file exists
// If it does not, validate only the site/ directory

_, err := os.Stat("anna.yml")
_, err := os.Stat("anna.json")
if os.IsNotExist(err) {
cmd.VanillaRender("site/")
return
}

// Read and parse the configuration file
annaConfigFile, err := os.ReadFile("anna.yml")
annaConfigFile, err := os.ReadFile("anna.json")
if err != nil {
cmd.ErrorLogger.Fatal(err)
}

var annaConfig AnnaConfig

err = yaml.Unmarshal(annaConfigFile, &annaConfig)
err = json.Unmarshal(annaConfigFile, &annaConfig)
if err != nil {
cmd.ErrorLogger.Fatal(err)
}

// Validating sites
validatedSites := false

for _, sites := range annaConfig.SiteDataPaths {
for _, sitePath := range sites {
cmd.ValidateHTMLContent(sitePath)
if !validatedSites {
validatedSites = true
}
for _, sitePath := range annaConfig.SiteDataPaths {
cmd.ValidateHTMLContent(sitePath)
if !validatedSites {
validatedSites = true
}
}

Expand All @@ -139,21 +133,21 @@ func (cmd *Cmd) LiveReloadManager() {
// Check if the configuration file exists
// If it does not, serve only the site/ directory

_, err := os.Stat("anna.yml")
_, err := os.Stat("anna.json")
if os.IsNotExist(err) {
cmd.StartLiveReload("site/")
return
}

// Read and parse the configuration file
annaConfigFile, err := os.ReadFile("anna.yml")
annaConfigFile, err := os.ReadFile("anna.json")
if err != nil {
cmd.ErrorLogger.Fatal(err)
}

var annaConfig AnnaConfig

err = yaml.Unmarshal(annaConfigFile, &annaConfig)
err = json.Unmarshal(annaConfigFile, &annaConfig)
if err != nil {
cmd.ErrorLogger.Fatal(err)
}
Expand All @@ -162,12 +156,10 @@ func (cmd *Cmd) LiveReloadManager() {
if cmd.ServeSpecificSite == "" {
cmd.StartLiveReload("site/")
} else {
for _, sites := range annaConfig.SiteDataPaths {
for _, sitePath := range sites {
if strings.Compare(cmd.ServeSpecificSite, sitePath) == 0 {
cmd.StartLiveReload(sitePath)
return
}
for _, sitePath := range annaConfig.SiteDataPaths {
if strings.Compare(cmd.ServeSpecificSite, sitePath) == 0 {
cmd.StartLiveReload(sitePath)
return
}
}

Expand Down Expand Up @@ -205,7 +197,7 @@ func (cmd *Cmd) VanillaRender(siteDirPath string) {

helper.CreateRenderedDir(siteDirPath)

p.ParseConfig(siteDirPath + "layout/config.yml")
p.ParseConfig(siteDirPath + "layout/config.json")
p.ParseRobots(siteDirPath+"layout/robots.txt", siteDirPath+"rendered/robots.txt")

fileSystem := os.DirFS(siteDirPath + "content/")
Expand Down
2 changes: 1 addition & 1 deletion cmd/anna/livereload.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newLiveReload(siteDataPath string) *liveReload {
errorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
fileTimes: make(map[string]time.Time),
rootDirs: []string{siteDataPath},
extensions: []string{".go", ".md", ".yml"},
extensions: []string{".go", ".md", ".json"},
siteDataPath: siteDataPath,
}
return &lr
Expand Down
79 changes: 43 additions & 36 deletions cmd/anna/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,47 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"

"gopkg.in/yaml.v3"
"github.com/anna-ssg/anna/v2/pkg/parser"
)

type Config struct {
BaseURL string `yaml:"baseURL"`
SiteTitle string `yaml:"siteTitle"`
SiteScripts []string `yaml:"siteScripts"`
Author string `yaml:"author"`
ThemeURL string `yaml:"themeURL"`
Navbar []string `yaml:"navbar"`
}

type WizardServer struct {
server *http.Server
server *http.Server
serveMux *http.ServeMux

// Common logger for all parser functions
InfoLogger *log.Logger
// Common logger for all parser functions
ErrorLogger *log.Logger
}

var FormSubmittedCh = make(chan struct{})

func NewWizardServer(addr string) *WizardServer {
return &WizardServer{
server: &http.Server{
Addr: addr,
},
serveMuxLocal := http.NewServeMux()

wizardServer := WizardServer{
serveMux: serveMuxLocal,
server: &http.Server{Addr: addr, Handler: serveMuxLocal},
InfoLogger: log.New(os.Stderr, "INFO\t", log.Ldate|log.Ltime),
ErrorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
}

return &wizardServer
}

func (ws *WizardServer) Start() {
http.HandleFunc("/submit", ws.handleSubmit)
ws.serveMux.HandleFunc("/submit", ws.handleSubmit)
fs := http.FileServer(http.Dir("./site/static/wizard"))
http.Handle("/", fs)
fmt.Printf("Wizard is running at: http://localhost%s\n", ws.server.Addr)
ws.serveMux.Handle("/", fs)
ws.InfoLogger.Printf("Wizard is running at: http://localhost%s\n", ws.server.Addr)

if err := ws.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Could not start server: %v", err)
ws.ErrorLogger.Fatalf("Could not start server: %v", err)
}
}

Expand All @@ -51,44 +55,47 @@ func (ws *WizardServer) Stop() error {
func (ws *WizardServer) handleSubmit(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
ws.ErrorLogger.Println("Method not allowed")
return
}
var config Config

var config parser.LayoutConfig
err := json.NewDecoder(r.Body).Decode(&config)

err = ws.writeConfigToFile(config)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = writeConfigToFile(config)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
ws.ErrorLogger.Println(err)
return
}
FormSubmittedCh <- struct{}{}
}

func writeConfigToFile(config Config) error {
configFilePath := "./site/layout/config.yml"
func (ws *WizardServer) writeConfigToFile(config parser.LayoutConfig) error {
configFilePath := "./site/layout/config.json"
if err := os.MkdirAll(filepath.Dir(configFilePath), 0755); err != nil {
return err
}

file, err := os.Create(configFilePath)
marshaledJsonConfig, err := json.Marshal(config)
if err != nil {
ws.ErrorLogger.Fatal(err)
}

configFile, err := os.Create(configFilePath)
if err != nil {
return err
}
defer func() {
err = file.Close()
err = configFile.Close()
if err != nil {
log.Fatal(err)
ws.ErrorLogger.Fatal(err)
}
}()
os.WriteFile(configFilePath, marshaledJsonConfig, 0666)

// Encode the config into YAML format and write it to the file.
if err := yaml.NewEncoder(file).Encode(&config); err != nil {
if err != nil {
return err
}

return nil
}

var FormSubmittedCh = make(chan struct{})
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
RenderSpecificSite: renderSpecificSite,
ServeSpecificSite: serve,
ErrorLogger: log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
InfoLogger: log.New(os.Stderr, "LOG\t", log.Ldate|log.Ltime|log.Lshortfile),
InfoLogger: log.New(os.Stderr, "LOG\t", log.Ldate|log.Ltime),
}

if serve != "" {
Expand Down Expand Up @@ -61,9 +61,9 @@ func main() {
if err := server.Stop(); err != nil {
annaCmd.InfoLogger.Println(err)
}
annaCmd.VanillaRenderManager()
annaCmd.LiveReloadManager()
}

annaCmd.VanillaRenderManager()
},
}
Expand Down
Loading

0 comments on commit 91fc217

Please sign in to comment.