-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from marcusleonas/develop
Added build & serve commands
- Loading branch information
Showing
6 changed files
with
246 additions
and
8 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |