tdiary 4.0.1 → 4.0.1.20130903

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,159 +28,161 @@ class HTMLwithPygments < Redcarpet::Render::HTML
28
28
  end
29
29
 
30
30
  module TDiary
31
- class GfmSection
32
- include SectionBase
33
- include Twitter::Autolink
34
-
35
- def initialize(fragment, author = nil)
36
- @author = author
37
- @subtitle, @body = fragment.split(/\n/, 2)
38
- @subtitle.sub!(/^\#\s*/,'')
39
- @body ||= ''
40
-
41
- @categories = get_categories
42
- @stripped_subtitle = strip_subtitle
43
-
44
- @subtitle_to_html = @subtitle ? to_html('# ' + @subtitle).gsub(/\A<h\d>|<\/h\d>\z/io, '') : nil
45
- @stripped_subtitle_to_html = @stripped_subtitle ? to_html('# ' + @stripped_subtitle).gsub(/\A<h\d>|<\/h\d>\z/io, '') : nil
46
- @body_to_html = to_html(@body)
47
- end
48
-
49
- def subtitle=(subtitle)
50
- @subtitle = (subtitle || '').sub(/^# /,"\##{categories_to_string} ")
51
- @strip_subtitle = strip_subtitle
52
- end
31
+ module Style
32
+ class GfmSection
33
+ include BaseSection
34
+ include Twitter::Autolink
35
+
36
+ def initialize(fragment, author = nil)
37
+ @author = author
38
+ @subtitle, @body = fragment.split(/\n/, 2)
39
+ @subtitle.sub!(/^\#\s*/,'')
40
+ @body ||= ''
41
+
42
+ @categories = get_categories
43
+ @stripped_subtitle = strip_subtitle
44
+
45
+ @subtitle_to_html = @subtitle ? to_html('# ' + @subtitle).gsub(/\A<h\d>|<\/h\d>\z/io, '') : nil
46
+ @stripped_subtitle_to_html = @stripped_subtitle ? to_html('# ' + @stripped_subtitle).gsub(/\A<h\d>|<\/h\d>\z/io, '') : nil
47
+ @body_to_html = to_html(@body)
48
+ end
53
49
 
54
- def categories=(categories)
55
- @subtitle = "#{categories_to_string} " + (strip_subtitle || '')
56
- @strip_subtitle = strip_subtitle
57
- end
50
+ def subtitle=(subtitle)
51
+ @subtitle = (subtitle || '').sub(/^# /,"\##{categories_to_string} ")
52
+ @strip_subtitle = strip_subtitle
53
+ end
58
54
 
59
- def to_src
60
- r = ''
61
- r << "\# #{@subtitle}\n" if @subtitle
62
- r << @body
63
- end
55
+ def categories=(categories)
56
+ @subtitle = "#{categories_to_string} " + (strip_subtitle || '')
57
+ @strip_subtitle = strip_subtitle
58
+ end
64
59
 
65
- def do_html4(date, idx, opt)
66
- subtitle = to_html('# ' + @subtitle)
67
- subtitle.sub!( %r!<h3>(.+?)</h3>!m ) do
68
- "<h3><%= subtitle_proc( Time.at( #{date.to_i} ), #{$1.dump.gsub( /%/, '\\\\045' )} ) %></h3>"
60
+ def to_src
61
+ r = ''
62
+ r << "\# #{@subtitle}\n" if @subtitle
63
+ r << @body
69
64
  end
70
- if opt['multi_user'] and @author then
71
- subtitle.sub!(/<\/h3>/,%Q|[#{@author}]</h3>|)
65
+
66
+ def do_html4(date, idx, opt)
67
+ subtitle = to_html('# ' + @subtitle)
68
+ subtitle.sub!( %r!<h3>(.+?)</h3>!m ) do
69
+ "<h3><%= subtitle_proc( Time.at( #{date.to_i} ), #{$1.dump.gsub( /%/, '\\\\045' )} ) %></h3>"
70
+ end
71
+ if opt['multi_user'] and @author then
72
+ subtitle.sub!(/<\/h3>/,%Q|[#{@author}]</h3>|)
73
+ end
74
+ r = subtitle
75
+ r << @body_to_html
72
76
  end
73
- r = subtitle
74
- r << @body_to_html
75
- end
76
77
 
77
- private
78
+ private
78
79
 
79
- def to_html(string)
80
- renderer = HTMLwithPygments.new(:hard_wrap => true)
81
- extensions = {:fenced_code_blocks => true, :tables => true, :no_intra_emphasis => true}
82
- r = Redcarpet::Markdown.new(renderer, extensions).render(string)
80
+ def to_html(string)
81
+ renderer = HTMLwithPygments.new(:hard_wrap => true)
82
+ extensions = {:fenced_code_blocks => true, :tables => true, :no_intra_emphasis => true}
83
+ r = Redcarpet::Markdown.new(renderer, extensions).render(string)
83
84
 
84
- # Twitter Autolink
85
- r = auto_link(r)
85
+ # Twitter Autolink
86
+ r = auto_link(r)
86
87
 
87
- if r =~ /(<pre>|<code>)/
88
- r.gsub!(/<a class=\"tweet-url username\" href=\".*?\">(.*?)<\/a>/){ $1 }
89
- end
88
+ if r =~ /(<pre>|<code>)/
89
+ r.gsub!(/<a class=\"tweet-url username\" href=\".*?\">(.*?)<\/a>/){ $1 }
90
+ end
90
91
 
91
- # except url autolink in plugin block
92
- if r =~ /\{\{.+?\}\}/
93
- r.gsub!(/<a href=\"(.*?)\" rel=\"nofollow\">.*?<\/a>/){ $1 }
94
- r.gsub!(/\{\{(.+?)\}\}/) { "<%=#{CGI.unescapeHTML($1).gsub(/&#39;/, "'").gsub(/&quot;/, '"')}%>" }
95
- end
92
+ # except url autolink in plugin block
93
+ if r =~ /\{\{.+?\}\}/
94
+ r.gsub!(/<a href=\"(.*?)\" rel=\"nofollow\">.*?<\/a>/){ $1 }
95
+ r.gsub!(/\{\{(.+?)\}\}/) { "<%=#{CGI.unescapeHTML($1).gsub(/&#39;/, "'").gsub(/&quot;/, '"')}%>" }
96
+ end
96
97
 
97
- # ignore duplicate autolink
98
- if r =~ /<a href="<a href="/
99
- r.gsub!(/<a href="<a href=".*?" rel="nofollow">(.*?)<\/a>"(.*?)>(.*?)<\/a>/) do
100
- "<a href=\"#{$1}\" rel=\"nofollow\"#{$2}>#{$3}</a>"
101
- end
102
- end
103
- # ignore auto imagelink
104
- if r =~ /<img src="<a href="/
105
- r.gsub!(/<img src="<a href=".*?" rel="nofollow">(.*?)<\/a>"(?: alt="(.*?)")?>/){ "<img src=\"#{$1}\" alt=\"#{$2}\">" }
106
- end
98
+ # ignore duplicate autolink
99
+ if r =~ /<a href="<a href="/
100
+ r.gsub!(/<a href="<a href=".*?" rel="nofollow">(.*?)<\/a>"(.*?)>(.*?)<\/a>/) do
101
+ "<a href=\"#{$1}\" rel=\"nofollow\"#{$2}>#{$3}</a>"
102
+ end
103
+ end
104
+ # ignore auto imagelink
105
+ if r =~ /<img src="<a href="/
106
+ r.gsub!(/<img src="<a href=".*?" rel="nofollow">(.*?)<\/a>"(?: alt="(.*?)")?>/){ "<img src=\"#{$1}\" alt=\"#{$2}\">" }
107
+ end
107
108
 
108
- # emoji
109
- r = r.emojify
109
+ # emoji
110
+ r = r.emojify
110
111
 
111
- # diary anchor
112
- r.gsub!(/<h(\d)/) { "<h#{$1.to_i + 2}" }
113
- r.gsub!(/<\/h(\d)/) { "</h#{$1.to_i + 2}" }
112
+ # diary anchor
113
+ r.gsub!(/<h(\d)/) { "<h#{$1.to_i + 2}" }
114
+ r.gsub!(/<\/h(\d)/) { "</h#{$1.to_i + 2}" }
114
115
 
115
- # my syntax
116
- r.gsub!(/\((.*?)\)\[(\d{4}|\d{6}|\d{8}|\d{8}-\d+)[^\d]*?#?([pct]\d+)?\]/) {
117
- unless $1.empty?
118
- %Q|<%=my "#{$2}#{$3}", "#{$1}" %>|
119
- else
120
- %Q|<%=my "#{$2}#{$3}", "#{$2}#{$3}" %>|
121
- end
122
- }
116
+ # my syntax
117
+ r.gsub!(/\((.*?)\)\[(\d{4}|\d{6}|\d{8}|\d{8}-\d+)[^\d]*?#?([pct]\d+)?\]/) {
118
+ unless $1.empty?
119
+ %Q|<%=my "#{$2}#{$3}", "#{$1}" %>|
120
+ else
121
+ %Q|<%=my "#{$2}#{$3}", "#{$2}#{$3}" %>|
122
+ end
123
+ }
123
124
 
124
- r
125
- end
125
+ r
126
+ end
126
127
 
127
- def get_categories
128
- return [] unless @subtitle
129
- cat = /(\\?\[([^\[]+?)\\?\])+/.match(@subtitle).to_a[0]
130
- return [] unless cat
131
- cat.scan(/\\?\[(.*?)\\?\]/).collect do |c|
132
- c[0].split(/,/)
133
- end.flatten
134
- end
128
+ def get_categories
129
+ return [] unless @subtitle
130
+ cat = /(\\?\[([^\[]+?)\\?\])+/.match(@subtitle).to_a[0]
131
+ return [] unless cat
132
+ cat.scan(/\\?\[(.*?)\\?\]/).collect do |c|
133
+ c[0].split(/,/)
134
+ end.flatten
135
+ end
135
136
 
136
- def strip_subtitle
137
- return nil unless @subtitle
138
- r = @subtitle.sub(/^((\\?\[[^\[]+?\]\\?)+\s+)?/, '')
139
- if r.empty?
140
- nil
141
- else
142
- r
137
+ def strip_subtitle
138
+ return nil unless @subtitle
139
+ r = @subtitle.sub(/^((\\?\[[^\[]+?\]\\?)+\s+)?/, '')
140
+ if r.empty?
141
+ nil
142
+ else
143
+ r
144
+ end
143
145
  end
144
146
  end
145
- end
146
147
 
147
- class GfmDiary
148
- include DiaryBase
149
- include CategorizableDiary
148
+ class GfmDiary
149
+ include BaseDiary
150
+ include CategorizableDiary
150
151
 
151
- def initialize(date, title, body, modified = Time.now)
152
- init_diary
153
- replace( date, title, body )
154
- @last_modified = modified
155
- end
152
+ def initialize(date, title, body, modified = Time.now)
153
+ init_diary
154
+ replace( date, title, body )
155
+ @last_modified = modified
156
+ end
156
157
 
157
- def style
158
- 'GFM'
159
- end
158
+ def style
159
+ 'GFM'
160
+ end
160
161
 
161
- def append(body, author = nil)
162
- section = nil
163
- body.each_line do |l|
164
- case l
165
- when /^\#[^\#]/
166
- @sections << GfmSection.new(section, author) if section
167
- section = l
168
- else
169
- section = '' unless section
170
- section << l
162
+ def append(body, author = nil)
163
+ section = nil
164
+ body.each_line do |l|
165
+ case l
166
+ when /^\#[^\#]/
167
+ @sections << GfmSection.new(section, author) if section
168
+ section = l
169
+ else
170
+ section = '' unless section
171
+ section << l
172
+ end
171
173
  end
174
+ if section
175
+ section << "\n" unless section =~ /\n\n\z/
176
+ @sections << GfmSection.new(section, author)
177
+ end
178
+ @last_modified = Time.now
179
+ self
172
180
  end
173
- if section
174
- section << "\n" unless section =~ /\n\n\z/
175
- @sections << GfmSection.new(section, author)
176
- end
177
- @last_modified = Time.now
178
- self
179
- end
180
181
 
181
- def add_section(subtitle, body)
182
- @sections = GfmSection.new("\# #{subtitle}\n\n#{body}")
183
- @sections.size
182
+ def add_section(subtitle, body)
183
+ @sections = GfmSection.new("\# #{subtitle}\n\n#{body}")
184
+ @sections.size
185
+ end
184
186
  end
185
187
  end
186
188
  end
@@ -55,11 +55,11 @@ module RD
55
55
 
56
56
  def apply_to_DescListItem(element, term, description)
57
57
  %Q[<dt>#{term.join}</dt>] +
58
- if description.empty? then
59
- "\n"
60
- else
61
- %Q[\n<dd>\n#{description.join("\n").chomp}\n</dd>]
62
- end
58
+ if description.empty? then
59
+ "\n"
60
+ else
61
+ %Q[\n<dd>\n#{description.join("\n").chomp}\n</dd>]
62
+ end
63
63
  end
64
64
 
65
65
  def apply_to_MethodList(element, items)
@@ -129,7 +129,7 @@ module RD
129
129
 
130
130
  if cat
131
131
  r =
132
- cat.scan(/\[(.*?)\]/).collect do |c|
132
+ cat.scan(/\[(.*?)\]/).collect do |c|
133
133
  %Q|<%= category_anchor("#{c[0]}") %>|
134
134
  end.join + subtitle
135
135
  else
@@ -187,173 +187,175 @@ MSG
187
187
  end
188
188
 
189
189
  module TDiary
190
- class RDSection
191
- include RD
192
-
193
- attr_reader :author, :categories, :subtitle, :stripped_subtitle
194
- attr_reader :body_to_html, :subtitle_to_html, :stripped_subtitle_to_html
195
-
196
- def initialize( fragment, author = nil )
197
- @author = author
198
- if /\A=(?!=)/ =~ fragment then
199
- @subtitle, @body = fragment.split( /\n/, 2 )
200
- @subtitle.sub!( /^\=\s*/, '' )
201
- else
202
- @subtitle = nil
203
- @body = fragment.dup
190
+ module Style
191
+ class RDSection
192
+ include RD
193
+
194
+ attr_reader :author, :categories, :subtitle, :stripped_subtitle
195
+ attr_reader :body_to_html, :subtitle_to_html, :stripped_subtitle_to_html
196
+
197
+ def initialize( fragment, author = nil )
198
+ @author = author
199
+ if /\A=(?!=)/ =~ fragment then
200
+ @subtitle, @body = fragment.split( /\n/, 2 )
201
+ @subtitle.sub!( /^\=\s*/, '' )
202
+ else
203
+ @subtitle = nil
204
+ @body = fragment.dup
205
+ end
206
+ @body = @body || ''
207
+ @body.sub!( /[\n\r]+\Z/, '' )
208
+ @body << "\n\n"
209
+
210
+ @categories = get_categories
211
+ @stripped_subtitle = strip_subtitle
212
+
213
+ @subtitle_to_html = manufacture(@subtitle, true)
214
+ @stripped_subtitle_to_html = manufacture(@stripped_subtitle, true)
215
+ @body_to_html = manufacture(@body, false)
216
+ end
217
+
218
+ def subtitle=(subtitle)
219
+ cat_str = ""
220
+ @categories.each {|cat|
221
+ cat_str << "[#{cat}]"
222
+ }
223
+ cat_str << " " unless cat_str.empty?
224
+ @subtitle = subtitle ? (cat_str + subtitle) : nil
225
+ @stripped_subtitle = strip_subtitle
226
+ end
227
+
228
+ def body=(str)
229
+ @body = str
230
+ end
231
+
232
+ def body
233
+ @body.dup
234
+ end
235
+
236
+ def categories=(categories)
237
+ @categories = categories
238
+ cat_str = ""
239
+ categories.each {|cat|
240
+ cat_str << "[#{cat}]"
241
+ }
242
+ cat_str << " " unless cat_str.empty?
243
+ @subtitle = @subtitle ? (cat_str + @stripped_subtitle) : nil
244
+ @stripped_subtitle = strip_subtitle
204
245
  end
205
- @body = @body || ''
206
- @body.sub!( /[\n\r]+\Z/, '' )
207
- @body << "\n\n"
208
246
 
209
- @categories = get_categories
210
- @stripped_subtitle = strip_subtitle
247
+ def to_src
248
+ r = ''
249
+ r << "= #{@subtitle}\n" if @subtitle
250
+ r << @body
251
+ end
211
252
 
212
- @subtitle_to_html = manufacture(@subtitle, true)
213
- @stripped_subtitle_to_html = manufacture(@stripped_subtitle, true)
214
- @body_to_html = manufacture(@body, false)
215
- end
216
-
217
- def subtitle=(subtitle)
218
- cat_str = ""
219
- @categories.each {|cat|
220
- cat_str << "[#{cat}]"
221
- }
222
- cat_str << " " unless cat_str.empty?
223
- @subtitle = subtitle ? (cat_str + subtitle) : nil
224
- @stripped_subtitle = strip_subtitle
225
- end
226
-
227
- def body=(str)
228
- @body = str
229
- end
230
-
231
- def body
232
- @body.dup
233
- end
234
-
235
- def categories=(categories)
236
- @categories = categories
237
- cat_str = ""
238
- categories.each {|cat|
239
- cat_str << "[#{cat}]"
240
- }
241
- cat_str << " " unless cat_str.empty?
242
- @subtitle = @subtitle ? (cat_str + @stripped_subtitle) : nil
243
- @stripped_subtitle = strip_subtitle
244
- end
253
+ def html( date, idx, opt, mode = :HTML)
254
+ if mode == :CHTML
255
+ visitor = RD2tDiaryCHTMLVistor.new( date, idx, opt, @author)
256
+ section_open = "<%=section_enter_proc( Time::at( #{date.to_i} ))%>\n"
257
+ section_close = "<%=section_leave_proc( Time::at( #{date.to_i} ))%>\n"
258
+ else
259
+ visitor = RD2tDiaryVisitor.new( date, idx, opt, @author )
260
+ section_open = %Q[<div class="section">\n<%=section_enter_proc( Time::at( #{date.to_i} ))%>\n]
261
+ section_close = "<%=section_leave_proc( Time::at( #{date.to_i} ))%>\n</div>\n"
262
+ end
245
263
 
246
- def to_src
247
- r = ''
248
- r << "= #{@subtitle}\n" if @subtitle
249
- r << @body
250
- end
264
+ src = to_src.split(/^/)
265
+ src.unshift("=begin\n").push("=end\n")
266
+ tree = RDTree.new( src, nil, nil)
267
+ begin
268
+ tree.parse
269
+ rescue ParseError
270
+ raise SyntaxError, $!.message
271
+ end
251
272
 
252
- def html( date, idx, opt, mode = :HTML)
253
- if mode == :CHTML
254
- visitor = RD2tDiaryCHTMLVistor.new( date, idx, opt, @author)
255
- section_open = "<%=section_enter_proc( Time::at( #{date.to_i} ))%>\n"
256
- section_close = "<%=section_leave_proc( Time::at( #{date.to_i} ))%>\n"
257
- else
258
- visitor = RD2tDiaryVisitor.new( date, idx, opt, @author )
259
- section_open = %Q[<div class="section">\n<%=section_enter_proc( Time::at( #{date.to_i} ))%>\n]
260
- section_close = "<%=section_leave_proc( Time::at( #{date.to_i} ))%>\n</div>\n"
273
+ r = "#{section_open}#{visitor.visit( tree )}#{section_close}"
261
274
  end
262
275
 
263
- src = to_src.split(/^/)
264
- src.unshift("=begin\n").push("=end\n")
265
- tree = RDTree.new( src, nil, nil)
266
- begin
267
- tree.parse
268
- rescue ParseError
269
- raise SyntaxError, $!.message
276
+ private
277
+ def manufacture(str, subtitle = false)
278
+ return nil unless str
279
+ src = str.strip.split(/^/).unshift("=begin\n").push("=end\n")
280
+ visitor = RD2tDiaryVisitor.new
281
+ tree = RDTree.new(src, nil, nil)
282
+ begin
283
+ r = visitor.visit( tree.parse )
284
+ r.gsub!(/<\/?p>/, '') if subtitle
285
+ r
286
+ rescue ParseError
287
+ str
288
+ end
270
289
  end
271
290
 
272
- r = "#{section_open}#{visitor.visit( tree )}#{section_close}"
273
- end
274
-
275
- private
276
- def manufacture(str, subtitle = false)
277
- return nil unless str
278
- src = str.strip.split(/^/).unshift("=begin\n").push("=end\n")
279
- visitor = RD2tDiaryVisitor.new
280
- tree = RDTree.new(src, nil, nil)
281
- begin
282
- r = visitor.visit( tree.parse )
283
- r.gsub!(/<\/?p>/, '') if subtitle
284
- r
285
- rescue ParseError
286
- str
291
+ def get_categories
292
+ return [] unless @subtitle
293
+ cat = /^(\[(.*?)\])+/.match(@subtitle).to_a[0]
294
+ return [] unless cat
295
+ cat.scan(/\[(.*?)\]/).collect do |c|
296
+ c[0].split(/,/)
297
+ end.flatten
287
298
  end
288
- end
289
-
290
- def get_categories
291
- return [] unless @subtitle
292
- cat = /^(\[(.*?)\])+/.match(@subtitle).to_a[0]
293
- return [] unless cat
294
- cat.scan(/\[(.*?)\]/).collect do |c|
295
- c[0].split(/,/)
296
- end.flatten
297
- end
298
299
 
299
- def strip_subtitle
300
- return nil unless @subtitle
301
- @subtitle.sub(/^(\[(.*?)\])+/,'')
300
+ def strip_subtitle
301
+ return nil unless @subtitle
302
+ @subtitle.sub(/^(\[(.*?)\])+/,'')
303
+ end
302
304
  end
303
- end
304
305
 
305
- class RdDiary
306
- include DiaryBase
307
- include CategorizableDiary
308
-
309
- def initialize( date, title, body, modified = Time::now )
310
- init_diary
311
- replace( date, title, body )
312
- @last_modified = modified
313
- end
314
-
315
- def style
316
- 'RD'
317
- end
318
-
319
- def append( body, author = nil )
320
- section = nil
321
- body.lines.each do |l|
322
- case l
323
- when /^=(begin|end)\b/
324
- # do nothing
325
- when /^=[^=]/
326
- @sections << RDSection::new( section, author ) if section
327
- section = l
328
- else
329
- section = '' unless section
330
- section << l
306
+ class RdDiary
307
+ include BaseDiary
308
+ include CategorizableDiary
309
+
310
+ def initialize( date, title, body, modified = Time::now )
311
+ init_diary
312
+ replace( date, title, body )
313
+ @last_modified = modified
314
+ end
315
+
316
+ def style
317
+ 'RD'
318
+ end
319
+
320
+ def append( body, author = nil )
321
+ section = nil
322
+ body.lines.each do |l|
323
+ case l
324
+ when /^=(begin|end)\b/
325
+ # do nothing
326
+ when /^=[^=]/
327
+ @sections << RDSection::new( section, author ) if section
328
+ section = l
329
+ else
330
+ section = '' unless section
331
+ section << l
332
+ end
331
333
  end
334
+ @sections << RDSection::new( section, author ) if section
335
+ @last_modified = Time::now
336
+ self
332
337
  end
333
- @sections << RDSection::new( section, author ) if section
334
- @last_modified = Time::now
335
- self
336
- end
337
-
338
- def add_section(subtitle, body)
339
- sec = RDSection::new("\n")
340
- sec.subtitle = subtitle
341
- sec.body = body
342
- @sections << sec
343
- @sections.size
344
- end
345
-
346
- def to_html( opt = {}, mode = :HTML )
347
- r = ''
348
- idx = 1
349
- each_section do |section|
350
- r << section.html( date, idx, opt, mode )
351
- idx += 1
338
+
339
+ def add_section(subtitle, body)
340
+ sec = RDSection::new("\n")
341
+ sec.subtitle = subtitle
342
+ sec.body = body
343
+ @sections << sec
344
+ @sections.size
345
+ end
346
+
347
+ def to_html( opt = {}, mode = :HTML )
348
+ r = ''
349
+ idx = 1
350
+ each_section do |section|
351
+ r << section.html( date, idx, opt, mode )
352
+ idx += 1
353
+ end
354
+ return r
352
355
  end
353
- return r
354
- end
355
356
 
356
- undef :to_html4, :to_chtml
357
+ undef :to_html4, :to_chtml
358
+ end
357
359
  end
358
360
  end
359
361
 
@@ -363,4 +365,3 @@ end
363
365
  # tab-width: 3
364
366
  # ruby-indent-level: 3
365
367
  # End:
366
-