tdiary 4.0.1.20130903 → 4.0.1.20130929

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,478 +0,0 @@
1
- # -*- coding: utf-8; -*-
2
- #
3
- # wiki_style.rb: WikiWiki style for tDiary 2.x format. $Revision: 1.29 $
4
- #
5
- # if you want to use this style, add @style into tdiary.conf below:
6
- #
7
- # @style = 'Wiki'
8
- #
9
- # Copyright (C) 2003, TADA Tadashi <sho@spc.gr.jp>
10
- # You can distribute this under GPL.
11
- #
12
- require 'tdiary/wiki_parser'
13
-
14
- module TDiary
15
- class WikiSection
16
- attr_reader :subtitle, :author
17
- attr_reader :categories, :stripped_subtitle
18
- attr_reader :subtitle_to_html, :stripped_subtitle_to_html, :body_to_html
19
-
20
- def initialize( fragment, author = nil )
21
- @author = author
22
- if fragment[0] == ?! then
23
- @subtitle, @body = fragment.split( /\n/, 2 )
24
- @subtitle.sub!( /^\!\s*/, '' )
25
- else
26
- @subtitle = nil
27
- @body = fragment.dup
28
- end
29
- @body = @body || ''
30
- @body.sub!( /[\n\r]+\Z/, '' )
31
- @body << "\n\n"
32
- @parser = WikiParser::new( :wikiname => false ).parse( to_src )
33
-
34
- @categories = get_categories
35
- @stripped_subtitle = strip_subtitle
36
-
37
- @subtitle_to_html = @subtitle ? to_html("!#{@subtitle}") : nil
38
- @stripped_subtitle_to_html = @stripped_subtitle ? to_html("!#{@stripped_subtitle}") : nil
39
- @body_to_html = to_html(@body)
40
- end
41
-
42
- def subtitle=(subtitle)
43
- cat_str = ""
44
- @categories.each {|cat|
45
- cat_str << "[#{cat}]"
46
- }
47
- cat_str << " " unless cat_str.empty?
48
- @subtitle = subtitle ? (cat_str + subtitle) : nil
49
- @stripped_subtitle = strip_subtitle
50
- end
51
-
52
- def body
53
- @body.dup
54
- end
55
-
56
- def body=(str)
57
- @body = str
58
- end
59
-
60
- def categories=(categories)
61
- @categories = categories
62
- cat_str = ""
63
- categories.each {|cat|
64
- cat_str << "[#{cat}]"
65
- }
66
- cat_str << " " unless cat_str.empty?
67
- @subtitle = @subtitle ? (cat_str + @stripped_subtitle) : nil
68
- @stripped_subtitle = strip_subtitle
69
- end
70
-
71
- def to_src
72
- r = ''
73
- r << "! #{@subtitle}\n" if @subtitle
74
- r << @body
75
- end
76
-
77
- def html4( date, idx, opt )
78
- r = %Q[<div class="section">\n]
79
- r << %Q[<%=section_enter_proc( Time::at( #{date.to_i} ) )%>\n]
80
- r << do_html4( @parser, date, idx, opt )
81
- r << %Q[<%=section_leave_proc( Time::at( #{date.to_i} ) )%>\n]
82
- r << "</div>\n"
83
- end
84
-
85
- def do_html4( parser, date, idx, opt )
86
- main_buff = ''
87
- subtitle_buff = ''
88
- r = main_buff
89
- stat = nil
90
- subtitle = false
91
- parser.each do |s|
92
- stat = s if s.class == Symbol
93
- case s
94
-
95
- # subtitle heading
96
- when :HS1
97
- r << "<h3>"
98
- subtitle = true
99
- r = subtitle_buff = ''
100
- when :HE1
101
- r = main_buff
102
- r << "<%= subtitle_proc( Time::at( #{date.to_i} ), #{subtitle_buff.dump.gsub( /%/, '\\\\045' )} ) %>"
103
- r << "</h3>\n"
104
-
105
- # other headings
106
- when :HS2, :HS3, :HS4, :HS5; r << "<h#{s.to_s[2,1].to_i + 2}>"
107
- when :HE2, :HE3, :HE4, :HE5; r << "</h#{s.to_s[2,1].to_i + 2}>\n"
108
-
109
- # pargraph
110
- when :PS
111
- r << '<p>'
112
- r << "<%= subtitle_proc( Time::at( #{date.to_i} ), nil ) %>" if ( !subtitle and date )
113
- when :PE; r << "</p>\n"
114
-
115
- # horizontal line
116
- when :RS; r << "<hr>\n"
117
- when :RE
118
-
119
- # blockquote
120
- when :QS; r << "<blockquote>\n"
121
- when :QE; r << "</blockquote>\n"
122
-
123
- # list
124
- when :US; r << "<ul>\n"
125
- when :UE; r << "</ul>\n"
126
-
127
- # ordered list
128
- when :OS; r << "<ol>\n"
129
- when :OE; r << "</ol>\n"
130
-
131
- # list item
132
- when :LS; r << "<li>"
133
- when :LE; r << "</li>\n"
134
-
135
- # definition list
136
- when :DS; r << "<dl>\n"
137
- when :DE; r << "</dl>\n"
138
- when :DTS; r << "<dt>"
139
- when :DTE; r << "</dt>"
140
- when :DDS; r << "<dd>"
141
- when :DDE; r << "</dd>\n"
142
-
143
- # formatted text
144
- when :FS; r << '<pre>'
145
- when :FE; r << "</pre>\n"
146
-
147
- # table
148
- when :TS; r << "<table border=\"1\">\n"
149
- when :TE; r << "</table>\n"
150
- when :TRS; r << "<tr>\n"
151
- when :TRE; r << "</tr>\n"
152
- when :TDS; r << "<td>"
153
- when :TDE; r << "</td>"
154
-
155
- # emphasis
156
- when :ES; r << "<em>"
157
- when :EE; r << "</em>"
158
-
159
- # strong
160
- when :SS; r << "<strong>"
161
- when :SE; r << "</strong>"
162
-
163
- # delete
164
- when :ZS; r << "<del>"
165
- when :ZE; r << "</del>"
166
-
167
- # Keyword
168
- when :KS; r << '<'
169
- when :KE; r << '>'
170
-
171
- # Plugin
172
- when :GS; r << '<%='
173
- when :GE; r << '%>'
174
-
175
- # URL
176
- when :XS; #r << '<a href="'
177
- when :XE; #r << '</a>'
178
-
179
- else
180
- s = CGI::escapeHTML( s ) unless stat == :GS
181
- case stat
182
- when :KS
183
- r << keyword(s)
184
- when :XS
185
- case s
186
- when /^mailto:/
187
- r << %Q[<a href="#{s}">#{s.sub( /^mailto:/, '' )}</a>]
188
- when /\.(jpg|jpeg|png|gif)$/
189
- r << %Q[<img src="#{s}" alt="#{File::basename( s )}">]
190
- else
191
- r << %Q[<a href="#{s}">#{s}</a>]
192
- end
193
- else
194
- r << s if s.class == String
195
- end
196
- end
197
- end
198
- r
199
- end
200
-
201
- def chtml( date, idx, opt )
202
- main_buff = ''
203
- subtitle_buff = ''
204
- r = main_buff
205
- stat = nil
206
- subtitle = false
207
- @parser.each do |s|
208
- stat = s if s.class == Symbol
209
- case s
210
-
211
- # subtitle heading
212
- when :HS1
213
- r << "<H3>"
214
- subtitle = true
215
- r = subtitle_buff = ''
216
- when :HE1
217
- r = main_buff
218
- r << "<%= subtitle_proc( Time::at( #{date.to_i} ), #{subtitle_buff.dump.gsub( /%/, '\\\\045' )} ) %>"
219
- r << "</H3>\n"
220
-
221
- # other headings
222
- when :HS2, :HS3, :HS4, :HS5; r << "<H#{s.to_s[2,1].to_i + 2}>"
223
- when :HE2, :HE3, :HE4, :HE5; r << "</H#{s.to_s[2,1].to_i + 2}>\n"
224
-
225
- # paragraph
226
- when :PS
227
- r << "<P>"
228
- r << "<%= subtitle_proc( Time::at( #{date.to_i} ), nil ) %>" if ( !subtitle and date )
229
- when :PE; r << "</P>\n"
230
-
231
- # horizontal line
232
- when :RS; r << "<HR>\n"
233
- when :RE
234
-
235
- # blockquote
236
- when :QS; r << "<BLOCKQUOTE>\n"
237
- when :QE; r << "</BLOCKQUOTE>\n"
238
-
239
- # list
240
- when :US; r << "<UL>\n"
241
- when :UE; r << "</UL>\n"
242
-
243
- # ordered list
244
- when :OS; r << "<OL>\n"
245
- when :OE; r << "</OL>\n"
246
-
247
- # list item
248
- when :LS; r << "<LI>"
249
- when :LE; r << "</LI>\n"
250
-
251
- # definition list
252
- when :DS; r << "<DL>\n"
253
- when :DE; r << "</DL>\n"
254
- when :DTS; r << "<DT>"
255
- when :DTE; r << "</DT>"
256
- when :DDS; r << "<DD>"
257
- when :DDE; r << "</DD>\n"
258
-
259
- # formatted text
260
- when :FS; r << '<PRE>'
261
- when :FE; r << "</PRE>\n"
262
-
263
- # table
264
- when :TS; r << "<TABLE BORDER=\"1\">\n"
265
- when :TE; r << "</TABLE>\n"
266
- when :TRS; r << "<TR>\n"
267
- when :TRE; r << "</TR>\n"
268
- when :TDS; r << "<TD>"
269
- when :TDE; r << "</TD>"
270
-
271
- # emphasis
272
- when :ES; r << "<EM>"
273
- when :EE; r << "</EM>"
274
-
275
- # strong
276
- when :SS; r << "<STRONG>"
277
- when :SE; r << "</STRONG>"
278
-
279
- # delete
280
- when :ZS; r << "<DEL>"
281
- when :ZE; r << "</DEL>"
282
-
283
- # Keyword
284
- when :KS; r << '<'
285
- when :KE; r << '>'
286
-
287
- # Plugin
288
- when :GS; r << '<%='
289
- when :GE; r << '%>'
290
-
291
- # URL
292
- when :XS; r << '<A HREF="'
293
- when :XE; r << '</A>'
294
-
295
- else
296
- s = CGI::escapeHTML( s ) unless stat == :GS
297
- case stat
298
- when :KS
299
- r << keyword(s, true)
300
- when :XS
301
- r << s << '">' << s.sub( /^mailto:/, '' )
302
- else
303
- r << s if s.class == String
304
- end
305
- end
306
- end
307
- r
308
- end
309
-
310
- def to_s
311
- to_src
312
- end
313
-
314
- private
315
- def keyword( s, mobile = false )
316
- r = ''
317
- if /\|/ =~ s
318
- k, u = s.split( /\|/, 2 )
319
- if /^(\d{4}|\d{6}|\d{8}|\d{8}-\d+)[^\d]*?#?([pct]\d+)?$/ =~ u then
320
- r << %Q[%=my '#{$1}#{$2}', '#{k}' %]
321
- elsif /:/ =~ u
322
- scheme, path = u.split( /:/, 2 )
323
- if /\A(?:http|https|ftp|mailto)\z/ =~ scheme
324
- if mobile
325
- r << %Q[A HREF="#{u}">#{k}</A]
326
- else
327
- r << %Q[a href="#{u}">#{k}</a]
328
- end
329
- else
330
- r << %Q[%=kw '#{u}', '#{k}'%]
331
- end
332
- else
333
- r << %Q[a href="#{u}">#{k}</a]
334
- end
335
- else
336
- r << %Q[%=kw '#{s}' %]
337
- end
338
- r
339
- end
340
-
341
- def to_html(string)
342
- parser = WikiParser::new( :wikiname => false ).parse( string )
343
- parser.delete_at(0) if parser[0] == :HS1
344
- parser.delete_at(-1) if parser[-1] == :HE1
345
- r = do_html4(parser, nil, nil, {})
346
- if r == ""
347
- nil
348
- else
349
- r
350
- end
351
- end
352
-
353
- def get_categories
354
- return [] unless @subtitle
355
- cat = /^(\[([^\[]+?)\])+/.match(@subtitle).to_a[0]
356
- return [] unless cat
357
- cat.scan(/\[(.*?)\]/).collect do |c|
358
- c[0].split(/,/)
359
- end.flatten
360
- end
361
-
362
- def strip_subtitle
363
- return nil unless @subtitle
364
- r = @subtitle.sub(/^(\[[^\[]+?\])+\s*/,'')
365
- if r == ""
366
- nil
367
- else
368
- r
369
- end
370
- end
371
- end
372
-
373
- class WikiDiary
374
- include DiaryBase
375
- include CategorizableDiary
376
-
377
- def initialize( date, title, body, modified = Time::now )
378
- init_diary
379
- replace( date, title, body )
380
- @last_modified = modified
381
- end
382
-
383
- def style
384
- 'Wiki'
385
- end
386
-
387
- def replace( date, title, body )
388
- set_date( date )
389
- set_title( title )
390
- @sections = []
391
- append( body )
392
- end
393
-
394
- def append( body, author = nil )
395
- section = nil
396
- body.each do |l|
397
- case l
398
- when /^\![^!]/
399
- @sections << WikiSection::new( section, author ) if section
400
- section = l
401
- else
402
- section = '' unless section
403
- section << l
404
- end
405
- end
406
- @sections << WikiSection::new( section, author ) if section
407
- @last_modified = Time::now
408
- self
409
- end
410
-
411
- def each_section
412
- @sections.each do |section|
413
- yield section
414
- end
415
- end
416
-
417
- def add_section(subtitle, body)
418
- sec = WikiSection::new("\n")
419
- sec.subtitle = subtitle
420
- sec.body = body
421
- @sections << sec
422
- @sections.size
423
- end
424
-
425
- def delete_section(index)
426
- @sections.delete_at(index - 1)
427
- end
428
-
429
- def to_src
430
- r = ''
431
- each_section do |section|
432
- r << section.to_src
433
- end
434
- r
435
- end
436
-
437
- def to_html( opt, mode = :HTML )
438
- case mode
439
- when :CHTML
440
- to_chtml( opt )
441
- else
442
- to_html4( opt )
443
- end
444
- end
445
-
446
- def to_html4( opt )
447
- r = ''
448
- idx = 1
449
- each_section do |section|
450
- r << section.html4( date, idx, opt )
451
- idx += 1
452
- end
453
- r
454
- end
455
-
456
- def to_chtml( opt )
457
- r = ''
458
- idx = 1
459
- each_section do |section|
460
- r << section.chtml( date, idx, opt )
461
- idx += 1
462
- end
463
- r
464
- end
465
-
466
- def to_s
467
- "date=#{date.strftime('%Y%m%d')}, title=#{title}, body=[#{@sections.join('][')}]"
468
- end
469
- end
470
- end
471
-
472
-
473
- # Local Variables:
474
- # mode: ruby
475
- # indent-tabs-mode: t
476
- # tab-width: 3
477
- # ruby-indent-level: 3
478
- # End:
@@ -1,165 +0,0 @@
1
- # -*- coding: utf-8; -*-
2
- require 'spec_helper'
3
-
4
- require 'tdiary'
5
- require 'misc/style/emptdiary/emptdiary_style'
6
-
7
- describe TDiary::Style::EmptdiaryDiary do
8
- before do
9
- @diary = TDiary::Style::EmptdiaryDiary.new(Time::at( 1041346800 ), "TITLE", "")
10
- end
11
-
12
- describe '#append' do
13
- before do
14
- source = <<-'EOF'
15
- subTitle
16
- <p>honbun</p>
17
-
18
- subTitle2
19
- <p>honbun</p>
20
- EOF
21
- @diary.append(source)
22
- end
23
-
24
- context 'HTML' do
25
- before do
26
- @html = <<-'EOF'
27
- <div class="section">
28
- <%=section_enter_proc( Time::at( 1041346800 ) )%>
29
- <h3><%= subtitle_proc( Time::at( 1041346800 ), "subTitle" ) %></h3>
30
- <p>honbun</p><%=section_leave_proc( Time::at( 1041346800 ) )%>
31
- </div><div class="section">
32
- <%=section_enter_proc( Time::at( 1041346800 ) )%>
33
- <h3><%= subtitle_proc( Time::at( 1041346800 ), "subTitle2" ) %></h3>
34
- <p>honbun</p><%=section_leave_proc( Time::at( 1041346800 ) )%>
35
- </div>
36
- EOF
37
- end
38
- it { @diary.to_html.should eq @html.chomp }
39
- end
40
-
41
- context 'CHTML' do
42
- before do
43
- @html = <<-'EOF'
44
- <%=section_enter_proc( Time::at( 1041346800 ) )%>
45
- <H3><%= subtitle_proc( Time::at( 1041346800 ), "subTitle" ) %></H3>
46
- <p>honbun</p><%=section_leave_proc( Time::at( 1041346800 ) )%>
47
- <%=section_enter_proc( Time::at( 1041346800 ) )%>
48
- <H3><%= subtitle_proc( Time::at( 1041346800 ), "subTitle2" ) %></H3>
49
- <p>honbun</p><%=section_leave_proc( Time::at( 1041346800 ) )%>
50
- EOF
51
- end
52
- it { @diary.to_html({}, :CHTML).should eq @html }
53
- end
54
- end
55
-
56
- describe '#replace' do
57
- before do
58
- source = <<-'EOF'
59
- subTitle
60
- <p>honbun</p>
61
-
62
- subTitle2
63
- <p>honbun</p>
64
- EOF
65
- @diary.append(source)
66
-
67
- replaced = <<-'EOF'
68
- replaceTitle
69
- <p>replace</p>
70
-
71
- replaceTitle2
72
- <p>replace</p>
73
-
74
- EOF
75
- @diary.replace(Time::at( 1041346800 ), "TITLE", replaced)
76
-
77
- @html = <<-'EOF'
78
- <div class="section">
79
- <%=section_enter_proc( Time::at( 1041346800 ) )%>
80
- <h3><%= subtitle_proc( Time::at( 1041346800 ), "replaceTitle" ) %></h3>
81
- <p>replace</p><%=section_leave_proc( Time::at( 1041346800 ) )%>
82
- </div><div class="section">
83
- <%=section_enter_proc( Time::at( 1041346800 ) )%>
84
- <h3><%= subtitle_proc( Time::at( 1041346800 ), "replaceTitle2" ) %></h3>
85
- <p>replace</p><%=section_leave_proc( Time::at( 1041346800 ) )%>
86
- </div>
87
- EOF
88
- end
89
- it { @diary.to_html.should eq @html.chomp }
90
- end
91
-
92
- describe 'test_emptdiary_style_plugin' do
93
- before do
94
- source = <<-'EOF'
95
- sectionTitle
96
- <p>body</p>
97
- <%=pre <<'_PRE'
98
- #include <stdio.h>
99
-
100
- /* comment */
101
- int
102
- main (int argc, char *argv[])
103
- {
104
- puts ("Hello world.");
105
- }
106
- _PRE
107
- %>
108
- EOF
109
- @diary.append(source)
110
- end
111
-
112
- context 'HTML' do
113
- before do
114
- @html = <<-'EOF'
115
- <div class="section">
116
- <%=section_enter_proc( Time::at( 1041346800 ) )%>
117
- <h3><%= subtitle_proc( Time::at( 1041346800 ), "sectionTitle" ) %></h3>
118
- <p>body</p>
119
- <%=pre <<'_PRE'
120
- #include <stdio.h>
121
-
122
- /* comment */
123
- int
124
- main (int argc, char *argv[])
125
- {
126
- puts ("Hello world.");
127
- }
128
- _PRE
129
- %><%=section_leave_proc( Time::at( 1041346800 ) )%>
130
- </div>
131
- EOF
132
- end
133
- it { @diary.to_html.should eq @html.chomp }
134
- end
135
-
136
- context 'CHTML' do
137
- before do
138
- @html = <<-'EOF'
139
- <%=section_enter_proc( Time::at( 1041346800 ) )%>
140
- <H3><%= subtitle_proc( Time::at( 1041346800 ), "sectionTitle" ) %></H3>
141
- <p>body</p>
142
- <%=pre <<'_PRE'
143
- #include <stdio.h>
144
-
145
- /* comment */
146
- int
147
- main (int argc, char *argv[])
148
- {
149
- puts ("Hello world.");
150
- }
151
- _PRE
152
- %><%=section_leave_proc( Time::at( 1041346800 ) )%>
153
- EOF
154
- end
155
- it { @diary.to_html({}, :CHTML).should eq @html }
156
- end
157
- end
158
- end
159
-
160
- # Local Variables:
161
- # mode: ruby
162
- # indent-tabs-mode: t
163
- # tab-width: 3
164
- # ruby-indent-level: 3
165
- # End: