cal-html.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. ;;; cal-html.el --- functions for printing HTML calendars
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Anna M. Bigatti <bigatti@dima.unige.it>
  4. ;; Keywords: calendar
  5. ;; Human-Keywords: calendar, diary, HTML
  6. ;; Created: 23 Aug 2002
  7. ;; Package: calendar
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This package writes HTML calendar files using the user's diary
  21. ;; file. See the Emacs manual for details.
  22. ;;; Code:
  23. (require 'calendar)
  24. (defgroup calendar-html nil
  25. "Options for HTML calendars."
  26. :prefix "cal-html-"
  27. :group 'calendar)
  28. (defcustom cal-html-directory "~/public_html"
  29. "Directory for HTML pages generated by cal-html."
  30. :type 'string
  31. :group 'calendar-html)
  32. (defcustom cal-html-print-day-number-flag nil
  33. "Non-nil means print the day-of-the-year number in the monthly cal-html page."
  34. :type 'boolean
  35. :group 'calendar-html)
  36. (defcustom cal-html-year-index-cols 3
  37. "Number of columns in the cal-html yearly index page."
  38. :type 'integer
  39. :group 'calendar-html)
  40. (defcustom cal-html-day-abbrev-array calendar-day-abbrev-array
  41. "Array of seven strings for abbreviated day names (starting with Sunday)."
  42. :set-after '(calendar-day-abbrev-array)
  43. :type '(vector (string :tag "Sun")
  44. (string :tag "Mon")
  45. (string :tag "Tue")
  46. (string :tag "Wed")
  47. (string :tag "Thu")
  48. (string :tag "Fri")
  49. (string :tag "Sat"))
  50. :group 'calendar-html)
  51. (defcustom cal-html-css-default
  52. (concat
  53. "<STYLE TYPE=\"text/css\">\n"
  54. " BODY { background: #bde; }\n"
  55. " H1 { text-align: center; }\n"
  56. " TABLE { padding: 2pt; }\n"
  57. " TH { background: #dee; }\n"
  58. " TABLE.year { width: 100%; }\n"
  59. " TABLE.agenda { width: 100%; }\n"
  60. " TABLE.header { width: 100%; text-align: center; }\n"
  61. " TABLE.minical TD { background: white; text-align: center; }\n"
  62. " TABLE.agenda TD { background: white; text-align: left; }\n"
  63. " TABLE.agenda TH { text-align: left; width: 20%; }\n"
  64. " SPAN.NO-YEAR { color: #0b3; font-weight: bold; }\n"
  65. " SPAN.ANN { color: #0bb; font-weight: bold; }\n"
  66. " SPAN.BLOCK { color: #048; font-style: italic; }\n"
  67. "</STYLE>\n\n")
  68. "Default cal-html css style. You can override this with a \"cal.css\" file."
  69. :type 'string
  70. :group 'calendar-html)
  71. ;;; End customizable variables.
  72. ;;; HTML and CSS code constants.
  73. (defconst cal-html-e-document-string "<BR><BR>\n</BODY>\n</HTML>"
  74. "HTML code for end of page.")
  75. (defconst cal-html-b-tablerow-string "<TR>\n"
  76. "HTML code for beginning of table row.")
  77. (defconst cal-html-e-tablerow-string "</TR>\n"
  78. "HTML code for end of table row.")
  79. (defconst cal-html-b-tabledata-string " <TD>"
  80. "HTML code for beginning of table data.")
  81. (defconst cal-html-e-tabledata-string " </TD>\n"
  82. "HTML code for end of table data.")
  83. (defconst cal-html-b-tableheader-string " <TH>"
  84. "HTML code for beginning of table header.")
  85. (defconst cal-html-e-tableheader-string " </TH>\n"
  86. "HTML code for end of table header.")
  87. (defconst cal-html-e-table-string
  88. "</TABLE>\n<!-- ================================================== -->\n"
  89. "HTML code for end of table.")
  90. (defconst cal-html-minical-day-format " <TD><a href=%s#%d>%d</TD>\n"
  91. "HTML code for a day in the minical - links NUM to month-page#NUM.")
  92. (defconst cal-html-b-document-string
  93. (concat
  94. "<HTML>\n"
  95. "<HEAD>\n"
  96. "<TITLE>Calendar</TITLE>\n"
  97. "<!--This buffer was produced by cal-html.el-->\n\n"
  98. cal-html-css-default
  99. "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"cal.css\">\n"
  100. "</HEAD>\n\n"
  101. "<BODY>\n\n")
  102. "Initial block for html page.")
  103. (defconst cal-html-html-subst-list
  104. '(("&" . "&amp;")
  105. ("\n" . "<BR>\n"))
  106. "Alist of symbols and their HTML replacements.")
  107. (defun cal-html-comment (string)
  108. "Return STRING as html comment."
  109. (format "<!-- ====== %s ====== -->\n"
  110. (replace-regexp-in-string "--" "++" string)))
  111. (defun cal-html-href (link string)
  112. "Return a hyperlink to url LINK with text STRING."
  113. (format "<A HREF=\"%s\">%s</A>" link string))
  114. (defun cal-html-h3 (string)
  115. "Return STRING as html header h3."
  116. (format "\n <H3>%s</H3>\n" string))
  117. (defun cal-html-h1 (string)
  118. "Return STRING as html header h1."
  119. (format "\n <H1>%s</H1>\n" string))
  120. (defun cal-html-th (string)
  121. "Return STRING as html table header."
  122. (format "%s%s%s" cal-html-b-tableheader-string string
  123. cal-html-e-tableheader-string))
  124. (defun cal-html-b-table (arg)
  125. "Return table tag with attribute ARG."
  126. (format "\n<TABLE %s>\n" arg))
  127. (defun cal-html-monthpage-name (month year)
  128. "Return name of html page for numeric MONTH and four-digit YEAR.
  129. For example, \"2006-08.html\" for 8 2006."
  130. (format "%d-%.2d.html" year month))
  131. (defun cal-html-insert-link-monthpage (month year &optional change-dir)
  132. "Insert a link to the html page for numeric MONTH and four-digit YEAR.
  133. If optional argument CHANGE-DIR is non-nil and MONTH is 1 or 2,
  134. the link points to a different year and so has a directory part."
  135. (insert (cal-html-h3
  136. (cal-html-href
  137. (concat (and change-dir
  138. (member month '(1 12))
  139. (format "../%d/" year))
  140. (cal-html-monthpage-name month year))
  141. (calendar-month-name month)))))
  142. (defun cal-html-insert-link-yearpage (month year)
  143. "Insert a link tagged with MONTH name, to index page for four-digit YEAR."
  144. (insert (cal-html-h1
  145. (format "%s %s"
  146. (calendar-month-name month)
  147. (cal-html-href "index.html" (number-to-string year))))))
  148. (defun cal-html-year-dir-ask-user (year)
  149. "Prompt for the html calendar output directory for four-digit YEAR.
  150. Return the expanded directory name, which is based on
  151. `cal-html-directory' by default."
  152. (expand-file-name (read-directory-name
  153. "Enter HTML calendar directory name: "
  154. (expand-file-name (format "%d" year)
  155. cal-html-directory))))
  156. ;;------------------------------------------------------------
  157. ;; page header
  158. ;;------------------------------------------------------------
  159. (defun cal-html-insert-month-header (month year)
  160. "Insert the header for the numeric MONTH page for four-digit YEAR.
  161. Contains links to previous and next month and year, and current minical."
  162. (insert (cal-html-b-table "class=header"))
  163. (insert cal-html-b-tablerow-string)
  164. (insert cal-html-b-tabledata-string) ; month links
  165. (calendar-increment-month month year -1) ; previous month
  166. (cal-html-insert-link-monthpage month year t) ; t --> change-dir
  167. (calendar-increment-month month year 1) ; current month
  168. (cal-html-insert-link-yearpage month year)
  169. (calendar-increment-month month year 1) ; next month
  170. (cal-html-insert-link-monthpage month year t) ; t --> change-dir
  171. (insert cal-html-e-tabledata-string)
  172. (insert cal-html-b-tabledata-string) ; minical
  173. (calendar-increment-month month year -1)
  174. (cal-html-insert-minical month year)
  175. (insert cal-html-e-tabledata-string)
  176. (insert cal-html-e-tablerow-string) ; end
  177. (insert cal-html-e-table-string))
  178. ;;------------------------------------------------------------
  179. ;; minical: a small month calendar with links
  180. ;;------------------------------------------------------------
  181. (defun cal-html-insert-minical (month year)
  182. "Insert a minical for numeric MONTH of YEAR."
  183. (let* ((blank-days ; at start of month
  184. (mod (- (calendar-day-of-week (list month 1 year))
  185. calendar-week-start-day)
  186. 7))
  187. (last (calendar-last-day-of-month month year))
  188. (end-blank-days ; at end of month
  189. (mod (- 6 (- (calendar-day-of-week (list month last year))
  190. calendar-week-start-day))
  191. 7))
  192. (monthpage-name (cal-html-monthpage-name month year))
  193. date)
  194. ;; Start writing table.
  195. (insert (cal-html-comment "MINICAL")
  196. (cal-html-b-table "class=minical border=1 align=center"))
  197. ;; Weekdays row.
  198. (insert cal-html-b-tablerow-string)
  199. (dotimes (i 7)
  200. (insert (cal-html-th
  201. (aref cal-html-day-abbrev-array
  202. (mod (+ i calendar-week-start-day) 7)))))
  203. (insert cal-html-e-tablerow-string)
  204. ;; Initial empty slots.
  205. (insert cal-html-b-tablerow-string)
  206. (dotimes (_i blank-days)
  207. (insert
  208. cal-html-b-tabledata-string
  209. cal-html-e-tabledata-string))
  210. ;; Numbers.
  211. (dotimes (i last)
  212. (insert (format cal-html-minical-day-format monthpage-name i (1+ i)))
  213. ;; New row?
  214. (if (and (zerop (mod (+ i 1 blank-days) 7))
  215. (/= (1+ i) last))
  216. (insert cal-html-e-tablerow-string
  217. cal-html-b-tablerow-string)))
  218. ;; End empty slots (for some browsers like konqueror).
  219. (dotimes (i end-blank-days)
  220. (insert
  221. cal-html-b-tabledata-string
  222. cal-html-e-tabledata-string)))
  223. (insert cal-html-e-tablerow-string
  224. cal-html-e-table-string
  225. (cal-html-comment "MINICAL end")))
  226. ;;------------------------------------------------------------
  227. ;; year index page with minicals
  228. ;;------------------------------------------------------------
  229. (defun cal-html-insert-year-minicals (year cols)
  230. "Make a one page yearly mini-calendar for four-digit YEAR.
  231. There are 12/cols rows of COLS months each."
  232. (insert cal-html-b-document-string)
  233. (insert (cal-html-h1 (number-to-string year)))
  234. (insert (cal-html-b-table "class=year")
  235. cal-html-b-tablerow-string)
  236. (dotimes (i 12)
  237. (insert cal-html-b-tabledata-string)
  238. (cal-html-insert-link-monthpage (1+ i) year)
  239. (cal-html-insert-minical (1+ i) year)
  240. (insert cal-html-e-tabledata-string)
  241. (if (zerop (mod (1+ i) cols))
  242. (insert cal-html-e-tablerow-string
  243. cal-html-b-tablerow-string)))
  244. (insert cal-html-e-tablerow-string
  245. cal-html-e-table-string
  246. cal-html-e-document-string))
  247. ;;------------------------------------------------------------
  248. ;; HTMLify
  249. ;;------------------------------------------------------------
  250. (defun cal-html-htmlify-string (string)
  251. "Protect special characters in STRING from HTML.
  252. Characters are replaced according to `cal-html-html-subst-list'."
  253. (if (stringp string)
  254. (replace-regexp-in-string
  255. (regexp-opt (mapcar 'car cal-html-html-subst-list))
  256. (lambda (x)
  257. (cdr (assoc x cal-html-html-subst-list)))
  258. string)
  259. ""))
  260. (defun cal-html-htmlify-entry (entry)
  261. "Convert a diary entry ENTRY to html with the appropriate class specifier."
  262. (let ((start
  263. (cond
  264. ((string-match "block" (nth 2 entry)) "BLOCK")
  265. ((string-match "anniversary" (nth 2 entry)) "ANN")
  266. ((not (string-match
  267. (number-to-string (nth 2 (car entry)))
  268. (nth 2 entry)))
  269. "NO-YEAR")
  270. (t "NORMAL"))))
  271. (format "<span class=%s>%s</span>" start
  272. (cal-html-htmlify-string (cadr entry)))))
  273. (defun cal-html-htmlify-list (date-list date)
  274. "Return a string of concatenated, HTML-ified diary entries.
  275. DATE-LIST is a list of diary entries. Return only those matching DATE."
  276. (mapconcat (lambda (x) (cal-html-htmlify-entry x))
  277. (let (result)
  278. (dolist (p date-list (reverse result))
  279. (and (car p)
  280. (calendar-date-equal date (car p))
  281. (setq result (cons p result)))))
  282. "<BR>\n "))
  283. ;;------------------------------------------------------------
  284. ;; Monthly calendar
  285. ;;------------------------------------------------------------
  286. (autoload 'diary-list-entries "diary-lib")
  287. (defun cal-html-list-diary-entries (d1 d2)
  288. "Generate a list of all diary-entries from absolute date D1 to D2."
  289. (diary-list-entries (calendar-gregorian-from-absolute d1)
  290. (1+ (- d2 d1)) t))
  291. (defun cal-html-insert-agenda-days (month year diary-list)
  292. "Insert HTML commands for a range of days in monthly calendars.
  293. HTML commands are inserted for the days of the numeric MONTH in
  294. four-digit YEAR. Diary entries in DIARY-LIST are included."
  295. (let ((blank-days ; at start of month
  296. (mod (- (calendar-day-of-week (list month 1 year))
  297. calendar-week-start-day)
  298. 7))
  299. (last (calendar-last-day-of-month month year))
  300. date)
  301. (insert "<a name=0>\n")
  302. (insert (cal-html-b-table "class=agenda border=1"))
  303. (dotimes (i last)
  304. (setq date (list month (1+ i) year))
  305. (insert
  306. (format "<a name=%d></a>\n" (1+ i)) ; link
  307. cal-html-b-tablerow-string
  308. ;; Number & day name.
  309. cal-html-b-tableheader-string
  310. (if cal-html-print-day-number-flag
  311. (format "<em>%d</em>&nbsp;&nbsp;"
  312. (calendar-day-number date))
  313. "")
  314. (format "%d&nbsp;%s" (1+ i)
  315. (aref calendar-day-name-array
  316. (calendar-day-of-week date)))
  317. cal-html-e-tableheader-string
  318. ;; Diary entries.
  319. cal-html-b-tabledata-string
  320. (cal-html-htmlify-list diary-list date)
  321. cal-html-e-tabledata-string
  322. cal-html-e-tablerow-string)
  323. ;; If end of week and not end of month, make new table.
  324. (if (and (zerop (mod (+ i 1 blank-days) 7))
  325. (/= (1+ i) last))
  326. (insert cal-html-e-table-string
  327. (cal-html-b-table
  328. "class=agenda border=1")))))
  329. (insert cal-html-e-table-string))
  330. (defun cal-html-one-month (month year dir)
  331. "Write an HTML calendar file for numeric MONTH of YEAR in directory DIR."
  332. (let ((diary-list (cal-html-list-diary-entries
  333. (calendar-absolute-from-gregorian (list month 1 year))
  334. (calendar-absolute-from-gregorian
  335. (list month
  336. (calendar-last-day-of-month month year)
  337. year)))))
  338. (with-temp-buffer
  339. (insert cal-html-b-document-string)
  340. (cal-html-insert-month-header month year)
  341. (cal-html-insert-agenda-days month year diary-list)
  342. (insert cal-html-e-document-string)
  343. (write-file (expand-file-name
  344. (cal-html-monthpage-name month year) dir)))))
  345. ;;; User commands.
  346. ;;;###cal-autoload
  347. (defun cal-html-cursor-month (month year dir &optional event)
  348. "Write an HTML calendar file for numeric MONTH of four-digit YEAR.
  349. The output directory DIR is created if necessary. Interactively,
  350. MONTH and YEAR are taken from the calendar cursor position, or from
  351. the position specified by EVENT. Note that any existing output files
  352. are overwritten."
  353. (interactive (let* ((event last-nonmenu-event)
  354. (date (calendar-cursor-to-date t event))
  355. (month (calendar-extract-month date))
  356. (year (calendar-extract-year date)))
  357. (list month year (cal-html-year-dir-ask-user year) event)))
  358. (make-directory dir t)
  359. (cal-html-one-month month year dir))
  360. ;;;###cal-autoload
  361. (defun cal-html-cursor-year (year dir &optional event)
  362. "Write HTML calendar files (index and monthly pages) for four-digit YEAR.
  363. The output directory DIR is created if necessary. Interactively,
  364. YEAR is taken from the calendar cursor position, or from the position
  365. specified by EVENT. Note that any existing output files are overwritten."
  366. (interactive (let* ((event last-nonmenu-event)
  367. (year (calendar-extract-year
  368. (calendar-cursor-to-date t event))))
  369. (list year (cal-html-year-dir-ask-user year) event)))
  370. (make-directory dir t)
  371. (with-temp-buffer
  372. (cal-html-insert-year-minicals year cal-html-year-index-cols)
  373. (write-file (expand-file-name "index.html" dir)))
  374. (dotimes (i 12)
  375. (cal-html-one-month (1+ i) year dir)))
  376. (provide 'cal-html)
  377. ;;; cal-html.el ends here