cal-html.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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-holidays t
  52. "If non-nil, include holidays as well as diary entries."
  53. :version "24.2"
  54. :type 'boolean
  55. :group 'calendar-html)
  56. (defcustom cal-html-css-default
  57. (concat
  58. "<STYLE TYPE=\"text/css\">\n"
  59. " BODY { background: #bde; }\n"
  60. " H1 { text-align: center; }\n"
  61. " TABLE { padding: 2pt; }\n"
  62. " TH { background: #dee; }\n"
  63. " TABLE.year { width: 100%; }\n"
  64. " TABLE.agenda { width: 100%; }\n"
  65. " TABLE.header { width: 100%; text-align: center; }\n"
  66. " TABLE.minical TD { background: white; text-align: center; }\n"
  67. " TABLE.agenda TD { background: white; text-align: left; }\n"
  68. " TABLE.agenda TH { text-align: left; width: 20%; }\n"
  69. " SPAN.NO-YEAR { color: #0b3; font-weight: bold; }\n"
  70. " SPAN.ANN { color: #0bb; font-weight: bold; }\n"
  71. " SPAN.BLOCK { color: #048; font-style: italic; }\n"
  72. " SPAN.HOLIDAY { color: #f00; font-weight: bold; }\n"
  73. "</STYLE>\n\n")
  74. "Default cal-html css style. You can override this with a \"cal.css\" file."
  75. :type 'string
  76. :version "24.2" ; added SPAN.HOLIDAY
  77. :group 'calendar-html)
  78. ;;; End customizable variables.
  79. ;;; HTML and CSS code constants.
  80. (defconst cal-html-e-document-string "<BR><BR>\n</BODY>\n</HTML>"
  81. "HTML code for end of page.")
  82. (defconst cal-html-b-tablerow-string "<TR>\n"
  83. "HTML code for beginning of table row.")
  84. (defconst cal-html-e-tablerow-string "</TR>\n"
  85. "HTML code for end of table row.")
  86. (defconst cal-html-b-tabledata-string " <TD>"
  87. "HTML code for beginning of table data.")
  88. (defconst cal-html-e-tabledata-string " </TD>\n"
  89. "HTML code for end of table data.")
  90. (defconst cal-html-b-tableheader-string " <TH>"
  91. "HTML code for beginning of table header.")
  92. (defconst cal-html-e-tableheader-string " </TH>\n"
  93. "HTML code for end of table header.")
  94. (defconst cal-html-e-table-string
  95. "</TABLE>\n<!-- ================================================== -->\n"
  96. "HTML code for end of table.")
  97. (defconst cal-html-minical-day-format " <TD><a href=%s#%d>%d</TD>\n"
  98. "HTML code for a day in the minical - links NUM to month-page#NUM.")
  99. (defconst cal-html-b-document-string
  100. (concat
  101. "<HTML>\n"
  102. "<HEAD>\n"
  103. "<TITLE>Calendar</TITLE>\n"
  104. "<!--This buffer was produced by cal-html.el-->\n\n"
  105. cal-html-css-default
  106. "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"cal.css\">\n"
  107. "</HEAD>\n\n"
  108. "<BODY>\n\n")
  109. "Initial block for html page.")
  110. (defconst cal-html-html-subst-list
  111. '(("&" . "&amp;")
  112. ("\n" . "<BR>\n"))
  113. "Alist of symbols and their HTML replacements.")
  114. (defun cal-html-comment (string)
  115. "Return STRING as html comment."
  116. (format "<!-- ====== %s ====== -->\n"
  117. (replace-regexp-in-string "--" "++" string)))
  118. (defun cal-html-href (link string)
  119. "Return a hyperlink to url LINK with text STRING."
  120. (format "<A HREF=\"%s\">%s</A>" link string))
  121. (defun cal-html-h3 (string)
  122. "Return STRING as html header h3."
  123. (format "\n <H3>%s</H3>\n" string))
  124. (defun cal-html-h1 (string)
  125. "Return STRING as html header h1."
  126. (format "\n <H1>%s</H1>\n" string))
  127. (defun cal-html-th (string)
  128. "Return STRING as html table header."
  129. (format "%s%s%s" cal-html-b-tableheader-string string
  130. cal-html-e-tableheader-string))
  131. (defun cal-html-b-table (arg)
  132. "Return table tag with attribute ARG."
  133. (format "\n<TABLE %s>\n" arg))
  134. (defun cal-html-monthpage-name (month year)
  135. "Return name of html page for numeric MONTH and four-digit YEAR.
  136. For example, \"2006-08.html\" for 8 2006."
  137. (format "%d-%.2d.html" year month))
  138. (defun cal-html-insert-link-monthpage (month year &optional change-dir)
  139. "Insert a link to the html page for numeric MONTH and four-digit YEAR.
  140. If optional argument CHANGE-DIR is non-nil and MONTH is 1 or 2,
  141. the link points to a different year and so has a directory part."
  142. (insert (cal-html-h3
  143. (cal-html-href
  144. (concat (and change-dir
  145. (member month '(1 12))
  146. (format "../%d/" year))
  147. (cal-html-monthpage-name month year))
  148. (calendar-month-name month)))))
  149. (defun cal-html-insert-link-yearpage (month year)
  150. "Insert a link tagged with MONTH name, to index page for four-digit YEAR."
  151. (insert (cal-html-h1
  152. (format "%s %s"
  153. (calendar-month-name month)
  154. (cal-html-href "index.html" (number-to-string year))))))
  155. (defun cal-html-year-dir-ask-user (year)
  156. "Prompt for the html calendar output directory for four-digit YEAR.
  157. Return the expanded directory name, which is based on
  158. `cal-html-directory' by default."
  159. (expand-file-name (read-directory-name
  160. "Enter HTML calendar directory name: "
  161. (expand-file-name (format "%d" year)
  162. cal-html-directory))))
  163. ;;------------------------------------------------------------
  164. ;; page header
  165. ;;------------------------------------------------------------
  166. (defun cal-html-insert-month-header (month year)
  167. "Insert the header for the numeric MONTH page for four-digit YEAR.
  168. Contains links to previous and next month and year, and current minical."
  169. (insert (cal-html-b-table "class=header"))
  170. (insert cal-html-b-tablerow-string)
  171. (insert cal-html-b-tabledata-string) ; month links
  172. (calendar-increment-month month year -1) ; previous month
  173. (cal-html-insert-link-monthpage month year t) ; t --> change-dir
  174. (calendar-increment-month month year 1) ; current month
  175. (cal-html-insert-link-yearpage month year)
  176. (calendar-increment-month month year 1) ; next month
  177. (cal-html-insert-link-monthpage month year t) ; t --> change-dir
  178. (insert cal-html-e-tabledata-string)
  179. (insert cal-html-b-tabledata-string) ; minical
  180. (calendar-increment-month month year -1)
  181. (cal-html-insert-minical month year)
  182. (insert cal-html-e-tabledata-string)
  183. (insert cal-html-e-tablerow-string) ; end
  184. (insert cal-html-e-table-string))
  185. ;;------------------------------------------------------------
  186. ;; minical: a small month calendar with links
  187. ;;------------------------------------------------------------
  188. (autoload 'holiday-in-range "holidays")
  189. (defun cal-html-insert-minical (month year)
  190. "Insert a minical for numeric MONTH of YEAR."
  191. (let* ((blank-days ; at start of month
  192. (mod (- (calendar-day-of-week (list month 1 year))
  193. calendar-week-start-day)
  194. 7))
  195. (last (calendar-last-day-of-month month year))
  196. (end-blank-days ; at end of month
  197. (mod (- 6 (- (calendar-day-of-week (list month last year))
  198. calendar-week-start-day))
  199. 7))
  200. (monthpage-name (cal-html-monthpage-name month year))
  201. date)
  202. ;; Start writing table.
  203. (insert (cal-html-comment "MINICAL")
  204. (cal-html-b-table "class=minical border=1 align=center"))
  205. ;; Weekdays row.
  206. (insert cal-html-b-tablerow-string)
  207. (dotimes (i 7)
  208. (insert (cal-html-th
  209. (aref cal-html-day-abbrev-array
  210. (mod (+ i calendar-week-start-day) 7)))))
  211. (insert cal-html-e-tablerow-string)
  212. ;; Initial empty slots.
  213. (insert cal-html-b-tablerow-string)
  214. (dotimes (_i blank-days)
  215. (insert
  216. cal-html-b-tabledata-string
  217. cal-html-e-tabledata-string))
  218. ;; Numbers.
  219. (dotimes (i last)
  220. (insert (format cal-html-minical-day-format monthpage-name i (1+ i)))
  221. ;; New row?
  222. (if (and (zerop (mod (+ i 1 blank-days) 7))
  223. (/= (1+ i) last))
  224. (insert cal-html-e-tablerow-string
  225. cal-html-b-tablerow-string)))
  226. ;; End empty slots (for some browsers like konqueror).
  227. (dotimes (i end-blank-days)
  228. (insert
  229. cal-html-b-tabledata-string
  230. cal-html-e-tabledata-string)))
  231. (insert cal-html-e-tablerow-string
  232. cal-html-e-table-string
  233. (cal-html-comment "MINICAL end")))
  234. ;;------------------------------------------------------------
  235. ;; year index page with minicals
  236. ;;------------------------------------------------------------
  237. (defun cal-html-insert-year-minicals (year cols)
  238. "Make a one page yearly mini-calendar for four-digit YEAR.
  239. There are 12/cols rows of COLS months each."
  240. (insert cal-html-b-document-string)
  241. (insert (cal-html-h1 (number-to-string year)))
  242. (insert (cal-html-b-table "class=year")
  243. cal-html-b-tablerow-string)
  244. (dotimes (i 12)
  245. (insert cal-html-b-tabledata-string)
  246. (cal-html-insert-link-monthpage (1+ i) year)
  247. (cal-html-insert-minical (1+ i) year)
  248. (insert cal-html-e-tabledata-string)
  249. (if (zerop (mod (1+ i) cols))
  250. (insert cal-html-e-tablerow-string
  251. cal-html-b-tablerow-string)))
  252. (insert cal-html-e-tablerow-string
  253. cal-html-e-table-string
  254. cal-html-e-document-string))
  255. ;;------------------------------------------------------------
  256. ;; HTMLify
  257. ;;------------------------------------------------------------
  258. (defun cal-html-htmlify-string (string)
  259. "Protect special characters in STRING from HTML.
  260. Characters are replaced according to `cal-html-html-subst-list'."
  261. (if (stringp string)
  262. (replace-regexp-in-string
  263. (regexp-opt (mapcar 'car cal-html-html-subst-list))
  264. (lambda (x)
  265. (cdr (assoc x cal-html-html-subst-list)))
  266. string)
  267. ""))
  268. (defun cal-html-htmlify-entry (entry &optional class)
  269. "Convert a diary entry ENTRY to html with the appropriate class specifier.
  270. Optional argument CLASS is the class specifier to use."
  271. (let ((start
  272. (cond
  273. (class)
  274. ((string-match "block" (nth 2 entry)) "BLOCK")
  275. ((string-match "anniversary" (nth 2 entry)) "ANN")
  276. ((not (string-match
  277. (number-to-string (nth 2 (car entry)))
  278. (nth 2 entry)))
  279. "NO-YEAR")
  280. (t "NORMAL"))))
  281. (format "<span class=%s>%s</span>" start
  282. (cal-html-htmlify-string (cadr entry)))))
  283. (defun cal-html-htmlify-list (date-list date &optional holidays)
  284. "Return a string of concatenated, HTML-ified diary entries.
  285. DATE-LIST is a list of diary entries. Return only those matching DATE.
  286. Optional argument HOLIDAYS non-nil means the input is actually a list
  287. of holidays, rather than diary entries."
  288. (mapconcat (lambda (x) (cal-html-htmlify-entry x (if holidays "HOLIDAY")))
  289. (let (result)
  290. (dolist (p date-list (reverse result))
  291. (and (car p)
  292. (calendar-date-equal date (car p))
  293. (setq result (cons p result)))))
  294. "<BR>\n "))
  295. ;;------------------------------------------------------------
  296. ;; Monthly calendar
  297. ;;------------------------------------------------------------
  298. (autoload 'diary-list-entries "diary-lib")
  299. (defun cal-html-list-diary-entries (d1 d2)
  300. "Generate a list of all diary-entries from absolute date D1 to D2."
  301. (diary-list-entries (calendar-gregorian-from-absolute d1)
  302. (1+ (- d2 d1)) t))
  303. (defun cal-html-insert-agenda-days (month year diary-list holiday-list)
  304. "Insert HTML commands for a range of days in monthly calendars.
  305. HTML commands are inserted for the days of the numeric MONTH in
  306. four-digit YEAR. Includes diary entries in DIARY-LIST, and
  307. holidays in HOLIDAY-LIST."
  308. (let ((blank-days ; at start of month
  309. (mod (- (calendar-day-of-week (list month 1 year))
  310. calendar-week-start-day)
  311. 7))
  312. (last (calendar-last-day-of-month month year))
  313. date)
  314. (insert "<a name=0>\n")
  315. (insert (cal-html-b-table "class=agenda border=1"))
  316. (dotimes (i last)
  317. (setq date (list month (1+ i) year))
  318. (insert
  319. (format "<a name=%d></a>\n" (1+ i)) ; link
  320. cal-html-b-tablerow-string
  321. ;; Number & day name.
  322. cal-html-b-tableheader-string
  323. (if cal-html-print-day-number-flag
  324. (format "<em>%d</em>&nbsp;&nbsp;"
  325. (calendar-day-number date))
  326. "")
  327. (format "%d&nbsp;%s" (1+ i)
  328. (aref calendar-day-name-array
  329. (calendar-day-of-week date)))
  330. cal-html-e-tableheader-string
  331. ;; Diary entries.
  332. cal-html-b-tabledata-string
  333. (cal-html-htmlify-list holiday-list date t)
  334. (and holiday-list diary-list "<BR>\n")
  335. (cal-html-htmlify-list diary-list date)
  336. cal-html-e-tabledata-string
  337. cal-html-e-tablerow-string)
  338. ;; If end of week and not end of month, make new table.
  339. (if (and (zerop (mod (+ i 1 blank-days) 7))
  340. (/= (1+ i) last))
  341. (insert cal-html-e-table-string
  342. (cal-html-b-table
  343. "class=agenda border=1")))))
  344. (insert cal-html-e-table-string))
  345. (defun cal-html-one-month (month year dir)
  346. "Write an HTML calendar file for numeric MONTH of YEAR in directory DIR."
  347. (let* ((d1 (calendar-absolute-from-gregorian (list month 1 year)))
  348. (d2 (calendar-absolute-from-gregorian
  349. (list month
  350. (calendar-last-day-of-month month year)
  351. year)))
  352. (diary-list (cal-html-list-diary-entries d1 d2))
  353. (holiday-list (if cal-html-holidays (holiday-in-range d1 d2))))
  354. (with-temp-buffer
  355. (insert cal-html-b-document-string)
  356. (cal-html-insert-month-header month year)
  357. (cal-html-insert-agenda-days month year diary-list holiday-list)
  358. (insert cal-html-e-document-string)
  359. (write-file (expand-file-name
  360. (cal-html-monthpage-name month year) dir)))))
  361. ;;; User commands.
  362. ;;;###cal-autoload
  363. (defun cal-html-cursor-month (month year dir &optional event)
  364. "Write an HTML calendar file for numeric MONTH of four-digit YEAR.
  365. The output directory DIR is created if necessary. Interactively,
  366. MONTH and YEAR are taken from the calendar cursor position, or from
  367. the position specified by EVENT. Note that any existing output files
  368. are overwritten."
  369. (interactive (let* ((event last-nonmenu-event)
  370. (date (calendar-cursor-to-date t event))
  371. (month (calendar-extract-month date))
  372. (year (calendar-extract-year date)))
  373. (list month year (cal-html-year-dir-ask-user year) event)))
  374. (make-directory dir t)
  375. (cal-html-one-month month year dir))
  376. ;;;###cal-autoload
  377. (defun cal-html-cursor-year (year dir &optional event)
  378. "Write HTML calendar files (index and monthly pages) for four-digit YEAR.
  379. The output directory DIR is created if necessary. Interactively,
  380. YEAR is taken from the calendar cursor position, or from the position
  381. specified by EVENT. Note that any existing output files are overwritten."
  382. (interactive (let* ((event last-nonmenu-event)
  383. (year (calendar-extract-year
  384. (calendar-cursor-to-date t event))))
  385. (list year (cal-html-year-dir-ask-user year) event)))
  386. (make-directory dir t)
  387. (with-temp-buffer
  388. (cal-html-insert-year-minicals year cal-html-year-index-cols)
  389. (write-file (expand-file-name "index.html" dir)))
  390. (dotimes (i 12)
  391. (cal-html-one-month (1+ i) year dir)))
  392. (provide 'cal-html)
  393. ;;; cal-html.el ends here