tdiary-contrib 5.6.0 → 5.7.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dadce25e1099a56e29e765b144c8f5a7202a360498a751cf4acc86956735a0b3
4
- data.tar.gz: 88ab9fb07d9ec6184fd351500de27918ce25ab51a509786e4961e656fe2eebf6
3
+ metadata.gz: 29f3866866d1d47d3b1f65a0acb469fe14c6431da8715bb274fbff996523d235
4
+ data.tar.gz: 27936de12e7b65c800b7bbc98d3ed7032ed1b8b4ff4538e6c9dfad3efcf89273
5
5
  SHA512:
6
- metadata.gz: 26d5b213738f06dfa008fdf61bfcb727db91114884695a631090ed28a35c992f2e6f4dfa3b35ec1f9dc0215f9eb73ad6c297705b4d8438ee53fc7d00905eab77
7
- data.tar.gz: a55afe77f933e050528aa12fac26260a350e3c5f2cf3f3425ef942ba98559f18f37e726826591660349805c171a009f11cdf5a148cce74ecf6b667a33849f24a
6
+ metadata.gz: 7322400963c11cf51cdbfbf5c2a4171b56a98aee1af4f5cb1c2654ed7d3a519e51d9ce9c294f00eb3e9f38ae86f2710f42355d792ce479f04374c28614d4cdf0
7
+ data.tar.gz: c54cb29c5aaeac5a329d8e4890258be8f487affbafa79f4be659d4410dfba7e5f6224aa1ab963d0ab71c43673a946cbf458c4616195f8c1ff6d8e898f397743d
@@ -1,5 +1,5 @@
1
1
  module TDiary
2
2
  class Contrib
3
- VERSION = "5.6.0"
3
+ VERSION = "5.7.0"
4
4
  end
5
5
  end
