In article <200002011307.WAA02306@udlew10.uldev.lsi.nec.co.jp>,
Hironori Sakamoto <w3m-dev@mi.med.tohoku.ac.jp> writes:
> $@$J$+$J$+LLGr$$$G$9$M!#(JCGI $@$GNI$/$3$3$^$G!#(J
Geocities $@$N%f!<%6%$%s%?%U%'!<%9$r$A$g$C$H;29M$K$7$F$$$^$9!%(J
$@$G$b!$$d$C$Q$j$A$g$C$HLLE]$+$J$"!%(J
> $@$I$&$;$J$i!"(JSECTION $@$d(J item $@$NJB$SJQ$($,=PMh$k$H$$$$$H(J
> $@;W$&$N$G$9$,$I$&$G$7$g$&$+!#(J
> $@$=$l$+$i!"$I$3$+$K%"%s%+!<$b$7$/$O(J [GOTO URL] $@$_$?$$$J%\%?%s$r(J
> $@@_Dj$9$k$N$O$I$&$G$7$g$&!#(J
$@$d$C$F$_$^$7$?!%(J
# $@$G$bI8=`E:IU$9$k$J$i(J C $@$G=q$-D>$7$+$J$"(J
-- $@;37ABg3X9)3XIt(J $@EE;R>pJs9)3X2J(J $@0KF#(J $@>4B'(J tel&fax: 0238-26-3369 E-mail: aito@eie.yz.yamagata-u.ac.jp# ... make $@$7$?$"$H(J /usr/local/bin/ruby $@$,$J$+$C$?$i!$<+F0E*$K(J # ruby $@$r%@%&%s%m!<%I$7$F2rE`(J; configure; make install $@$9$k$H$+!%(J
---------------------------------------------------------------------- #!/usr/local/bin/ruby require 'cgi-lib'
def quoteForHtml(s) r = s.dup r.gsub!(/&/,'&') r.gsub!(/</,'<') r.gsub!(/>/,'>') r end
class Bookmark BookmarkEntry = Struct.new("BookmarkEntry","title","url") BookmarkHashItem = Struct.new("BookmarkHashItem","index","items") BookmarkItem = Struct.new("BookmarkItem","section","items") attr :filename,true def initialize @bookmarks = [] @section = nil @hash = {} @section_no = 0 @filename = nil end def setSection(section) @section = section if @hash[section].nil? then @section_no = @bookmarks.size s = [] @hash[section] = BookmarkHashItem.new(@section_no,s) @bookmarks[@section_no] = BookmarkItem.new(@section,s) else @section_no = @hash[section].index end end def searchSection(section) if @hash[section].nil? then raise "#{section}: No such section" end @hash[section] end def changeSectionName(old,new) s = searchSection(old) @bookmarks[s.index] = BookmarkItem.new(new,s.items) @hash.delete(old) @hash[new] = s end def deleteItem(section,item_no) s = searchSection(section) s.items.delete_at(item_no) end def changeItem(section,item_no,title,url) s = searchSection(section) s.items[item_no] = BookmarkEntry.new(title,url) end def add(title,url) if @section.nil? then raise "No section specified" end @bookmarks[@section_no].items.push(BookmarkEntry.new(title,url)) end def each_section for i in @bookmarks yield i end end def Bookmark.read_from(file) bmark = Bookmark.new bmark.filename = file f = open(file) while f.gets if /^<h2>(.*)<\/h2>$/ then bmark.setSection($1) elsif /^<li><a href="(.*)">(.*)<\/a>/ then bmark.add($2,$1) end end f.close bmark end
def save f = open(@filename,"w") f.print "<html><head><title>Bookmarks</title></head>\n<body>\n<h1>Bookmarks</h1>\n" each_section do |sec,items| f.print "<h2>#{sec}</h2>\n" f.print "<ul>\n" for i in items f.print "<li><a href=\"#{i.url}\">#{i.title}</a>\n" end f.print "<!--End of section (do not delete this comment)-->\n</ul>\n" end f.print "</body>\n</html>\n" f.close end def print_edit_panel print "Content-Type: text/html\n\n" print "<html><head><title>Bookmark Editor</title></head><body>\n" each_section do |sec,bms| s = quoteForHtml(sec) print "<p><form method=\"GET\" action=\"#{$selfname}\">\n" print "<input type=\"hidden\" name=\"mode\" value=\"section\">\n" print "<input type=\"hidden\" name=\"section\" value=\"#{s}\">\n" print "SECTION: <input type=\"text\" name=\"new_name\" value=\"#{s}\">\n" # print "<input type=\"submit\" name=\"delete_section\" value=\"DELETE\">\n" print "<input type=\"submit\" name=\"change_section\" value=\"CHANGE\">\n" print "<input type=\"submit\" name=\"move_section_pos\" value=\"MOVE THIS SECTION\">\n" print "</form><p>\n" print "<ul>\n" for i in 0..bms.size-1 b = bms[i] s = quoteForHtml(sec) t = quoteForHtml(b.title) u = quoteForHtml(b.url) print "<li><form method=\"GET\" action=\"#{$selfname}\">\n" print "<input type=\"hidden\" name=\"mode\" value=\"item\">\n" print "<input type=\"hidden\" name=\"section\" value=\"#{s}\">\n" print "<input type=\"hidden\" name=\"bookmark_no\" value=\"#{i}\">\n" print "<nobr>TITLE:<input type=\"text\" size=\"40\" name=\"new_title\" value=\"#{t}\"></nobr>\n" print "<nobr>URL:<input type=\"text\" name=\"new_url\" value=\"#{u}\"></nobr>\n" print "<a href=\"#{u}\">[REFER]</a><br>\n" print "<input type=\"submit\" name=\"change_item\" value=\"CHANGE\">\n" print "<input type=\"submit\" name=\"move_item\" value=\"MOVE\">\n" print "<input type=\"submit\" name=\"move_section\" value=\"MOVE TO OTHER SECTION\">\n" print "<input type=\"submit\" name=\"delete_item\" value=\"DELETE\">\n" print "</form></li><p>\n" end print "</ul>\n" end print "</body></html>\n" end def update_page(nback=1) save print "Content-Type: text/plain\n" for i in 1..nback print "W3m-control: BACK\n" end print "W3m-control: RELOAD\n" print "\n" end
def cancel_page print "Content-Type: text/plain\n" print "W3m-control: BACK\n" print "W3m-control: BACK\n" print "\n" end def section_op(cgi) section = cgi['section'] raise "No section specified" if section.nil? if cgi['change_section'] != nil then changeSectionName(section,cgi['new_name']) update_page elsif cgi['move_section_pos'] != nil then move_section_pos_page(section) end end
def move_section_pos(cgi) section = cgi['section'] from_pos = searchSection(section).index to_pos = cgi['section_pos'].to_i move_item_in_array(@bookmarks,from_pos,to_pos) @hash[section].index = to_pos update_page(2) end def move_section_pos_page(section) qsection = quoteForHtml(section) s = searchSection(section) print "Content-Type: text/html\n\n" print "<html><head><title>Move section</title></head><body>\n" print "Move section \n" print "<form method=\"GET\" action=\"#{$selfname}\">\n" print "<input type=\"hidden\" name=\"mode\" value=\"move_section_pos\">\n" print "<input type=\"hidden\" name=\"section\" value=\"#{qsection}\">\n" print "[#{qsection}] before " print "<select name=\"section_pos\">" for i in 0..@bookmarks.size-1 sec = @bookmarks[i].section qsec = quoteForHtml(sec) if i < s.index then print "<option value=\"#{i}\">#{qsec}</option>\n" elsif i > s.index then print "<option value=\"#{i-1}\">#{qsec}</option>\n" end end print "<option value=\"#{@bookmarks.size-1}\">BOTTOM</option>\n" print "</select><br><input type=\"submit\" value=\"OK\">\n" print "</form>\n" print "<form method=\"GET\" action=\"#{$selfname}\">\n" print "<input type=\"hidden\" name=\"mode\" value=\"cancel\">\n" print "<input type=\"submit\" value=\"CANCEL\">\n" print "</form\n" print "</body></html>\n" end
def item_op(cgi) section = cgi['section'] raise "No section specified" if section.nil? item_no = cgi['bookmark_no'].to_i
if cgi['change_item'] != nil then changeItem(section,item_no,cgi['new_title'],cgi['new_url']) update_page elsif cgi['delete_item'] != nil then deleteItem(section,item_no) update_page elsif cgi['move_section'] != nil then move_item_between_section_page(section,item_no) elsif cgi['move_item'] != nil then move_item_within_section_page(section,item_no) end end
def move_section(cgi) old_section = cgi['section'] new_section = cgi['to_section'] item_no = cgi['bookmark_no'].to_i o_sec = searchSection(old_section) n_sec = searchSection(new_section) entry = o_sec.items[item_no] o_sec.items.delete_at(item_no) n_sec.items.push(entry) update_page(2) end
def move_item_in_array(ary,from_pos,to_pos) item = ary[from_pos] if from_pos < to_pos then for i in from_pos..to_pos-1 ary[i] = ary[i+1] end ary[to_pos] = item elsif from_pos > to_pos then from_pos.downto(to_pos+1) do |i| ary[i] = ary[i-1] end ary[to_pos] = item end end
def move_item_within_section(cgi) s = searchSection(cgi['section']) from_pos = cgi['bookmark_no'].to_i to_pos = cgi['to_pos'].to_i move_item_in_array(s.items,from_pos,to_pos) update_page(2) end
def move_item_between_section_page(section,item_no) qsection = quoteForHtml(section) s = searchSection(section) print "Content-Type: text/html\n\n" print "<html><head><title>Move an item between sections</title></head><body>\n" print "<h1>Move the following item:</h1>\n" print "<form method=\"GET\" action=\"#{$selfname}\">\n" print "<input type=\"hidden\" name=\"mode\" value=\"move_section\">\n" print "<input type=\"hidden\" name=\"section\" value=\"#{qsection}\">\n" print "<input type=\"hidden\" name=\"bookmark_no\" value=\"#{item_no}\">\n" print "<table>\n" print "<tr><td>TITLE<td>",quoteForHtml(s.items[item_no].title),"\n" print "<tr>URL<td>",quoteForHtml(s.items[item_no].url),"\n" print "</table><p>\n" print "From section #{qsection} to <select name=\"to_section\">" each_section do |sec,items| qsec = quoteForHtml(sec) print "<option value=\"#{qsec}\">#{qsec}</option>\n" end print "</select><br><input type=\"submit\" value=\"OK\">\n" print "</form>\n" print "<form method=\"GET\" action=\"#{$selfname}\">\n" print "<input type=\"hidden\" name=\"mode\" value=\"cancel\">\n" print "<input type=\"submit\" value=\"CANCEL\">\n" print "</form\n" print "</body></html>\n" end
def move_item_within_section_page(section,item_no) qsection = quoteForHtml(section) s = searchSection(section) print "Content-Type: text/html\n\n" print "<html><head><title>Move an item between sections</title></head><body>\n" print "<h1>Move the following item:</h1>\n" print "<form method=\"GET\" action=\"#{$selfname}\">\n" print "<input type=\"hidden\" name=\"mode\" value=\"move_item\">\n" print "<input type=\"hidden\" name=\"section\" value=\"#{qsection}\">\n" print "<input type=\"hidden\" name=\"bookmark_no\" value=\"#{item_no}\">\n" print "<table>\n" print "<tr><td>TITLE<td>",quoteForHtml(s.items[item_no].title),"\n" print "<tr>URL<td>",quoteForHtml(s.items[item_no].url),"\n" print "</table><p>\n" print "before <select name=\"to_pos\">\n" for i in 0..s.items.size-1 q = quoteForHtml(s.items[i].title) if i < item_no then print "<option value=\"#{i}\">#{q}</option>\n" elsif i > item_no then print "<option value=\"#{i-1}\">#{q}</option>\n" end end print "<option value=\"#{s.items.size-1}\">BOTTOM\n" print "</select><br><input type=\"submit\" value=\"OK\">\n" print "</form>\n" print "<form method=\"GET\" action=\"#{$selfname}\">\n" print "<input type=\"hidden\" name=\"mode\" value=\"cancel\">\n" print "<input type=\"submit\" value=\"CANCEL\">\n" print "</form\n" print "</body></html>\n" end end
################## M A I N
$selfname = "/cgi-bin/"+File.basename($0)
cgi = CGI.new
#p cgi bookmarkfile = cgi["bookmark"] if bookmarkfile.nil? then bookmarkfile = File.expand_path("~/.w3m/bookmark.html") end
b = Bookmark.read_from(bookmarkfile)
case cgi['mode'] when "section" b.section_op(cgi) when "item" b.item_op(cgi) when "move_section" b.move_section(cgi) when "move_item" b.move_item_within_section(cgi) when "move_section_pos" b.move_section_pos(cgi) when "cancel" b.cancel_page else b.print_edit_panel end
This archive was generated by hypermail 2b29 : Wed Jul 19 2000 - 10:30:54 CDT