164 lines
4.4 KiB
Executable File

#! /usr/bin/env slope
(define default-index "/usr/local/share/shlog/index.html")
(define root-path (path-abs "~/.local/share/shlog"))
(define root-glob "/home/*/.local/share/shlog/*.txt")
(define html-head
`<!DOCTYPE html>
<html lang="en">
<title>RTC Shlog</title>
<h1>RTC Shlog</h1>
<p>If your post is not appearing here, check that it is world readable. Shlog will no longer add posts to the index that are not world readable. This gives you the ability to easily remove posts and ensures that users can read things shown on this index.</p>
(define create-root (lambda (path) (mkdir root-path 0755 #t)))
(define from-filename (lambda (str)
(set! str (path-base str))
(slice str (+ (string-index-of str "-") 1) (- (length str) 4))
" ")))
(define to-filename (lambda (str)
(regex-replace `\s` str "_")))))
(define edit (lambda ()
(define files
(lambda (ln)
[ln (path-base ln)])
(path-glob (path-join root-path "*"))))
(define counter 0)
(lambda (l)
(display "\033[1m" counter "\033[0m\t" (car (cdr l)) "\n")
(set! counter (+ counter 1)))
(display "\nWhich file # do you want to edit? ")
(define i (string->number (read-line)))
(if (and i (>= i 0) (< i (length files)))
(subprocess [(get-editor) (car (ref files i))])
(error "edit-file"))))
(define get-editor (lambda ()
(define editor (env "EDITOR"))
(if (equal? editor "") (set! editor "nano"))
(define new-file (lambda (str)
(subprocess [(get-editor) (to-filename str)])))
(define error (lambda (type)
(case type
("new-file" (write "shlog new [post-title]\n" stderr))
("build-path" (write "shlog build [output-path]\n" stderr))
("index-write" (write "Could not write index file\n" stderr))
("edit-file" (write "Could not open file for editing\n" stderr))
(else (write "unknown error" stderr)))
(exit 1)))
(define browse (lambda (path)
(if (not path)
(set! path default-index))
(subprocess ["lynx" path])))
(define build (lambda (destination)
(define out (string-make-buf))
(write html-head out)
(write (string-format "<p>Last generated: <i>%v</i></p><ul id=\"posts\">" (date)) out)
(lambda (line)
`<li><a href="file://%v">%v</a> (%v)</li>`
(car line)
(ref line 3)
(ref line 2))
(lambda (p)
p ; full path [0]
(path-base p) ; filename [1]
(regex-replace `/home/([\w-]+)/.*` p "$1") ; username [2]
(from-filename (path-base p)) ; Post title [3]
(lambda (p)
(>= (string->number (ref (assoc (file-stat p) "modestring") 2)) 4))
(path-glob root-glob)))
(write "</ul></body></html>" out)
(define outfile (file-create destination))
(if outfile
(write (read-all out) outfile)
(close outfile))
(error "index-write")))))
(define print-help (lambda ()
`shlog - a local plaintext blogging system
Read: shlog
shlog browse
shlog browse [custom-index-path]
Write: shlog new [post-title]
Edit : shlog edit
Build: shlog build [index-path]
Help : shlog help
shlog -h
shlog --help
Data : ~/.local/share/shlog
Index: /usr/local/share/shlog/index.html
Code :
(define main (lambda ()
(if (not (path-exists? root-path))
(create-root root-path))
(set! sys-args (cdr sys-args))
(if (null? sys-args)
(browse #f)
(if (or (member? sys-args "-h") (member? sys-args "--help"))
(case (car sys-args)
("help" (print-help))
(if (null? (cdr sys-args))
(browse #f)
(browse (path-abs (car (cdr sys-args))))))
(if (null? (cdr sys-args))
(error "new-file")
(new-file (car (cdr sys-args)))))
(if (null? (cdr sys-args))
(build default-index)
(build (path-abs (car (cdr sys-args))))))
(else (print-help)))))))