# Copyright (c) 2003 URABE Shyouhei <root@mput.dip.jp>
#
# Permission is hereby granted, free of  charge, to any person obtaining a copy
# of  this code, to  deal in  the code  without restriction,  including without
# limitation  the rights  to  use, copy,  modify,  merge, publish,  distribute,
# sublicense, and/or sell copies of the code, and to permit persons to whom the
# code is furnished to do so, subject to the following conditions:
#
#     The above copyright notice and this permission notice shall be
#     included in all copies or substantial portions of the code.
#
# THE  CODE IS  PROVIDED "AS  IS",  WITHOUT WARRANTY  OF ANY  KIND, EXPRESS  OR
# IMPLIED,  INCLUDING BUT  NOT LIMITED  TO THE  WARRANTIES  OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE  AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS  OR COPYRIGHT  HOLDERS  BE LIABLE  FOR  ANY CLAIM,  DAMAGES OR  OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF  OR IN CONNECTION WITH  THE CODE OR THE  USE OR OTHER  DEALINGS IN THE
# CODE.

# title_tag2.rb to enhance <title> element of generating HTML
# $Id: title_tag.rb,v 1.3 2004/05/16 15:27:19 kazuhiko Exp $
# To use, just put this file into plugin folder.

###############################################################################
#
# title_tag_14.rb
#
# The title_tag_14.rb is a plugin for tDiary 1.4.*.
# This source code is based on misc/plugin/title_tag.rb in tDiary 2.0.0.
#
# Outline:
#   A day title and section title is displayed on the <title> element.
#   The action at the times other than a day display is as before.
#   At the time of a day display, a day title and all subtitle displays
#   on the <title> element.
#
# Usage:
#   To use, just put this file (title_tag_14.rb) into plugin folder.
#
# ChangeLog:
#
#   2004-11-27 NI-Lab. <nilab@hamal.freemail.ne.jp>
#     * Fix bug: Checking a paragraph.subtitle isn't nil.
#     * Checking a diary.title isn't nil.
#
#   2004-11-22 NI-Lab. <nilab@hamal.freemail.ne.jp>
#     * The method remove_html_tag moves to new created module Title_Tag_14.
#
#   2004-11-21 NI-Lab. <nilab@hamal.freemail.ne.jp>
#     * The first version of title_tag_14.rb
#
# Modifier: NI-Lab.
# Copyright(c) 2004 NI-Lab. <nilab@hamal.freemail.ne.jp>
# Access: http://www.nilab.info/ or http://nilab.hp.infoseek.co.jp/
#
###############################################################################

alias :title_tag2 :title_tag

module Title_Tag_14
	def remove_html_tag(str)
		r = str.dup # copying string object
		# This part is taken from TDiary.Plugin.apply_plugin of tDiary 2.0.0
		r.gsub!( /<[^"'<>]*(?:"[^"]*"[^"'<>]*|'[^']*'[^"'<>]*)*(?:>|(?=<)|$)/, '' )
		return r
	end
	module_function :remove_html_tag
end

def title_tag
	if @mode == 'day' and diary = @diaries[@date.strftime('%Y%m%d')] then
		title = ''
		title << @html_title
		if @date != nil then
			title << "(#{@date.strftime( '%Y-%m-%d' )})"
		end
		if diary.title != nil and !diary.title.empty? then
			title << ' - '
			title << Title_Tag_14.remove_html_tag(diary.title)
		end
		diary.each_paragraph do |paragraph|
			if paragraph.subtitle != nil and !paragraph.subtitle.empty? then
				title << ' - '
				title << Title_Tag_14.remove_html_tag(paragraph.subtitle)
			end
		end
		return "<title>#{CGI::escapeHTML(title)}</title>"
	else
		return title_tag2
	end
end


# Local Variables:
# mode: ruby
# code: euc-jp-unix
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# fill-column: 79
# default-justification: full
# End:
# vi: ts=3 sw=3
