mediumize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1964c6e63a9d3561cd24cf4633fe846d3a6f0e94
4
+ data.tar.gz: 9178c9913c3c51fd1ab7dc6b207aca3c03104cf0
5
+ SHA512:
6
+ metadata.gz: 28798849e33e8d8737c59257da79680a7124bb7046d60e60900f2ccc64c815fb7c0e48c3a8cff14841d003741dad94d4d62c2abb2d9737f1d08c6579cbc5dd92
7
+ data.tar.gz: 66e52b03cbda0f4726239eafab854fe190392b87c331d06b97e5c6985451cdd432996acc26eb7bb9a3473e2ce835b0c5050a1bd5eb6a33acf07c570dec5766d1
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mediumize.gemspec
4
+ gemspec
@@ -0,0 +1,7 @@
1
+
2
+ guard :minitest do
3
+ # with Minitest::Spec
4
+ watch(%r{^spec/(.*)_spec\.rb$})
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
6
+ watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
7
+ end
@@ -0,0 +1,68 @@
1
+ # Mediumize
2
+
3
+ [![Build Status](https://travis-ci.org/jondot/mediumize.svg?branch=master)](https://travis-ci.org/jondot/mediumize)
4
+
5
+ Automatically post (and cross-post) your markdown style blog posts to your [Medium](http://medium.com) account from [Jekyll](http://jekyllrb.com/), [Middleman](middlemanapp.com), [Hugo](http://gohugo.io/) and others.
6
+
7
+ Mediumize will only publish drafts, and never publicly.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'mediumize'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install mediumize
25
+
26
+ ## Usage
27
+
28
+ Either via command line (suitable for manual / Hugo flows):
29
+
30
+ $ mediumize -t your-medium-integration-token file1.md file2.md ... fileN.md
31
+
32
+ Or, integrate it via Ruby into your Jekyll / Middleman flow:
33
+
34
+ ```ruby
35
+ require 'mediumize'
36
+ p = Mediumize::Publisher(
37
+ :token => "your-medium-integration-token",
38
+ :frontmatter => true
39
+ )
40
+
41
+ %w{
42
+ file1.md
43
+ file2.md
44
+ fileN.md
45
+ }.each do |file|
46
+ puts p.publish(file)
47
+ end
48
+ ```
49
+
50
+ ## Development
51
+
52
+ 1. `git clone https://github.com/jondot/mediumize && cd mediumize`
53
+ 2. `bundle`
54
+ 3. `rake test`
55
+ 4. Optionally, use guard
56
+
57
+
58
+ # Contributing
59
+
60
+ Fork, implement, add tests, pull request, get my everlasting thanks and a respectable place here :).
61
+
62
+ ### Thanks:
63
+
64
+ To all [contributors](https://github.com/jondot/mediumize/graphs/contributors)
65
+
66
+ # Copyright
67
+
68
+ Copyright (c) 2016 [Dotan Nahum](http://gplus.to/dotan) [@jondot](http://twitter.com/jondot). See [LICENSE](LICENSE.txt) for further details.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "spec"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mediumize"
5
+ require "slop"
6
+
7
+ def bail(err, opts)
8
+ puts err
9
+ puts
10
+ puts opts
11
+ exit(1)
12
+ end
13
+
14
+ begin
15
+ o = Slop::Options.new
16
+ o.bool '-m', '--frontmatter', 'a hostname', default: true
17
+ o.string '-t', '--token', 'your Medium integration token'
18
+ o.on '--version', 'print the version' do
19
+ puts Mediumize::VERSION
20
+ exit
21
+ end
22
+ o.on '--help' do
23
+ puts o
24
+ exit
25
+ end
26
+ opts = Slop::Parser.new(o).parse(ARGV)
27
+
28
+ unless opts[:token]
29
+ bail "Error: please provide a token", opts
30
+ end
31
+
32
+ p = Mediumize::Publisher.new(opts.to_h)
33
+ opts.arguments.each do |arg|
34
+ p.publish(arg)
35
+ end
36
+
37
+ rescue
38
+ bail "Error: #{$!}", o
39
+ end
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,50 @@
1
+ require "mediumize/version"
2
+ require "mediumize/frontmatter"
3
+ require "mediumize/normalizers/links"
4
+ require "mediumize/normalizers/title"
5
+ require "mediumize/renderer"
6
+ require "medium"
7
+
8
+ module HashExt
9
+ def slice(*keys)
10
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
11
+ keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
12
+ end
13
+ end
14
+
15
+ Hash.send(:include, HashExt)
16
+
17
+ # patch medium api, upstream fix
18
+ # with time, replace medium-sdk-ruby entirely
19
+ module Medium
20
+ class Posts
21
+ def create(user, opts)
22
+ @client.post "users/#{user['data']['id']}/posts",
23
+ build_request_with(opts)
24
+ end
25
+ end
26
+ end
27
+
28
+ module Mediumize
29
+ class Publisher
30
+ def initialize(opts={})
31
+ @opts = opts
32
+ @renderer = Mediumize::Renderer.new(opts.slice(:frontmatter))
33
+ @client = Medium::Client.new(:integration_token => opts[:token])
34
+ end
35
+
36
+ def publish(file)
37
+ content = @renderer.render(File.read(file), file)
38
+ fetch_me!
39
+
40
+ res = @client.posts.create(@me, content.merge(:publish_status => "draft"))
41
+ data = Medium::Client.validate(res)["data"]
42
+ { :id => data["id"], :url => data["url"] }
43
+ end
44
+
45
+ def fetch_me!
46
+ @me ||= @client.users.me
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,15 @@
1
+ require 'yaml'
2
+
3
+ module Mediumize
4
+ class Frontmatter
5
+ YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
6
+
7
+ def self.parse(content)
8
+ if content =~ YAML_FRONT_MATTER_REGEXP
9
+ return $', YAML.load($1) if $'
10
+ end
11
+ raise "Cannot parse frontmatter file (#{content.length} chars):\n--start\n#{content[0..200]}\n--end\n"
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,18 @@
1
+ require 'uri'
2
+
3
+ module Mediumize
4
+ module Normalizers
5
+ class Links
6
+ def initialize(base)
7
+ @base = base
8
+ end
9
+ def normalize(doc)
10
+ doc.css('img').select{|img| img.attr('src') }.each do |img|
11
+ img.set_attribute("src", URI.join(@base, img.attr('src')))
12
+ end
13
+ doc
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,14 @@
1
+ module Mediumize
2
+ module Normalizers
3
+ class Title
4
+ def normalize(doc)
5
+ unless doc.title
6
+ doc.title = doc.text()[0..80].strip.chomp
7
+ end
8
+ doc
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+
@@ -0,0 +1,38 @@
1
+ require "github/markup"
2
+ require "mediumize/frontmatter"
3
+ require "nokogiri"
4
+
5
+ module Mediumize
6
+ class Renderer
7
+ def initialize(opts={})
8
+ @opts = opts
9
+ @normalizers = [
10
+ Normalizers::Title.new
11
+ ]
12
+ if @opts[:base_url]
13
+ @normalizers << Normalizers::Links.new(@opts[:base_url])
14
+ end
15
+ end
16
+
17
+ def render(content, file, opts={})
18
+ if(@opts[:frontmatter])
19
+ content, meta = Frontmatter.parse(content)
20
+ opts[:title] ||= meta["title"]
21
+ end
22
+
23
+ markup = GitHub::Markup.render(file, content)
24
+ doc = Nokogiri::HTML(markup)
25
+
26
+ @normalizers.each{|n| doc = n.normalize(doc) }
27
+
28
+ doc.title = opts[:title] if opts[:title]
29
+ {
30
+ :title => doc.title,
31
+ :content => doc.at('body').children.to_html,
32
+ :content_format => "html"
33
+ }
34
+ end
35
+
36
+ end
37
+ end
38
+
@@ -0,0 +1,3 @@
1
+ module Mediumize
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mediumize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mediumize"
8
+ spec.version = Mediumize::VERSION
9
+ spec.authors = ["Dotan Nahum"]
10
+ spec.email = ["jondotan@gmail.com"]
11
+
12
+ spec.summary = %q{Turn your markdown style blog posts from Jekyll, middleman and others to Medium posts automatically.}
13
+ spec.description = %q{Turn your markdown style blog posts from Jekyll, middleman and others to Medium posts automatically.}
14
+ spec.homepage = "https://github.com/jondot/mediumize"
15
+
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'nokogiri', '~> 1.6.7'
23
+ spec.add_dependency 'github-markup', '~> 1.4.0'
24
+ spec.add_dependency 'kramdown', '~> 1.9.0'
25
+ spec.add_dependency 'medium-sdk-ruby'
26
+ spec.add_dependency 'slop', '~> 4.2.1'
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.10"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "minitest"
31
+ spec.add_development_dependency "webmock"
32
+ spec.add_development_dependency "minitest-vcr"
33
+ end
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mediumize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dotan Nahum
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: github-markup
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: kramdown
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: medium-sdk-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: slop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 4.2.1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 4.2.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.10'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.10'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: minitest-vcr
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Turn your markdown style blog posts from Jekyll, middleman and others
154
+ to Medium posts automatically.
155
+ email:
156
+ - jondotan@gmail.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - ".travis.yml"
163
+ - Gemfile
164
+ - Guardfile
165
+ - README.md
166
+ - Rakefile
167
+ - bin/mediumize
168
+ - bin/setup
169
+ - lib/mediumize.rb
170
+ - lib/mediumize/frontmatter.rb
171
+ - lib/mediumize/normalizers/links.rb
172
+ - lib/mediumize/normalizers/title.rb
173
+ - lib/mediumize/renderer.rb
174
+ - lib/mediumize/version.rb
175
+ - mediumize.gemspec
176
+ homepage: https://github.com/jondot/mediumize
177
+ licenses: []
178
+ metadata: {}
179
+ post_install_message:
180
+ rdoc_options: []
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ requirements: []
194
+ rubyforge_project:
195
+ rubygems_version: 2.4.5.1
196
+ signing_key:
197
+ specification_version: 4
198
+ summary: Turn your markdown style blog posts from Jekyll, middleman and others to
199
+ Medium posts automatically.
200
+ test_files: []