jekyll-crosspost-to-medium 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 014f7c663962554bbb1f39e9a6889cce44064fde
4
+ data.tar.gz: 13d8a678881304d719cf326237494773eef4b161
5
+ SHA512:
6
+ metadata.gz: 4abdd83a7f67d103e4c8d30d8a84ec2e8bcf54f4dafae45b211e718216bd9b0c79df4c547a1f685ef431f1ed45ce2225c3cac1748dc5265349938e9869b08b51
7
+ data.tar.gz: ab179d87375bfd7eea281957899badaa6e0ad54756434eea0a968759a3c787d0a03562bcc3851c8cec1133b63657cca91c3145af0870a8298bc46638b4fcfa0e
@@ -0,0 +1,163 @@
1
+ # By Aaron Gustafson, based on the work of Jeremy Keith
2
+ # https://github.com/aarongustafson/jekyll-crosspost_to_medium
3
+ # https://gist.github.com/adactio/c174a4a68498e30babfd
4
+ # Licence : MIT
5
+ #
6
+ # This generator cross-posts entries to Medium. To work, this script requires
7
+ # a MEDIUM_USER_ID environment variable and a MEDIUM_INTEGRATION_TOKEN.
8
+ #
9
+ # The generator will only pick up posts with the following front matter:
10
+ #
11
+ # `crosspost_to_medium: true`
12
+ #
13
+ # You can control crossposting globally by setting `enabled: true` under the
14
+ # `jekyll-crosspost_to_medium` variable in your Jekyll configuration file.
15
+ # Setting it to false will skip the processing loop entirely which can be
16
+ # useful for local preview builds.
17
+
18
+ require 'json'
19
+ require 'net/http'
20
+ require 'net/https'
21
+ require 'kramdown'
22
+ require 'uri'
23
+
24
+ module Jekyll
25
+ class MediumCrossPostGenerator < Generator
26
+ safe true
27
+ priority :low
28
+
29
+ def generate(site)
30
+ @site = site
31
+
32
+ # puts "Kicking off cross-posting to Medium"
33
+ @settings = @site.config['jekyll-crosspost_to_medium']
34
+
35
+ globally_enabled = @settings['enabled'] || true
36
+ cache_dir = @settings['cache'] || @site.config['source'] + '/.jekyll-crosspost_to_medium'
37
+ @crossposted_file = File.join(cache_dir, "medium_crossposted.yml")
38
+
39
+ if globally_enabled
40
+ # puts "Cross-posting enabled"
41
+ user_id = ENV['MEDIUM_USER_ID'] or false
42
+ token = ENV['MEDIUM_INTEGRATION_TOKEN'] or false
43
+
44
+ if ! user_id or ! token
45
+ raise ArgumentError, "MediumCrossPostGenerator: Environment variables not found"
46
+ return
47
+ end
48
+
49
+ if defined?(cache_dir)
50
+ FileUtils.mkdir_p(cache_dir)
51
+
52
+ if File.exists?(@crossposted_file)
53
+ crossposted = open(@crossposted_file) { |f| YAML.load(f) }
54
+ else
55
+ crossposted = []
56
+ end
57
+
58
+ # If Jekyll 3.0, use hooks
59
+ if (Jekyll.const_defined? :Hooks)
60
+ Jekyll::Hooks.register :posts, :post_render do |post|
61
+ if ! post.published?
62
+ next
63
+ end
64
+
65
+ crosspost = post.data.include? 'crosspost_to_medium'
66
+ if ! crosspost or ! post.data['crosspost_to_medium']
67
+ next
68
+ end
69
+
70
+ content = post.content
71
+ url = "#{@site.config['url']}#{post.url}"
72
+ title = post.data['title']
73
+
74
+ crosspost_payload(crossposted, post, content, title, url)
75
+ end
76
+ else
77
+ markdown_converter = @site.getConverterImpl(Jekyll::Converters::Markdown)
78
+ @site.posts.each do |post|
79
+
80
+ if ! post.published?
81
+ next
82
+ end
83
+
84
+ crosspost = post.data.include? 'crosspost_to_medium'
85
+ if ! crosspost or ! post.data['crosspost_to_medium']
86
+ next
87
+ end
88
+
89
+ # Convert the content
90
+ content = markdown_converter.convert(post.content)
91
+ # Render any plugins
92
+ content = (Liquid::Template.parse content).render @site.site_payload
93
+ # Update absolute URLs
94
+ content = content.gsub /href=(["'])\//, "href=\1#{@site.config['url']}/"
95
+ content = content.gsub /src=(["'])\//, "src=\1#{@site.config['url']}/"
96
+
97
+ url = "#{@site.config['url']}#{post.url}"
98
+ title = post.title
99
+
100
+ crosspost_payload(crossposted, post, content, title, url)
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+
108
+ def crosspost_payload(crossposted, post, content, title, url)
109
+ # Prepend the title and add a link back to originating site
110
+ content.prepend("<h1>#{title}</h1>")
111
+ content << "<p><i>This article was originally posted <a href=\"#{url}\" rel=\"canonical\">on my own site</a>.</i></p>"
112
+
113
+ # Strip domain name from the URL we check against
114
+ url = url.sub(/^#{@site.config['url']}?/,'')
115
+
116
+ # Only cross-post if content has not already been cross-posted
117
+ if url and ! crossposted.include? url
118
+ payload = {
119
+ 'title' => title,
120
+ 'contentFormat' => "html",
121
+ 'content' => content,
122
+ 'tags' => post.data['tags'],
123
+ 'publishStatus' => @settings['status'] || "public",
124
+ 'license' => @settings['license'] || "all-rights-reserved",
125
+ 'canonicalUrl' => url
126
+ }
127
+
128
+ crosspost_to_medium(payload)
129
+ crossposted << url
130
+
131
+ # Update cache
132
+ File.open(@crossposted_file, 'w') { |f| YAML.dump(crossposted, f) }
133
+ end
134
+ end
135
+
136
+
137
+ def crosspost_to_medium(payload)
138
+ puts "Cross-posting “#{payload['title']}” to Medium"
139
+
140
+ user_id = ENV['MEDIUM_USER_ID'] or false
141
+ token = ENV['MEDIUM_INTEGRATION_TOKEN'] or false
142
+ medium_api = URI.parse("https://api.medium.com/v1/users/#{user_id}/posts")
143
+
144
+ # Build the connection
145
+ https = Net::HTTP.new(medium_api.host, medium_api.port)
146
+ https.use_ssl = true
147
+ request = Net::HTTP::Post.new(medium_api.path)
148
+
149
+ # Set the headers
150
+ request['Authorization'] = "Bearer #{token}"
151
+ request['Content-Type'] = "application/json"
152
+ request['Accept'] = "application/json"
153
+ request['Accept-Charset'] = "utf-8"
154
+
155
+ # Set the payload
156
+ request.body = JSON.generate(payload)
157
+
158
+ # Post it
159
+ response = https.request(request)
160
+ end
161
+
162
+ end
163
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-crosspost-to-medium
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Gustafson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: aaron@easy-designs.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/jekyll-crosspost-to-medium.rb
20
+ homepage: http://rubygems.org/gems/jekyll-crosspost-to-medium
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Crosspost to Medium Generator for Jekyll
44
+ test_files: []