1 ;;; nxhtml-js.el --- Javascript support functions
3 ;; Author: Lennart Borgman (lennart O borgman A gmail O com)
4 ;; Created: Sat Apr 28 2007
6 ;; Last-Updated: 2008-12-28 Sun
11 ;; Fxeatures that might be required by this library:
15 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
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.
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.
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
47 (eval-when-compile (require 'nxhtml nil t))
48 (eval-when-compile (require 'nxhtml-mode nil t))
50 (defun nxhtml-add-link (type src silent)
51 ;;<script type="text/javascript" src="EmacsW32.js"></script>
59 (concat "<script type=\"text/javascript\" src=\"" src "\"></script>\n"))
61 (concat "<link rel=\"stylesheet\" href=\"" src "\" type=\"text/css\" media=\"screen\"/>\n"))
63 (error "Bad type=%s" type))
65 (goto-char (point-min))
66 (when (search-forward link nil t)
68 (let ((temp-ovl (make-overlay (match-beginning 0)
70 (after-string " <-- It is already here "))
73 (put-text-property 0 (length after-string)
74 'face '(:background "red")
76 (overlay-put temp-ovl 'face '(:background "yellow"))
77 (overlay-put temp-ovl 'priority 100)
78 (overlay-put temp-ovl 'after-string after-string)
82 (error (message "%s" (error-message-string err))))
83 (delete-overlay temp-ovl))
85 (unless (search-forward "</head>" nil t)
87 (unless (y-or-n-p "Can't find </head>, insert link to script here? ")
92 (indent-according-to-mode))))))
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/'.
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)
120 (defconst nxhtml-smoothgallery-mark "<!-- SmoothGallery -->")
121 (defun nxhtml-smoothgallery-find ()
125 (goto-char (point-min))
126 (when (search-forward nxhtml-smoothgallery-mark nil t)
127 (back-to-indentation)
132 ;; (1+ (not (any ">")))
134 ;; "\">" (eval nxhtml-smoothgallery-mark))
135 ;;(concat "<div id=\"\\([^>]+\\)\">" nxhtml-smoothgallery-mark)
143 ,nxhtml-smoothgallery-mark))
146 (copy-marker (match-beginning 0))
147 (buffer-substring-no-properties
148 (match-beginning 1) (match-end 1))))))))
150 (defun nxhtml-smoothgallery-mk-jsmark (name)
151 (concat "new gallery($('" name "'), {"))
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)))
158 (defun nxhtml-smoothgallery-add (point-name)
161 (setq point-name (nxhtml-smoothgallery-find))
163 (setq point-name "myGallery")))
164 (let ((name (if (consp point-name)
167 (where (when (consp point-name)
170 (goto-char (point-min))
171 (search-forward "<body")
174 (setq where (point-marker))
175 (insert-and-indent "<div id=\"" name "\">" nxhtml-smoothgallery-mark
178 (unless (nxhtml-smoothgallery-find-script name)
181 (insert-and-indent "<script type=\"text/javascript\">
182 function startGallery() {
183 var myGallery = new gallery($('" name "'), {
192 window.onDomReady(startGallery);
194 (indent-according-to-mode))
197 (defun nxhtml-smoothgallery-add-img (imgsrc thumbsrc title description)
198 (interactive (let ((gallery (nxhtml-smoothgallery-find)))
200 (goto-char (car gallery)))
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: ")
208 (unless thumbsrc (setq thumbsrc imgsrc))
209 (let ((gallery (nxhtml-smoothgallery-find)))
211 (setq gallery (nxhtml-smoothgallery-add nil)))
212 (goto-char (car gallery))
215 <div class=\"imageElement\">
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)\" />
222 ;; (when (file-exists-p src)
223 ;; (let ((sizes (image-size (create-image src) t)))
225 ;; " width=\"" (format "%d" (car sizes)) "\""
226 ;; " height=\"" (format "%d" (cdr sizes)) "\"")
230 (defun insert-and-indent (&rest lines)
231 (let ((lines (split-string (apply 'concat lines) "[\n\r]")))
234 (indent-according-to-mode))))
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
240 ;;; nxhtml-js.el ends here