# -*- coding: utf-8; -*- =begin = emptDiary style((-$Id: emptdiary_style.rb,v 1.11 2008-03-02 09:01:20 kazuhiko Exp $-)) == Summary This style is an extension to the tDiary style which allows plug-in arguments to have empty lines. In short, this style preserves empty lines between <% and %> when splltting the input into sections. The latest version of this file can be downloaded from (()). == Usage if you want to use this style, add the following line into tdiary.conf: @style = 'emptdiary' Please see README.rd or README.rd.en for further explanation. == Acknowledgements This style is realized using TdiarySection and TdiaryDiary as super-classes. I thank the authors of tdiary_style.rb for providing such flexible classes. ZundaDiary::to_html4 and ZundaDiary::to_chtml are copied from tdiary_style.rb and slightly edited as follows: * split_unless_plugin() is inserted before each collect() * Regexp is chanegd from ^ to \A == Copyright Copyright 2003 zunda Permission is granted for use, copying, modification, distribution, and distribution of modified versions of this work under the terms of GPL version 2 or later. =end =begin ChangeLog * Mon Feb 17, 2003 zunda - copied from zunda_style.rb, with which I used for preliminary tests. * Wed Feb 26, 2003 zunda - TDiary::Zunda::String class to handle strings better (Thanks to Mitsuru Shimamura for the error report) * Wed Dec 22, 2004 zunda - override body_to_html =end require 'tdiary/tdiary_style' =begin == Classes and methods defined in this style Please note that not all are documented. =end module TDiary =begin === TDiary::Zunda::ZundaString < String Extended String class not to divide things between <% and %>. --- TDiary::Zunda::ZundaString.split_unless_plugin ( delimiter = "\n\n" ) Returns an array of ZundaString splitted at ((|delimiter|)) which is outside of <% and %> pairs. Specify ((|delimiter|)) as a String showing a fragment of Regexp. This will be combined in a Regexp like: /(#{delimiter)|<%|%>)/. =end class Zunda class ZundaString < String def split_unless_plugin( delimiter = "\n\n+" ) result = Array.new fragment = '' nest = 0 remain = self.gsub(/.*?(#{delimiter}|<%|%>)/m) do fragment += $& case $1 when '<%' nest += 1 when '%>' nest -= 1 else if nest == 0 then fragment.sub!( /#{delimiter}\z/, '' ) result << Zunda::ZundaString.new( fragment ) unless fragment.empty? fragment = '' end end '' end fragment += remain fragment.sub!( /#{delimiter}\z/, '' ) result << Zunda::ZundaString.new( fragment ) unless fragment.empty? result end end end =begin === TDiary::EmptdiartySection < TdiarySection Almost the same as TdiarySection but usess split_unless_plugin instead of split. initialize method is overrideen. =end class ZundaSection < TdiarySection def initialize( fragment, author = nil ) @author = author lines = fragment.split_unless_plugin( "\n+" ) if lines.size > 1 then if /\A<#{p}

" end end html end end =begin === TDiary::ZundaDiary < TdiaryDiary Almost the same as TdiarySection but usess split_unless_plugin instead of split. append method is overriden and makes ZundaSection with body being an ZundaString. Also, to_html4 and to_chtml methods are overridden to split_unless_plugin before collect'ing the body of the sections. =end class ZundaDiary < TdiaryDiary def style 'zunda' end def append( body, author = nil ) Zunda::ZundaString.new(body.gsub( /\r/, '' )).split_unless_plugin( "\n\n+" ).each do |fragment| section = ZundaSection::new( fragment, author ) @sections << section if section end @last_modified = Time::now self end def to_html4( opt ) r = '' each_section do |section| r << %Q[
\n] r << %Q[<%=section_enter_proc( Time::at( #{date.to_i} ) )%>\n] if section.subtitle then r << %Q[

<%= subtitle_proc( Time::at( #{date.to_i} ), #{section.subtitle.dump.gsub( /%/, '\\\\045' )} ) %>

\n] end if /\A#{section.body.split_unless_plugin( "\n+" ).collect{|l|l.chomp.sub( /\A[  ]/u, '')}.join( "

\n

" )}

] else r << %Q[

<%= subtitle_proc( Time::at( #{date.to_i} ), nil ) %>] r << %Q[#{section.body.split_unless_plugin( "\n+" ).collect{|l|l.chomp.sub( /\A[  ]/u, '' )}.join( "

\n

" )}

] end r << %Q[<%=section_leave_proc( Time::at( #{date.to_i} ) )%>\n] r << %Q[
] end r end def to_chtml( opt ) r = '' each_section do |section| r << %Q[<%=section_enter_proc( Time::at( #{date.to_i} ) )%>\n] if section.subtitle then r << %Q[

<%= subtitle_proc( Time::at( #{date.to_i} ), #{section.subtitle.dump.gsub( /%/, '\\\\045' )} ) %>

\n] end if /\A#{section.body.split_unless_plugin( "\n+" ).collect{|l|l.chomp.sub( /\A[  ]/u, '' )}.join( "

\n

" )}

] else r << %Q[

<%= subtitle_proc( Time::at( #{date.to_i} ), nil ) %>] r << %Q[#{section.body.split_unless_plugin( "\n+" ).collect{|l|l.chomp.sub( /\A[  ]/u, '' )}.join( "

\n

" )}

] end r << %Q[<%=section_leave_proc( Time::at( #{date.to_i} ) )%>\n] end r end end end