File: html-helper.el

package info (click to toggle)
ecb 2.50%2Bgit20170628-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 5,908 kB
  • sloc: lisp: 28,154; makefile: 260; sh: 43
file content (244 lines) | stat: -rw-r--r-- 6,886 bytes parent folder | download | duplicates (3)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
;;; html-helper.el --- 

;; Copyright (C) 2001 Jesper Nordenberg

;; Author: Jesper Nordenberg <mayhem@home.se>

;; 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 2, 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
;; GNU Emacs; see the file COPYING.  If not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;;; Commentary:

;; Functions for generating HTML pages.

;; $Id$

;;; Code:
(defconst h-br "<br>\n")
(defconst h-hr "<hr>")

(defvar h-body-bgcolor)
(setq h-body-bgcolor "white")

(defvar h-link-fgcolor)
(defvar h-vlink-fgcolor)
(defvar h-alink-fgcolor)
(setq h-link-fgcolor "#0000ff")
(setq h-vlink-fgcolor "#008000")
(setq h-alink-fgcolor "#0080ff")

(defvar h-section-title-bgcolor)
(defvar h-section-title-fgcolor)
(defvar h-section-text-bgcolor)
(defvar h-section-text-fgcolor)
(setq h-section-title-bgcolor "#404080")
(setq h-section-title-fgcolor "#ffffff")
(setq h-section-text-bgcolor "#8080a0")
(setq h-section-text-fgcolor "#000000")

(defun h-line (&optional width-percent align)
  (concat "<hr" (h-get-attrs
                 (delete nil
                         (list (if (numberp width-percent)
                                   (cons 'width
                                         (concat (number-to-string width-percent)
                                                 "%")))
                               (if align
                                   (cons 'align
                                         (if (stringp align)
                                             align
                                           (symbol-name align)))))))
          ">"))

(defun h-date()
  (format-time-string "%Y-%m-%d"))

(defun h-element(name items &optional newline)
  (concat "<" name (h-get-attrs items) ">" (h-list-to-str items) "</" name ">" (if newline "\n" "")))

(defun h-list-to-str(items)
  (if (listp items)
      (if (symbolp (car items))
	  ""
	(mapconcat
	 '(lambda (item)
	    (h-list-to-str item))
	  items ""))
    items))

(defun h-filter(items prefix &optional suffix)
  (mapcar
   (lambda (item)
     (concat (if prefix prefix "") (h-list-to-str item) (if suffix suffix "")))
   items))

(defun h-get-attrs(items &optional defaults)
  (let ((attr-str "")
	(l items)
	al)
    (while l
      (when (and (listp (car l)) (symbolp (caar l)))
	(setq al (cons (car l) al))
	(setq attr-str (concat attr-str " " (symbol-name (caar l))
			       (if (cdar l)
				   (concat "='" (cdar l) "'")
				 ""))))
      (setq l (cdr l)))
    (concat
     attr-str
     (mapconcat
      '(lambda (item)
	 (if (not (assoc (car item) al))
	     (concat " " (symbol-name (car item)) (if (cdr item)
						      (concat "='" (cdr item) "'")
						    ""))
	   ""))
      defaults ""))))

(defun h-doc(filename title &rest items)
  (set-buffer (find-file-noselect filename))
  (setq buffer-read-only nil)
  (erase-buffer)
  (insert (concat "<!-- This file was automatically generated by Emacs "
		  emacs-version " at " (current-time-string)
		  ", do not edit! -->\n<html>\n<head>\n<STYLE TYPE=\"text/css\">\nA:hover {background-color: #c0c0c0}\nA:link, A:visited, A:active { text-decoration: none }\n OL,UL,P,BODY,TD,TR,TH,FORM {font-family : verdana,arial,helvetica,sans-serif; } \n</STYLE>\n<title>" title ;;
		  "</title>\n</head>\n\n<body"
		  (h-get-attrs items (list (cons 'bgcolor h-body-bgcolor)
					   (cons 'link h-link-fgcolor)
					   (cons 'vlink h-vlink-fgcolor)
					   (cons 'alink h-alink-fgcolor)))
		  ">\n" (h-list-to-str items) "\n</body>\n</html>\n"))
  (setq buffer-read-only t)
  (save-buffer))

(defun h-center(&rest items)
  (h-element "center" items))

(defun h-h1(&rest items)
  (concat "\n" (h-element "h1" items t)))

(defun h-h2(&rest items)
  (concat "\n" (h-element "h2" items t)))

(defun h-h3(&rest items)
  (concat "\n" (h-element "h3" items t)))

(defun h-h4(&rest items)
  (concat "\n" (h-element "h4" items t)))

(defun h-b(&rest items)
  (h-element "b" items))

(defun h-p(&rest items)
  (h-element "p" items t))

(defun h-i(&rest items)
  (h-element "i" items t))

(defun h-fsize(size &rest items)
  (h-element "font" (cons (cons 'size size) items)))

(defun h-fcolor(color &rest items)
  (h-element "font" (cons (cons 'color color) items)))

(defun h-list(&rest items)
  (h-element "ul" (h-filter (if (listp (car items))
				(car items)
			      items)
			    "<li>" "\n") t))

(defun h-numbered-list(&rest items)
  (h-element "ol" (h-filter (if (listp (car items))
				(car items)
			      items)
			    "<li>" "\n") t))

(defun h-bullet-list(&rest items)
  (h-element "ul" (h-filter (if (listp (car items))
				(car items)
			      items)
			    "<li>" "\n") t))

(defun h-email(email &rest items)
  (concat
   "<a href='mailto:" email "'>"
   (if items
       (h-list-to-str items)
     email)
   "</a>"))

(defun h-link(url &rest items)
  (concat
   "<a href='" url "'" (h-get-attrs items) ">"
   (if items
       (h-list-to-str items)
     url)
   "</a>"))

(defun h-tag(name &rest items)
  (concat
   "<a name='" name "'>"
   (if items
       (h-list-to-str items)
     "")
   "</a>"))

(defun h-img(url &rest items)
  (concat "<img src='" url "' " (h-list-to-str items) ">"))

(defun h-columns(&rest columns)
  (h-element "table" (h-filter columns "<td valign='top'>") t))

(defun h-table(&rest items)
  (h-element "table" (cons "\n" items) t))

(defun h-td(&rest items)
  (h-element "td" items))

(defun h-tr(&rest items)
  (h-element "tr" items t))

(defun h-section(title &rest items)
  (concat
   (h-table '(width . "100%")
	    '(cellspacing . "0")
	    '(cellpadding . "2")
	    (h-tr (cons 'bgcolor h-section-title-bgcolor)
		  (h-td (h-fcolor h-section-title-fgcolor (h-b title))))
	    (h-tr (cons 'bgcolor h-section-text-bgcolor) (h-td (h-fcolor h-section-text-fgcolor items))))
   h-br))

(defun h-sub-section(title &rest items)
  (concat
   (h-table '(width . "100%")
	    '(cellspacing . "0")
	    '(cellpadding . "2")
	    (h-tr (h-td (h-b title))
	    (h-tr (h-td items))))
   h-br))

(defun h-bullet-link-list(bullet items &optional target)
  (h-table
   (mapconcat
    (lambda (item)
      (h-tr '(valign . "top")
            (h-td '(nowrap) (h-img bullet) " "
                  (h-link (car item)
			  (h-b (if (cdr item) (cadr item) (car item)))
			  (cons 'target target)))
	    (h-td (if (and (cdr item) (cddr item)) (caddr item) ""))))
    items "")))



(provide 'html-helper)