tdiary-style-gfm 1.4.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: 798d07b48bbca9f0f9ffca0fb1d90c97043e8cb83497f850302d60195e5ac52e
4
- data.tar.gz: 53535af4b6082a81ba979426c6f042d76cee778e6bbecf90bd95b1ae3de501fd
3
+ metadata.gz: 5a996b82990d85cb5097270545e898de741322c225aaf1160381ec536518b27c
4
+ data.tar.gz: 58e231c56262ea42c3d18eb854948dafdae129b8cf801dd9aa7911a2e63f32ed
5
5
  SHA512:
6
- metadata.gz: daaf886192bb969c9c890956b0c479d686c7a10e43a0ca2bd92d3ee27204797946a951982dfed67f9a099706bab0ff6ce05403daabd127057645835b50f3339a
7
- data.tar.gz: 0f909bf89fdfb1de58c1dac1bf800b1161275e6439811874b675802379d2ddae3fb2744beb3274e12c9a5b32c4abd485ce2a652a5b307d4a27ea19fd37dce73c
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.4.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>"
@@ -85,48 +103,22 @@ module TDiary
85
103
 
86
104
  # 4. Convert miscellaneous
87
105
  if pre_tag_stashes.none? && code_tag_stashes.none?
88
- # STAGE 1: Stash all existing <a> tags from CommonMarker output
89
- original_a_tag_stashes = []
90
- r.gsub!(/(<a\s[^>]*href=[^>]*>.*?<\/a>)/im) do |match|
91
- original_a_tag_stashes.push(match)
92
- "@@TDIARY_GFM_A_TAG_#{original_a_tag_stashes.length - 1}@@"
93
- end
94
-
95
- # STAGE 2: Protect YouTube @-URLs that might still be plain text
96
- youtube_link_data_for_twitter_text_protection = []
97
- r.gsub!(/((?:https?:\/\/)?(?:www\.)?youtube\.com\/@([a-zA-Z0-9_.-]+))/) do |match|
98
- original_text = $1
99
- placeholder = "@@YT_LINK_PROTECT_#{youtube_link_data_for_twitter_text_protection.length}@@"
100
- youtube_link_data_for_twitter_text_protection << { placeholder: placeholder, original_text: original_text }
101
- placeholder
102
- end
103
-
104
- # STAGE 3: Apply Twitter's autolinker for @mentions and lists.
105
- if TDiary.configuration.options['gfm.twitter_autolink'] != false
106
- r = Twitter::TwitterText::Autolink.auto_link_usernames_or_lists(r)
107
- end
108
-
109
- # STAGE 4: Restore protected YouTube links (as new <a> tags)
110
- youtube_link_data_for_twitter_text_protection.each do |link_data|
111
- original_text = link_data[:original_text]
112
- href = original_text
113
-
114
- if !href.start_with?('http://') && !href.start_with?('https://')
115
- if href.include?('youtube.com/')
116
- href = "https://#{href}"
117
- else
118
- 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
119
114
  end
115
+ token
116
+ elsif skip_until
117
+ token
118
+ else
119
+ autolink_text(token)
120
120
  end
121
-
122
- youtube_html_link = "<a href=\"#{CGI.escapeHTML(href)}\">#{CGI.escapeHTML(original_text)}</a>"
123
- r.gsub!(link_data[:placeholder], youtube_html_link)
124
- end
125
-
126
- # STAGE 5: Restore original <a> tags
127
- original_a_tag_stashes.each.with_index do |tag_content, i|
128
- r.gsub!("@@TDIARY_GFM_A_TAG_#{i}@@", tag_content)
129
- end
121
+ }.join
130
122
  end
131
123
 
132
124
  r = r.emojify
@@ -170,6 +162,41 @@ module TDiary
170
162
  r
171
163
  end
172
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
+
173
200
  def get_categories
174
201
  return [] unless @subtitle
175
202
  cat = /(\\?\[([^\[]+?)\\?\])+/.match(@subtitle).to_a[0]
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.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SHIBATA Hiroshi
@@ -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.6.9
85
+ rubygems_version: 4.0.20
86
86
  specification_version: 4
87
87
  summary: GFM Style for tDiary
88
88
  test_files: []