/
etdiary_style.rb
340 lines (317 loc) · 7.1 KB
/
etdiary_style.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#
# etdiary_style.rb: tDiary style class for etDiary format.
# $Id: etdiary_style.rb,v 1.2 2003-02-23 07:18:12 tadatadashi Exp $
#
# if you want to use this style, add @style into tdiary.conf below:
#
# @style = 'etDiary'
#
module TDiary
class EtdiarySection
attr_reader :subtitle, :bodies, :author, :anchor_type
attr_reader :categories, :stripped_subtitle
def initialize( title, author = nil )
@subtitle = title
if @subtitle then
if "" == @subtitle then
@subtitle = nil
@anchor_type = :P
elsif "<>" == @subtitle then
@subtitle = nil
@anchor_type = :A
elsif /^<>/ =~ @subtitle then
@subtitle = @subtitle[2..-1]
@anchor_type = :H4
else
@anchor_type = :H3
end
else
@subtitle = nil
@anchor_type = nil
end
@bodies = []
@categories = get_categories
@stripped_subtitle = strip_subtitle
end
def body
if @bodies then
@bodies.join('')
else
''
end
end
def << (string)
@bodies << string
end
def to_src
s = ''
case @anchor_type
when :A
s << "<<<>>>"
when :P
s << "<<>>"
when :H4
s << "[#{@author}]" if @author
s << "<<<>" + @subtitle + ">>"
when :H3
s << "[#{@author}]" if @author
s << "<<" + @subtitle + ">>"
end
s + ( if "" != body then body else "\n" end )
end
def to_s
"subtitle=#{@subtitle}, body=#{body}"
end
def get_categories
return [] unless @subtitle
cat = /^(\[(.*?)\])+/.match(@subtitle).to_a[0]
return [] unless cat
cat.scan(/\[(.*?)\]/).collect do |c|
c[0].split(/,/)
end.flatten
end
def categorized_subtitle
return "" unless @subtitle
cat = /^(\[(.*?)\])+/.match(@subtitle).to_a[0]
return @stripped_subtitle unless cat
cat.gsub(/\[(.*?)\]/) do
$1.split(/,/).collect do |c|
%Q|<%= category_anchor("#{c}") %>|
end.join
end + @stripped_subtitle
end
def strip_subtitle
return nil unless @subtitle
@subtitle.sub(/^(\[(.*?)\])+/,'')
end
end
class EtHtml4Factory
def initialize( opt, idx = 1 )
@opt = opt
@idx = idx
end
def title( date, fragment )
return nil if nil == fragment.anchor_type
name = 'p%02d' % @idx
@idx += 1
if :A == fragment.anchor_type then
if @opt['anchor'] then
return "<a name=\"#{name}\"></a>"
else
return nil
end
end
r = "<a"
r << " name=\"#{name}\"" if @opt['anchor']
r << " href=\"" + @opt['index']
r << "<%=anchor \"" + date.strftime('%Y%m%d') + "#" + name + "\" %>\">"
r << @opt['section_anchor'] + "</a>"
r << "[" + fragment.author + "]" if fragment.author
r << fragment.categorized_subtitle if fragment.subtitle
case fragment.anchor_type
when :P
r
when :H4
"<h4>" + r + ":</h4>\n"
when :H3
"<h3>" + r + "</h3>\n"
end
end
def section_start
"<div class=\"section\">\n"
end
def section_end
"</div>\n"
end
def block_title?( fragment )
case fragment.anchor_type
when :H3, :H4
true
else
false
end
end
def p_start
"<p>"
end
def p_end
"</p>"
end
def pre_start
"<pre>"
end
def pre_end
"</pre>"
end
end
class EtCHtmlFactory
def initialize( opt, idx = 1 )
@opt = opt
@idx = idx
end
def title( date, fragment )
return nil if nil == fragment.anchor_type
name = 'p%02d' % @idx
@idx += 1
r = "<A NAME=\"#{name}\">"
return r + "</A>" if :A == fragment.anchor_type
r << "*" if :A != fragment.anchor_type
r << "</A> "
r << "[" + fragment.author + "]" if fragment.author
r << fragment.subtitle if fragment.subtitle
case fragment.anchor_type
when :P
r
when :H4
r + ": "
when :H3
"<P>" + r + "</P>\n"
end
end
def section_start
""
end
def section_end
""
end
def block_title?( fragment )
case fragment.anchor_type
when :H3
true
else
false
end
end
def p_start
"<P>"
end
def p_end
"</P>"
end
def pre_start
"<PRE>"
end
def pre_end
"</PRE>"
end
end
class EtdiaryDiary
include DiaryBase
include CategorizableDiary
TAG_BEG_REGEXP = /\A<([A-Za-z]*)([^>]*)>([^\r]*)\z/
TAG_END_REGEXP = /\A([^\r]*)<\/([A-Za-z]*)>\n*\z/
PRE_REGEXP = /\A<[Pp][Rr][Ee][^>]*>([^\r]*)<\/[Pp][Rr][Ee]>\n*\z/
TITLE_REGEXP = /\A<<([^\r]*?)>>[^>]/
def initialize( date, title, body, modified = Time::now )
init_diary
replace( date, title, body )
@last_modified = modified
end
def style
'etDiary'
end
def replace( date, title, body )
set_date( date )
set_title( title )
@sections = []
append( body )
end
def append( body, author = nil )
section = EtdiarySection::new( nil, author )
buffer = nil
tag_kind = nil
( body.gsub("\r",'').sub(/\A\n*/,'') + "\n" ).each("") do |fragment|
if buffer and TAG_END_REGEXP =~ fragment and $2.downcase == tag_kind then
section << buffer + fragment.sub(/\n*\z/,"\n\n")
tag_kind = nil
buffer = nil
elsif buffer then
buffer << fragment
else
title = TITLE_REGEXP.match(fragment+"\n").to_a[1]
if title then
@sections << section
section = EtdiarySection::new( title, author )
fragment = fragment[ title.length + 4 .. -1 ]
end
if TAG_BEG_REGEXP =~ fragment then
tag_kind = $1.downcase
if TAG_END_REGEXP =~ fragment and $2.downcase == tag_kind then
section << fragment.sub(/\n*\z/,"\n\n")
tag_kind = nil
else
buffer = fragment
end
else
section << fragment
end
end
end
@zerosection = @sections.shift
@sections << section
@last_modified = Time::now
self
end
def each_section
@sections.each do |section|
yield section
end
end
def to_src
src = if @zerosection then @zerosection.to_src else '' end
each_section do |section|
src << section.to_src
end
src.sub(/\n*\z/,"\n")
end
def to_html_section(section, factory, title = nil)
return '' unless section.bodies
r = ''
section.bodies.each do |fragment|
if PRE_REGEXP =~ fragment then
r << factory.pre_start
r << $1.gsub("&","&").gsub("<","<").gsub(">",">")
r << factory.pre_end << "\n"
elsif /\A</ =~ fragment then
r << fragment.sub( /\n*\z/, "\n" )
else
r << factory.p_start
r << title if title
r << fragment.sub(/\A\n*/,"\n").sub( /\n*\z/, "\n" + factory.p_end + "\n" )
end
end
r
end
def to_html( opt, mode = :HTML )
case mode
when :CHTML
f = EtCHtmlFactory::new(opt)
else
f = EtHtml4Factory::new(opt)
end
r = f.section_start
if @zerosection then
r << to_html_section(@zerosection,f)
end
each_section do |section|
if :H3 == section.anchor_type and r != f.section_start then
r << f.section_end << "\n" << f.section_start
end
s = to_html_section(section,f)
title = f.title( date, section ) || ''
if f.block_title?(section) then
r << title << s
elsif /\A</ =~ section.body then
r << f.p_start << title << f.p_end << s
elsif s then
r << to_html_section( section, f, title )
end
end
r + f.section_end
end
def to_s
"date=#{date.strftime('%Y%m%d')}, title=#{title}, " \
+ "body=[#{@sections.join('][')}]"
end
end
end