Template
1
0
Fork 0

Re-worked stylegen

This commit is contained in:
Atridad Lahiji 2024-05-07 18:03:54 -06:00
parent 7463d1a252
commit 00bb8d776d
No known key found for this signature in database
25 changed files with 95 additions and 77 deletions

View file

@ -6,7 +6,7 @@ tmp_dir = "tmp"
args_bin = ["-ip", "127.0.0.1", "-port", "3000"] args_bin = ["-ip", "127.0.0.1", "-port", "3000"]
bin = "./tmp/main" bin = "./tmp/main"
pre_cmd = [] pre_cmd = []
cmd = "go build -o ./tmp/main . & cd lib/stylegen && ./gen.sh" cmd = "go build -o ./tmp/main . & cd lib/stylegen && ./gen.sh -e html -d ../../pages/templates -o ../../public/css"
delay = 1000 delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata", "lib/stylegen"] exclude_dir = ["assets", "tmp", "vendor", "testdata", "lib/stylegen"]
exclude_file = [] exclude_file = []

2
.gitignore vendored
View file

@ -5,4 +5,4 @@ airbin
tmp/ tmp/
*.rdb *.rdb
.DS_Store .DS_Store
tailwind.config.*.js tailwind.config.js

View file

@ -17,7 +17,6 @@ type SSEServerType struct {
} }
var SSEServer *SSEServerType var SSEServer *SSEServerType
var mutex = &sync.Mutex{}
func init() { func init() {
SSEServer = &SSEServerType{ SSEServer = &SSEServerType{
@ -90,24 +89,21 @@ func SetSSEHeaders(c echo.Context) {
} }
func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client chan string) { func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client chan string) {
// Create a new context that is not tied to the client's request context
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Cancel the context when the function returns defer cancel()
// Use a separate goroutine to monitor the client's request context
go func() { go func() {
<-c.Request().Context().Done() <-c.Request().Context().Done()
cancel() // Cancel the context when the client disconnects cancel()
}() }()
var mutex sync.Mutex
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
// The context has been canceled, either by the client disconnecting
// or by the function returning. Stop trying to send messages.
return return
default: default:
// The client is still connected. Continue processing messages.
msg, err := pubsub.ReceiveMessage(ctx) msg, err := pubsub.ReceiveMessage(ctx)
if err != nil { if err != nil {
log.Printf("Failed to receive message: %v", err) log.Printf("Failed to receive message: %v", err)
@ -117,17 +113,15 @@ func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client
data := fmt.Sprintf("data: %s\n\n", msg.Payload) data := fmt.Sprintf("data: %s\n\n", msg.Payload)
mutex.Lock() mutex.Lock()
_, err = c.Response().Write([]byte(data)) defer mutex.Unlock()
mutex.Unlock()
if err != nil {
log.Printf("Failed to write message: %v", err)
return // Stop processing if an error occurs
}
// Check if the ResponseWriter is nil before trying to flush it
if c.Response().Writer != nil { if c.Response().Writer != nil {
// Check if the ResponseWriter implements http.Flusher before calling Flush _, err = c.Response().Write([]byte(data))
if err != nil {
log.Printf("Failed to write message: %v", err)
return
}
flusher, ok := c.Response().Writer.(http.Flusher) flusher, ok := c.Response().Writer.(http.Flusher)
if ok { if ok {
flusher.Flush() flusher.Flush()
@ -135,7 +129,8 @@ func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client
log.Println("Failed to flush: ResponseWriter does not implement http.Flusher") log.Println("Failed to flush: ResponseWriter does not implement http.Flusher")
} }
} else { } else {
log.Println("Failed to flush: ResponseWriter is nil") log.Println("Failed to write: ResponseWriter is nil")
return
} }
} }
} }

View file

@ -1,4 +1,26 @@
#!/bin/sh #!/bin/bash
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-e|--extensions)
EXTENSIONS="$2"
shift; shift
;;
-d|--directory)
DIRECTORY="$2"
shift; shift
;;
-o|--output-dir)
OUTPUT_DIR="$2"
shift; shift
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
OS=$(uname -s) OS=$(uname -s)
ARCH=$(uname -m) ARCH=$(uname -m)
@ -41,39 +63,52 @@ else
chmod +x $BINARY chmod +x $BINARY
fi fi
echo $BINARY echo "Extensions: $EXTENSIONS"
echo "Directory: $DIRECTORY"
echo "Output Directory: $OUTPUT_DIR"
# Infer pages from .html files in the pages directory # Set default values if not provided
PAGES=$(ls ../../pages/templates/*.html | xargs -n 1 basename | sed 's/\.[^.]*$//') OUTPUT_DIR="${OUTPUT_DIR:-../../public/css}"
DIRECTORY="${DIRECTORY:-.}"
# Run the binary for each page if [[ -z "$EXTENSIONS" ]]; then
for PAGE in $PAGES; do echo "No extensions provided."
( exit 1
# Detect which partials are being used in this page fi
PARTIALS=$(grep -o -E '{{template "[^"]+' ../../pages/templates/${PAGE}.html | cut -d'"' -f2 | xargs -I{} echo \"../../pages/templates/partials/{}.html\")
# Generate an array of partials and join them with commas # Initialize an array for name conditions
PARTIALS_ARRAY=$(echo $PARTIALS | tr ' ' ',') name_conditions=()
# Always include the "header" partial and any other partials that are always used # Assuming $EXTENSIONS is a comma-separated list of extensions
PARTIALS_ARRAY=\"../../pages/templates/partials/header.html\",\"../../pages/templates/partials/global.html\",$PARTIALS_ARRAY IFS=',' read -ra ADDR <<< "$EXTENSIONS"
for ext in "${ADDR[@]}"; do
# Generate Tailwind config for this page name_conditions+=(-name "*.$ext")
echo "module.exports = {
content: [\"../../pages/templates/${PAGE}.html\", \"../../pages/templates/layouts/*.html\", $PARTIALS_ARRAY],
theme: {
extend: {},
},
daisyui: {
themes: [\"night\"],
},
plugins: [require('daisyui'), require('@tailwindcss/typography')],
}" > tailwind.config.${PAGE}.js
# Run the binary with the generated config
$BINARY build -i ./base.css -c tailwind.config.${PAGE}.js -o ../../public/css/styles.${PAGE}.css --minify
) &
done done
# Use find with the array of conditions
INCLUDE_FILES=$(find "$DIRECTORY" -type f \( "${name_conditions[@]}" \))
echo "Files found: $INCLUDE_FILES"
# Optionally, remove leading './' if necessary
INCLUDE_FILES=$(echo "$INCLUDE_FILES" | sed 's|^\./||')
INCLUDE_FILES_ARRAY=$(echo "$INCLUDE_FILES" | awk '{printf "\"%s\",", $0}' | sed 's/,$//')
# Generate Tailwind config
echo "module.exports = {
content: [$INCLUDE_FILES_ARRAY],
theme: {
extend: {},
},
daisyui: {
themes: [\"night\"],
},
plugins: [require('daisyui'), require('@tailwindcss/typography')],
}" > tailwind.config.js
# Run the binary with the generated config
$BINARY build -i ./base.css -c tailwind.config.js -o "${OUTPUT_DIR}/styles.css" --minify
# Wait for all background processes to finish # Wait for all background processes to finish
wait wait

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Blog
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.blog.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}
@ -19,4 +19,4 @@ Atridad Lahiji // Blog
{{end}} {{end}}
{{define "foot"}} {{define "foot"}}
{{end}} {{end}}

View file

@ -7,20 +7,17 @@ Atridad Lahiji // [SOMEPAGE]
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.[SOMEPAGE].css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}
<h1 class="text-4xl font-extrabold text-white sm:text-8xl"> <h1 class="text-4xl font-extrabold text-white sm:text-8xl">
New <span New <span class="bg-gradient-to-r from-pink-500 to-blue-500 bg-clip-text text-transparent">Page</span>
class="bg-gradient-to-r from-pink-500 to-blue-500 bg-clip-text text-transparent" </h1>
>Page</span
>
</h1>
{{end}} {{end}}
{{define "foot"}} {{define "foot"}}
<script src="/public/js/htmx.base.js"></script> <script src="/public/js/htmx.base.js"></script>
<script src="/public/js/htmx.sse.js"></script> <script src="/public/js/htmx.sse.js"></script>
<script src="/public/js/hyperscript.js"></script> <script src="/public/js/hyperscript.js"></script>
{{end}} {{end}}

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Root
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.home.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Post
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.post.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Projects
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.projects.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Talks
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.talks.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Testimonials
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.testimonials.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Tools
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.tools.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}

View file

@ -7,7 +7,7 @@ Atridad Lahiji // Tools // Resizer
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.tools.resize.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}

View file

@ -5,7 +5,7 @@ Atridad Lahiji // Tools // SSE Demo
{{end}} {{end}}
{{define "head"}} {{define "head"}}
<link rel="stylesheet" href="/public/css/styles.tools.ssedemo.css" /> <link rel="stylesheet" href="/public/css/styles.css" />
{{end}} {{end}}
{{define "main"}} {{define "main"}}

File diff suppressed because one or more lines are too long

1
public/css/styles.css vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long