tdiary-style-gfm 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e50242d3e4c9de5a6471cd39305d19ecc63db7b3
4
- data.tar.gz: 7581983b9fab9eee31fd720f5f0b3cca84cce960
3
+ metadata.gz: 50018890677590a304c7094e1bd1fbe484f31d5d
4
+ data.tar.gz: c4d004346168e24cb8069d4378bb1ab2cf22aaa3
5
5
  SHA512:
6
- metadata.gz: 4db97817b2b3d1cf8142598fb6726e37a1cccf7c1e7f58c3878b4289c2b3a85e2ab2c7c11a5d72c88506143d6c53a39275df852d85e2f7da4456f50eb1834011
7
- data.tar.gz: 34b54f4903d6675fbf27e15228b46289b0305594a52bb1fd4b90473e4e141a9b026dbb1bee7fbbcfe9bfe326ce83a05f9c9a3f95088b4e41f6bb5090da7a0af3
6
+ metadata.gz: eaf9f2404e1f8fd4513fb787afbb06436a5983817cc2909bdfa03b2fd62e2c53d4a47a016d5486dda3e60189b8a1472a3e8f0ef2a83cb52c1d33b91926a7c02f
7
+ data.tar.gz: 395925663a461a320025b993767afaf3c5071d3207b1baa0a4a68981c57b7624c0a65a881d12fe01f2c9434bdd68b5cc7800459c216d2eb1c9a8f9a6310ca0d5
@@ -52,9 +52,11 @@ module TDiary
52
52
  private
53
53
 
54
54
  def to_html(string)
55
+ r = string.dup
56
+
55
57
  # 1. Stash plugin calls
56
58
  plugin_stashes = []
57
- r = string.gsub(/\{\{(.*?)\}\}/) do
59
+ r.gsub!(/\{\{(.*?)\}\}/) do
58
60
  # Convert `{{ }}' to erb tags
59
61
  plugin_stashes.push("<%=#{$1}%>")
60
62
  "@@tdiary_style_gfm_plugin#{plugin_stashes.length - 1}@@"
@@ -63,21 +65,27 @@ module TDiary
63
65
  # 2. Apply markdown conversion
64
66
  r = GitHub::Markdown.to_html(r, :gfm) do |code, lang|
65
67
  begin
66
- Pygments.highlight(code, :lexer => lang)
68
+ Pygments.highlight(code, lexer: lang)
67
69
  rescue Exception => ex
68
70
  "<div class=\"highlight\"><pre>#{CGI.escapeHTML(code)}</pre></div>"
69
71
  end
70
72
  end
71
73
 
72
- # 3. Stash <pre> tags
74
+ # 3. Stash <pre> and <code> tags
73
75
  pre_tag_stashes = []
74
- r.gsub!(/<pre>(.*?)<\/pre>/) do |matched|
76
+ r.gsub!(/<pre(.*?)<\/pre>/m) do |matched|
75
77
  pre_tag_stashes.push(matched)
76
78
  "@@tdiary_style_gfm_pre_tag#{pre_tag_stashes.length - 1}@@"
77
79
  end
78
80
 
81
+ code_tag_stashes = []
82
+ r.gsub!(/<code(.*?)<\/code>/m) do |matched|
83
+ code_tag_stashes.push(matched)
84
+ "@@tdiary_style_gfm_code_tag#{code_tag_stashes.length - 1}@@"
85
+ end
86
+
79
87
  # 4. Convert miscellaneous
80
- unless r =~ /(<pre>|<code>)/
88
+ if pre_tag_stashes.none? && code_tag_stashes.none?
81
89
  r = Twitter::Autolink.auto_link_usernames_or_lists(r)
82
90
  end
83
91
 
@@ -96,10 +104,13 @@ module TDiary
96
104
  end
97
105
  }
98
106
 
99
- # 5. Unstash pre and plugin
107
+ # 5. Unstash <pre>, <code> and plugin call
100
108
  pre_tag_stashes.each.with_index do |str, i|
