elserv (0.4.0+0.20011203cvs-17.1) 03_emacs-wiki-multiencoding

Summary

 es-wiki.el |  139 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 77 insertions(+), 62 deletions(-)

    
download this patch

Patch contents

#! /bin/sh /usr/share/dpatch/dpatch-run
## emacs-wiki-multiencoding.dpatch by Junichi Uekawa <dancer@debian.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: support different encodings in emacs-wiki

@DPATCH@
diff -urNad elserv-0.4.0+0.20011203cvs~/es-wiki.el elserv-0.4.0+0.20011203cvs/es-wiki.el
--- elserv-0.4.0+0.20011203cvs~/es-wiki.el	2001-07-18 09:51:12.000000000 +0900
+++ elserv-0.4.0+0.20011203cvs/es-wiki.el	2006-09-21 08:13:24.499993928 +0900
@@ -1,6 +1,7 @@
 ;;; es-wiki.el -- Elserv interface for emacs-wiki.
 
 ;; Copyright (C) 2001 Yuuichi Teranishi <teranisi@gohome.org>
+;; Copyright (C) 2002,2003 Junichi Uekawa <dancer@netfort.gr.jp>
 
 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
 ;; Keywords: HTTP
@@ -27,6 +28,7 @@
 ;;; Code:
 (require 'emacs-wiki)
 (require 'elserv)
+(require 'mcharset)
 
 ;; Set action as relative path!
 (defvar elserv-wiki-http-edit-form
@@ -39,7 +41,7 @@
 </form>\n")
 
 (defvar elserv-wiki-http-search-form
-    "
+  "
 <form method=\"GET\" action=\"searchwiki?get\">
   <center>
     Search for: <input type=\"text\" size=\"50\" name=\"q\" value=\"\">
@@ -121,49 +123,60 @@
 	      "]]\n")
     "No WikiNames"))
 
+(defun elserv-wiki-read-charset-from-html ()
+  "Read the html header information and return the charset information"
+  (let* (charset)
+    (goto-line 1)
+    (re-search-forward "content=\"text.html; charset=\\([^\"]+\\)")
+    (setq charset (intern (match-string 1)))
+    charset))
+
 (defun elserv-wiki-render-page (result name)
-  (cond ((string= name emacs-wiki-index-page)
-	 (with-current-buffer (emacs-wiki-generate-index t t)
-	   (emacs-wiki-replace-markup "Wiki Index")
-	   (elserv-set-result-header
-	    result
-	    (list 'content-type "text/html; charset=iso-2022-jp"))
-	   (elserv-set-result-body result (encode-coding-string
-					   (buffer-string)
-					   'iso-2022-jp))
-	   (kill-buffer (current-buffer))))
-	((string= name "WikiNames")
-	 (with-temp-buffer
-	   (insert (elserv-wiki-interwiki-page))
-	   (emacs-wiki-replace-markup "WikiNames")
-	   (elserv-set-result-header
-	    result (list 'content-type "text/html"))
-	   (elserv-set-result-body result (buffer-string))))
-	(t
-	 (let ((file (and (not (emacs-wiki-private-p name))
-			  (cdr (assoc name (emacs-wiki-file-alist)))))
-	       (system-time-locale "C")
-	       (inhibit-read-only t))
-	   (if (null file)
-	       (signal 'elserv-file-not-found
-		       (concat "Page not found"
-			       (format "Wiki page %s not found" name)))
-	     (with-temp-buffer
-	       (let ((modified-time (nth 5 (file-attributes file))))
-		 (insert-file-contents-as-binary file)
-		 (decode-coding-region (point-min) (point-max) 'iso-2022-jp)
-		 (setq buffer-file-name name)
-		 (emacs-wiki-replace-markup name)
-		 (set-buffer-modified-p nil)
-		 (elserv-set-result-header
-		  result
-		  (list 'content-type "text/html; charset=iso-2022-jp"
-			'last-modified (format-time-string "%a, %e %b %Y %T %Z"
-							   modified-time)))
-		 (elserv-set-result-body result
-					 (encode-coding-string
-					  (buffer-string)
-					  'iso-2022-jp)))))))))
+  (let* (charset)
+    (cond ((string= name emacs-wiki-index-page)
+	   (with-current-buffer (emacs-wiki-generate-index t t)
+	     (emacs-wiki-replace-markup "Wiki Index")
+	     (setq charset (elserv-wiki-read-charset-from-html))
+	     (elserv-set-result-header
+	      result
+	      (list 'content-type (concat "text/html; charset=" (symbol-name charset))))
+	     (elserv-set-result-body result (encode-coding-string
+					     (buffer-string)
+					     charset))
+	     (kill-buffer (current-buffer))))
+	  ((string= name "WikiNames")
+	   (with-temp-buffer
+	     (insert (elserv-wiki-interwiki-page))
+	     (emacs-wiki-replace-markup "WikiNames")
+	     (setq charset (elserv-wiki-read-charset-from-html))
+	     (elserv-set-result-header
+	      result (list 'content-type "text/html"))
+	     (elserv-set-result-body result (buffer-string))))
+	  (t
+	   (let ((file (and (not (emacs-wiki-private-p name))
+			    (cdr (assoc name (emacs-wiki-file-alist)))))
+		 (system-time-locale "C")
+		 (inhibit-read-only t))
+	     (if (null file)
+		 (signal 'elserv-file-not-found
+			 (concat "Page not found"
+				 (format "Wiki page %s not found" name)))
+	       (with-temp-buffer
+		 (let ((modified-time (nth 5 (file-attributes file))))
+		   (insert-file-contents file)
+		   (setq buffer-file-name name)
+		   (emacs-wiki-replace-markup name)
+		   (setq charset (elserv-wiki-read-charset-from-html))
+		   (set-buffer-modified-p nil)
+		   (elserv-set-result-header
+		    result
+		    (list 'content-type (concat "text/html; charset=" (symbol-name charset))
+			  'last-modified (format-time-string "%a, %e %b %Y %T %Z"
+							     modified-time)))
+		   (elserv-set-result-body result
+					   (encode-coding-string
+					    (buffer-string)
+					    charset))))))))))
 
 (defun elserv-wiki-edit-page (result page-name)
   (let ((emacs-wiki-http-edit-form elserv-wiki-http-edit-form))
@@ -172,18 +185,19 @@
       (with-temp-buffer
 	(emacs-wiki-setup-edit-page page-name)
 	;; this is required because of the : in the name
-	(elserv-set-result-header
-	 result
-	 (list 'content-type "text/html; charset=iso-2022-jp"))
-	(elserv-set-result-body result
-				(encode-coding-string
-				 (buffer-string)
-				 'iso-2022-jp))))))
-
+	(let* ((charset (detect-mime-charset-region (point-min) (point-max))))
+	  (elserv-set-result-header
+	   result
+	   (list 'content-type (concat "text/html; charset=" (symbol-name charset))))
+	  (elserv-set-result-body result
+				  (encode-coding-string
+				   (buffer-string)
+				   'iso-2022-jp)))))))
+  
 (defun elserv-wiki-change-page (result request)
   (let* ((res (elserv-url-decode (plist-get request 'body)))
 	 (page (caar res))
-  	 (text (cdar res))
+	 (text (cdar res))
 	 (len (length text))
 	 (require-final-newline t)
 	 (pos 0) illegal user)
@@ -220,7 +234,7 @@
 	  (set-file-modes buffer-file-name ?\666))
       (kill-buffer (current-buffer)))
     (with-temp-buffer
-      (emacs-wiki-file-alist)	; force re-check
+      (emacs-wiki-file-alist)		; force re-check
       (insert "<redirect url=\"wiki?" page "\" delay=\"3\">")
       (insert "Thank you, your changes have been saved to " page)
       (insert ".  You will be redirected to "
@@ -246,14 +260,15 @@
     (with-current-buffer (emacs-wiki-grep term)
       (emacs-wiki-wikify-search-results term)
       (emacs-wiki-replace-markup "Search Results")
-      (elserv-set-result-header
-       result
-       (list 'content-type "text/html;charset=iso-2022-jp"))
-      (elserv-set-result-body result
-			      (encode-coding-string
-			       (buffer-string) 'iso-2022-jp))
-      (kill-buffer (current-buffer)))))
-  
+      (let* ((charset (elserv-wiki-read-charset-from-html)))
+	(elserv-set-result-header
+	 result
+	 (list 'content-type (concat "text/html;charset=" (symbol-name charset))))
+	(elserv-set-result-body result
+				(encode-coding-string
+				 (buffer-string) charset))
+	(kill-buffer (current-buffer))))))
+
 (defun elserv-wiki-function (result path ppath request)
   (if (string= path "")
       (elserv-make-redirect result 
@@ -263,7 +278,7 @@
     (let ((emacs-wiki-serving-p t)
 	  (emacs-wiki-publishing-footer elserv-wiki-publishing-footer))
       (cond
-       ((string= path "/") ; default page.
+       ((string= path "/")		; default page.
 	(elserv-wiki-render-page result emacs-wiki-default-page))
        ((string-match "\\`/wiki\\?\\(.+\\)" path)
 	(elserv-wiki-render-page result (match-string 1 path)))