#lang pollen/mode racket/base

; SPDX-License-Identifier: BlueOak-1.0.0
; This file is licensed under the Blue Oak Model License 1.0.0.

;; Script/template for starting a new article

(require racket/date
         racket/string
         racket/file
         racket/system
         "../dust.rkt")

(provide main)

(define (normalize str)
  (define alphanum-only
    (regexp-replace* #rx"[^A-Za-z0-9 ]" str ""))
  (string-normalize-spaces (string-downcase alphanum-only) #px"\\s+" "-"))

(define (make-filename basename)
  (build-path (current-directory) articles-folder (string-append basename ".poly.pm")))

(define (comment . strs)
  (format "◊; ~a" (apply string-append strs)))

(define date-string
  (parameterize [(date-display-format 'iso-8601)]
    (date->string (current-date))))

(define (make-template-contents title)
  ◊string-append{
 #lang pollen

 ◊comment{Copyright ◊(substring date-string 0 4) by ◊|default-authorname|. All Rights Reserved.}

 ◊"◊"(define-meta published "◊date-string")
 ◊"◊"(define-meta conceal   "blog,feed")  ; Edit/delete this line when ready to publish
 ◊"◊;"(define-meta series "seriesname")

 ◊"◊"title{◊title}

 Write here!})

(define (main)
  (display "Enter title: ")
  (define title (read-line))
  (cond [(non-empty-string? title)
         (define post-file (make-filename (normalize title)))
         (define post-contents (make-template-contents title))
         (display-to-file post-contents post-file)
         (displayln (format "Saved to ~a" post-file))

         ; the + argument tells vim to place the cursor at the last line of the file.
         (system (format "mvim + ~a" post-file))]))
