yatex
changeset 138:b7b54906ac3b dev
add newpage.rb
author | yuuji@gentei.org |
---|---|
date | Wed, 07 Jul 2010 22:27:25 +0900 |
parents | 7919fbbb33c0 |
children | e9c1e80f232e |
files | newpage.rb yahtml.el yatexlib.el |
diffstat | 3 files changed, 165 insertions(+), 5 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/newpage.rb Wed Jul 07 22:27:25 2010 +0900 1.3 @@ -0,0 +1,124 @@ 1.4 +#!/usr/bin/env ruby 1.5 +# THIS is very very tentative. Insufficient examination of function. 1.6 +# Create new HTML file referring other HTML file in the same directory. 1.7 +# Example: 1.8 +# newpage.rb Create new index.html by copying template. 1.9 +# newpage.rb foo.html Create new foo.html whose by copying header 1.10 +# and footer from index.html. 1.11 +# newpage.rb d/sub.html Create new directory d (if necessary) and 1.12 +# d/sub.html by copying header/footer from 1.13 +# index.html in a same directory or parent 1.14 +# directory rewriting href to css file 1.15 +# considering relative path. 1.16 +# newpage.rb -o [file] Forcibly overwrite existing file. 1.17 +# newpage.rb -c cssfile Set `cssfile' as defualt css. 1.18 +# newpage.rb -t template Set `template' as HTML template. 1.19 +require 'fileutils' 1.20 + 1.21 +mydir=File.dirname($0) 1.22 +myname=File.basename($0, ".rb") 1.23 + 1.24 + 1.25 +index = 'index.html' 1.26 +cssdefault = nil 1.27 +overwrite = nil 1.28 +template = __FILE__ #File.expand_path(myname+".html", mydir) 1.29 + 1.30 +def guesscss(dir) 1.31 + 1.32 +end 1.33 + 1.34 +while ARGV[0] && /^-/ =~ (a0=ARGV[0].dup) && ARGV.shift 1.35 + break if /^--$/ =~ a0 1.36 + while /^-[A-Za-z]/ =~ a0 1.37 + case a0 1.38 + when "-c" # css 1.39 + ARGV.shift; cssdefault = ARGV[0] 1.40 + when "-t" # template 1.41 + ARGV.shift; cssdefault = ARGV[0] 1.42 + when "-o" # overwrite 1.43 + overwrite = true 1.44 + end 1.45 + a0.sub!(/-.(.*)/, '-\\1') 1.46 + end 1.47 +end 1.48 + 1.49 +outfile = ARGV[0]||index 1.50 +if !overwrite && test(?s, outfile) then 1.51 + STDERR.printf("File \`%s' exists. Use -o option to overwrite.\n", outfile) 1.52 + exit 1 1.53 +end 1.54 + 1.55 +# set css default file 1.56 +dots = 0 1.57 +of = outfile 1.58 +dots+=1 while "." != (of=File.dirname(of)) 1.59 +cssdir = "../"*dots 1.60 + 1.61 +# set copy source 1.62 +outdir = File.dirname(outfile) 1.63 +if "index.html" == File.basename(outfile) 1.64 + src = (dots == 0 ? template : "index.html") 1.65 +elsif test(?s, outdir+"/index.html") 1.66 + src = outdir+"/index.html" 1.67 +else 1.68 + src = template 1.69 +end 1.70 + 1.71 +FileUtils.mkdir_p(outdir) 1.72 + 1.73 +cssfile = cssdir+"main.css" 1.74 +name = File.basename(outfile, ".html") 1.75 +begin 1.76 + open(outfile, "w") do |out| 1.77 + #IO.foreach(src) do |line| 1.78 + if src == __FILE__ 1.79 + input = DATA 1.80 + else 1.81 + input = open(src, "r") 1.82 + end 1.83 + begin 1.84 + html = input.readlines.join 1.85 + html.sub!(%r|^<h1.*<\/h1>|i, sprintf("<h1>%s</h1>\n", name)) 1.86 + if !html.gsub!("__CSSFILE__", cssfile) 1.87 + html.gsub!(/href=(['\"])(.*\.css)\1/, 'href="\1"') 1.88 + end 1.89 + html.gsub!("__TITLE__", name) 1.90 + out.print html 1.91 + ensure 1.92 + input.close 1.93 + end 1.94 + end 1.95 + printf(<<_EOS_, outfile, name) 1.96 +<a href="%s">%s</a> 1.97 +_EOS_ 1.98 +rescue 1.99 + p $! 1.100 + STDERR.printf(<<'_EOS_', outfile, outfile) 1.101 +Cannot output to [%s]. Do 1.102 + chmod +w %s 1.103 +or 1.104 + chmod +w . 1.105 +or change output directory. 1.106 +_EOS_ 1.107 + exit 1 1.108 +end 1.109 + 1.110 +__END__ 1.111 +<html> 1.112 +<head> 1.113 +<title>__TITLE__</title> 1.114 +<style type="text/css"> 1.115 +<!-- 1.116 +/* Local CSS here */ 1.117 +--> 1.118 +</style> 1.119 +<link rel="stylesheet" type="text/css" href="__CSSFILE__"> 1.120 +</head> 1.121 + 1.122 +<body> 1.123 +<h1>__TITLE__</h1> 1.124 + 1.125 +<!--#include virtual="/~yuuji/signature.html"--> 1.126 +</body> 1.127 +</html>
2.1 --- a/yahtml.el Thu Jun 24 16:01:45 2010 +0900 2.2 +++ b/yahtml.el Wed Jul 07 22:27:25 2010 +0900 2.3 @@ -1,6 +1,6 @@ 2.4 ;;; -*- Emacs-Lisp -*- 2.5 ;;; (c) 1994-2010 by HIROSE Yuuji [yuuji(@)yatex.org] 2.6 -;;; Last modified Thu Jun 24 16:00:42 2010 on firestorm 2.7 +;;; Last modified Wed Jul 7 22:12:55 2010 on firestorm 2.8 ;;; $Id$ 2.9 2.10 (defconst yahtml-revision-number "1.74.2" 2.11 @@ -880,6 +880,25 @@ 2.12 "class(or class list delimited by \\[quoted-insert] SPC): ")) 2.13 nil YaTeX-minibuffer-completion-map nil))) 2.14 2.15 +(defvar yahtml-newpage-command "newpage.rb" 2.16 + "*Command name to create new HTML file referring to index.html. 2.17 +This command should create new HTML file named argument 1 and 2.18 +output string like `<a href=\"newfile.html\">anchor tag</a>'. 2.19 +This program should take -o option to overwrite existing HTML file.") 2.20 +(defun yahtml-newpage (file ov) 2.21 + "Create newpage via newpage script" 2.22 + (interactive 2.23 + (list 2.24 + (let (insert-default-directory) 2.25 + (read-file-name "New webpage file name: " "")) 2.26 + current-prefix-arg)) 2.27 + (if (and (file-exists-p file) (not ov)) 2.28 + (error "%s already exists. Call this with universal argument to force overwrite." file)) 2.29 + (insert (substring 2.30 + (YaTeX-command-to-string 2.31 + (concat yahtml-newpage-command " " (if ov "-o ") file)) 2.32 + 0 -1))) 2.33 + 2.34 ;;; ---------- Add-in ---------- 2.35 (defun yahtml-addin (form) 2.36 "Check add-in function's existence and call it if exists." 2.37 @@ -2463,9 +2482,9 @@ 2.38 ;;; ---------- Lint and Browsing ---------- 2.39 ;;; 2.40 (defun yahtml-browse-menu () 2.41 - "Browsing menu" 2.42 + "Browsing or other external process invokation menu." 2.43 (interactive) 2.44 - (message "J)weblint p)Browse R)eload...") 2.45 + (message "J)weblint p)Browse R)eload N)ewpage...") 2.46 (let ((c (char-to-string (read-char)))) 2.47 (cond 2.48 ((string-match "j" c) 2.49 @@ -2473,7 +2492,9 @@ 2.50 ((string-match "[bp]" c) 2.51 (yahtml-browse-current-file)) 2.52 ((string-match "r" c) 2.53 - (yahtml-browse-reload))))) 2.54 + (yahtml-browse-reload)) 2.55 + ((string-match "n" c) 2.56 + (call-interactively 'yahtml-newpage))))) 2.57 2.58 (if (fboundp 'wrap-function-to-control-ime) 2.59 (wrap-function-to-control-ime 'yahtml-browse-menu t nil))
3.1 --- a/yatexlib.el Thu Jun 24 16:01:45 2010 +0900 3.2 +++ b/yatexlib.el Wed Jul 07 22:27:25 2010 +0900 3.3 @@ -2,7 +2,7 @@ 3.4 ;;; YaTeX and yahtml common libraries, general functions and definitions 3.5 ;;; yatexlib.el 3.6 ;;; (c)1994-2009 by HIROSE Yuuji.[yuuji@yatex.org] 3.7 -;;; Last modified Thu May 27 15:09:44 2010 on firestorm 3.8 +;;; Last modified Wed Jul 7 06:48:48 2010 on firestorm 3.9 ;;; $Id$ 3.10 3.11 ;; General variables 3.12 @@ -822,6 +822,21 @@ 3.13 (win-switch-to-window 1 (- last-command-char win:base-key))) 3.14 3.15 ;;;###autoload 3.16 +(defun YaTeX-command-to-string (cmd) 3.17 + (if (fboundp 'shell-command-to-string) 3.18 + (funcall 'shell-command-to-string cmd) 3.19 + (let ((tbuf " *tmpout*")) 3.20 + (if (get-buffer-create tbuf) (kill-buffer tbuf)) 3.21 + (let ((standard-output (get-buffer-create tbuf))) 3.22 + (unwind-protect 3.23 + (save-excursion 3.24 + (call-process 3.25 + shell-file-name nil tbuf nil YaTeX-shell-command-option cmd) 3.26 + (set-buffer tbuf) 3.27 + (buffer-string)) 3.28 + (kill-buffer tbuf)))))) 3.29 + 3.30 +;;;###autoload 3.31 (defun YaTeX-reindent (col) 3.32 "Remove current indentation and reindento to COL column." 3.33 (save-excursion