tdiary-style-gfm 1.3.0 → 1.5.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: e4b1ca75344a93394fd50ce1a1a669a34ee8ca866f6a5b5e1d119451d72c0ad8
4
- data.tar.gz: f1674c88db424f583984aebb78093a6e660c74d8deb9c6548090b85c9c51ca76
3
+ metadata.gz: 5a996b82990d85cb5097270545e898de741322c225aaf1160381ec536518b27c
4
+ data.tar.gz: 58e231c56262ea42c3d18eb854948dafdae129b8cf801dd9aa7911a2e63f32ed
5
5
  SHA512:
6
- metadata.gz: a98d14ca1a84c737e0c984d83034167a02e616cc63a0aa770f02abd1a76184639bddd6944fe46d31297cde5bb29881091f55086d895a60165ef61d20e43b0db7
7
- data.tar.gz: 2958a5471431c367b984265a858c7ae9cd30aff7fcdb9112b86ee4f894a539bfadbe7771ad0cbaf3babbb77da83a133a0e0ab0dd5ec7113822235bc3a80d0434
6
+ metadata.gz: fbe2101056de6b1b9a21fcefa4360c757c1af04f1333bcb345578e74e315819f0f4ffc8f0d47591517476a2fb9ba2e7846a87e2a24bbd8552d2f4aa0626d3298
7
+ data.tar.gz: a744ac14db04091529607f499d580171bdf7cbd072d93ae621f84884a2072e197e44ac62ab20dfbcc6e173e6dac86d57e924c2add5be36a11673e05a22d1cd13
@@ -1,7 +1,7 @@
1
1
  module TDiary
2
2
  module Style
3
3
  module Gfm
4
- VERSION = "1.3.0"
4
+ VERSION = "1.5.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,6 +1,21 @@
1
1
  # -*- coding: utf-8; -*-
2
2
 
3
3
  require 'commonmarker'
4
+ require 'json'
5
+
6
+ # twitter-text 3.1.0 passes JSON.parse options as a positional hash while loading,
7
+ # which json 3 rejects. Prepend before the require so its own definition loses.
8
+ module Twitter
9
+ module TwitterText
10
+ class Configuration
11
+ singleton_class.prepend(Module.new do
12
+ def parse_string(string, options = {})
13
+ JSON.parse(string, **options, symbolize_names: true)
14
+ end
15
+ end)
16
+ end
17
+ end
18
+ end
4
19
  require 'twitter-text'
5
20
 
6
21
  module TDiary
@@ -37,6 +52,9 @@ module TDiary
37
52
  end
38
53
 
39
54
  def do_html4(date, idx, opt)
55
+ # An empty `#` only separates sections, so skip the heading but keep @subtitle for to_src.
56
+ return @body_to_html if @subtitle.empty?
57
+
40
58
  subtitle = to_html('# ' + @subtitle)
41
59
  subtitle.sub!( %r!<h3>(.+?)</h3>!m ) do
42
60
  "<h3><%= subtitle_proc( Time.at( #{date.to_i} ), #{$1.dump.gsub( /%/, '\\\\045' )} ) %></h3>"
@@ -63,7 +81,12 @@ module TDiary
63
81
  r = replaced_r
64
82
 
65
83
  # 2. Apply markdown conversion
66
- r = CommonMarker.render_html(r, [:DEFAULT, :UNSAFE], [:autolink, :table])
84
+ r = Commonmarker.to_html(r,
85
+ options: {
86
+ render: { unsafe: true },
87
+ extension: { header_ids: nil, tagfilter: false }
88
+ }
89
+ )
67
90
 
68
91
  # 3. Stash <pre> and <code> tags
69
92
  pre_tag_stashes = []
@@ -80,46 +103,22 @@ module TDiary
80
103
 
81
104
  # 4. Convert miscellaneous
82
105
  if pre_tag_stashes.none? && code_tag_stashes.none?
83
- # STAGE 1: Stash all existing <a> tags from CommonMarker output
84
- original_a_tag_stashes = []
85
- r.gsub!(/(<a\s[^>]*href=[^>]*>.*?<\/a>)/im) do |match|
86
- original_a_tag_stashes.push(match)
87
- "@@TDIARY_GFM_A_TAG_#{original_a_tag_stashes.length - 1}@@"
88
- end
89
-
90
- # STAGE 2: Protect YouTube @-URLs that might still be plain text
91
- youtube_link_data_for_twitter_text_protection = []
92
- r.gsub!(/((?:https?:\/\/)?(?:www\.)?youtube\.com\/@([a-zA-Z0-9_.-]+))/) do |match|
93
- original_text = $1
94
- placeholder = "@@YT_LINK_PROTECT_#{youtube_link_data_for_twitter_text_protection.length}@@"
95
- youtube_link_data_for_twitter_text_protection << { placeholder: placeholder, original_text: original_text }
96
- placeholder
97
- end
98
-
99
- # STAGE 3: Apply Twitter's autolinker for @mentions and lists.
100
- r = Twitter::TwitterText::Autolink.auto_link_usernames_or_lists(r)
101
-
102
- # STAGE 4: Restore protected YouTube links (as new <a> tags)
103
- youtube_link_data_for_twitter_text_protection.each do |link_data|
104
- original_text = link_data[:original_text]
105
- href = original_text
106
-
107
- if !href.start_with?('http://') && !href.start_with?('https://')
108
- if href.include?('youtube.com/')
109
- href = "https://#{href}"
110
- else
111
- href = "http://#{href}"
106
+ # Link only text outside of tags, leaving @ in attributes like <iframe src=".../@user/embed"> and in <a>, <script> and <style> as is
107
+ skip_until = nil
108
+ r = r.split(/(<[^>]*>)/).map {|token|
109
+ if token.start_with?('<')
110
+ if skip_until
111
+ skip_until = nil if skip_until.match?(token)
112
+ elsif /\A<(a|script|style)\b/i =~ token
113
+ skip_until = /\A<\/#{$1}\s*>/i
112
114
  end
115
+ token
116
+ elsif skip_until
117
+ token
118
+ else
119
+ autolink_text(token)
113
120
  end
114
-
115
- youtube_html_link = "<a href=\"#{CGI.escapeHTML(href)}\">#{CGI.escapeHTML(original_text)}</a>"
116
- r.gsub!(link_data[:placeholder], youtube_html_link)
117
- end
118
-
119
- # STAGE 5: Restore original <a> tags
120
- original_a_tag_stashes.each.with_index do |tag_content, i|
121
- r.gsub!("@@TDIARY_GFM_A_TAG_#{i}@@", tag_content)
122
- end
121
+ }.join
123
122
  end
124
123
 
125
124
  r = r.emojify
@@ -163,6 +162,41 @@ module TDiary
163
162
  r
164
163
  end
165
164
 
165
+ def autolink_text(text)
166
+ # STAGE 1: Protect YouTube @-URLs from Twitter's autolinker
167
+ youtube_link_data_for_twitter_text_protection = []
168
+ text.gsub!(/((?:https?:\/\/)?(?:www\.)?youtube\.com\/@([a-zA-Z0-9_.-]+))/) do |match|
169
+ original_text = $1
170
+ placeholder = "@@YT_LINK_PROTECT_#{youtube_link_data_for_twitter_text_protection.length}@@"
171
+ youtube_link_data_for_twitter_text_protection << { placeholder: placeholder, original_text: original_text }
172
+ placeholder
173
+ end
174
+
175
+ # STAGE 2: Apply Twitter's autolinker for @mentions and lists.
176
+ if TDiary.configuration.options['gfm.twitter_autolink'] != false
177
+ text = Twitter::TwitterText::Autolink.auto_link_usernames_or_lists(text)
178
+ end
179
+
180
+ # STAGE 3: Restore protected YouTube links (as new <a> tags)
181
+ youtube_link_data_for_twitter_text_protection.each do |link_data|
182
+ original_text = link_data[:original_text]
183
+ href = original_text
184
+
185
+ if !href.start_with?('http://') && !href.start_with?('https://')
186
+ if href.include?('youtube.com/')
187
+ href = "https://#{href}"
188
+ else
189
+ href = "http://#{href}"
190
+ end
191
+ end
192
+
193
+ youtube_html_link = "<a href=\"#{CGI.escapeHTML(href)}\">#{CGI.escapeHTML(original_text)}</a>"
194
+ text.gsub!(link_data[:placeholder], youtube_html_link)
195
+ end
196
+
197
+ text
198
+ end
199
+
166
200
  def get_categories
167
201
  return [] unless @subtitle
168
202
  cat = /(\\?\[([^\[]+?)\\?\])+/.match(@subtitle).to_a[0]
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_dependency 'commonmarker', '< 1.0'
22
+ spec.add_dependency 'commonmarker', '>= 1.0'
23
23
  spec.add_dependency 'twitter-text', '>= 2.0'
24
24
  spec.add_dependency 'emot'
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tdiary-style-gfm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SHIBATA Hiroshi
@@ -13,14 +13,14 @@ dependencies:
13
13
  name: commonmarker
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - "<"
16
+ - - ">="
17
17
  - !ruby/object:Gem::Version
18
18
  version: '1.0'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
- - - "<"
23
+ - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: '1.0'
26
26
  - !ruby/object:Gem::Dependency
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  requirements: []
85
- rubygems_version: 3.7.0.dev
85
+ rubygems_version: 4.0.20
86
86
  specification_version: 4
87
87
  summary: GFM Style for tDiary
88
88
  test_files: []