Index: tdiary.rb =================================================================== --- tdiary.rb (Revision 29308) +++ tdiary.rb (Revision 29346) @@ -24,6 +24,7 @@ require 'erb' end require 'compatible' +require 'digest/md5' =begin == String class @@ -583,6 +584,14 @@ end end + def self.calc_section_id(section, section_index) + "#{section_index.to_i}:" + Digest::MD5.hexdigest( section.to_src ) + # section_id.to_i will return the section index. + end + def calc_section_id(section, section_index) + self.class.calc_section_id(section, section_index) + end + private # loading tdiary.conf in current directory def load @@ -1444,6 +1453,39 @@ end DIRTY_NONE end + + # Seek the section to edit. + # If fail, query param(section_id or section) is simply ignored. + @section = @section_id = nil + if @cgi.valid? 'section_id' + section_id = @cgi.params['section_id'][0].force_encoding("US-ASCII") + sidx = 0 + @diary.each_section do |section| sidx += 1 + sid = conf.calc_section_id( section, sidx ) + if sid == section_id + @section = section + @section_id = sid + break + end + end + end + unless @section + section_index = if @cgi.valid? 'section_id' and @cgi.params['old'][0] == @date.strftime('%Y%m%d') + @cgi.params['section_id'][0].force_encoding("US-ASCII").to_i + elsif @cgi.valid? 'section' + @cgi.params['section'][0].to_i + else + nil + end + sidx = 0 + @diary.each_section do |section| sidx += 1 + if sidx == section_index + @section = section + @section_id = conf.calc_section_id( section, sidx ) + break + end + end + end end end @@ -1468,6 +1510,27 @@ @diary.show( ! @hide ) DIRTY_NONE end + + # Seek the section to update. + # If fail, show error message (see skel/preview.rhtml). + @section = @section_id = nil + if @cgi.valid? 'section_id' and @old_date == @date.strftime( '%Y%m%d' ) + section_id = @cgi.params['section_id'][0].force_encoding("US-ASCII") + diary = nil + @io.transaction( Time.local( @old_date[0,4].to_i, @old_date[4,2].to_i, @old_date[6,2].to_i ) ) do |diaries| + diary = diaries[@old_date] + DIRTY_NONE + end + sidx = 0 + diary.each_section do |section| sidx += 1 + sid = conf.calc_section_id( section, sidx ) + if sid == section_id + @section = section + @section_id = sid + break + end + end + end end def eval_rhtml( prefix = '' ) @@ -1492,6 +1555,7 @@ @title = conf.to_native( cgi.params['title'][0] ) @body = conf.to_native( cgi.params['body'][0] ) @hide = cgi.params['hide'][0] == 'true' ? true : false + @author = conf.multi_user ? cgi.remote_user : nil super end @@ -1516,7 +1580,6 @@ rescue TDiaryError @date = newdate end - @author = @conf.multi_user ? @cgi.remote_user : nil @io.transaction( @date ) do |diaries| @diaries = diaries @@ -1542,14 +1605,43 @@ def initialize( cgi, rhtm, conf ) super old_date = @cgi.params['old'][0] + section_id = @cgi.valid?('section_id') ? @cgi.params['section_id'][0] : nil @io.transaction( @date ) do |diaries| @diaries = diaries @diary = self[@date] if @diary then if @date.strftime( '%Y%m%d' ) != old_date then - @diary.append( @body, @append ) + @diary.append( @body, @author ) @diary.set_title( @title ) if @title.length > 0 + elsif section_id + section_to_update = nil + sections = [] + sidx = 0 + @diary.each_section do |section| sidx += 1 + sid = conf.calc_section_id( section, sidx ) + if sid == section_id + section_to_update = section + sections.push [@body, @author] + else + sections.push [section.to_src, section.author] + end + end + if section_to_update + @diary.replace( @date, @title, '' ) + sections.each do |body, author| + @diary.append( body, author ) + # No Good if the style of diary needs something + # like a section-separator and which does not + # appear in the source of each section. + end + else + raise <<-"ERRMSG".gsub(/^\t{8}/, '').chomp! + The section to update was not found. + It might be updated after you had started to edit. + Go back and confirm the latest content, then retry. + ERRMSG + end else @diary.replace( @date, @title, @body ) end Index: skel/preview.rhtml =================================================================== --- skel/preview.rhtml (Revision 29308) +++ skel/preview.rhtml (Revision 29346) @@ -66,8 +66,19 @@