tdiary 4.0.1.20130903 → 4.0.1.20130929

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,195 +0,0 @@
1
- # -*- coding: utf-8; -*-
2
- #
3
- # gfm_style.rb: "GitHub Flavored Markdown" (GFM) style for tDiary 2.x format.
4
- #
5
- # if you want to use this style, add @style into tdiary.conf below:
6
- #
7
- # @style = 'GFM'
8
- #
9
- # Copyright (C) 2003, TADA Tadashi <sho@spc.gr.jp>
10
- # Copyright (C) 2004, MoonWolf <moonwolf@moonwolf.com>
11
- # Copyright (C) 2012, kdmsnr <kdmsnr@gmail.com>
12
- # Copyright (C) 2013, hsbt <shibata.hiroshi@gmail.com>
13
- # You can distribute this under GPL.
14
- #
15
-
16
- require 'redcarpet'
17
- require 'pygments'
18
- require 'twitter-text'
19
-
20
- class HTMLwithPygments < Redcarpet::Render::HTML
21
- def block_code(code, language)
22
- Pygments.highlight(code, :lexer => language)
23
- rescue Exception
24
- <<-HTML
25
- <div class="highlight"><pre>#{CGI.escapeHTML(code)}</pre></div>
26
- HTML
27
- end
28
- end
29
-
30
- module TDiary
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
49
-
50
- def subtitle=(subtitle)
51
- @subtitle = (subtitle || '').sub(/^# /,"\##{categories_to_string} ")
52
- @strip_subtitle = strip_subtitle
53
- end
54
-
55
- def categories=(categories)
56
- @subtitle = "#{categories_to_string} " + (strip_subtitle || '')
57
- @strip_subtitle = strip_subtitle
58
- end
59
-
60
- def to_src
61
- r = ''
62
- r << "\# #{@subtitle}\n" if @subtitle
63
- r << @body
64
- end
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
76
- end
77
-
78
- private
79
-
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)
84
-
85
- # Twitter Autolink
86
- r = auto_link(r)
87
-
88
- if r =~ /(<pre>|<code>)/
89
- r.gsub!(/<a class=\"tweet-url username\" href=\".*?\">(.*?)<\/a>/){ $1 }
90
- end
91
-
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
97
-
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
108
-
109
- # emoji
110
- r = r.emojify
111
-
112
- # diary anchor
113
- r.gsub!(/<h(\d)/) { "<h#{$1.to_i + 2}" }
114
- r.gsub!(/<\/h(\d)/) { "</h#{$1.to_i + 2}" }
115
-
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
- }
124
-
125
- r
126
- end
127
-
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
136
-
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
145
- end
146
- end
147
-
148
- class GfmDiary
149
- include BaseDiary
150
- include CategorizableDiary
151
-
152
- def initialize(date, title, body, modified = Time.now)
153
- init_diary
154
- replace( date, title, body )
155
- @last_modified = modified
156
- end
157
-
158
- def style
159
- 'GFM'
160
- end
161
-
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
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
180
- end
181
-
182
- def add_section(subtitle, body)
183
- @sections = GfmSection.new("\# #{subtitle}\n\n#{body}")
184
- @sections.size
185
- end
186
- end
187
- end
188
- end
189
-
190
- # Local Variables:
191
- # mode: ruby
192
- # indent-tabs-mode: t
193
- # tab-width: 3
194
- # ruby-indent-level: 3
195
- # End:
@@ -1,71 +0,0 @@
1
- =begin
2
- = RDスタイル
3
- == RDスタイルとは
4
- Ruby 関連のドキュメントを書くために考案された汎用のドキュメントフォーマット
5
- RD風の書式で日記を記述できます。
6
-
7
- * ((<What is RD? What is RDtool?|URL:http://www2.pos.to/~tosh/ruby/rdtool/ja/whats.html>))
8
-
9
- == RDスタイルを使うために
10
- RDスタイルを使うには RDtool がインストールされている必要があります。 RDtool
11
- は RAA((-Ruby Application Archive-)) から入手できます。
12
-
13
- ((<RDtool|URL:http://raa.ruby-lang.org/list.rhtml?name=rdtool>))
14
-
15
- rd_style.rb をインストールディレクトリにあるtdiary ディレクトリ (tdiary_style.rbのあるディレクトリ) にコピーし、tdiary.confに以下の設定をします。
16
-
17
- @style = 'RD'
18
-
19
- 必須ではありませんが、プラグイン集に含まれる a.rb をインストールすると RD のリファレンス風にリンクを記述できます。便利なプラグインなのでインストールすることを勧めます。記述例は後述の URL を参照してください。
20
-
21
- == RD との違い
22
- === Headline element
23
- '=' 1つが <h3> になりセクションの開始になります。'==' が <h4>、'===' が <h5>、
24
- '====' が <h6> となります。'+'、'++' は使用しません。
25
-
26
- === MethodList
27
- (('---')) に HTML のタグを与えると、HTML のブロック要素に変換されます。(('---'))に何も与えない場合は <hr> へ変換されます。
28
-
29
- --- <hr>
30
- ---
31
- --- <blockquote>
32
- この部分は引用です。
33
-
34
- === Inline element
35
- ==== プラグイン
36
- 本来はキーボード入力を表す (('((% %))')) が tDiary のプラグインになります。
37
-
38
- ((%amazon '4906470068'%))
39
-
40
- ==== 生HTML
41
- (('((: :))')) で囲むと生の HTML を記述できます。
42
-
43
- ((:<del>この部分は取り消し</de>:))
44
-
45
- ==== URL
46
- URL 以外に画像、RAA、Ruby関連のメーリングリストへのリンクが RWiki
47
- ((-書式にRDを使用するWikiクローン-)) 同様、簡単に記述できるようになっていま
48
- す。
49
-
50
- ((<テスト画像|IMG:test.jpg>))
51
- ((<RAA:tdiary>))
52
- ((<ruby-list:0001>))
53
-
54
- 上記以外の形式は myプラグイン((-tDiary 標準なので何もせずに使えます-))と aプラグイン((-インストールしなくてもRDスタイルは使えますが、便利ですのでインストールすることをお勧めします-))へ展開されます。それにより自分の日記へのリンクや aプラグインの辞書へ登録されている URL へのリンクが RD の reference風に記述できます。
55
- aプラグインの使い方はプラグイン附属のドキュメントを参照してください。
56
-
57
- myプラグインに展開されるリンク
58
-
59
- ((<そのツッコミ|20030308#c01>))
60
-
61
- aプラグインに展開されるリンク
62
-
63
- ((<key>))
64
- ((<key:20020329.html>))
65
- ((<こちら|key:20020329.html>))
66
-
67
- === その他
68
- '=begin', '=end' は必要ありません。
69
-
70
- =end
71
-
@@ -1,367 +0,0 @@
1
- # -*- coding: utf-8; -*-
2
- #
3
- # rd_style.rb: RD style for tDiary 2.x format. $Revision: 1.32 $
4
- # based on Wiki style which Copyright belongs to TADA Tadashi.
5
- #
6
- # if you want to use this style, install RDtool
7
- # and add @style into tdiary.conf below:
8
- #
9
- # @style = 'RD'
10
- #
11
- # about RDtool: http://raa.ruby-lang.org/list.rhtml?name=rdtool
12
- #
13
- # ref_extension codes come from rd2html-ext
14
- # http://raa.ruby-lang.org/list.rhtml?name=rd2html-ext
15
- #
16
- # Copyright (C) 2003, UECHI Yasumasa <uechi@potaway.net>
17
- # You can distribute this under GPL.
18
- #
19
- require 'rd/rdfmt'
20
- require 'rd/rd2html-lib'
21
-
22
- module RD
23
- TDIARY_BASE_LEVEL = 2
24
-
25
- class RD2tDiaryVisitor < RD2HTMLVisitor
26
- def initialize( date=nil, idx=nil, opt=nil, author=nil )
27
- @td_date = date
28
- @td_idx = idx
29
- @td_opt = opt
30
- @td_author = author
31
- super()
32
- end
33
-
34
- def apply_to_DocumentElement(element, content)
35
- ret = ""
36
- ret << html_body(content) + "\n"
37
- ret
38
- end
39
-
40
- def html_body(contents)
41
- content = contents.join("\n")
42
- end
43
- private :html_body
44
-
45
- def apply_to_Headline(element, title)
46
- level = element.level + TDIARY_BASE_LEVEL
47
- title = title.join
48
- if level == 3
49
- r = %Q[<h#{level}><%= subtitle_proc( Time::at( #{@td_date.to_i} ), #{title.to_s.dump.gsub( /%/, '\\\\045' )} ) %></h#{level}>]
50
- else
51
- r = %Q[<h#{level}>#{title}</h#{level}>]
52
- end
53
- r
54
- end
55
-
56
- def apply_to_DescListItem(element, term, description)
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
63
- end
64
-
65
- def apply_to_MethodList(element, items)
66
- if /^(<.+>)?$/ =~ element.items[0].term.to_label
67
- %Q[#{items.join("\n").chomp}\n]
68
- else
69
- %Q[<dl>\n#{items.join("\n").chomp}\n</dl>]
70
- end
71
- end
72
-
73
- def apply_to_MethodListItem(element, term, description)
74
- case term
75
- when /^&lt;([^\s]+)\s*.*&gt;/
76
- closetag = "</#{CGI.unescapeHTML($1)}>"
77
- r = CGI.unescapeHTML(term)
78
- if description.size > 0
79
- r << %Q[\n#{description.join("\n")}\n]
80
- r << closetag
81
- end
82
- r
83
- when ''
84
- "<hr>"
85
- else
86
- super
87
- end
88
- end
89
-
90
- # use for tDiary plugin :-p
91
- def apply_to_Keyboard(element, content)
92
- plugin, args = CGI.unescapeHTML(content.join("")).split(/\s+/, 2)
93
- %Q[<%=#{plugin} #{args}%>]
94
- end
95
-
96
- # use for native html
97
- def apply_to_Index(element, content)
98
- CGI.unescapeHTML(content.join)
99
- end
100
-
101
- def apply_to_Footnote(element, content)
102
- escaped_content = content.join.gsub( /%>/, "%%>" )
103
- %Q|<%=fn apply_plugin( #{escaped_content.dump} ) %>|
104
- end
105
-
106
- def apply_to_RefToElement(element, content)
107
- label = element.to_label
108
- key, opt = label.split(/:/, 2)
109
-
110
- case key
111
- when "IMG"
112
- ref_ext_IMG(label, content.join, opt)
113
- when "RAA"
114
- ref_ext_RAA(label, content.join, opt)
115
- when /^ruby-(talk|list|dev|math|ext|core)$/
116
- ref_ext_RubyML(label, content.join, key, opt)
117
- when /^(\d{4}|\d{6}|\d{8}|\d{8}-\d+)[^\d]*?#?([pct]\d+)?$/
118
- %Q[<%=my "#{key}","#{content.join}"%>]
119
- else
120
- opt = "" unless opt # case of no ":"
121
- %Q[<%=a "#{key}","#{opt}","#{content.join}"%>]
122
- end
123
- end
124
-
125
- private
126
- def categorized_subtitle( title )
127
- cat = /^(\[(.*?)\])+/.match(title.to_s).to_a[0]
128
- subtitle = $'
129
-
130
- if cat
131
- r =
132
- cat.scan(/\[(.*?)\]/).collect do |c|
133
- %Q|<%= category_anchor("#{c[0]}") %>|
134
- end.join + subtitle
135
- else
136
- r = title.to_s
137
- end
138
- r
139
- end
140
-
141
- def ref_ext_RubyML(label, content, ml, article)
142
- article.sub!(/^0+/, '')
143
- content = "[#{label}]" if label == content
144
-
145
- %Q[<a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/#{ ml }/#{ article }">#{ content }</a>]
146
- end
147
-
148
- def ref_ext_RAA(label, content, name)
149
- name = CGI.escape(name)
150
- content = "[#{label}]" if label == content
151
- %Q[<a href="http://raa.ruby-lang.org/list.rhtml?name=#{ name }">#{ content }</a>]
152
- end
153
-
154
- def ref_ext_IMG(label, content, src)
155
- label.to_s == content.to_s and content = src
156
- %Q[<img src="#{src}" alt="#{content}">]
157
- end
158
- end
159
-
160
- class RD2tDiaryCHTMLVistor < RD2tDiaryVisitor
161
- def apply_to_Headline(element, title)
162
- level = element.level + TDIARY_BASE_LEVEL
163
- title = title.join
164
- if level == 3
165
- r = %Q[<H#{level}><%= subtitle_proc( Time::at( #{@td_date.to_i} ), #{title.to_s.dump.gsub( /%/, '\\\\045' )} ) %></H#{level}>]
166
- else
167
- r = %Q[<H#{level}>#{title}</H#{level}>]
168
- end
169
- r
170
- end
171
- end
172
-
173
- class RDInlineParser
174
- def on_error(et, ev, values)
175
- lines_of_rest = @src.rest.lines.to_a.length
176
- prev_words = prev_words_on_error(ev)
177
- at = 4 + prev_words.length
178
- message = <<-MSG
179
- RD syntax error: line #{@blockp.line_index - lines_of_rest - 1}:
180
- ...#{prev_words} #{(ev||'')} #{next_words_on_error()} ...
181
- MSG
182
- message << " " * at + "^" * (ev ? ev.length : 0) + "\n"
183
- raise ParseError, message
184
- end
185
- end
186
-
187
- end
188
-
189
- module TDiary
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
245
- end
246
-
247
- def to_src
248
- r = ''
249
- r << "= #{@subtitle}\n" if @subtitle
250
- r << @body
251
- end
252
-
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
263
-
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
272
-
273
- r = "#{section_open}#{visitor.visit( tree )}#{section_close}"
274
- end
275
-
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
289
- end
290
-
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
298
- end
299
-
300
- def strip_subtitle
301
- return nil unless @subtitle
302
- @subtitle.sub(/^(\[(.*?)\])+/,'')
303
- end
304
- end
305
-
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
333
- end
334
- @sections << RDSection::new( section, author ) if section
335
- @last_modified = Time::now
336
- self
337
- end
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
355
- end
356
-
357
- undef :to_html4, :to_chtml
358
- end
359
- end
360
- end
361
-
362
- # Local Variables:
363
- # mode: ruby
364
- # indent-tabs-mode: t
365
- # tab-width: 3
366
- # ruby-indent-level: 3
367
- # End: