-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathebdb-html.el
158 lines (134 loc) · 4.7 KB
/
ebdb-html.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
;;; ebdb-html.el --- EBDB HTML integration -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2024 Free Software Foundation, Inc.
;; Author: Eric Abrahamsen <[email protected]>
;; Maintainer: Eric Abrahamsen <[email protected]>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file contains code for "doing HTML things" with EBDB records.
;; Right now that only means formatters for exporting EBDB records as
;; HTML.
;;; Code:
(require 'ebdb-format)
(defgroup ebdb-html nil
"Customization options for EBDB with HTML."
:group 'ebdb)
(defclass ebdb-html-formatter (ebdb-formatter)
((post-format-function :initform #'html-mode))
:abstract t
:documentation "Formatter responsible for HTML-specific field
formatting.")
(defclass ebdb-html-formatter-html5 (ebdb-html-formatter
ebdb-formatter-freeform)
((header :initform '((ebdb-record-person ebdb-field-notes))))
:documentation "HTML formatter for \"block-style\" HTML
formatting.")
(cl-defmethod ebdb-fmt-record :around ((_fmt ebdb-html-formatter-html5)
(_rec ebdb-record))
(concat
"<article class=\"ebdb-record\">\n"
(cl-call-next-method)
"\n</article>\n"))
(cl-defmethod ebdb-fmt-record-header ((fmt ebdb-html-formatter-html5)
(rec ebdb-record)
header-fields)
(concat
"<header>\n"
(format "<h1>%s</h1>\n" (ebdb-record-name-string rec))
(mapconcat
(pcase-lambda ((map style inst _class))
(format "<p>%s</p>" (mapconcat (lambda (f) (ebdb-fmt-field fmt f style rec)) inst ", ")))
header-fields
"\n")
"</header>\n"))
(cl-defmethod ebdb-fmt-compose-fields ((fmt ebdb-html-formatter-html5)
(rec ebdb-record)
&optional field-list _depth)
"This particular implementation uses description lists (<dl>)."
(when field-list
(concat
"<dl>\n"
(mapconcat
(pcase-lambda ((map style inst class))
(concat
(format "<dt>%s</dt>" (ebdb-fmt-field-label
fmt
(if (= 1 (length inst))
(car inst)
class)
style
rec))
(mapconcat
(lambda (f)
(format "<dd>%s</dd>" (ebdb-fmt-field fmt f style rec)))
inst
"\n")))
field-list "\n")
"</dl>\n")))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-html-formatter)
(field ebdb-field-mail)
_style
(_rec ebdb-record))
(with-slots (mail aka) field
(format "<a href=\"mailto:%s\">%s</a>" mail (or aka mail))))
(defclass ebdb-html-formatter-tabular (ebdb-formatter-tabular
ebdb-html-formatter)
;; We put the <tr> elements in manually.
((record-separator :initform "")
(field-separator :initform "</td><td>")))
(defcustom ebdb-html-default-formatter-tabular
(make-instance 'ebdb-html-formatter-tabular
:label "html table"
:fields '(mail-primary))
"The default HTML table formatter."
:type 'ebdb-html-formatter-tabular)
(defcustom ebdb-html-default-formatter-html5
(make-instance 'ebdb-html-formatter-html5
:label "html5 block"
:include '(mail-primary ebdb-field-phone ebdb-field-address ebdb-field-notes))
"The default HTML5 block formatter."
:type 'ebdb-html-formatter-html5)
(cl-defmethod ebdb-fmt-header ((fmt ebdb-html-formatter-tabular)
_records)
(with-slots (fields) fmt
(concat
"<table>\n<tr><th>Name</th><th>"
(mapconcat
(lambda (f)
(cond
((stringp f) f)
((or (class-p f)
(eieio-object-p f))
(ebdb-fmt-field-label fmt f 'normal))
((symbolp f)
(symbol-name f))))
fields
"</th><th>")
"</th></tr>\n")))
(cl-defmethod ebdb-fmt-footer ((_fmt ebdb-html-formatter-tabular)
_records)
"\n</table>")
(cl-defmethod ebdb-fmt-record ((_fmt ebdb-html-formatter-tabular)
(_rec ebdb-record))
"Wrap records in <tr> elements.
This is done in lieu of a `record-separator' slot, since it's
around each record, not between records."
(concat "<tr>"
(cl-call-next-method)
"</tr>"))
(cl-defmethod ebdb-fmt-compose-fields :around ((_fmt ebdb-html-formatter-tabular)
(_rec ebdb-record)
&optional _field-list _depth)
(concat "<td>"
(cl-call-next-method)
"</td>"))
(provide 'ebdb-html)
;;; ebdb-html.el ends here