middleman-wordpress 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2e98f72df5ee4e68c6582e6f65cc0a707421d35
4
+ data.tar.gz: 82d7df54de017b726ea3694aa1e25b60058fdee2
5
+ SHA512:
6
+ metadata.gz: 2fcf67f21d416c0d1586b4fc63c2d7ab0a24b6bd99c5db74e39f75da674d58745ac5a18a30472bb2910e0848872afa12f44a8405a82242b3c44f60f82e87b22d
7
+ data.tar.gz: 95b6a9cee59b3e0e94e9c47cf37d4fcade837c652350685ce4cd1a77589637e89a255690099c0eb7d89402f84249c18b0710cfbc16a8fae220a2c28c8b553db3
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ # Ignore bundler lock file
2
+ /Gemfile.lock
3
+
4
+ # Ignore pkg folder
5
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ # If you do not have OpenSSL installed, update
2
+ # the following line to use "http://" instead
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in middleman-wordpress.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem 'rake'
10
+ gem 'rdoc'
11
+ gem 'yard'
12
+ end
13
+
14
+ group :test do
15
+ gem 'cucumber'
16
+ gem 'fivemat'
17
+ gem 'aruba'
18
+ gem 'rspec'
19
+ end
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # middleman-wordpress
2
+
3
+ An extension for Middleman that pulls content from WordPress.
4
+
5
+ ## About
6
+
7
+ This extension pulls content from WordPress via the WP REST API plugin for WordPress.
8
+
9
+ ## Installation
10
+
11
+ ```
12
+ gem install middleman-wordpress
13
+ ```
14
+
15
+ ## Configuration
16
+
17
+ in `config.rb`
18
+
19
+ ```ruby
20
+ activate :wordpress do |wp|
21
+ wp.uri = 'example.com'
22
+ wp.custom_post_types = [:events, :resources, :other_things]
23
+ end
24
+ ```
25
+
26
+ ## Import Data
27
+
28
+ To import data, run:
29
+
30
+ ```
31
+ middleman wordpress
32
+ ```
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'cucumber/rake/task'
5
+
6
+ Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t|
7
+ t.cucumber_opts = "--color --tags ~@wip --strict --format #{ENV['CUCUMBER_FORMAT'] || 'Fivemat'}"
8
+ end
9
+
10
+ require 'rake/clean'
11
+
12
+ task test: ['cucumber']
13
+
14
+ task default: :test
@@ -0,0 +1,4 @@
1
+ PROJECT_ROOT_PATH = File.dirname(File.dirname(File.dirname(__FILE__)))
2
+ require 'middleman-core'
3
+ require 'middleman-core/step_definitions'
4
+ require File.join(PROJECT_ROOT_PATH, 'lib', 'middleman-wordpress')
@@ -0,0 +1,8 @@
1
+ # Require core library
2
+ require 'middleman-core'
3
+
4
+ require 'middleman-wordpress/version'
5
+ require 'middleman-wordpress/core'
6
+ require 'middleman-wordpress/commands/wordpress'
7
+
8
+ ::Middleman::Extensions.register(:wordpress, MiddlemanWordPress::Core)
@@ -0,0 +1,128 @@
1
+ require 'json'
2
+ require 'fileutils'
3
+ require 'middleman-core/cli'
4
+ require 'wp/api'
5
+
6
+ module Middleman
7
+ module Cli
8
+ class WordPress < Thor
9
+ include Thor::Actions
10
+
11
+ # Path where Middleman expects the local data to be stored
12
+ MIDDLEMAN_LOCAL_DATA_FOLDER = 'data'
13
+
14
+ check_unknown_options!
15
+
16
+ namespace :wordpress
17
+ desc 'wordpress', 'Import data from WordPress'
18
+
19
+ # @todo option to rebuild site if data changes
20
+ # method_option "rebuild", aliases: "-r", desc: "Rebuilds the site if there were changes to the imported data"
21
+
22
+ def self.source_root
23
+ ENV['MM_ROOT']
24
+ end
25
+
26
+ # Tell Thor to exit with a nonzero exit code on failure
27
+ def self.exit_on_failure?
28
+ true
29
+ end
30
+
31
+ def wordpress
32
+ ::Middleman::Application.server.inst
33
+
34
+ # Create data directory if it does not exist
35
+ Dir.mkdir('data') unless File.exists?('data')
36
+
37
+ # Remove all WordPress files
38
+ FileUtils.rm_rf(Dir.glob('data/wordpress_*'))
39
+
40
+ # Instantiate the client
41
+ @api = WP::API[MiddlemanWordPress.options.uri]
42
+
43
+ # Build-up posts
44
+ posts = []
45
+ posts.concat fetch_pages_collection
46
+ posts.concat fetch_posts_collection(:posts)
47
+
48
+ MiddlemanWordPress.options.custom_post_types.each do |post_type|
49
+ posts.concat fetch_posts_collection(post_type)
50
+ end
51
+
52
+ # Strip out headers; keep attributes
53
+ posts.map!{|post| post.attributes}
54
+
55
+ # Derive all the post types
56
+ post_types = []
57
+ posts.each{|post| post_types << post['type']}
58
+ post_types.uniq!
59
+
60
+ # Save the posts out to disc in collections by post type
61
+ post_types.each do |post_type|
62
+ collection_name = post_type.pluralize
63
+ collection = posts.select{|post| post['type'] == post_type}
64
+ extension = "json"
65
+
66
+ File.open("data/wordpress_#{collection_name}.#{extension}", "w") do |f|
67
+ f.write(collection.to_json)
68
+ end
69
+ end
70
+ end
71
+
72
+ protected
73
+
74
+ def fetch_pages_collection
75
+ pages = []
76
+ page = 1
77
+ limit = 10
78
+
79
+ tmp_pages = fetch_pages(page, limit)
80
+ while !tmp_pages.empty?
81
+ pages.concat tmp_pages
82
+ page = page + 1
83
+ tmp_pages = fetch_pages(page, limit)
84
+ end
85
+
86
+ pages
87
+ end
88
+
89
+ def fetch_posts_collection(type)
90
+ type = type.to_s.singularize
91
+ posts = []
92
+ page = 1
93
+ limit = 10
94
+
95
+ tmp_posts = fetch_posts(type, page, limit)
96
+ while !tmp_posts.empty?
97
+ posts.concat tmp_posts
98
+ page = page + 1
99
+ tmp_posts = fetch_posts(type, page, limit)
100
+ end
101
+
102
+ posts
103
+ end
104
+
105
+ def fetch_pages(page, limit)
106
+ begin
107
+ pages = @api.pages(page: page, posts_per_page: limit)
108
+ rescue WP::API::ResourceNotFoundError => e
109
+ # Who cares? We've reached the end of the list
110
+ pages = []
111
+ end
112
+
113
+ pages
114
+ end
115
+
116
+ def fetch_posts(type, page, limit)
117
+ begin
118
+ posts = @api.posts(type: type, page: page, posts_per_page: limit)
119
+ rescue WP::API::ResourceNotFoundError => e
120
+ # Who cares? We've reached the end of the list
121
+ posts = []
122
+ end
123
+
124
+ posts
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,25 @@
1
+ module MiddlemanWordPress
2
+ class << self
3
+ attr_reader :options
4
+ end
5
+
6
+ class Core < Middleman::Extension
7
+ option :uri, nil, "The WordPress API uri"
8
+ option :custom_post_types, [], "Custom post types"
9
+
10
+ def initialize(app, options_hash={}, &block)
11
+ super
12
+
13
+ MiddlemanWordPress.instance_variable_set('@options', options)
14
+ end
15
+
16
+ helpers do
17
+ Dir["data/wordpress_*"].each do |file|
18
+ define_method(file.gsub("data/wordpress_", "")) do
19
+ file = File.read(file)
20
+ return JSON.parse(file)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module MiddlemanWordPress
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'middleman-wordpress'
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require 'middleman-wordpress/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "middleman-wordpress"
8
+ s.version = MiddlemanWordPress::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Nickolas Kenyeres"]
11
+ s.email = ["nkenyeres@gmail.com"]
12
+ s.homepage = "https://github.com/knicklabs/middleman-wordpress"
13
+ s.summary = %q{An extension for Middleman that pulls content from WordPress via WP REST API}
14
+ s.description = %q{An extension for Middleman that enables the building of static websites using WordPress-managed content. This extension pulls content from WordPress via the WP REST API plugin for WordPress.}
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # The version of middleman-core your extension depends on
22
+ s.add_runtime_dependency("middleman-core", [">= 3.3.12"])
23
+ s.add_runtime_dependency("wp-api", [">= 0.1.3"])
24
+
25
+ # Additional dependencies
26
+ # s.add_runtime_dependency("gem-name", "gem-version")
27
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-wordpress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nickolas Kenyeres
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: middleman-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.3.12
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.3.12
27
+ - !ruby/object:Gem::Dependency
28
+ name: wp-api
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.3
41
+ description: An extension for Middleman that enables the building of static websites
42
+ using WordPress-managed content. This extension pulls content from WordPress via
43
+ the WP REST API plugin for WordPress.
44
+ email:
45
+ - nkenyeres@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - README.md
53
+ - Rakefile
54
+ - features/support/env.rb
55
+ - lib/middleman-wordpress.rb
56
+ - lib/middleman-wordpress/commands/wordpress.rb
57
+ - lib/middleman-wordpress/core.rb
58
+ - lib/middleman-wordpress/version.rb
59
+ - lib/middleman_extension.rb
60
+ - middleman-wordpress.gemspec
61
+ homepage: https://github.com/knicklabs/middleman-wordpress
62
+ licenses: []
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: An extension for Middleman that pulls content from WordPress via WP REST
84
+ API
85
+ test_files:
86
+ - features/support/env.rb