From b22897135ebd86ef602d79016dc0d4077c4a7053 Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Wed, 25 Mar 2026 10:20:19 +0100 Subject: [PATCH] feat: --generate-template --- .gitignore | 4 +- kewt.sh | 146 ++++++++++++++++--------------------------------- site/site.conf | 3 + 3 files changed, 50 insertions(+), 103 deletions(-) diff --git a/.gitignore b/.gitignore index eda6761..d2efdfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ out/ -kewt -site.conf -template.html +kewt \ No newline at end of file diff --git a/kewt.sh b/kewt.sh index 14817e9..7a02d5c 100755 --- a/kewt.sh +++ b/kewt.sh @@ -13,17 +13,19 @@ Usage: $invoked_as [--from ] [--to ] $invoked_as --new [title] $invoked_as --update [dir] $invoked_as --post + $invoked_as --generate-template $invoked_as --version $invoked_as --help Options: - --help Show this help message. - --new [title] Create a new site directory (default: site) - --update [dir] Update site.conf and template.html with latest defaults (defaults to current directory) - --post Create a new empty post file in the configured posts_dir with current date and time as name - --version Show version information. - --from Source directory (default: site) - --to Output directory (default: out) + --help Show this help message. + --new [title] Create a new site directory (default: site) + --update [dir] Update site.conf and template.html with latest defaults (defaults to current directory) + --post Create a new empty post file in the configured posts_dir with current date and time as name + --generate-template [path] Generate a new template file at (default: template.html) + --version Show version information. + --from Source directory (default: site) + --to Output directory (default: out) EOF } @@ -33,18 +35,7 @@ awk_dir="$script_dir/awk" KEWT_TMPDIR=$(mktemp -d "/tmp/kewt_run.XXXXXX") trap 'rm -rf "$KEWT_TMPDIR"' EXIT HUP INT TERM - - -create_new_site() { - new_title="$1" - new_dir="site" - [ -n "$new_title" ] && new_dir="$new_title" - - [ -e "$new_dir" ] && die "Target '$new_dir' already exists." - - mkdir -p "$new_dir" - cat > "$new_dir/site.conf" <<'EOF' -title = "kewt" +DEFAULT_CONF='title = "kewt" style = "kewt" dir_indexes = true single_file_index = true @@ -68,11 +59,9 @@ base_url = "" generate_feed = false feed_file = "rss.xml" posts_dir = "" -custom_admonitions = "" -EOF +custom_admonitions = ""' - cat > "$new_dir/template.html" <<'EOF' - +DEFAULT_TMPL=' @@ -93,8 +82,29 @@ EOF
{{CONTENT}}
{{FOOTER}}
- -EOF +' + + +generate_template() { + _gt_path="$1" + [ -e "$_gt_path" ] && die "File '$_gt_path' already exists." + _gt_dir=$(dirname "$_gt_path") + [ -d "$_gt_dir" ] || mkdir -p "$_gt_dir" + printf '%s\n' "$DEFAULT_TMPL" > "$_gt_path" + echo "Generated template at '$_gt_path'." + exit 0 +} + +create_new_site() { + new_title="$1" + new_dir="site" + [ -n "$new_title" ] && new_dir="$new_title" + + [ -e "$new_dir" ] && die "Target '$new_dir' already exists." + + mkdir -p "$new_dir" + printf '%s\n' "$DEFAULT_CONF" > "$new_dir/site.conf" + printf '%s\n' "$DEFAULT_TMPL" > "$new_dir/template.html" printf "# _kewt_ website\n" > "$new_dir/index.md" if [ -n "$new_title" ]; then @@ -147,33 +157,7 @@ update_site() { # Generate default site.conf default_conf="$KEWT_TMPDIR/default_site.conf" - cat > "$default_conf" <<'CONFEOF' -title = "kewt" -style = "kewt" -dir_indexes = true -single_file_index = true -flatten = false -order = "" -home_name = "Home" -show_home_in_nav = true -nav_links = "" -nav_extra = "" -footer = "made with kewt" -logo = "" -display_logo = false -display_title = true -logo_as_favicon = true -favicon = "" -generate_page_title = true -error_page = "not_found.html" -versioning = false -enable_header_links = true -base_url = "" -generate_feed = false -feed_file = "rss.xml" -posts_dir = "" -custom_admonitions = "" -CONFEOF + printf '%s\n' "$DEFAULT_CONF" > "$default_conf" # Update site.conf if [ ! -f "$target_conf" ]; then @@ -203,30 +187,7 @@ CONFEOF # Update template.html if [ -f "$target_tmpl" ]; then default_tmpl="$KEWT_TMPDIR/default_template.html" - cat > "$default_tmpl" <<'TMPLEOF' - - - - - - {{TITLE}} - - - {{HEAD_EXTRA}} - - - -
-

{{HEADER_BRAND}}

-
- - - -
{{CONTENT}}
-
{{FOOTER}}
- - -TMPLEOF + printf '%s\n' "$DEFAULT_TMPL" > "$default_tmpl" if cmp -s "$default_tmpl" "$target_tmpl" 2>/dev/null; then echo "template.html is already up to date." else @@ -274,6 +235,14 @@ while [ $# -gt 0 ]; do shift fi ;; + --generate-template) + generate_template_path="template.html" + if [ $# -gt 1 ] && [ "${2#-}" = "$2" ]; then + generate_template_path="$2" + shift + fi + generate_template "$generate_template_path" + ;; --update) update_dir="." if [ $# -gt 1 ] && [ "${2#-}" = "$2" ]; then @@ -610,30 +579,7 @@ template="$src/template.html" [ -f "$template" ] || template="./template.html" if [ ! -f "$template" ]; then template="$KEWT_TMPDIR/default_template.html" - cat > "$template" <<'EOF' - - - - - - {{TITLE}} - - - {{HEAD_EXTRA}} - - - -
-

{{HEADER_BRAND}}

-
- - - -
{{CONTENT}}
- - - -EOF + printf '%s\n' "$DEFAULT_TMPL" > "$template" fi [ -d "$out" ] && rm -rf "$out" diff --git a/site/site.conf b/site/site.conf index 8ca7ac6..c6f894b 100644 --- a/site/site.conf +++ b/site/site.conf @@ -20,3 +20,6 @@ versioning = true enable_header_links = true base_url = "https://kewt.krzak.org" custom_admonitions = "" +generate_feed = false +feed_file = "rss.xml" +posts_dir = "" \ No newline at end of file