xemacsでPSGMLを使うメモ

自分のサイトをhtmlからxhtmlに変更した時に、今までhtmlを書くときに使用していたhtml-helper-modeからPSGMLを使うように変更してみた時のメモです。

PSGMLのインストール

PSGMLパッケージは、xemacs-sumo elispパッケージがインストールされていれば、既に入っているのでインストール作業は不要。 ;-)

.emacs.elの変更

下記のように追加

.emacs.elに追加
;; templatesの自動挿入
(load "autoinsert")
(add-hook 'find-file-hooks 'auto-insert)
	  
(setq auto-mode-alist
      (append '(
		("\\.html$"  . xml-mode)
		("\\.xhtml$" . xml-mode)
		)
	      auto-mode-alist))
	  
(setq html-mode-hook
      '(lambda ()
	 (auto-fill-mode 0)
	 ))
(autoload 'sgml-mode "psgml" "Major mode to edit SGML files." t)
(autoload 'xml-mode "psgml" "Major mode to edit XML files." t)
(autoload 'html-mode "psgml-html" "Major mode to edit html files" t)
(add-hook 'sgml-mode-hook
          (function
           (lambda ()
	     (auto-fill-mode 0)
	     ;; sgml user options
	     (setq sgml-set-face                           t
		   ;; 現在どのelement内に位置するかmode lineに非表示
		   sgml-live-element-indicator             nil
		   ;; 自動的にDTDの解析を行なう
		   sgml-auto-activate-dtd                  t
		   ;; validateするときに、ファイルを保存するか尋ねる
		   sgml-offer-save                         t
		   ;; リージョンを選択しタグを挿入?
		   sgml-tag-region-if-active               t
		   ;; 終了タグ追加時に、要素の終わりにある空白を削除
		   sgml-normalize-trims                    t
		   ;; 要素内に必要な子要素を自動挿入
		   sgml-auto-insert-required-elements      t
		   ;; タグ挿入時に終了タグも挿入する
		   sgml-balanced-tag-edit                  nil
		   ;; タグを省略した場合、そのタグがあると仮定しタグの記述を行なう
		   ;sgml-omittag-transparent                nil
		   ;; タグを挿入した後のカーソルの位置(nil:タグの中 nonil:タグ後ろ)
		   sgml-leave-point-after-insert           nil
		   sgml-insert-missing-element-comment     nil
		   ;; 終了タグを改行して挿入
		   sgml-insert-end-tag-on-new-line         nil
		   sgml-warn-about-undefined-elements      t
		   sgml-warn-about-undefined-entities      t
		   ;; 未定義タグは無視
		   sgml-ignore-undefined-elements          t
		   sgml-recompile-out-of-date-cdtd         nil
		   ;;sgml-catalog-files
		   ;;sgml-ecat-files
		   sgml-insert-defaulted-attributes        t
		   )
	     ;; sgml file options
	     (setq sgml-omittag                    nil    ;; 省略可能なタグの省略を不許可
		   sgml-shorttag                   nil    ;; <>タグの不許可
		   sgml-general-insert-case        'lower
		   sgml-entity-insert-case         'lower
		   sgml-namecase-general           t
		   sgml-minimize-attributes        t      ;; checked="checked"等をcheckedと書くことを許す
		   sgml-always-quote-attributes    t      ;; 属性値を常に引用符で括るか
		   sgml-indent-step                2      ;; indent
		   sgml-indent-data                t      ;; データ混在内容でも字下げを行なうか
		   ;;sgml-doctype
		   ;;sgml-parent-document
		   ;;sgml-default-dtd-file
		   ;;sgml-local-catalogs
		   ;;sgml-local-ecat-files
		   )
	  
	  
	     ;; 更新タイムスタンプの自動挿入
	     (setq time-stamp-line-limit 10000)
	     (if (not (memq 'time-stamp write-file-hooks))
		 (setq write-file-hooks
		       (cons 'time-stamp write-file-hooks)))
	     (setq time-stamp-format "%3a %3b %02d %02H:%02M:%02S %:y %Z")
	     (setq time-stamp-start "Last modified:[ \t]")
	     (setq time-stamp-end "$")
	  
	     ;; xhtml template
	     (setq auto-insert-alist
		   (append '((("\\.[x]?html$" . "XHTML Document")
			      nil
			      "<?xml version=\"1.0\" encoding=\"EUC-JP\"?><!-- -*- xml -*- -->\n"
			      "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
			      "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
			      "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja\" lang=\"ja\">\n"
			      "  <head>\n"
			      "    <meta http-equiv=\"Content-type\" content='text/html; charset=\"EUC-JP\"' />\n"
			      "    <meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n"
			      "    <meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\" />\n"
			      "    <title></title>\n"
			      "  </head>\n"
			      "\n"
			      "  <body>\n"
			      "    <h1></h1>\n\n"
			      "\n\n    <hr/>\n"
			      "    <address>\n"
			      "    " (xhtml-return-created-string)
			      "       Last modified:\ \n"
			      "    </address>\n"
			      "\n  </body>\n</html>\n")
			     )
			   auto-insert-alist))
	  
	     ;; 作成日 <!-- Created:  --> を作成する関数
	     (defun xhtml-return-created-string()
	       "Return a \"Created:\" string."
	       (let ((time (current-time-string)))
		 (concat "<!-- Created: "
			 (substring time 0 20)
			 (nth 1 (current-time-zone))
			 " "
			 (substring time -4)
			 " -->\n")))
	     )))

(setq sgml-custom-dtd
 '(("HTML 4.01 Strict" 
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"
\"http://www.w3.org/TR/html4/strict.dtd\">")
   ("HTML 4.01 Translational" 
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Translational//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">")
   ("HTML 4.01 Frameset" 
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\"
\"http://www.w3.org/TR/html4/frameset.dtd\">")
   ("XHTML 1.0 Strict" 
    "<?xml version=\"1.0\" encoding=\"EUC-JP\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja\" lang=\"ja\">")
   ("XHTML 1.0 Translational" 
    "<?xml version=\"1.0\" encoding=\"EUC-JP\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Translational//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja\" lang=\"ja\">")
   ("XHTML 1.0 Frameset" 
    "<?xml version=\"1.0\" encoding=\"EUC-JP\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ja\" lang=\"ja\">")
))
	

apacheのssl機能が使用出来るならば、最初のxml宣言の部分は下記のようにして、IEの場合xml宣言を出力しないようにしておいたほうがよいかもしれない。

長いので複数行に書いていますが、1行に書かないとブラウザでパースエラーになる場合もあります。
<!--#if expr="$HTTP_USER_AGENT != /compatible; MSIE/" -->
<?xml version="1.0" encoding="EUC-JP"?><!-- -*- xml -*- -->
<!--#endif -->	

雛形の自動挿入

html-helper-modeでは新規作成の場合、雛形を自動挿入してくれましたが、 PSGMLではそのような機能は無いようなので、autoinsertを使い自動挿入します。挿入するテンプレートはauto-insert-alistに定義しています。また、xhtml-return-created-string関数定義して、作成時の時間を挿入しています。

これらにより、新規作成した場合は、下記の様な雛形が挿入されます。

自動挿入された雛形
<?xml version="1.0" encoding="EUC-JP"?><!-- -*- xml -*- -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
  <head>
    <meta http-equiv="Content-type" content='text/html; charset="EUC-JP"' />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <title></title>
  </head>

  <body>
    <h1></h1>



    <hr/>
    <address>
    <!-- Created: Wed Jun 11 15:40:48 JST 2008 -->
       Last modified: 
    </address>

  </body>
</html>
	

更新時間の挿入

コンテンツを更新した場合、下記のようにLast modified:に更新時間を挿入するようにtime-stampの設定を行ないます。

    <address>
    <!-- Created: Wed Jun 11 15:40:48 JST 2008 -->
       Last modified: Wed Jun 11 15:59:35 2008 JST
    </address>	

cdtdの再コンパイル

編集中のxhtmlがどのDTDを使用しているかM-x sgml-general-dtd-infoとして表示させてみると、下記のようになっている。

            Doctype: html
       Compiled DTD: /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/cdtd/xhtml1-strict
      Element types: 77
           Entities: 253
 Parameter entities: 56
         Files used: /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/xhtml1-strict.dtd
                     /usr/local/users/nk/cvs/xprm/xemacs-packages/psgml-dtds/etc/xhtml-special.ent
                     /usr/local/users/nk/cvs/xprm/xemacs-packages/psgml-dtds/etc/xhtml-symbol.ent
                     /usr/local/users/nk/cvs/xprm/xemacs-packages/psgml-dtds/etc/xhtml-lat1.ent
                     /usr/local/users/nk/cvs/xprm/xemacs-packages/psgml-dtds/etc/xhtml1-strict.dtd	

/usr/local/users/nk/cvs/xprm/xemacs-packages/psgml-dtds/etc/って何処だよ! な感じで気持悪いので修正してみる。

PSGMLでは、DTDをコンパイルしたcdtdファイルと言う物を使うらしく、/usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/cdtdに置いてある。これを全部消して、再コンパイルすれば良い様なので試してみる。

# /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/cdtd/
# rm *
# /usr/local/bin/xemacs -batch -q -l psgml-parse.elc -l psgml-dtd.elc  -f sgml-compile-all-dtds
#
      

再度emacsを立ち上げてxhtmlファイルを開き、M-x sgml-general-dtd-infoをすると、パスが正常な場所を指しています。

            Doctype: html
       Compiled DTD: /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/cdtd/xhtml1-strict
      Element types: 77
           Entities: 253
 Parameter entities: 56
         Files used: /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/xhtml1-strict.dtd
                     /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/xhtml-special.ent
                     /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/xhtml-symbol.ent
                     /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/xhtml-lat1.ent
                     /usr/local/lib/xemacs/xemacs-packages/etc/psgml-dtds/xhtml1-strict.dtd
Last modified: Wed Jun 11 15:58:04 2008 JST