#lang racket/base

(require pollen/decode
         txexpr
         yarn/markup
         yarn/string
         yarn/tools)

(provide
 (all-defined-out)
 (all-from-out yarn/markup))

(module+ setup
  (provide block-tags)
  (define block-tags blocks-elements))

;; Customized paragraph decoder replaces single newlines within paragraphs
;; with single spaces instead of <br> tags. Allows for “semantic line wrapping”.
(define (decode-hardwrapped-paragraphs xs)
  (define (no-linebreaks xs)
    (decode-linebreaks xs " "))
  (decode-paragraphs xs 'paragraph #:linebreak-proc no-linebreaks))

(define (root . elems)
  (validate-txexpr `(test ,@elems))
  (check-title elems)
  `(document ,@(decode-hardwrapped-paragraphs elems)))

(define (check-title elems)
  (cond
    [(and (not (meta-set? 'title))
          ((tx-is? 'poetry #:has-attrs 'title) (car elems)))
     (set-meta 'title (format "‘~a’" (attr-ref (car elems) 'title)))
     (set-meta 'title-supplied? #t)]
    [(not (meta-set? 'title))
     (set-meta 'title (first-words elems 5))]))