Skip to content

Commit 973e780

Browse files
committed
add: Support CSS styling of rendered content
1 parent a76e576 commit 973e780

File tree

14 files changed

+3529
-25
lines changed

14 files changed

+3529
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Support CSS styling of rendered content
13+
814
## [0.3.1] - 2023-11-09
915

1016
### Added

cmd/root.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
type RootCmdConfig struct {
2323
host string
2424
port string
25+
26+
style string
2527
}
2628

2729
func NewRootCmd() *command.Command {
@@ -44,6 +46,8 @@ func NewRootCmd() *command.Command {
4446
func (c *RootCmdConfig) RegisterFlags(fs *flag.FlagSet) {
4547
fs.StringVar(&c.host, "host", "127.0.0.1", "The address the server listens to.")
4648
fs.StringVar(&c.port, "port", "1337", "The port the server listens on.")
49+
50+
fs.StringVar(&c.style, "style", "plain", "The CSS style to use.")
4751
}
4852

4953
func (c *RootCmdConfig) Exec(ctx context.Context, args []string) error {
@@ -64,7 +68,10 @@ func (c *RootCmdConfig) run(ctx context.Context, file string) error {
6468
}
6569

6670
srv := server.Server{
67-
Title: r.File,
71+
Config: server.ServerConfig{
72+
Title: file,
73+
Style: c.style,
74+
},
6875
Renderer: r,
6976
}
7077

pkg/server/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
assets/

pkg/server/files.go

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,79 @@ import (
44
"embed"
55
"fmt"
66
"io/fs"
7+
"log"
78
"net/http"
9+
"strings"
10+
"text/template"
811
)
912

10-
//go:generate cp -r ../../web/static/ ./assets
11-
//go:embed assets/index.html
12-
//go:embed assets/js/client.js
13-
var assets embed.FS
13+
//go:generate mkdir -p assets
14+
//go:generate cp -r ../../web/static/ ./assets/static
15+
//go:generate cp -r ../../web/templates/ ./assets/templates
1416

15-
func fileServer() (http.Handler, error) {
16-
fsys, err := fs.Sub(assets, "assets")
17+
var (
18+
//go:embed assets/static
19+
staticFS embed.FS
20+
staticDir string = "assets/static"
21+
22+
//go:embed assets/templates
23+
templatesFS embed.FS
24+
templatesDir string = "assets/templates"
25+
)
26+
27+
func staticFileServer() (http.Handler, error) {
28+
fsys, err := fs.Sub(staticFS, staticDir)
1729
if err != nil {
1830
return nil, fmt.Errorf("error creating filesystem subtree: %w", err)
1931
}
2032

2133
return http.FileServer(http.FS(fsys)), nil
2234
}
35+
36+
type Style struct {
37+
Name string
38+
Variant string
39+
}
40+
41+
func rootHandler(s string) http.Handler {
42+
unpack := func(vals []string, vars ...*string) {
43+
for i, val := range vals {
44+
*vars[i] = val
45+
}
46+
}
47+
48+
var style Style
49+
unpack(strings.Split(s, ":"), &style.Name, &style.Variant)
50+
51+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52+
if r.URL.Path != "/" {
53+
http.NotFound(w, r)
54+
return
55+
}
56+
57+
templateFiles := []string{
58+
templatesDir + "/" + "index.html",
59+
templatesDir + "/" + "styles" + "/" + style.Name + ".html",
60+
}
61+
62+
ts, err := template.ParseFS(templatesFS, templateFiles...)
63+
if err != nil {
64+
log.Println(err.Error())
65+
status := http.StatusInternalServerError
66+
http.Error(w, http.StatusText(status), status)
67+
return
68+
}
69+
70+
data := struct {
71+
Style Style
72+
}{
73+
Style: style,
74+
}
75+
if err := ts.Execute(w, data); err != nil {
76+
log.Println(err.Error())
77+
status := http.StatusInternalServerError
78+
http.Error(w, http.StatusText(status), status)
79+
return
80+
}
81+
})
82+
}

pkg/server/server.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ import (
1111
"github.com/cluttrdev/showdown/pkg/content"
1212
)
1313

14+
type ServerConfig struct {
15+
Title string
16+
Style string
17+
}
18+
1419
type Server struct {
15-
Title string
20+
Config ServerConfig
1621
Renderer content.Renderer
1722

1823
sockets map[*websocket.Conn]context.CancelFunc
@@ -57,14 +62,15 @@ func (s *Server) Serve(ctx context.Context, addr string) error {
5762
}
5863

5964
func (s *Server) routes() (*http.ServeMux, error) {
60-
fs, err := fileServer()
65+
sfs, err := staticFileServer()
6166
if err != nil {
6267
return nil, err
6368
}
6469

6570
mux := http.NewServeMux()
6671

67-
mux.Handle("/", fs)
72+
mux.Handle("/", rootHandler(s.Config.Style))
73+
mux.Handle("/static/", http.StripPrefix("/static", sfs))
6874
mux.Handle("/ws", websocket.Handler(s.handleWebSocket))
6975
mux.HandleFunc("/shutdown", s.handleShutdown)
7076

pkg/server/sockets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (s *Server) handleWebSocket(ws *websocket.Conn) {
3333
}
3434
}()
3535

36-
if err := sendMessage(ws, MessageTypeTitle, s.Title); err != nil {
36+
if err := sendMessage(ws, MessageTypeTitle, s.Config.Title); err != nil {
3737
log.Printf("error sending title: %v\n", err)
3838
}
3939

0 commit comments

Comments
 (0)