data/plugin/amp.rb ADDED
@@ -0,0 +1,156 @@
1
+ # amp.rb
2
+ #
3
+ # generate AMP style HTML
4
+ #
5
+ # Copyright (c) 2016 MATSUOKA Kohei
6
+ # Distributed under the GPL2 or any later version.
7
+ #
8
+ module AMP
9
+ def amp_header_procs
10
+ @amp_header_procs ||= []
11
+ end
12
+
13
+ def amp_body_enter_procs
14
+ @amp_body_enter_procs ||= []
15
+ end
16
+
17
+ def add_amp_header_proc(&block)
18
+ amp_header_procs << block if block_given?
19
+ end
20
+
21
+ def add_amp_body_enter_proc(&block)
22
+ amp_body_enter_procs << block if block_given?
23
+ end
24
+
25
+ def amp_header_proc
26
+ amp_header_procs.map{|proc| proc.call }.join("\n")
27
+ end
28
+
29
+ def amp_body_enter_proc
30
+ amp_body_enter_procs.map {|proc| proc.call }.join("\n")
31
+ end
32
+ end
33
+ extend AMP
34
+
35
+ add_header_proc do
36
+ if @mode == 'day'
37
+ begin
38
+ diary = @diaries[@date.strftime('%Y%m%d')]
39
+ %Q|<link rel="amphtml" href="#{amp_html_url(diary)}">|
40
+ rescue NoMethodError
41
+ ''
42
+ end
43
+ end
44
+ end
45
+
46
+ add_content_proc('amp') do |date|
47
+ begin
48
+ diary = @diaries[date]
49
+ ERB.new(amp_template).result(binding)
50
+ rescue NoMethodError
51
+ raise TDiary::NotFound
52
+ end
53
+ end
54
+
55
+ def amp_body(diary)
56
+ apply_plugin(diary.to_html)
57
+ .gsub(/<img\s/, '<amp-img layout="responsive" ')
58
+ .gsub(/<script[^<]+<\/script>/, '')
59
+ end
60
+
61
+ def amp_canonical_url(diary)
62
+ URI.join(@conf.base_url, anchor(diary.date.strftime('%Y%m%d')))
63
+ end
64
+
65
+ def amp_day_title(diary)
66
+ title_proc(Time::at(@date.to_i), diary.title)
67
+ end
68
+
69
+ def amp_html_url(diary)
70
+ uri = amp_canonical_url(diary)
71
+ uri.query = [uri.query, "plugin=amp"].compact.join '&'
72
+ uri
73
+ end
74
+
75
+ def amp_style
76
+ base_css = amp_base_css
77
+ theme_css = amp_theme_css
78
+ .gsub(/^@charset.*$/, '')
79
+ .gsub(/!important/, '')
80
+
81
+ <<-EOL
82
+ #{base_css}
83
+ #{theme_css}
84
+ EOL
85
+ end
86
+
87
+ def amp_base_css
88
+ base_css_path = theme_paths_local.map {|path|
89
+ File.join(File.dirname(path), "base.css")
90
+ }.find {|path|
91
+ File.exist?(path)
92
+ }
93
+ base_css_path ? File.read(base_css_path) : ''
94
+ end
95
+
96
+ def amp_theme_css
97
+ _, location, theme = @conf.theme.match(%r|(\w+)/(\w+)|).to_a
98
+ case location
99
+ when 'online'
100
+ require 'uri'
101
+ require 'open-uri'
102
+ uri = URI.parse(theme_url_online(theme))
103
+ uri.scheme ||= 'https'
104
+ URI.parse(uri.to_s).read
105
+ when 'local'
106
+ theme_path = theme_paths_local.map {|path|
107
+ File.join(File.dirname(path), "#{theme}/#{theme}.css")
108
+ }.find {|path|
109
+ File.exist?(path)
110
+ }
111
+ theme_path ? File.read(theme_path) : ''
112
+ end
113
+ end
114
+
115
+ def amp_title
116
+ @conf.html_title
117
+ end
118
+
119
+ def amp_template
120
+ <<~'HTML'
121
+ <!doctype html>
122
+ <html ⚡ lang="ja">
123
+ <head>
124
+ <meta charset="utf-8">
125
+ <%= amp_header_proc %>
126
+ <script async src="https://cdn.ampproject.org/v0.js"></script>
127
+ <title><%= amp_title %></title>
128
+ <link rel="canonical" href="<%= amp_canonical_url(diary) %>" />
129
+ <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
130
+ <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
131
+ <style amp-custom>
132
+ <%= amp_style %>
133
+ </style>
134
+ </head>
135
+ <body>
136
+ <%= amp_body_enter_proc %>
137
+ <div class="whole-content">
138
+ <div class="adminmenu">
139
+ <%= navi_user %>
140
+ </div>
141
+ <div class="content">
142
+ <h1><a href="<%= amp_canonical_url(diary) %>"><%= amp_title %></a></h1>
143
+ <div class="day">
144
+ <h2><%= amp_day_title(diary) %></h2>
145
+ <div class="body">
146
+ <%= amp_body(diary) %>
147
+ </div>
148
+ </div>
149
+ </div>
150
+ <div class="footer">
151
+ </div>
152
+ </div>
153
+ </body>
154
+ </html>
155
+ HTML
156
+ end
@@ -18,6 +18,11 @@ add_header_proc do
18
18
  uri: 'h3 a',
19
19
  title: 'h3',
20
20
  container: 'h3'
21
+ },
22
+ 'div.day:not(:has(div.section h3 a))': {
23
+ uri: 'h2 span.date a',
24
+ title: 'h2',
25
+ container: 'h2'
21
26
  }
22
27
  }
23
28
  };
@@ -26,8 +31,8 @@ add_header_proc do
26
31
  return;
27
32
  }
28
33
 
