zona.kak

· ficd's pastes · raw

expires: 2025-10-30

 1provide-module zona %~
 2# utility commands for writing blog posts with zona
 3
 4## begin public options
 5declare-option -docstring %{
 6    URL of the zona server.
 7} \
 8str zona_url "http://localhost:8000"
 9
10declare-option -docstring %{
11    Full path to the zona project.
12} \
13str zona_path "/home/fic/dev/ficd.sh"
14
15declare-option -docstring %{
16    Command to call zona.
17} \
18str zona_cmd "zona"
19
20declare-option -docstring %{
21    Command to open URL.
22} \
23str zona_url_cmd "firefox"
24## end public options
25
26declare-option -hidden int zona_pid 0
27
28define-command -docstring %{
29    Open the current page in browser.
30    -delay switch waits for 1 second before opening.
31} \
32-params 0..1 zona-open %{
33    nop %sh{
34        {
35        	if [ "$1" = '-delay' ]; then
36        		sleep 1
37        	fi
38        	url="$kak_opt_zona_url"
39        	case "$kak_buffile" in
40        	*/content/*.md | */content/*.html)
41        		rel="${kak_buffile#*/content/}"
42        		rel="${rel%.md}"
43        		rel="${rel%.html}"
44        		case "$rel" in
45        		*/index) rel="${rel%/index}" ;;
46        		index) rel="" ;;
47        		esac
48        		url="${url}/${rel}"
49        		;;
50        	esac
51        	"$kak_opt_zona_url_cmd" "$url"
52        } >/dev/null 2>&1 </dev/null &
53    }
54}
55
56define-command -docstring %{
57    Start the preview server.
58} \
59zona-start-preview %{
60    try %{
61         evaluate-commands %sh{
62            cd "$kak_opt_zona_path" || exit 1
63            {
64                exec "$kak_opt_zona_cmd" serve
65            } >/dev/null 2>&1 </dev/null &
66            pid="$!"
67            printf 'echo -debug zona pid: "%s"\n' "$pid"
68            printf 'set-option global zona_pid %s\n' "$pid"
69            echo "zona-open -delay"
70        }
71        hook global KakEnd .* %{
72            try %{
73                zona-stop-preview
74            }
75        }
76    } catch %{
77        fail 'Failed to start server:' %val{error}
78    }
79}
80
81define-command -docstring %{
82    Stop the preview server.
83} \
84zona-stop-preview %{
85    evaluate-commands %sh{
86        if [ "$kak_opt_zona_pid" -eq 0 ]; then
87            echo "fail Zona is not currently running!"
88            exit 1
89        fi
90        if kill -0 "$kak_opt_zona_pid" 2>/dev/null; then
91            kill "$kak_opt_zona_pid"
92            printf 'set-option global zona_pid 0\n'
93        else
94            printf 'fail Process %s does not exist' "$kak_opt_zona_pid"
95        fi
96    }
97}
98
99~