templates.lisp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ;;;; templates.lisp
  2. ;;;;
  3. ;;;; Copyright (c) 2016 Samuel W. Flint <swflint@flintfam.org>
  4. (in-package #:collect)
  5. ;;; "collect" goes here.
  6. (defmacro define-html-template (name input &optional docstring)
  7. "Generate an HTML template function"
  8. (let* ((old-name name)
  9. (name (intern (string-upcase old-name)))
  10. (docstring (if docstring
  11. docstring
  12. (format nil "Template caller for ~a, as ~a" old-name name)))
  13. (input (merge-pathnames input (asdf:system-source-directory '#:collect))))
  14. `(defun ,name (&rest args)
  15. ,docstring
  16. (with-output-to-string (*standard-output*)
  17. (funcall (create-template-printer ,input) args)))))
  18. (defmacro generate-static (name filename type script)
  19. "Generate a static page"
  20. (let* ((name (intern (string-upcase name)))
  21. (type (getf (list :js "application/javascript"
  22. :javascript "application/javascript"
  23. :css "text/css")
  24. type)))
  25. `(define-route ,name (,filename :content-type ,type)
  26. ,script)))
  27. (defmacro define-css-page (name filename &rest body)
  28. "Generate a static CSS page"
  29. (let* ((name (intern (string-upcase filename)))
  30. (css-chunks (map 'list #'compile-and-write body))
  31. (css (format nil "~{~a~^~%~%~}" css-chunks)))
  32. `(generate-static ,name
  33. ,filename
  34. :css
  35. ,css)))
  36. (defmacro define-js-page (name filename &rest script)
  37. (let ((name (intern (string-upcase filename)))
  38. (js (ps* script)))
  39. `(generate-static ,name
  40. ,filename
  41. :javascript
  42. ,js)))
  43. (setq parenscript:*js-string-delimiter* #\"
  44. html-template:*string-modifier* #'cl:identity)
  45. (define-css-page main.css "main.css"
  46. (*
  47. :margin 0
  48. :padding 0
  49. :font-family "Georgia, Palantino, Times, 'Times New Roman', sans-serif")
  50. (body
  51. :backgroud "#fff")
  52. (a
  53. :text-decoration "none")
  54. ("a:link, a:visited"
  55. :color "#f30")
  56. ("a:hover"
  57. :color "#f90")
  58. (div.main-content
  59. :position "absolute"
  60. :top "40px"
  61. :left "280px"
  62. :width "500px"
  63. (h1
  64. :font-size "40px"
  65. :font-weight "normal"
  66. :line-height "43px"
  67. :letter-spacing "-1px")
  68. (div.results
  69. :margin-top "20px"
  70. (ul
  71. :list-style-type "none")
  72. (li
  73. :font-size "18px"
  74. :line-height "24px"
  75. :margin-left "70px"))
  76. (div.page-list
  77. :text-align "center"
  78. :float "bottom")
  79. (dl
  80. :margin-top "10px")
  81. (dt
  82. :font-size "18px"
  83. :font-weight "bold")
  84. (dd
  85. :margin-left "20px"
  86. :margin-bottom "15px"))
  87. (div.nav
  88. :position absolute
  89. :top "40px"
  90. :left "20px"
  91. :width "200px"
  92. :padding "20px 20px 0 0"
  93. :border-right "1px solid #ccc"
  94. :text-align "right"
  95. (h2
  96. :text-transform "uppercase"
  97. :font-size "13px"
  98. :color "#333"
  99. :letter-spacing "1px"
  100. :line-height "20px"
  101. (a
  102. :color "#333"))
  103. (ul
  104. :list-style-type "none"
  105. :margin "20px 0")
  106. (li
  107. :font-size "14px"
  108. :line-height "20 px")
  109. (hr
  110. :height "1px"
  111. :color "#ccc"
  112. :margin-top "5px"
  113. :margin-bottom "5px")))
  114. (define-js-page main.js "main.js")
  115. (define-html-template main-page "mainpage.httmpl")
  116. ;;; End collect