29
- var NodeInsert = function(evt) {
30
- Hatena.Star.EntryLoader.loadNewEntries(evt.target);
34
+ var NodeInsert = function() {
35
+ document.dispatchEvent(new Event('hatena:star:requestrendering'));
31
36
  };
32
37
  window.addEventListener('AutoPagerize_DOMNodeInserted', NodeInsert, false);
33
38
  window.addEventListener('AutoPatchWork.DOMNodeInserted', NodeInsert, false);
data/plugin/image_ex.rb CHANGED
@@ -249,7 +249,7 @@ add_form_proc do |date|
249
249
  return image_path
250
250
  end
251
251
 
252
- if @cgi.params['plugin_image_add'][0] && @cgi.params['plugin_image_file'][0].original_filename != ''
252
+ if @cgi.params['plugin_image_add'][0] && @cgi.params['plugin_image_file'][0] && @cgi.params['plugin_image_file'][0].original_filename != ''
253
253
  image_dir = @cgi.params['plugin_image_dir'][0].read
254
254
  image_filename = ''
255
255
  image_extension = ''
@@ -296,7 +296,7 @@ add_form_proc do |date|
296
296
  end
297
297
  end
298
298
 
299
- elsif @cgi.params['plugin_image_thumbnail'][0] && @cgi.params['plugin_image_file'][0].original_filename != ''
299
+ elsif @cgi.params['plugin_image_thumbnail'][0] && @cgi.params['plugin_image_file'][0] && @cgi.params['plugin_image_file'][0].original_filename != ''
300
300
  image_dir = @cgi.params['plugin_image_dir'][0].read
301
301
  image_filename = ''
302
302
  image_extension = ''
data/spec/amp_spec.rb ADDED
@@ -0,0 +1,125 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+ require 'spec_helper'
3
+ require 'tdiary'
4
+ require 'tdiary/plugin'
5
+
6
+ describe "amp plugin w/" do
7
+ before do
8
+ # stub of TDiary::IO::Diary
9
+ @diary = Object.new.tap {|diary|
10
+ allow(diary).to receive(:title) { 'Example Diary' }
11
+ allow(diary).to receive(:date) { Time.parse('2017/3/2') }
12
+ }
13
+ @conf = PluginFake::Config.new.tap {|conf|
14
+ # an empty directory, so that only amp.rb is loaded
15
+ conf.plugin_path = @cache_path
16
+ conf.lang = 'ja'
17
+ allow(conf).to receive(:base_url) { 'http://example.com/index.rb' }
18
+ }
19
+ @plugin = TDiary::Plugin.new(
20
+ debug: true,
21
+ conf: @conf,
22
+ date: Time.parse('2017/3/2'),
23
+ diaries: { '20170302' => @diary }
24
+ ).tap {|plugin|
25
+ plugin.load_plugin(plugin_path(:amp))
26
+ }
27
+ end
28
+
29
+ describe "#amp_canonical_url" do
30
+ subject { @plugin.amp_canonical_url(@diary).to_s }
31
+ it { expect(subject).to eq('http://example.com/index.rb?date=20170302') }
32
+ end
33
+
34
+ describe "#header_proc" do
35
+ subject { @plugin.__send__(:header_proc) }
36
+
37
+ describe "when @mode == 'day'" do
38
+ before { @plugin.instance_variable_set(:@mode, 'day') }
39
+ it { expect(subject).to eq('<link rel="amphtml" href="http://example.com/index.rb?date=20170302&plugin=amp">') }
40
+ end
41
+
42
+ describe "when @mode != 'day'" do
43
+ before { @plugin.instance_variable_set(:@mode, 'latest') }
44
+ it { expect(subject).to eq('') }
45
+ end
46
+ end
47
+
48
+ describe "#amp_theme_css" do
49
+ subject { @plugin.amp_theme_css }
50
+
51
+ describe "when local theme" do
52
+ before {
53
+ @conf.theme = 'local/default'
54
+ allow(@plugin).to receive(:theme_paths_local) { [File.join(TDiary.root, 'theme/*')] }
55
+ }
56
+ it { expect(subject).to include('Title: tDiary3 default') }
57
+ end
58
+
59
+ describe "when online theme" do
60
+ before {
61
+ @conf.theme = 'online/default'
62
+ allow(@plugin).to receive(:theme_url_online) { '//tdiary.github.io/tdiary-theme/default/default.css' }
63
+ stub_request(:get, 'https://tdiary.github.io/tdiary-theme/default/default.css')
64
+ .to_return(body: File.read(File.join(TDiary.root, 'theme/default/default.css')))
65
+ }
66
+ it { expect(subject).to include('Title: tDiary3 default') }
67
+ end
68
+ end
69
+
70
+ describe "#amp_day_title" do
71
+ subject { @plugin.amp_day_title(@diary) }
72
+ it { expect(subject).to eq('Example Diary') }
73
+ end
74
+
75
+ describe "#amp_body" do
76
+ subject { @plugin.amp_body(@diary) }
77
+
78
+ describe "when contain img element" do
79
+ before { allow(@diary).to receive(:to_html) {
80
+ <<-HTML
81
+ <h3>subsection</h3>
82
+ <p>This is a test diary.</p>
83
+ <img src="image.jpg" weight="640" height="480">
84
+ HTML
85
+ } }
86
+ it { expect(subject).to include('<h3>subsection</h3>') }
87
+ it { expect(subject).to include('<amp-img layout="responsive" src="image.jpg" weight="640" height="480">') }
88
+ end
89
+
90
+ describe "when contain script element" do
91
+ before { allow(@diary).to receive(:to_html) {
92
+ <<-HTML
93
+ <h3>subsection</h3>
94
+ <p>This is a test diary.</p>
95
+ <script>
96
+ console.log("test");
97
+ </script>
98
+ <noscript>noscript</noscript>
99
+ HTML
100
+ } }
101
+ it { expect(subject).to include('<h3>subsection</h3>') }
102
+ it { expect(subject).not_to include('<script>') }
103
+ it { expect(subject).to include('<noscript>noscript</noscript>') }
104
+ end
105
+ end
106
+
107
+ describe "#content_proc" do
108
+ subject { @plugin.__send__(:content_proc, 'amp', '20170302') }
109
+ before {
110
+ @conf.theme = 'local/default'
111
+ allow(@plugin).to receive(:theme_paths_local) { [File.join(TDiary.root, 'theme/*')] }
112
+ allow(@plugin).to receive(:navi_user) { '' }
113
+ allow(@diary).to receive(:to_html) { '<h3>sample</h3>' }
114
+ }
115
+ it { expect(subject).to include('<html ⚡ lang="ja">') }
116
+ it { expect(subject).to include('<h3>sample</h3>') }
117
+ end
118
+
119
+ describe "AMP module" do
120
+ subject { @plugin }
121
+ it { expect(subject.singleton_class.constants).to include(:AMP) }
122
+ it { expect(subject.methods).to include(:add_amp_header_proc) }
123
+ it { expect(subject.methods).to include(:amp_body_enter_proc) }
124
+ end
125
+ end
data/spec/spec_helper.rb CHANGED
@@ -115,7 +115,9 @@ class PluginFake
115
115
 
116
116
  class Config
117
117
 
118
- attr_accessor :index, :html_title, :cgi
118
+ attr_accessor :index, :update, :author_name, :author_mail, :index_page,
119
+ :html_title, :theme, :css, :date_format, :referer_table, :options, :cgi,
120
+ :plugin_path, :lang, :io_class
119
121
 
120
122
  def initialize
121
123
  @cgi = CGIFake.new
@@ -123,6 +125,7 @@ class PluginFake
123
125
  @options2 = {}
124
126
  @index = './'
125
127
  @html_title = ''
128
+ @io_class = DummyIO
126
129
 
127
130
  bot = ["bot", "spider", "antenna", "crawler", "moget", "slurp"]
128
131
  bot += @options['bot'] || []
@@ -190,6 +193,12 @@ class CGIFake
190
193
  end
191
194
  end
192
195
 
196
+ class DummyIO
197
+ def self.plugin_open(conf); nil; end
198
+ def self.plugin_close(storage); end
199
+ def self.plugin_transaction(storage, plugin); end
200
+ end
201
+
193
202
  def fake_plugin( name_sym, cgi=nil, base=nil, &block )
194
203
  plugin = PluginFake.new
195
204
  yield plugin if block_given?
@@ -311,17 +311,25 @@ If error, return a cons cell (ERRCODE . DESCRIPTION)."
311
311
  "\r\n"
312
312
  str))
