(in-package :cl-blog)

(defparameter *template-translator* (make-translator))

(define-translator *template-translator* rss-feed-header <cl-blog:rss-feed-header/>
  <link rel='alternate' type='application/rss+xml'
   title=?(format nil "RSS Feed for ~A" *blog-short-name*)
   href=?(archives-url :rss t)/>)

(define-translator *template-translator* new-entry <cl-blog:new-entry-link/>
  (<a href=? (urlstring *new-entry-url*)> "New entry"))

(define-translator *template-translator* login <cl-blog:login-link/>
  (<a href=? (urlstring *login-url*)> "Login"))

(define-translator *template-translator* logout <cl-blog:logout-link/>
  (<a href=? (urlstring *logout-url*)> "Logout"))

(define-translator *template-translator* archives <cl-blog:archives-link/>
  (<a href=? (archives-url)> "Archives"))

(define-translator *template-translator* rss-feed <cl-blog:rss-feed-link/>
  (<a href=? (archives-url :rss t)> "Syndicate (RSS)"))

(define-translator *template-translator* comments <cl-blog:comments-link/>
  (<a href=? (urlstring *email-redirect-url*)> "Send Comments"))

(define-translator *template-translator* comments <cl-blog:comments-link text=?text/>
  (<a href=? (urlstring *email-redirect-url*)> text))

(define-translator *template-translator* admin <cl-blog:admin-link/>
  (<a href=? (urlstring *admin-url*)> "Administrate"))

(define-translator *template-translator* user-settings <cl-blog:user-settings-link/>
  (<a href=?(if (boundp '*authenticated-user*)
                (concatenate 'string (urlstring *edit-user-url*) "?user=" (user-name *authenticated-user*))
                "Mickey Mouse!")> "Preferences"))

(define-translator *template-translator* recent-entry-links <cl-blog:recent-entry-links/>
  (<ul>
   (loop for i from 1 to 10
        for j in *blog-entries*
        collect (<li> (<a href=? (entry-link j)>
                          (blog-entry-title j))))))

(define-translator *template-translator* category-links <cl-blog:category-links/>
  (<ul> (loop for i in *categories*
           collect (<li> (<a href=?(make-archives-url :category i)> i)
                         (<a href=? (archives-url :category i :rss t)> "(RSS)")))))

(define-translator *template-translator* short-name <cl-blog:blog-short-name/>
  *blog-short-name*)

(define-translator *template-translator* blog-entry
    <cl-blog:present-blog-entry entry=?id/>
    (let* ((entry-id (parse-integer id :junk-allowed t))
           (entry-to-present (and entry-id (find-entry entry-id))))
      (if entry-to-present
          (format-entry entry-to-present)
          "No such entry could be found.")))

(define-translator *template-translator* search-entries/count
    <cl-blog:search-entries count=?count/>
    (let ((count-limit (parse-integer count :junk-allowed t)))
      (if count-limit
          (<div class='entries'>
           (loop for i below count-limit
              for j in *blog-entries*
              collect (format-entry j)))
          "Malformed count limit.")))

(define-translator *template-translator* search-entries
    <cl-blog:search-entries/>
  <cl-blog:search-entries count='10'/>)

(defun n-days-ago (n)
  (let ((beginning-of-today
         (with-date (get-universal-time) nil
           (encode-universal-time 0 0 0 day-of-month month year))))
    (- beginning-of-today
       (* n 24 60 60))))

(define-translator *template-translator* search-entries/days
    <cl-blog:search-entries days=?days/>
    (let ((days (parse-integer days :junk-allowed t)))
      (if days
          (let ((n-days-ago (n-days-ago days)))
            (<div class='entries'>
                  (loop for entry in *blog-entries*
                     while (> (blog-entry-time entry) n-days-ago)
                     collect (format-entry entry)))))))

(define-translator *template-translator* user-descriptions <cl-blog:user-descriptions/>
  (<p>
   (loop for user in *users*
        unless (typep user 'administrator)
        collect (<div class='user-box'>
                      (<div class='user-name'>
                            (<a href=?(format nil "~A?user=~A"
                                              (urlstring *email-redirect-url*)
                                              (user-name user))>
                                              (user-display-name user)))
                      (<div class='user-description'>
                            (user-description user))))))

(define-translator *template-translator* if (<cl-blog:if var=?var> . body)
  (if (find var *template-conditionals* :test #'string-equal)
      body
      nil))

(define-translator *template-translator* if-invert (<cl-blog:if var=?var invert='true'> . body)
  (if (find var *template-conditionals* :test #'string-equal)
      nil
      body))