static web generator written in Go
Files | Log | Commits | Refs | README
Author: SM
Date: 2025-08-27
Subject: init
commit 48306f4c9d8b99a3465ce2863312e96e2eb94106 Author: SM <seb.michalk@gmail.com> Date: Wed Aug 27 10:45:41 2025 +0200 init diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..148a591 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Sebastian + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c84ccd7 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +mist +==== +*m*inimal stat*i*c *s*i*t*e generator written in Go. + +mist generates static HTML pages from markdown files in a flat directory +structure. A simple, more lightweight solution for basic static sites. + +mist includes a built-in web server. + +Usage +----- +Generate static site: + go run mist.go [-i input_dir] [-o output_dir] [-t template_file] + +Serve generated site: + go run mist.go -s port_number + +Options: + -i string input directory (default ".") + -o string output directory (default "out") + -t string template file (default "page.tmpl") + -s string serve on port (-s 8080) + +Template Variables +------------------ +provides the following template variables: + + .Title string // page title (filename without .md extension) + .Content string // HTML content from markdown + .Nav []string // array of other markdown files in directory + +Example template usage: + <h1>{{.Title}}</h1> + <div>{{.Content}}</div> + <nav> + {{range .Nav}} + <a href="{{.}}.html">{{.}}</a> + {{end}} + </nav> + +Features +-------- +- Flat directory structure +- Built-in development server +- Simple Go template system +- Minimal dependencies diff --git a/example/about.md b/example/about.md new file mode 100644 index 0000000..b369734 --- /dev/null +++ b/example/about.md @@ -0,0 +1,20 @@ +# About Us + +At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident. + +## Our Mission + +Similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus. + +## Our Story + +Id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. + +Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. + +## Team + +- **Lorem Ipsum** - Founder & CEO +- **Dolor Sit** - Lead Developer +- **Amet Consectetur** - Designer +- **Adipiscing Elit** - Marketing Director \ No newline at end of file diff --git a/example/contact.md b/example/contact.md new file mode 100644 index 0000000..7642156 --- /dev/null +++ b/example/contact.md @@ -0,0 +1,27 @@ +# Contact Us + +Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. + +## Get In Touch + +Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur. + +### Contact Information + +**Address:** +123 Lorem Street +Ipsum City, IC 12345 + +**Phone:** (555) 123-4567 +**Email:** hello@example.com + +**Business Hours:** +Monday - Friday: 9:00 AM - 6:00 PM +Saturday: 10:00 AM - 4:00 PM +Sunday: Closed + +## Send us a Message + +Vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores. + +*Note: This is a demo contact page. Please replace with actual contact information.* \ No newline at end of file diff --git a/example/index.md b/example/index.md new file mode 100644 index 0000000..4f41c8e --- /dev/null +++ b/example/index.md @@ -0,0 +1,13 @@ +# Welcome to Mist + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. + +## About This Site + +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. + +## Getting Started + +Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. \ No newline at end of file diff --git a/example/page.tmpl b/example/page.tmpl new file mode 100644 index 0000000..68f95ca --- /dev/null +++ b/example/page.tmpl @@ -0,0 +1,30 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>{{.Title}} - Mist Demo Site</title> + <link rel="stylesheet" href="style.css"> +</head> +<body> + <header> + <h1>Mist Demo Site</h1> + <nav> + <a href="index.html">Home</a> + {{range .Nav}} + <a href="{{.}}.html">{{. | title}}</a> + {{end}} + </nav> + </header> + + <main> + <article> + {{.Content}} + </article> + </main> + + <footer> + <p>© 2025 Mist Demo Site. Generated with <a href="https://github.com/nyangkosense/mist">mist</a>.</p> + </footer> +</body> +</html> diff --git a/mist.go b/mist.go new file mode 100644 index 0000000..6089156 --- /dev/null +++ b/mist.go @@ -0,0 +1,126 @@ +/* mist - minimal static site generator */ +/* see LICENSE for copyright and license details */ + +package main + +import ( + "flag" + "io" + "log" + "net/http" + "os" + "path/filepath" + "strings" + "text/template" + + "github.com/gomarkdown/markdown" +) + +type Page struct { + Title string + Content string + Nav []string +} + +func die(err error) { + if err != nil { + log.Fatal(err) + } +} + +func getTitle(path string) string { + name := filepath.Base(path) + return strings.TrimSuffix(name, ".md") +} + +func mkNav(dir string) []string { + files, err := os.ReadDir(dir) + die(err) + var nav []string + for _, f := range files { + if strings.HasSuffix(f.Name(), ".md") && f.Name() != "index.md" { + name := strings.TrimSuffix(f.Name(), ".md") + nav = append(nav, name) + } + } + return nav +} + +func copy(src, dst string) { + in, err := os.Open(src) + die(err) + defer in.Close() + out, err := os.Create(dst) + die(err) + defer out.Close() + io.Copy(out, in) +} + +func process(src, dst, tpl string) { + os.MkdirAll(dst, 0755) + + copy("style.css", filepath.Join(dst, "style.css")) + copy(tpl, filepath.Join(dst, "template.html")) + files, err := os.ReadDir(src) + die(err) + t, err := template.ParseFiles(tpl) + die(err) + nav := mkNav(src) + + for _, f := range files { + if strings.HasSuffix(f.Name(), ".md") { + md, err := os.ReadFile(filepath.Join(src, f.Name())) + die(err) + + page := Page{ + Title: getTitle(filepath.Join(src, f.Name())), + Content: string(markdown.ToHTML(md, nil, nil)), + Nav: nav, + } + + name := strings.TrimSuffix(f.Name(), ".md") + if name == "index" { + name = "index.html" + } else { + name += ".html" + } + + out, err := os.Create(filepath.Join(dst, name)) + die(err) + die(t.Execute(out, page)) + out.Close() + } else { + copy(filepath.Join(src, f.Name()), filepath.Join(dst, f.Name())) + } + } +} + +func serve(dir, port string) { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + if path == "/" { + path = "/index.html" + } else if !strings.HasSuffix(path, ".html") && !strings.Contains(path, ".") { + if _, err := os.Stat(filepath.Join(dir, path[1:]+".html")); err == nil { + path += ".html" + } + } + http.ServeFile(w, r, filepath.Join(dir, path[1:])) + }) + log.Printf("serving %s on :%s", dir, port) + log.Fatal(http.ListenAndServe(":"+port, nil)) +} + +func main() { + src := flag.String("i", ".", "input dir") + dst := flag.String("o", "out", "output dir") + tpl := flag.String("t", "page.tmpl", "template") + port := flag.String("s", "", "serve on port") + flag.Parse() + + if *port != "" { + serve(*dst, *port) + } else { + process(*src, *dst, *tpl) + } +} diff --git a/page.tmpl b/page.tmpl new file mode 100644 index 0000000..06d80e3 --- /dev/null +++ b/page.tmpl @@ -0,0 +1,29 @@ +<!doctype html> +<html lang="en"> +<head> +<meta charset="utf-8"> +<meta name="viewport" content="width=device-width, initial-scale=1"> +<title>{{.Title}}</title> +<link rel="stylesheet" href="style.css"> +</head> +<body> +<header> +<h1>{{.Title}}</h1> +</header> + +{{if .Nav}} +<nav title="Site navigation"> +<a href="/">home</a> {{range .Nav}}<a href="/{{.}}">{{.}}</a> {{end}} +</nav> +{{end}} + +<main> +{{.Content}} +</main> + +<footer> +<hr> +<small>powered by mist</small> +</footer> +</body> +</html> diff --git a/style.css b/style.css new file mode 100644 index 0000000..1e0d206 --- /dev/null +++ b/style.css @@ -0,0 +1,114 @@ +/* Mist Demo Site Styles */ + +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +body { + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; + line-height: 1.6; + color: #333; + max-width: 800px; + margin: 0 auto; + padding: 20px; + background-color: #fff; +} + +header { + border-bottom: 1px solid #eee; + padding-bottom: 20px; + margin-bottom: 30px; +} + +header h1 { + font-size: 2em; + margin-bottom: 10px; + color: #2c3e50; +} + +nav { + display: flex; + gap: 20px; +} + +nav a { + color: #3498db; + text-decoration: none; + padding: 5px 10px; + border-radius: 3px; + transition: background-color 0.2s; +} + +nav a:hover { + background-color: #f8f9fa; + color: #2980b9; +} + +main { + margin-bottom: 40px; +} + +article h1 { + color: #2c3e50; + margin-bottom: 20px; + font-size: 1.8em; +} + +article h2 { + color: #34495e; + margin: 30px 0 15px 0; + font-size: 1.4em; +} + +article h3 { + color: #34495e; + margin: 25px 0 10px 0; + font-size: 1.2em; +} + +article p { + margin-bottom: 15px; + text-align: justify; +} + +article ul { + margin: 15px 0 15px 30px; +} + +article li { + margin-bottom: 5px; +} + +footer { + border-top: 1px solid #eee; + padding-top: 20px; + text-align: center; + color: #7f8c8d; + font-size: 0.9em; +} + +footer a { + color: #3498db; + text-decoration: none; +} + +footer a:hover { + text-decoration: underline; +} + +@media (max-width: 600px) { + body { + padding: 10px; + } + + nav { + flex-direction: column; + gap: 10px; + } + + header h1 { + font-size: 1.5em; + } +} \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..cd9b928 --- /dev/null +++ b/test.sh @@ -0,0 +1,5 @@ +#!/bin/sh +go build +./mist -i example +open http://localhost:8080 & +./mist -s 8080