initial commit
[emacs-init.git] / nxhtml / nxhtml / nxhtmljs.el
1 ;;; nxhtml-js.el --- Javascript support functions
2 ;;
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: Sat Apr 28 2007
5 ;; Version: 0.1
6 ;; Last-Updated: 2008-12-28 Sun
7 ;; URL:
8 ;; Keywords:
9 ;; Compatibility:
10 ;;
11 ;; Fxeatures that might be required by this library:
12 ;;
13 ;;   None
14 ;;
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 ;;
17 ;;; Commentary:
18 ;;
19 ;;
20 ;;
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 ;;
23 ;;; Change log:
24 ;;
25 ;;
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;
28 ;; This program is free software; you can redistribute it and/or
29 ;; modify it under the terms of the GNU General Public License as
30 ;; published by the Free Software Foundation; either version 2, or
31 ;; (at your option) any later version.
32 ;;
33 ;; This program is distributed in the hope that it will be useful,
34 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
35 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36 ;; General Public License for more details.
37 ;;
38 ;; You should have received a copy of the GNU General Public License
39 ;; along with this program; see the file COPYING.  If not, write to
40 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
41 ;; Floor, Boston, MA 02110-1301, USA.
42 ;;
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;;
45 ;;; Code:
46
47 (eval-when-compile (require 'nxhtml nil t))
48 (eval-when-compile (require 'nxhtml-mode nil t))
49
50 (defun nxhtml-add-link (type src silent)
51   ;;<script type="text/javascript" src="EmacsW32.js"></script>
52   (catch 'exit
53     (save-excursion
54       (save-restriction
55         (widen)
56         (let ((here (point))
57               (link (cond
58                      ((eq type 'js)
59                       (concat "<script type=\"text/javascript\" src=\"" src "\"></script>\n"))
60                      ((eq type 'css)
61                       (concat "<link rel=\"stylesheet\" href=\"" src "\" type=\"text/css\" media=\"screen\"/>\n"))
62                      (t
63                       (error "Bad type=%s" type))
64                      )))
65           (goto-char (point-min))
66           (when (search-forward link nil t)
67             (unless silent
68               (let ((temp-ovl (make-overlay (match-beginning 0)
69                                             (match-end 0)))
70                     (after-string " <-- It is already here "))
71                 (condition-case err
72                     (progn
73                       (put-text-property 0 (length after-string)
74                                          'face '(:background "red")
75                                          after-string)
76                       (overlay-put temp-ovl 'face '(:background "yellow"))
77                       (overlay-put temp-ovl 'priority 100)
78                       (overlay-put temp-ovl 'after-string after-string)
79                       (redisplay t)
80                       (sit-for 3))
81                   (quit nil)
82                   (error (message "%s" (error-message-string err))))
83                 (delete-overlay temp-ovl))
84               (throw 'exit t)))
85           (unless (search-forward "</head>" nil t)
86             (goto-char here)
87             (unless (y-or-n-p "Can't find </head>, insert link to script here? ")
88               (throw 'exit nil)))
89           (beginning-of-line)
90           (insert link)
91           (beginning-of-line 0)
92           (indent-according-to-mode))))))
93
94 (defun nxhtml-smoothgallery-add-base (silent)
95   "Add links to javascript and style sheets.
96 This command add links to the javascript and style sheets that
97 comes with SmoothGallery, see URL
98 `http://smoothgallery.jondesign.net/'.
99
100 * NOTICE: The files are not added to your project. Instead the
101 files that comes with nXhtml are linked to directly."
102   (interactive (list nil))
103   (unless (buffer-file-name)
104     (error "Can't add SmoothGallery if buffer has no filename"))
105   (unless (memq major-mode '(html-mode nxhtml-mode))
106     (error "Wrong major mode"))
107   (let* ((libfile (locate-library "nxhtml"))
108          (jsdir-abs (expand-file-name "doc/js/smoothgallery/scripts/"
109                                     (file-name-directory libfile)))
110          (jsdir-rel (file-relative-name jsdir-abs (file-name-directory (buffer-file-name))))
111          (cssdir-abs (expand-file-name "doc/js/smoothgallery/css/"
112                                        (file-name-directory libfile)))
113          (cssdir-rel (file-relative-name cssdir-abs (file-name-directory (buffer-file-name)))))
114     (nxhtml-add-link 'js (concat jsdir-rel "mootools.js") silent)
115     (nxhtml-add-link 'js (concat jsdir-rel "jd.gallery.js") silent)
116     (nxhtml-add-link 'css (concat cssdir-rel "jd.gallery.css") silent)
117     (nxhtml-add-link 'css (concat cssdir-rel "layout.css") silent)
118     ))
119
120 (defconst nxhtml-smoothgallery-mark "<!-- SmoothGallery -->")
121 (defun nxhtml-smoothgallery-find ()
122   (save-excursion
123     (save-restriction
124       (widen)
125       (goto-char (point-min))
126       (when  (search-forward nxhtml-smoothgallery-mark nil t)
127         (back-to-indentation)
128         (when (looking-at
129                ;; (rx
130                ;;  "<div id=\""
131                ;;  (submatch
132                ;;   (1+ (not (any ">")))
133                ;;   )
134                ;;  "\">" (eval nxhtml-smoothgallery-mark))
135                ;;(concat "<div id=\"\\([^>]+\\)\">" nxhtml-smoothgallery-mark)
136                (rx-to-string
137                 `(and
138                   "<div id=\""
139                   (submatch
140                    (1+ (not (any ">")))
141                    )
142                   "\">"
143                   ,nxhtml-smoothgallery-mark))
144                )
145           (cons
146            (copy-marker (match-beginning 0))
147            (buffer-substring-no-properties
148            (match-beginning 1) (match-end 1))))))))
149
150 (defun nxhtml-smoothgallery-mk-jsmark (name)
151   (concat "new gallery($('" name "'), {"))
152
153 (defun nxhtml-smoothgallery-find-script (name)
154   (let ((jsmark (nxhtml-smoothgallery-mk-jsmark name)))
155     (goto-char (point-min))
156     (search-forward jsmark nil t)))
157
158 (defun nxhtml-smoothgallery-add (point-name)
159   (interactive "i")
160   (unless point-name
161     (setq point-name (nxhtml-smoothgallery-find))
162     (unless point-name
163       (setq point-name "myGallery")))
164   (let ((name (if (consp point-name)
165                   (cdr point-name)
166                 point-name))
167         (where (when (consp point-name)
168                  (car point-name))))
169     (unless where
170       (goto-char (point-min))
171       (search-forward "<body")
172       (search-forward ">")
173       (insert "\n")
174       (setq where (point-marker))
175       (insert-and-indent "<div id=\"" name "\">" nxhtml-smoothgallery-mark
176                          "\n</div>")
177       )
178     (unless (nxhtml-smoothgallery-find-script name)
179       (goto-char where)
180       (beginning-of-line)
181       (insert-and-indent "<script type=\"text/javascript\">
182           function startGallery() {
183             var myGallery = new gallery($('" name "'), {
184                 timed: true,
185                 delay: 9000,
186                 embedLinks: false,
187                 showArrows: true,
188                 showCarousel: false,
189                 showInfopane: true,
190             });
191           }
192           window.onDomReady(startGallery);
193         </script>")
194       (indent-according-to-mode))
195     (goto-char where)))
196
197 (defun nxhtml-smoothgallery-add-img (imgsrc thumbsrc title description)
198   (interactive (let ((gallery (nxhtml-smoothgallery-find)))
199                  (when gallery
200                    (goto-char (car gallery)))
201                  (list
202                   (nxhtml-read-url nil nil 'nxhtml-image-url-predicate "Image")
203                   (when (y-or-n-p "Include thumbnail? " )
204                     (nxhtml-read-url nil nil 'nxhtml-image-url-predicate "Thumbnail"))
205                   (read-string "Title: ")
206                   (read-string "Description: ")
207                   )))
208   (unless thumbsrc (setq thumbsrc imgsrc))
209   (let ((gallery (nxhtml-smoothgallery-find)))
210     (unless gallery
211       (setq gallery (nxhtml-smoothgallery-add nil)))
212     (goto-char (car gallery))
213     (end-of-line)
214     (insert-and-indent "
215        <div class=\"imageElement\">
216          <h3>" title "</h3>
217          <p>" description "</p>
218          <a href=\"#\" title=\"open image\" class=\"open\"></a>
219          <img src=\"" imgsrc "\" class=\"full\" alt=\"" title "\" />
220          <img src=\"" thumbsrc "\" class=\"thumbnail\" alt=\"" title " (thumbnail)\" />
221        </div>")
222 ;;     (when (file-exists-p src)
223 ;;       (let ((sizes (image-size (create-image src) t)))
224 ;;         (insert
225 ;;          " width=\""  (format "%d" (car sizes)) "\""
226 ;;          " height=\"" (format "%d" (cdr sizes)) "\"")
227 ;;         ))
228     ))
229
230 (defun insert-and-indent (&rest lines)
231   (let ((lines (split-string (apply 'concat lines) "[\n\r]")))
232     (dolist (line lines)
233         (insert "\n" line)
234         (indent-according-to-mode))))
235
236
237 (provide 'nxhtml-js)
238
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
240 ;;; nxhtml-js.el ends here