Skip to content

Commit

Permalink
add preview command
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowOnPaper committed Dec 15, 2023
1 parent 2592807 commit 68efb15
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 26 deletions.
7 changes: 3 additions & 4 deletions docs/malta.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
"pages": [
["Getting started", "/basics/setup"],
["Writing pages", "/basics/pages"],
["Configuring the sidebar", "/basics/sidebar"]
["Configuring the sidebar", "/basics/sidebar"],
["Commands", "/basics/commands"]
]
},
{
"title": "Guides",
"pages": [
["Deploying with GitHub Actions", "/guides/github-actions"]
]
"pages": [["Deploying with GitHub Actions", "/guides/github-actions"]]
}
]
}
29 changes: 29 additions & 0 deletions docs/pages/basics/commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: "Commands"
---

# Commands

## `build`

Generates HTML files to the `dist` directory.

```
malta build
```

## `preview`

Runs a preview server on localhost (port 3000) for the generated site.

```
malta preview
```

```
malta preview --port 5000
```

### Options

- `--port` (`-p`): Localhost port (number - `3000` by default)
5 changes: 3 additions & 2 deletions docs/pages/basics/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ Welcome to my documentation site.

## Generate HTML

Run `malta`, and your HTML files will be generated in the `dist` directory.
Run the `build` command to generate HTML files in the `dist` directory, and the `preview` to preview your site at port 3000 (default)

```
malta
malta build
malta preview
```
7 changes: 6 additions & 1 deletion docs/pages/basics/sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ Malta does not automatically generate the sidebar (navigation bar). You can defi
"pages": [
["Getting started", "/basics/setup"],
["Writing pages", "/basics/pages"],
["Configuring the sidebar", "/basics/sidebar"]
["Configuring the sidebar", "/basics/sidebar"],
["Commands", "/basics/commands"]
]
},
{
"title": "Guides",
"pages": [["Deploying with GitHub Actions", "/guides/github-actions"]]
}
]
}
Expand Down
26 changes: 14 additions & 12 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
rm -rf bin
cd src

echo 'building bin/darwin-amd64/malta'
GOOS=darwin GOARCH=amd64 go build -o bin/darwin-amd64/malta
echo 'building bin/darwin-arm64/malta'
GOOS=darwin GOARCH=arm64 go build -o bin/darwin-arm64/malta
echo 'building darwin-amd64...'
GOOS=darwin GOARCH=amd64 go build -o ../bin/darwin-amd64/malta
echo 'building darwin-arm64...'
GOOS=darwin GOARCH=arm64 go build -o ../bin/darwin-arm64/malta

echo 'building bin/linux-amd64/malta'
GOOS=linux GOARCH=amd64 go build -o bin/linux-amd64/malta
echo 'building bin/linux-arm64/malta'
GOOS=linux GOARCH=arm64 go build -o bin/linux-arm64/malta
echo 'building linux-amd64...'
GOOS=linux GOARCH=amd64 go build -o ../bin/linux-amd64/malta
echo 'building linux-arm64...'
GOOS=linux GOARCH=arm64 go build -o ../bin/linux-arm64/malta

echo 'building bin/windows-amd64/malta'
GOOS=windows GOARCH=amd64 go build -o bin/windows-amd64/malta
echo 'building bin/windows-386/malta'
GOOS=windows GOARCH=386 go build -o bin/windows-386/malta
echo 'building windows-amd64...'
GOOS=windows GOARCH=amd64 go build -o ../bin/windows-amd64/malta
echo 'building windows-386...'
GOOS=windows GOARCH=386 go build -o ../bin/windows-386/malta

cd ..
cd bin
for dir in $(ls -d *); do
tar cfzv "$dir".tgz $dir
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package build

import (
"bytes"
Expand Down
8 changes: 2 additions & 6 deletions malta.go → src/commands/build/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package build

import (
"bytes"
Expand Down Expand Up @@ -38,11 +38,7 @@ var mainCss []byte
//go:embed assets/markdown.css
var markdownCss []byte

func main() {
os.Exit(run())
}

func run() int {
func BuildCommand() int {
configJson, err := os.ReadFile("malta.config.json")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand Down
84 changes: 84 additions & 0 deletions src/commands/preview/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package preview

import (
"fmt"
"net/http"
"os"
"path"
"strconv"
"strings"
)

func PreviewCommand() int {
port := 3000
args := parseArgs(os.Args[2:])
portArg, ok := args["p"]
if !ok {
portArg, ok = args["port"]
}
if ok {
parsedPort, err := strconv.Atoi(portArg)
if err != nil {
fmt.Println("Invalid argument: 'port' must be a number")
return 1
}
port = parsedPort
}

http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
html, err := os.ReadFile(path.Join("dist", req.URL.Path+".html"))
if err != nil {
html, err = os.ReadFile(path.Join("dist", req.URL.Path, "index.html"))
if err != nil {
data, err := os.ReadFile(path.Join("dist", req.URL.Path))
if err != nil {
w.WriteHeader(404)
w.Write([]byte("404 - Not found"))
return
}
if strings.HasSuffix(req.URL.Path, ".css") {
w.Header().Set("Content-Type", "text/css")
}
w.WriteHeader(200)
w.Write(data)
return
}
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(200)
w.Write(html)
})
fmt.Printf("Starting server on port %v...\n", port)
err := http.ListenAndServe(fmt.Sprintf(":%v", port), nil)
fmt.Println(err)
return 1
}

func parseArgs(argList []string) map[string]string {
args := make(map[string]string)
for i := 0; i < len(argList); i++ {
item := argList[i]
if item == "" {
continue
}
if !strings.HasPrefix(item, "-") {
continue
}
item = strings.TrimLeft(item, "-")
if strings.Contains(item, "=") {
keyValue := strings.Split(item, "=")
key := keyValue[0]
args[key] = ""
if len(keyValue) > 1 {
args[key] = keyValue[1]
}
continue
}
if i+1 == len(argList) || strings.HasPrefix(argList[i+1], "-") {
args[item] = ""
continue
}
args[item] = argList[i+1]
}
return args
}
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"os"

"github.com/pilcrowOnPaper/malta/commands/build"
"github.com/pilcrowOnPaper/malta/commands/preview"
)

func main() {
if len(os.Args) == 1 {
fmt.Print(`
Usage:
malta build - build and generate HTML files
malta preview - preview build
`)
os.Exit(0)
}
if os.Args[1] == "build" {
os.Exit(build.BuildCommand())
}
if os.Args[1] == "preview" {
os.Exit(preview.PreviewCommand())
}
fmt.Printf("Unknown command: %s\n", os.Args[1])
os.Exit(1)
}

0 comments on commit 68efb15

Please sign in to comment.