OSDN Git Service

modieifed setting path only.
[feedblog/feedgenerator.git] / feedgenerator.rb
index 40924b2..de64c8b 100644 (file)
@@ -22,9 +22,13 @@ LOGINID = "login"
 # ログインパスワード
 PASSWORD = "password"
 # インターフェースのテーブルの幅
-TABLEWIDTH = 800
+TABLEWIDTH = 1000
 # XMLファイル格納先までの相対パス
 XMLPATH = "./../lunardial/xml/"
+# FeedBlogを設置したディレクトリのURL
+HOMEBASE = "https://lunardial.sakura.ne.jp/"
+# 入力されたフルパスURL(HOMEBASE)を置換する文字列
+RELAYPATH = "./"
 # loglist.xmlファイルの定義
 LISTXMLPATH = "#{XMLPATH}loglist.xml"
 # FeedBlog上の表示ページからログ格納ディレクトリまでのパス
@@ -37,6 +41,13 @@ USEFILEMANAGER = true
 FILEMANAGER = "./filemanager.rb"
 # XMLに書き込む際、改行部分を<br>のまま保持するか、改行記号に直すか
 REPLACEBRTAG = false
+# ファイルの書き込み時にENTRYのIDおよびURLを、FEEDオブジェクトから自動生成した値に置換するか否か
+REPLACEENTRYIDANDURL = false
+
+# EXPERIMENTAL AREA START
+TO_HTML_ENTRYTEMPLATE = 'htmltemp/diary.html.erb'
+TO_HTML_MAINTEMPLATE = 'htmltemp/main.html.erb'
+# EXPERIMENTAL AREA END
 
 # バージョン情報を示す文字列です
 APPVERSION = "- FeedGenerator for Ruby version 2.0.0.0 -<br>Copyright(c) 2009 Kureha.H (<a href=\"http://lunardial.sakura.ne.jp/\" target=\"_blank\">http://lunardial.sakura.ne.jp/</a>) & Yui Naruse (<a href=\"http://airemix.com/\" target=\"_blank\">http://airemix.com/</a>)"
@@ -56,9 +67,16 @@ class Object
   # _arg[1]_ :: モードの指定。例 : w:utf-8(書き込みモード・UTF-8エンコードでファイルを開く)
   def myopen(*arg)
     mode = arg[1]
-    arg[1] = mode[/[^:]+/] if mode && RUBY_VERSION < "1.8.7" && mode.include?(':')
+    rdonly_p = true
+    case mode
+      when String
+      arg[1] = mode[/[^:]+/] if RUBY_VERSION < "1.8.7" && mode.include?(':')
+      rdonly_p = /\A[^:]*[wa+]/ !~ mode
+      when Numeric
+      rdonly_p = !(mode & (IO::WRONY | IO::RDWR))
+    end
     open(*arg) do |f|
-      f.flock(/\A\w+r/ =~ mode ? File::LOCK_SH : File::LOCK_EX)
+      f.flock(rdonly_p ? File::LOCK_SH : File::LOCK_EX)
       return yield(f)
     end
   end
@@ -254,7 +272,8 @@ class Feed < AbstractEntry
   def to_s
     buf = []
     
-    buf.push("<feed #{@attr[:feedattr]}>")
+    # buf.push("<feed #{@attr[:feedattr]}>")
+    buf.push("<feed xml:lang='ja-jp' xmlns='http://www.w3.org/2005/Atom'>");
     buf.push("<title type=\"text\">#{@attr[:title]}</title>")
     buf.push("<subtitle type=\"text\">#{@attr[:subtitle]}</subtitle>")
     buf.push("<link rel=\"self\" type=\"application/atom+xml\" href=\"#{@attr[:self]}\" />")
@@ -283,7 +302,31 @@ class Feed < AbstractEntry
   #
   # _feed_ :: Feedオブジェクト
   # _entry_ :: Entryオブジェクトの配列