101
109
  r.sub!(/@@tdiary_style_gfm_pre_tag#{i}@@/, str)
102
110
  end
111
+ code_tag_stashes.each.with_index do |str, i|
112
+ r.sub!(/@@tdiary_style_gfm_code_tag#{i}@@/, str)
113
+ end
103
114
  plugin_stashes.each.with_index do |str, i|
104
115
  r.sub!(/@@tdiary_style_gfm_plugin#{i}@@/, str)
105
116
  end
@@ -139,12 +150,20 @@ module TDiary
139
150
  end
140
151
 
141
152
  def append(body, author = nil)
153
+ in_code_block = false
142
154
  section = nil
143
155
  body.each_line do |l|
144
156
  case l
145
157
  when /^\#[^\#]/
146
- @sections << GfmSection.new(section, author) if section
147
- section = l
158
+ if in_code_block
159
+ section << l
160
+ else
161
+ @sections << GfmSection.new(section, author) if section
162
+ section = l
163
+ end
164
+ when /^```/
165
+ in_code_block = !in_code_block
166
+ section << l
148
167
  else
149
168
  section = '' unless section
150
169
  section << l
@@ -1,7 +1,7 @@
1
1
  module TDiary
2
2
  module Style
3
3
  module Gfm
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
6
6
  end
7
7
  end
@@ -15,6 +15,10 @@ honbun
15
15
  ## subTitleH4
16
16
  honbun
17
17
 
18
+ ```
19
+ # comment in code block
20
+ ```
21
+
18
22
  EOF
19
23
  @diary.append(@source)
20
24
  end
@@ -30,6 +34,9 @@ honbun
30
34
  <h4>subTitleH4</h4>
31
35
 
32
36
  <p>honbun</p>
37
+
38
+ <pre><code># comment in code block
39
+ </code></pre>
33
40
  <%=section_leave_proc( Time.at( 1041346800 ) )%>
34
41
  </div>
35
42
  EOF
@@ -47,6 +54,9 @@ honbun
47
54
  <h4>subTitleH4</h4>
48
55
 
49
56
  <p>honbun</p>
57
+
58
+ <pre><code># comment in code block
59
+ </code></pre>
50
60
  <%=section_leave_proc( Time.at( 1041346800 ) )%>
51
61
  EOF
52
62
  end
@@ -349,25 +359,169 @@ http://example.com is example.com
349
359
  it { @diary.to_html.should eq @html }
350
360
  end
351
361
 
352
- describe 'emoji' do
353
- before do
354
- source = <<-'EOF'
362
+ context 'twitter username' do
363
+ describe 'in plain context' do
364
+ before do
365
+ source = <<-'EOF'
366
+ # subTitle
367
+
368
+ @a_matsuda is amatsuda
369
+ EOF
370
+ @diary.append(source)
371
+
372
+ @html = <<-'EOF'
373
+ <div class="section">
374
+ <%=section_enter_proc( Time.at( 1041346800 ) )%>
375
+ <h3><%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %></h3>
376
+ <p>@<a class="tweet-url username" href="https://twitter.com/a_matsuda" rel="nofollow">a_matsuda</a> is amatsuda</p>
377
+ <%=section_leave_proc( Time.at( 1041346800 ) )%>
378
+ </div>
379
+ EOF
380
+ end
381
+ it { @diary.to_html.should eq @html }
382
+ end
383
+
384
+ describe 'with <pre>' do
385
+ before do
386
+ source = <<-'EOF'
387
+ # subTitle
388
+
389
+ p :some_code
390
+
391
+ @a_matsuda is amatsuda
392
+ EOF
393
+ @diary.append(source)
394
+
395
+ @html = <<-'EOF'
396
+ <div class="section">
397
+ <%=section_enter_proc( Time.at( 1041346800 ) )%>
398
+ <h3><%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %></h3>
399
+ <pre><code>p :some_code
400
+ </code></pre>
401
+
402
+ <p>@a_matsuda is amatsuda</p>
403
+ <%=section_leave_proc( Time.at( 1041346800 ) )%>
404
+ </div>
405
+ EOF
406
+ end
407
+ it { @diary.to_html.should eq @html }
408
+ end
409
+
410
+ describe 'with <code>' do
411
+ before do
412
+ source = <<-'EOF'
413
+ # subTitle
414
+
415
+ `:some_code`
416
+
417
+ @a_matsuda is amatsuda
418
+ EOF
419
+ @diary.append(source)
420
+
421
+ @html = <<-'EOF'
422
+ <div class="section">
423
+ <%=section_enter_proc( Time.at( 1041346800 ) )%>
424
+ <h3><%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %></h3>
425
+ <p><code>:some_code</code></p>
426
+
427
+ <p>@a_matsuda is amatsuda</p>
428
+ <%=section_leave_proc( Time.at( 1041346800 ) )%>
429
+ </div>
430
+ EOF
431
+ end
432
+ it { @diary.to_html.should eq @html }
433
+ end
434
+ end
435
+
436
+ context 'emoji' do
437
+ describe 'in plain context' do
438
+ before do
439
+ source = <<-'EOF'
355
440
  # subTitle
356
441
 
357
442
  :sushi: は美味しい
358
- EOF
359
- @diary.append(source)
443
+ EOF
444
+ @diary.append(source)
360
445
 
361
- @html = <<-'EOF'
446
+ @html = <<-'EOF'
362
447
  <div class="section">
363
448
  <%=section_enter_proc( Time.at( 1041346800 ) )%>
364
449
  <h3><%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %></h3>
365
450
  <p><img src='http://www.emoji-cheat-sheet.com/graphics/emojis/sushi.png' width='20' height='20' title='sushi' alt='sushi' class='emoji' /> は美味しい</p>
366
451
  <%=section_leave_proc( Time.at( 1041346800 ) )%>
367
452
  </div>
368
- EOF
453
+ EOF
454
+ end
455
+ it { @diary.to_html.should eq @html }
456
+ end
457
+
458
+ describe 'in (multiline) <pre>' do
459
+ before do
460
+ source = <<-'EOF'
461
+ # subTitle
462
+
463
+ ```
464
+ :sushi: は
465
+ 美味しい
466
+ ```
467
+ EOF
468
+ @diary.append(source)
469
+
470
+ @html = <<-'EOF'
471
+ <div class="section">
472
+ <%=section_enter_proc( Time.at( 1041346800 ) )%>
473
+ <h3><%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %></h3>
474
+ <pre><code>:sushi: は
475
+ 美味しい
476
+ </code></pre>
477
+ <%=section_leave_proc( Time.at( 1041346800 ) )%>
478
+ </div>
479
+ EOF
480
+ end
481
+ it { @diary.to_html.should eq @html }
482
+ end
483
+
484
+ describe 'in <code>' do
485
+ before do
486
+ source = <<-'EOF'
487
+ # subTitle
488
+
489
+ `:sushi:` は美味しい
490
+ EOF
491
+ @diary.append(source)
492
+
493
+ @html = <<-'EOF'
494
+ <div class="section">
495
+ <%=section_enter_proc( Time.at( 1041346800 ) )%>
496
+ <h3><%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %></h3>
497
+ <p><code>:sushi:</code> は美味しい</p>
498
+ <%=section_leave_proc( Time.at( 1041346800 ) )%>
499
+ </div>
500
+ EOF
501
+ end
502
+ it { @diary.to_html.should eq @html }
503
+ end
504
+
505
+ describe 'in <code> (with attribute)' do
506
+ before do
507
+ source = <<-'EOF'
508
+ # subTitle
509
+
510
+ <code class="foo">:sushi:</code> は美味しい
511
+ EOF
512
+ @diary.append(source)
513
+
514
+ @html = <<-'EOF'
515
+ <div class="section">
516
+ <%=section_enter_proc( Time.at( 1041346800 ) )%>
517
+ <h3><%= subtitle_proc( Time.at( 1041346800 ), "subTitle" ) %></h3>
518
+ <p><code class="foo">:sushi:</code> は美味しい</p>
519
+ <%=section_leave_proc( Time.at( 1041346800 ) )%>
520
+ </div>
521
+ EOF
522
+ end
523
+ it { @diary.to_html.should eq @html }
369
524
  end
370
- it { @diary.to_html.should eq @html }
371
525
  end
372
526
 
373
527
  describe 'do not modify original string' do
metadata CHANGED
@@ -1,97 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tdiary-style-gfm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - SHIBATA Hiroshi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-01 00:00:00.000000000 Z
11
+ date: 2014-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: github-markdown
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pygments.rb
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: twitter-text
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.3'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - '>='
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - '>='
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  description: GFM Style for tDiary
@@ -101,9 +101,9 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - ".gitignore"
105
- - ".rspec"
106
- - ".travis.yml"
104
+ - .gitignore
105
+ - .rspec
106
+ - .travis.yml
107
107
  - Gemfile
108
108
  - LICENSE.txt
109
109
  - README.md
@@ -124,17 +124,17 @@ require_paths:
124
124
  - lib
125
125
  required_ruby_version: !ruby/object:Gem::Requirement
126
126
  requirements:
127
- - - ">="
127
+ - - '>='
128
128
  - !ruby/object:Gem::Version
129
129
  version: '0'
130
130
  required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  requirements:
132
- - - ">="
132
+ - - '>='
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
135
  requirements: []
136
136
  rubyforge_project:
137
- rubygems_version: 2.2.0
137
+ rubygems_version: 2.0.14
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: GFM Style for tDiary