Skip to content

Commit

Permalink
Merge pull request #1 from marcusleonas/develop
Browse files Browse the repository at this point in the history
Added build & serve commands
  • Loading branch information
marcusleonas authored Aug 6, 2024
2 parents 7189ba0 + 950b5bf commit d4e84a3
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cmd/assets/pico.min.css

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions cmd/assets/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/pico.min.css" />
<title>{{ .Title }}</title>
</head>
<body>
{{ .Content }}
</body>
</html>
148 changes: 148 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package cmd

import (
"bytes"
"embed"
"fmt"
"html/template"
"log"
"os"
"path/filepath"
"strings"

"github.com/marcusleonas/notebutler/lib"
"github.com/spf13/cobra"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/renderer/html"
)

var buildCmd = &cobra.Command{
Use: "build",
Short: "Build your notes to a html site",
Long: "Build your notes to a static html site, with picocss.",
Run: build,
}

func init() {
rootCmd.AddCommand(buildCmd)
}

//go:embed assets/*
var embedded embed.FS

func build(cmd *cobra.Command, args []string) {
lib.Check() // Checks if notebutler is initialised

notesDir := "notes"
htmlDir := "html"

if _, err := os.Stat(htmlDir); err == nil {
err = os.RemoveAll(htmlDir)
if err != nil {
log.Fatal(err)
}
}

err := os.MkdirAll(htmlDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}

copyCSS(htmlDir)

err = filepath.Walk(notesDir, func(p string, i os.FileInfo, err error) error {
if err != nil {
return err
}

if !i.IsDir() && strings.HasSuffix(p, ".md") {
convert(p, htmlDir)
}
return nil
})

if err != nil {
log.Fatal(err)
}

fmt.Println("Build successful!")
}

func copyCSS(htmlDir string) {
baseCSSTemplate, err := embedded.ReadFile("assets/pico.min.css")
if err != nil {
log.Fatal(err)
}

file, err := os.Create(filepath.Join(htmlDir, "pico.min.css"))
if err != nil {
log.Fatal(err)
}
defer file.Close()

_, err = file.Write(baseCSSTemplate)
if err != nil {
log.Fatal(err)
}
}

func convert(p string, htmlDir string) {
content, err := os.ReadFile(p)
if err != nil {
log.Fatal(err)
}

htmlContent := convertMarkdownToHTML(content)
if err != nil {
log.Fatal(err)
}
path := filepath.Join(htmlDir, strings.Replace(filepath.Base(p), ".md", ".html", 1))
file, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()

_, err = file.Write(htmlContent)
if err != nil {
log.Fatal(err)
}
}

func convertMarkdownToHTML(mdContent []byte) []byte {
var buf strings.Builder

md := goldmark.New(
goldmark.WithRendererOptions(html.WithUnsafe()),
)

if err := md.Convert(mdContent, &buf); err != nil {
log.Fatal(err)
}

baseHTMLTemplate, err := embedded.ReadFile("assets/template.html")
if err != nil {
log.Fatal(err)
}

tmpl, err := template.New("base").Parse(string(baseHTMLTemplate))
if err != nil {
log.Fatal(err)
}

data := struct {
Title string
Content template.HTML
}{
Title: "Notes", // Adjust title as needed
Content: template.HTML(buf.String()),
}

var finalHTML bytes.Buffer
err = tmpl.Execute(&finalHTML, data)
if err != nil {
log.Fatal(err)
}

return finalHTML.Bytes()
}
29 changes: 21 additions & 8 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
Expand All @@ -13,6 +14,7 @@ import (
stdTemplate "text/template"

"github.com/charmbracelet/huh"
"github.com/marcusleonas/notebutler/lib"
"github.com/spf13/cobra"
)

Expand All @@ -39,14 +41,17 @@ func new(cmd *cobra.Command, args []string) {
template += ".md"
}

if _, err := os.Stat(".notebutler"); os.IsNotExist(err) {
fmt.Println("Config directory (.notebutler) not found. Notebutler not initialized. Run `notebutler init` to initialize.")
os.Exit(1)
lib.Check() // Checks if notebutler is initialised

configBytes, err := os.ReadFile("notebutler.json")
if err != nil {
log.Fatal(err)
}

if _, err := os.Stat("notebutler.json"); os.IsNotExist(err) {
fmt.Println("Config file (notebutler.json) not found. Notebutler not initialized. Run `notebutler init` to initialize.")
os.Exit(1)
var config Config
err = json.Unmarshal(configBytes, &config)
if err != nil {
log.Fatal(err)
}

templates, err := os.ReadDir(".notebutler/templates")
Expand Down Expand Up @@ -94,13 +99,21 @@ func new(cmd *cobra.Command, args []string) {
log.Fatal(err)
}

now := time.Now().Format("2006-01-02 15:04:05")
d := time.Now().Format("01/02/2006")
t := time.Now().Format("15:04:05")
n := time.Now().Format("2006-01-02 15:04:05")
data := struct {
Name string
Notebook string
CreatedAt string
Date string
Time string
}{
Name: name,
CreatedAt: now,
Notebook: config.NotebookName,
CreatedAt: n,
Date: d,
Time: t,
}

var result bytes.Buffer
Expand Down
43 changes: 43 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"
"log"
"net/http"
"os"

"github.com/spf13/cobra"
)

var serveCmd = &cobra.Command{
Use: "serve",
Short: "Serve the generated HTML site locally",
Long: "Start a local server to preview the generated HTML site.",
Run: serve,
}

func init() {
rootCmd.AddCommand(serveCmd)
}

func serve(cmd *cobra.Command, args []string) {
htmlDir := "html"

// Ensure the directory exists
if _, err := os.Stat(htmlDir); os.IsNotExist(err) {
log.Fatalf("The directory %s does not exist. Please run `notebutler build` first.", htmlDir)
}

// Create a file server to serve the static files
fs := http.FileServer(http.Dir(htmlDir))

http.Handle("/", fs)

port := 8080
fmt.Printf("Serving files from %s on http://localhost:%d\n", htmlDir, port)

// Start the server
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
log.Fatal(err)
}
}
18 changes: 18 additions & 0 deletions lib/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lib

import (
"fmt"
"os"
)

func Check() {
if _, err := os.Stat(".notebutler"); os.IsNotExist(err) {
fmt.Println("Config directory (.notebutler) not found. Notebutler not initialized. Run `notebutler init` to initialize.")
os.Exit(1)
}

if _, err := os.Stat("notebutler.json"); os.IsNotExist(err) {
fmt.Println("Config file (notebutler.json) not found. Notebutler not initialized. Run `notebutler init` to initialize.")
os.Exit(1)
}
}

0 comments on commit d4e84a3

Please sign in to comment.