wjak56@gmail.com 9 years ago
parent
commit
af88410221
  1. 111
      create_post.rkt

111
create_post.rkt

@ -33,10 +33,12 @@
(hash-set! config 'password password)]
[(list "username" username)
(hash-set! config 'username username)]
[(list "url" url)
(hash-set! config 'url url)]
[val (error
(format "Invalid configuration line: ~a" val))]))
(curry hash-ref config))))
; The current list of commits (as a dynamically scoped name)
(define current-commits (make-parameter (list)))
@ -48,11 +50,14 @@
(match
(process command)
[(list out in pid errport _)
(let ([result (port->string out)])
(let ([result (port->string out)]
[err-result (port->string out)])
(close-input-port out)
(close-output-port in)
(close-input-port errport)
result)]))
(cond
[(not (string=? "" (string-trim result))) result]
[else #f]))]))
; XML-RPC string
(define (xstring str)
@ -146,7 +151,7 @@
(get-post-id
(port->string
(post-pure-port
(string->url "https://primop.me/blog/xmlrpc.php")
(string->url (your-config 'url))
(string->bytes/utf-8
(xexpr->string
(cond
@ -184,48 +189,86 @@
; Get a list of all commit refs
(define (get-commits)
(string-split
(system-result "git rev-list master")
(match (system-result "git rev-list master")
[#f ""]
[str str])
"\n"))
; Convert a commit ref into its post ID number (if it exists)
(define (commit->post-id post-name)
(compose1
string->number
string-trim
system-result
(curry format "git notes --ref=~a show ~a" post-name)))
(define (commit->post-id post-name commit-ref)
(string->number
(string-trim
(system-result
(format "git notes --ref=~a show ~a" post-name commit-ref)))))
(define (git-notes-ref post-name commit-ref)
(let ([result
(string-trim
(match
(system-result
(format "git notes --ref=~a show ~a"
post-name
commit-ref))
[#f ""]
[str str]))
])
(displayln (format "git notes ref result: ~a" result))
result))
; Grab a post id given a post name
; Return false if it does not exist
(define (git-href post-name)
(let ([convert-commit (commit->post-id post-name)])
(displayln (map convert-commit (current-commits)))
(match
(memf
convert-commit
(current-commits))
[(list-rest post-id _)
(convert-commit post-id)]
[x #f])))
(define git-href
(let ([def-val (gensym)])
(lambda (post-name [default def-val])
(match
(memf
(lambda (commit-ref)
(displayln (format "checking commit ~a" commit-ref))
(let ([notes (git-notes-ref post-name commit-ref)])
(match notes
["" (displayln (format "commit ~a did not have anything" commit-ref)) #f]
[result (displayln (format "found ~a in commit ~a" result commit-ref)) #t])))
(current-commits))
[(list-rest commit-id _) commit-id]
[_
(cond
[(eq? def-val default)
(raise
(exn:fail
(format
"git-href: no value found for key\n\t~a" post-name)
(current-continuation-marks)))]
[else default])]))))
; Add or refresh a post id associated with that post name
(define (git-set! post-name post-id)
(displayln (format "git setting ~a ~a" post-name post-id))
;(displayln (format "git setting ~a ~a" post-name post-id))
(system
(format
"git notes --ref=~a add HEAD -fm \"~a\""
post-name
post-id)))
(parameterize ([current-commits (get-commits)])
(for ([post (get-files)])
(match (git-href post)
[#f (displayln "new post!")
(when (empty? (current-commits))
(system "git commit -n --allow-empty -m \"bootstrap blog\""))
(let ([post-id (handle-post post #f)])
(git-set! post post-id))]
[post-id
(displayln (format "updating post ~a" post-id))
(git-set! post post-id)
(handle-post post post-id)])))
(parameterize ([current-commits (get-commits)])
(displayln (format "these are the current commits: ~a" (current-commits)))
(for ([post (get-files)])
;(call-with-values (lambda () (split-path (string->path post))) (compose1 displayln list))
(match (git-href post #f)
[#f (displayln "new post!")
(when (empty? (current-commits))
; Add a first commit if there are none so it can store the note properly!
(system "git commit -n --allow-empty -m \"bootstrap blog\""))
(git-set! post (handle-post post #f))]
[commit-id
(displayln (format "updating post ~a" post))
(displayln (format "the commit ref I got is ~a" commit-id))
(let ([post-id (commit->post-id post commit-id)])
(displayln post-id)
(displayln (format "the commit ref is ~a" commit-id))
(git-set! post post-id)
(handle-post post post-id))])))

Loading…
Cancel
Save