-  def self.to_xml(path, feed, entrylist)
+  def self.to_xml(path, feed, entrylist_tmp)
+    buf = []
+    entrylist = entrylist_tmp.dup
+    buf.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
+    buf.push("#{feed.to_s}\n")
+    entrylist.each { |entry|
+      if REPLACEENTRYIDANDURL
+        entry.entryid.gsub!(/^[^\?]*\?/, "")
+        entry.entryid = feed.url + "?" + entry.entryid
+        entry.url = feed.url + "#" + entry.entryid
+      end
+      buf.push("#{entry.to_s}\n")
+    }
+    buf.push("</feed>")
+    
+    myopen(path, "w") do |f|
+      f.print buf.join("\n")
+    end
+  end
+  
+  # XMLファイル出力用メソッドです
+  #
+  # _feed_ :: Feedオブジェクト
+  # _entry_ :: Entryオブジェクトの配列
+  def self.to_xml_plain(path, feed, entrylist)
     buf = []
     buf.push("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
     buf.push("#{feed.to_s}\n")
@@ -319,7 +362,7 @@ class Entry < AbstractEntry
     # 可視・不可視を示すハッシュキーを格納する配列です
     @display = {"entryid" => "none", "title" => "",
       "summary" => "", "published" => "none",
-      "updated" => "none", "url" => "none",
+      "updated" => "none", "url" => "",
       "content" => "", "others"=>"none"}
     
     # デバッグモードの場合、全ての入力要素を表示します
@@ -374,6 +417,45 @@ class Entry < AbstractEntry
     return entrylist
   end
   
+  # Atom XMLファイルを読み込んで解析し、テンプレートファイルにしたがってHTMLに変換するメソッドです
+  def self.to_html(xmlpath, destpath, entry_temppath, html_temppath)
+    # 引数チェック - 全必須
+    if xmlpath.empty? or destpath.empty? or entry_temppath.empty? or html_temppath.empty?
+      raise ArgumentError
+    end
+    
+    # 必須ファイル存在チェック
+    unless File.exist?(xmlpath) and File.exist?(entry_temppath) and File.exist?(html_temppath)
+      raise IOError
+    end
+    
+    # XML読み込み
+    entrylist = Entry.readxml(xmlpath)
+    
+    body = ''
+    entrylist.each { |e|
+      # Entry毎のHTML表示部分を生成
+      body << e.to_template(entry_temppath)
+    }
+    
+    # HTML全体のテンプレートを生成
+    html_temp = HtmlWriter.new(html_temppath, binding)
+    
+    # HTMLに書き込み
+    myopen(destpath, 'w:utf-8') { |f|
+      f.write(CGI.pretty(html_temp.to_code))
+    }
+  end
+  
+  # Entryをテンプレートに沿って変形するメソッド
+  def to_template(temppath)
+    erb = HtmlWriter.new(temppath, binding)
+    title = CGI.unescapeHTML(@attr[:title])
+    date = @attr[:published]
+    content = CGI.unescapeHTML(@attr[:content])
+    erb.to_code
+  end
+  
   # Entry挿入メソッド
   def self.insert(path, entry)
     feed = Feed.readxml(path)
@@ -443,7 +525,7 @@ class Entry < AbstractEntry
     str.strip!
     str.gsub!(/(&lt;\/(?:p|h\d|div)(?:&gt;|>))\n/i, '\1')
     str.gsub!(/\n/, '&lt;br&gt;') if REPLACEBRTAG
-    str.gsub!(/(&lt;(?:(?!&gt;).)*?)#{Regexp.escape(FEEDXMLDIR)}/) { "#$1#{XMLPATH}" }
+    str.gsub!(/(&lt;(?:(?!&gt;).)*?)#{Regexp.escape(RELAYPATH)}/) { "#$1#{HOMEBASE}" }
     str
   end
   
@@ -453,9 +535,9 @@ class Entry < AbstractEntry
     str = CGI.unescapeHTML(str)
     str.strip!
     str.gsub!(/(\r\n|\n)/, "")
-    str.gsub!(/<br>/i, "\n") if REPLACEBRTAG
-    str.gsub!(/(<br>|<\/p>|<\/h\d>|<\/div>)(?=[^\n])/i) { "#$1\n" } unless REPLACEBRTAG
-    str.gsub!(/(<[^>]*?)#{Regexp.escape(XMLPATH)}/) { "#$1#{FEEDXMLDIR}" }
+    str.gsub!(/<br>|<br[ ]*\/>/i, "\n") if REPLACEBRTAG
+    str.gsub!(/(<br>|<br[ ]*\/>|<\/p>|<\/h\d>|<\/div>)(?=[^\n])/i) { "#$1\n" } unless REPLACEBRTAG
+    str.gsub!(/(<[^>]*?)#{Regexp.escape(HOMEBASE)}/) { "#$1#{RELAYPATH}" }
     CGI.escapeHTML(str)
   end
   
@@ -464,8 +546,8 @@ class Entry < AbstractEntry
     str = @attr[:content].dup
     str = CGI.unescapeHTML(str)
     str.strip!
-    str.gsub!(/<br>/i, "\n") if REPLACEBRTAG
-    str.gsub!(/(<[^>]*?)#{Regexp.escape(FEEDXMLDIR)}/) { "#$1#{XMLPATH}" }
+    str.gsub!(/<br>|<br[ ]*\/>/i, "\n") if REPLACEBRTAG
+    str.gsub!(/(<[^>]*?)#{Regexp.escape(RELAYPATH)}/) { "#$1#{HOMEBASE}" }
     str
   end
   
@@ -635,20 +717,24 @@ class Controller
         case params["action"]
           # 確認画面
           when "confirm"
+          session["target_filepath"] = params["target_filepath"]
           db["newentry"] = Entry.new(params)
           db["newentry"].content = db["newentry"].content_for_blog
           # 記事の追記を実際にファイルに反映
           when "exec"
-          successed = Entry.insert(XMLPATH + File.basename(session["filepath"]), Entry.new(params))
+          session["target_filepath"] = params["target_filepath"]
+          successed = Entry.insert(XMLPATH + File.basename(params["target_filepath"]), Entry.new(params))
           unless successed
             db["error"] = "日記の新規追加に失敗しました。" 
             params["mode"] = "error"
           end
           # 画面を戻った際の処理
           when "back"
+          session["target_filepath"] = params["target_filepath"]
           db["newentry"] = Entry.new(params)
         else 
           # New Diary - Default
+          session["target_filepath"] = session["filepath"]
           db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
           db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
         end
@@ -658,24 +744,29 @@ class Controller
         case params["action"]
           # 編集画面
           when "edit"
+          session["target_filepath"] = params["target_filepath"]
           session["editid"] = cgi["editid"].to_s
-          db["editentry"] = Entry.select(XMLPATH + File.basename(session["filepath"]), session["editid"])
+          db["editentry"] = Entry.select(XMLPATH + File.basename(session["target_filepath"]), session["editid"])
           # 確認画面
           when "confirm"
+          session["target_filepath"] = params["target_filepath"]
           session["editid"] = cgi["editid"].to_s
           db["editentry"] = Entry.new(params)
           db["editentry"].content = db["editentry"].content_for_blog
           # 記事の変更を実際にファイルに反映
           when "exec"
-          successed = Entry.update(XMLPATH + File.basename(session["filepath"]), Entry.new(params))
+          session["target_filepath"] = params["target_filepath"]
+          successed = Entry.update(XMLPATH + File.basename(params["target_filepath"]), Entry.new(params))
           unless successed
             db["error"] = "日記の編集処理に失敗しました。<br>該当の日記が既に存在しない可能性があります。" 
             params["mode"] = "error"
           end
           when "back"
+          session["target_filepath"] = params["target_filepath"]
           db["editentry"] = Entry.new(params)
         else
           # Edit Diary - Default
+          session["target_filepath"] = session["filepath"]
           db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
           db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
         end
@@ -685,17 +776,24 @@ class Controller
         case params["action"]
           # 確認画面
           when "confirm"
+          session["target_filepath"] = params["target_filepath"]
           session["delid"] = cgi["delid"].to_s
-          db["delentry"] = Entry.select(XMLPATH + File.basename(session["filepath"]), session["delid"])
+          db["delentry"] = Entry.select(XMLPATH + File.basename(session["target_filepath"]), session["delid"])
           # 記事の削除を実際にファイルに反映
           when "exec"
-          successed = Entry.delete(XMLPATH + File.basename(session["filepath"]), cgi["delid"].to_s)
+          session["target_filepath"] = params["target_filepath"]
+          successed = Entry.delete(XMLPATH + File.basename(params["target_filepath"]), cgi["delid"].to_s)
           unless successed
             db["error"] = "日記の編集処理に失敗しました。<br>該当の日記が既に存在しない可能性があります。" 
             params["mode"] = "error"
           end
+          when "back"
+          session["target_filepath"] = params["target_filepath"]
+          db["feed"] = Feed.readxml(XMLPATH + File.basename(session["target_filepath"]))
+          db["entry"] = Entry.readxml(XMLPATH + File.basename(session["target_filepath"]))
         else
           # Delete Diary - Default
+          session["target_filepath"] = session["filepath"]
           db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
           db["entry"] = Entry.readxml(XMLPATH + File.basename(session["filepath"]))
         end
@@ -705,14 +803,18 @@ class Controller
         case params["action"]
           # 確認画面
           when "confirm"
+          session["target_filepath"] = params["target_filepath"]
           db["feed"] = Feed.new(params)
           # 実際にFeed情報の変更をファイルに反映
           when "back"
+          session["target_filepath"] = params["target_filepath"]
           db["feed"] = Feed.new(params)
           when "exec"
-          Feed.update(XMLPATH + File.basename(session["filepath"]), Feed.new(params))
+          session["target_filepath"] = params["target_filepath"]
+          Feed.update(XMLPATH + File.basename(params["target_filepath"]), Feed.new(params))
         else
-          
+          session["target_filepath"] = session["filepath"]
+          db["feed"] = Feed.readxml(XMLPATH + File.basename(session["filepath"]))
         end
         
         # ログ編集モード
@@ -940,10 +1042,8 @@ class Controller
   # マルチパートフォームの場合の処理です
   def Controller.MultiForm(cgi, session, params, db)
     db.transaction do
-      
       # loglist.xmlをロードします
       db["loglist"] = LogList.readxml(LISTXMLPATH)
-      
       case params["mode"]
         # 特定位置に挿入する場合の処理
         when "insert"
@@ -1221,10 +1321,15 @@ def main
     end
     
     # メニューとして表示されるHTML文字列です
+    if params["target_filepath"].blank?
+      menu = "<div class=\"divstyle\" style=\"width: #{TABLEWIDTH}px;\"><div class=\"divstyle\" align=\"center\" style=\"margin-bottom: 5px;\">現在編集中のファイル : #{session["filepath"]} "
+    else
+      menu = "<div class=\"divstyle\" style=\"width: #{TABLEWIDTH}px;\"><div class=\"divstyle\" align=\"center\" style=\"margin-bottom: 5px;\">現在編集中のファイル : #{session["target_filepath"]} "
+    end
     if USEFILEMANAGER == true
-      menu = "<div class=\"divstyle\" style=\"width: #{TABLEWIDTH}px;\"><div class=\"divstyle\" align=\"center\" style=\"margin-bottom: 5px;\">現在編集中のファイル : #{session["filepath"]} [ <a href=\"#{cgi.script_name}?mode=selectlog\">他のファイルを選択</a> ]</div>[ <a href=\"#{cgi.script_name}\">トップページ</a> | 記事管理 ( <a href=\"#{cgi.script_name}?mode=newentry\">作成</a> | <a href=\"#{cgi.script_name}?mode=editentry\">編集</a> | <a href=\"#{cgi.script_name}?mode=delentry\">消去</a> )  | <a href=\"#{cgi.script_name}?mode=editfeed\">XML情報編集</a> | <a href=\"#{cgi.script_name}?mode=log\">ログ管理</a> | <a href=\"#{cgi.script_name}?mode=import\">インポート</a> | <a href=\"#{FILEMANAGER}\" target=\"_blank\">ファイル管理</a> | <a href=\"#{cgi.script_name}?mode=reset\">初期化</a> | <a href=\"#{cgi.script_name}?mode=logout\">ログアウト</a> ]</div>"
+      menu += "[ <a href=\"#{cgi.script_name}?mode=selectlog\">他のファイルを選択</a> ]</div>[ <a href=\"#{cgi.script_name}\">トップページ</a> | 記事管理 ( <a href=\"#{cgi.script_name}?mode=newentry\">作成</a> | <a href=\"#{cgi.script_name}?mode=editentry\">編集</a> | <a href=\"#{cgi.script_name}?mode=delentry\">消去</a> )  | <a href=\"#{cgi.script_name}?mode=editfeed\">XML情報編集</a> | <a href=\"#{cgi.script_name}?mode=log\">ログ管理</a> | <a href=\"#{cgi.script_name}?mode=import\">インポート</a> | <a href=\"#{FILEMANAGER}\" target=\"_blank\">ファイル管理</a> | <a href=\"#{cgi.script_name}?mode=reset\">初期化</a> | <a href=\"#{cgi.script_name}?mode=logout\">ログアウト</a> ]</div>"
     else
-      menu = "<div class=\"divstyle\" style=\"width: #{TABLEWIDTH}px;\"><div class=\"divstyle\" align=\"center\" style=\"margin-bottom: 5px;\">現在編集中のファイル : #{session["filepath"]} [ <a href=\"#{cgi.script_name}?mode=selectlog\">他のファイルを選択</a> ]</div>[ <a href=\"#{cgi.script_name}\">トップページ</a> | 記事管理 ( <a href=\"#{cgi.script_name}?mode=newentry\">作成</a> | <a href=\"#{cgi.script_name}?mode=editentry\">編集</a> | <a href=\"#{cgi.script_name}?mode=delentry\">消去</a> )  | <a href=\"#{cgi.script_name}?mode=editfeed\">XML情報編集</a> | <a href=\"#{cgi.script_name}?mode=log\">ログ管理</a> | <a href=\"#{cgi.script_name}?mode=import\">インポート</a> | <a href=\"#{cgi.script_name}?mode=reset\">初期化</a> | <a href=\"#{cgi.script_name}?mode=logout\">ログアウト</a> ]</div>"
+      menu += "[ <a href=\"#{cgi.script_name}?mode=selectlog\">他のファイルを選択</a> ]</div>[ <a href=\"#{cgi.script_name}\">トップページ</a> | 記事管理 ( <a href=\"#{cgi.script_name}?mode=newentry\">作成</a> | <a href=\"#{cgi.script_name}?mode=editentry\">編集</a> | <a href=\"#{cgi.script_name}?mode=delentry\">消去</a> )  | <a href=\"#{cgi.script_name}?mode=editfeed\">XML情報編集</a> | <a href=\"#{cgi.script_name}?mode=log\">ログ管理</a> | <a href=\"#{cgi.script_name}?mode=import\">インポート</a> | <a href=\"#{cgi.script_name}?mode=reset\">初期化</a> | <a href=\"#{cgi.script_name}?mode=logout\">ログアウト</a> ]</div>"
     end
     
     # ビュー部分