313
313
  "\r\n"))
314
- (goto-char (point-min))
315
- (while (not (search-forward "</body>" nil t))
316
- (unless (accept-process-output connection tdiary-http-timeout)
317
- (error "HTTP fetch: Connection timeout!"))
318
- (goto-char (point-min)))
314
+ (catch 'done
315
+ (while t
316
+ (goto-char (point-min))
317
+ (when (or (search-forward "</body>" nil t)
318
+ (save-excursion
319
+ (goto-char (point-min))
320
+ (and (re-search-forward "^HTTP/1\\.[01] 30[0-9]" nil t)
321
+ (search-forward "\r\n\r\n" nil t)))
322
+ (not (memq (process-status connection) '(open run))))
323
+ (throw 'done t))
324
+ (unless (accept-process-output connection tdiary-http-timeout)
325
+ (when (memq (process-status connection) '(open run))
326
+ (error "HTTP fetch: Connection timeout!")))))
319
327
  (goto-char (point-min))
320
328
  (save-excursion
321
329
  (if (re-search-forward "HTTP/1.[01] \\([0-9][0-9][0-9]\\) \\(.*\\)" nil t)
322
330
  (let ((code (match-string 1))
323
331
  (desc (match-string 2)))
324
- (cond ((equal code "200")
332
+ (cond ((member code '("200" "301" "302" "303"))
325
333
  buf)
326
334
  (t
327
335
  (cons code desc)))))))))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tdiary-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.6.0
4
+ version: 5.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tDiary contributors
@@ -23,20 +23,6 @@ dependencies:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
25
  version: '5.0'
26
- - !ruby/object:Gem::Dependency
27
- name: pushbullet_ruby
28
- requirement: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
26
  - !ruby/object:Gem::Dependency
41
27
  name: holiday_japan
42
28
  requirement: !ruby/object:Gem::Requirement
@@ -248,6 +234,7 @@ files:
248
234
  - misc/section_footer2/twitter.yaml
249
235
  - misc/section_footer2/yahoo.yaml
250
236
  - plugin/add_bookmark.rb
237
+ - plugin/amp.rb
251
238
  - plugin/apple_webclip.rb
252
239
  - plugin/appstore.rb
253
240
  - plugin/bigpresen.rb
@@ -412,6 +399,7 @@ files:
412
399
  - plugin/youtube.rb
413
400
  - plugin/yshop.rb
414
401
  - plugin/zenback.rb
402
+ - spec/amp_spec.rb
415
403
  - spec/apple_webclip_spec.rb
416
404
  - spec/fixtures/flickr/5950109223.flickr.photos.getInfo.xml
417
405
  - spec/fixtures/flickr/5950109223.flickr.photos.getSizes.xml
@@ -550,6 +538,7 @@ rubygems_version: 3.6.9
550
538
  specification_version: 4
551
539
  summary: tDiary contributions package
552
540
  test_files:
541
+ - spec/amp_spec.rb
553
542
  - spec/apple_webclip_spec.rb
554
543
  - spec/fixtures/flickr/5950109223.flickr.photos.getInfo.xml
555
544
  - spec/fixtures/flickr/5950109223.flickr.photos.getSizes.xml