ascii_press 0.3.0 → 0.5.2
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 +4 -4
- data/README.md +8 -23
- data/lib/ascii_press.rb +57 -17
- data/lib/ascii_press/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8f78af8f04afac24ade0bd60bc336fc12d22408
|
4
|
+
data.tar.gz: 967d7ec8a7a89913f0b18abd59272089c6beea94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76597f4f7881a111821d526f0a84cc08106ac1824d13852b12098445f7f0856f2175bfcd1d2dad02fe91b4aa71ec87ddba82e6810d356ca6ab1bfa865111708e
|
7
|
+
data.tar.gz: c9218ef67f48ce921db46d839f631bccceb55002ab302125bb085e6bcbac1dbead9c6eeb4bbf259e6e33fcb26337cafab27633bd63097d2d3f08c592336d591c
|
data/README.md
CHANGED
@@ -28,36 +28,21 @@ WP_USERNAME = 'fake'
|
|
28
28
|
WP_PASSWORD = 'pass'
|
29
29
|
WP_POST_TYPE = 'faq'
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
# `:extra_tags_proc`: Proc which is given the `AsciiPress::Renderer::Rendering` object (see below). Whatever is returned will be used as the WordPress Post's tags
|
36
|
-
renderer = AsciiPress::Renderer.new(extra_tags_proc: -> (rendering) { ['tag-for-all'] }, asciidoc_options: {attributes: 'allow-uri-read'})
|
37
|
-
|
38
|
-
wp_syncer = AsciiPress::WordPressSyncer.new(WP_HOSTNAME, WP_USERNAME, WP_PASSWORD, renderer, WP_POST_TYPE
|
31
|
+
renderer = AsciiPress::Renderer.new(tags_proc: -> (rendering) { rendering.tags + ['tag-for-all'] },
|
32
|
+
asciidoc_options: {attributes: 'allow-uri-read'})
|
33
|
+
|
34
|
+
syncer = AsciiPress::WordPressSyncer.new(WP_HOSTNAME, WP_USERNAME, WP_PASSWORD, renderer, WP_POST_TYPE
|
39
35
|
post_type: ENV['BLOG_POST_TYPE'],
|
40
36
|
logger: LOGGER,
|
41
37
|
delete_not_found: delete_not_found,
|
42
38
|
generate_tags: true,
|
43
39
|
filter_proc: filter_proc)
|
44
|
-
```
|
45
|
-
|
46
|
-
### `AsciiPress::Renderer::Rendering`
|
47
|
-
|
48
|
-
Provides the following methods:
|
49
40
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
* `tags` The tags which will be set in `WordPress`
|
54
|
-
|
55
|
-
|
56
|
-
## Development
|
57
|
-
|
58
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
41
|
+
adoc_file_paths = `find . -name "*.adoc"`.split(/[\n\r]+/)
|
42
|
+
syncer.sync(adoc_file_paths)
|
43
|
+
```
|
59
44
|
|
60
|
-
|
45
|
+
See the [API documentation](http://www.rubydoc.info/github/cheerfulstoic/ascii_press/master) for more details on arguments and options
|
61
46
|
|
62
47
|
## Contributing
|
63
48
|
|
data/lib/ascii_press.rb
CHANGED
@@ -9,29 +9,47 @@ require 'json'
|
|
9
9
|
require 'active_support/core_ext/enumerable'
|
10
10
|
|
11
11
|
module AsciiPress
|
12
|
+
# For setting the default logger
|
12
13
|
def self.logger=(new_logger)
|
13
14
|
@logger = new_logger
|
14
15
|
end
|
15
16
|
|
17
|
+
# A default STDOUT logger
|
16
18
|
def self.logger
|
17
19
|
@logger || Logger.new(STDOUT)
|
18
20
|
end
|
19
21
|
|
20
22
|
class Renderer
|
21
23
|
class Rendering
|
22
|
-
|
24
|
+
# @return [String] The HTML resulting from the asciidoc
|
25
|
+
attr_reader :html
|
26
|
+
|
27
|
+
# @return [Asciidoctor::Document] The document from the +asciidoctor+ gem
|
28
|
+
attr_reader :doc
|
29
|
+
|
30
|
+
# @return [Hash] The adoc file's attributes standardized with symbol keys and string values
|
31
|
+
attr_reader :data
|
32
|
+
|
33
|
+
# @return [Array <String>] The tags which will be set in +WordPress+
|
23
34
|
attr_accessor :tags
|
24
35
|
|
36
|
+
# @return [String] The title that will be used
|
37
|
+
attr_accessor :title
|
38
|
+
|
39
|
+
# Create a new {Rendering} object (intended to be used by Syncers like {WordPressSyncer})
|
25
40
|
def initialize(html, doc, data)
|
26
41
|
@html = html
|
27
42
|
@doc = doc
|
28
43
|
@data = data
|
44
|
+
@title = doc.doctitle
|
29
45
|
end
|
30
46
|
|
47
|
+
# @!visibility private
|
31
48
|
def attribute_value(name, default = nil)
|
32
49
|
doc.attributes[name.to_s] || default
|
33
50
|
end
|
34
51
|
|
52
|
+
# @!visibility private
|
35
53
|
def list_attribute_value(name, default = [])
|
36
54
|
value = attribute_value(name, :VALUE_DOES_NOT_EXIST)
|
37
55
|
if value == :VALUE_DOES_NOT_EXIST
|
@@ -41,15 +59,23 @@ module AsciiPress
|
|
41
59
|
end
|
42
60
|
end
|
43
61
|
|
62
|
+
# @!visibility private
|
44
63
|
def attribute_exists?(name)
|
45
64
|
doc.attributes.key?(name.to_s)
|
46
65
|
end
|
47
66
|
end
|
48
67
|
|
49
|
-
|
68
|
+
# @param options [Hash]
|
69
|
+
# @option options [Hash] :asciidoc_options Passed directly to the +Asciidoctor.load+ method. See the {http://asciidoctor.org/rdoc/Asciidoctor.html AsciiDoctor documentation}
|
70
|
+
# @option options [Proc] :before_convertion Proc which is given the asciidoctor text. Whatever is returned is passed to +Asciidoctor.load+. See the {http://asciidoctor.org/rdoc/Asciidoctor.html AsciiDoctor documentation}
|
71
|
+
# @option options [Proc] :after_conversion Proc which is given the html text after the Asciidoctor conversion. Whatever is returned will be uploaded to WordPress
|
72
|
+
# @option options [Proc] :rendering_proc Proc which is given the {Rendering} object (see below). Changes made be made to the rendering in-place
|
73
|
+
#
|
74
|
+
def initialize(options = {asciidoc_options: {}})
|
50
75
|
@options = options
|
51
76
|
end
|
52
77
|
|
78
|
+
# @!visibility private
|
53
79
|
def render(adoc_file_path)
|
54
80
|
doc = nil
|
55
81
|
errors = capture_stderr do
|
@@ -79,12 +105,13 @@ module AsciiPress
|
|
79
105
|
rendering.tags << 'public' if rendering.attribute_exists?(:public)
|
80
106
|
rendering.tags << 'private' if rendering.attribute_exists?(:private)
|
81
107
|
|
82
|
-
if @options[:
|
83
|
-
rendering.tags
|
108
|
+
if @options[:rendering_proc]
|
109
|
+
rendering.tags = @options[:rendering_proc].call(rendering)
|
84
110
|
end
|
85
111
|
end
|
86
112
|
end
|
87
113
|
|
114
|
+
private
|
88
115
|
def capture_stderr
|
89
116
|
real_stderr, $stderr = $stderr, StringIO.new
|
90
117
|
yield
|
@@ -95,9 +122,21 @@ module AsciiPress
|
|
95
122
|
end
|
96
123
|
|
97
124
|
class WordPressSyncer
|
98
|
-
|
99
|
-
|
100
|
-
|
125
|
+
# Creates a synchronizer object which can be used to synchronize a set of asciidoc files to WordPress posts
|
126
|
+
# @param hostname [String] Hostname for WordPress blog
|
127
|
+
# @param username [String] Wordpress username
|
128
|
+
# @param password [String] Wordpress password
|
129
|
+
# @param post_type [String] Wordpress post type to synchronize posts with
|
130
|
+
# @param renderer [Renderer] Renderer object which will be used to process asciidoctor files
|
131
|
+
# @param options [Hash]
|
132
|
+
# @option options [Logger] :logger Logger to be used for informational output. Defaults to {AsciiPress.logger}
|
133
|
+
# @option options [Proc] :filter_proc Proc which is given an +AsciiDoctor::Document+ object and returns +true+ or +false+ to decide if a document should be synchronized
|
134
|
+
# @option options [Boolean] :delete_not_found Should posts on the WordPress server which don't match any documents locally get deleted?
|
135
|
+
# @option options [Boolean] :generate_tags Should asciidoctor tags be synchronized to WordPress? (defaults to +false+)
|
136
|
+
# @option options [String] :post_status The status to assign to posts when they are synchronized. Defaults to +'draft'+. See the {https://github.com/zachfeldman/rubypress rubypress} documentation
|
137
|
+
def initialize(hostname, username, password, post_type, renderer, options = {})
|
138
|
+
@hostname = hostname
|
139
|
+
@wp_client = Rubypress::Client.new(host: @hostname, username: username, password: password)
|
101
140
|
@post_type = post_type
|
102
141
|
@logger = options[:logger] || AsciiPress.logger
|
103
142
|
@renderer = renderer || Renderer.new
|
@@ -111,6 +150,8 @@ module AsciiPress
|
|
111
150
|
log :info, "Got #{@all_pages_by_post_name.size} pages from the database"
|
112
151
|
end
|
113
152
|
|
153
|
+
# @param adoc_file_path [Array <String>] Paths of the asciidoctor files to synchronize
|
154
|
+
# @param custom_fields [Hash] Custom fields for WordPress.
|
114
155
|
def sync(adoc_file_paths, custom_fields = {})
|
115
156
|
synced_post_names = []
|
116
157
|
|
@@ -124,12 +165,14 @@ module AsciiPress
|
|
124
165
|
|
125
166
|
log :info, "Deleting missing post_name: #{post_name_to_delete} (post ##{post_id})"
|
126
167
|
|
127
|
-
send_message(:deletePost, blog_id: @
|
168
|
+
send_message(:deletePost, blog_id: @hostname, post_id: post_id)
|
128
169
|
end
|
129
170
|
|
130
171
|
end
|
131
172
|
end
|
132
173
|
|
174
|
+
private
|
175
|
+
|
133
176
|
def sync_file_path(adoc_file_path, custom_fields = {})
|
134
177
|
rendering = @renderer.render(adoc_file_path)
|
135
178
|
|
@@ -140,7 +183,7 @@ module AsciiPress
|
|
140
183
|
return
|
141
184
|
end
|
142
185
|
|
143
|
-
title = rendering.
|
186
|
+
title = rendering.title
|
144
187
|
html = rendering.html
|
145
188
|
|
146
189
|
log :info, "Syncing to WordPress: #{title} (slug: #{slug})"
|
@@ -150,7 +193,7 @@ module AsciiPress
|
|
150
193
|
custom_fields_array = custom_fields.merge('adoc_attributes' => rendering.doc.attributes.to_json).map {|k, v| {key: k, value: v} }
|
151
194
|
content = {
|
152
195
|
post_type: @post_type,
|
153
|
-
post_date: Time.now - 60*60*24
|
196
|
+
post_date: Time.now - 60*60*24,
|
154
197
|
post_content: html,
|
155
198
|
post_title: title,
|
156
199
|
post_name: slug,
|
@@ -170,21 +213,18 @@ module AsciiPress
|
|
170
213
|
|
171
214
|
post_id = page['post_id'].to_i
|
172
215
|
|
173
|
-
log :info, "Editing Post ##{post_id} on _#{@
|
216
|
+
log :info, "Editing Post ##{post_id} on _#{@hostname}_ custom-field #{content[:custom_fields].inspect}"
|
174
217
|
|
175
|
-
send_message(:editPost, blog_id: @
|
218
|
+
send_message(:editPost, blog_id: @hostname, post_id: post_id, content: content)
|
176
219
|
else
|
177
|
-
log :info, "Making a new post for '#{title}' on _#{@
|
220
|
+
log :info, "Making a new post for '#{title}' on _#{@hostname}_"
|
178
221
|
|
179
|
-
send_message(:newPost, blog_id: @
|
222
|
+
send_message(:newPost, blog_id: @hostname, content: content)
|
180
223
|
end
|
181
224
|
|
182
225
|
slug
|
183
226
|
end
|
184
227
|
|
185
|
-
|
186
|
-
private
|
187
|
-
|
188
228
|
def new_content_same_as_page?(content, page)
|
189
229
|
main_keys_different = %i(post_content post_title post_name post_status).any? do |key|
|
190
230
|
content[key] != page[key.to_s]
|
data/lib/ascii_press/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ascii_press
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Underwood
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubypress
|