ascii_press 0.1.0 → 0.2.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
  SHA1:
3
- metadata.gz: 5f7edfe1b60ed197eec1d134ab544c9ea57770b3
4
- data.tar.gz: 799c3949985b289238251be57f10e4151007d376
3
+ metadata.gz: dffcb104898a26201bd65d8ee065e16b277d7c9e
4
+ data.tar.gz: bcef3472d8592080fffa9f3508311dc841fb2e40
5
5
  SHA512:
6
- metadata.gz: 2aa28ea027e775675c0ab012161db7c7ec1b2ff17e81a3e1448ab05ca5360f8021c1b72e0ea13410affa4c212f38e4d15c7cdd9d092d61e0c4c5e07e88ba189a
7
- data.tar.gz: ba868c289bd75043f42198d8e9c8cc6bc75742bd6040855c5c3ecd823faa80fee27f00c8465b60bfdd4e6e987195c2a074a8fb2c8b2fe648762d885704972933
6
+ metadata.gz: 75673e806f48540665343e9ce1db0e5bf283ed4d94656f4af9ecade5e5f3d1f3222819d7d3e914e2a40aa1a5417a9e69d3f31cabfaf7b444800936cb189ff750
7
+ data.tar.gz: e7da91e2342a3124cf930015accaa915c507c5e20996a769b0ab7cc5b75c3af78bfa97a177af801bed21b95aa03bb186aa9035504a2acad17d58086a81d74b8a
data/ascii_press.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency 'rubypress', '~> 1.1.1'
23
23
  spec.add_dependency 'asciidoctor', '~> 1.5.4'
24
24
  spec.add_dependency 'activesupport', '>= 3.0'
25
+ spec.add_dependency 'colorize', '>= 0.7.7'
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.11"
27
28
  spec.add_development_dependency "rake", "~> 10.0"
data/lib/ascii_press.rb CHANGED
@@ -4,6 +4,7 @@ require 'rubypress'
4
4
  require 'asciidoctor'
5
5
  require 'logger'
6
6
  require 'stringio'
7
+ require 'json'
7
8
 
8
9
  require 'active_support/core_ext/enumerable'
9
10
 
@@ -51,11 +52,22 @@ module AsciiPress
51
52
 
52
53
  def render(adoc_file_path)
53
54
  doc = nil
54
- errors = capture_stderr { doc = Asciidoctor.load_file(adoc_file_path, @options) }
55
+ errors = capture_stderr do
56
+ document_text = File.read(adoc_file_path)
57
+ base_dir = ::File.expand_path(File.dirname(adoc_file_path))
58
+ if before_convertion = @options[:before_convertion]
59
+ document_text = before_convertion.call(document_text)
60
+ end
61
+ doc = Asciidoctor.load(document_text, @options.merge(base_dir: base_dir))
62
+ end
55
63
  puts errors.split(/[\n\r]+/).reject {|line| line.match(/out of sequence/) }.join("\n")
56
64
 
57
65
  html = doc.convert
58
66
 
67
+ if after_conversion = @options[:after_conversion]
68
+ html = after_conversion.call(html)
69
+ end
70
+
59
71
  data = doc.attributes.each_with_object({}) do |(key, value), result|
60
72
  result[key.to_sym] = value.to_s
61
73
  end
@@ -86,12 +98,13 @@ module AsciiPress
86
98
  def initialize(blog_id, username, password, renderer, options = {})
87
99
  @blog_id = blog_id
88
100
  @wp_client = Rubypress::Client.new(host: @blog_id, username: username, password: password)
89
- @post_type = options[:post_type] || 'developer'
101
+ @post_type = options[:post_type] || 'faq'
90
102
  @logger = options[:logger] || AsciiPress.logger
91
103
  @renderer = renderer || Renderer.new
92
104
  @filter_proc = options[:filter_proc] || Proc.new { true }
93
105
  @delete_not_found = options[:delete_not_found]
94
106
  @generate_tags = options[:generate_tags]
107
+ @options = options
95
108
 
96
109
  all_pages = @wp_client.getPosts(filter: {post_type: @post_type, number: 1000})
97
110
  @all_pages_by_post_name = all_pages.index_by {|post| post['post_name'] }
@@ -134,14 +147,15 @@ module AsciiPress
134
147
 
135
148
  # log :info, "data: #{rendering.data.inspect}"
136
149
 
150
+ custom_fields_array = custom_fields.merge('adoc_attributes' => rendering.doc.attributes.to_json).map {|k, v| {key: k, value: v} }
137
151
  content = {
138
152
  post_type: @post_type,
139
153
  post_date: Time.now - 60*60*24*30,
140
154
  post_content: html,
141
155
  post_title: title,
142
156
  post_name: slug,
143
- post_status: 'publish',
144
- custom_fields: custom_fields
157
+ post_status: @options[:post_status] || 'draft',
158
+ custom_fields: custom_fields_array
145
159
  }
146
160
 
147
161
  content[:terms_names] = {post_tag: rendering.tags} if @generate_tags
@@ -168,8 +182,20 @@ module AsciiPress
168
182
  slug
169
183
  end
170
184
 
185
+
171
186
  private
172
187
 
188
+ def new_content_same_as_page?(content, page)
189
+ main_keys_different = %i(post_content post_title post_name post_status).any? do |key|
190
+ content[key] != page[key.to_s]
191
+ end
192
+
193
+ page_fields = page['custom_fields'].each_with_object({}) {|field, h| h[field['key']] = field['value'] }
194
+ content_fields = content[:custom_fields].each_with_object({}) {|field, h| h[field[:key].to_s] = field[:value] }
195
+
196
+ !main_keys_different && oyyyy
197
+ end
198
+
173
199
  def log(level, message)
174
200
  @logger.send(level, "WORDPRESS: #{message}")
175
201
  end
@@ -180,4 +206,45 @@ module AsciiPress
180
206
  end
181
207
  end
182
208
  end
209
+
210
+ DEFAULT_SLUG_RULES = {
211
+ 'Cannot start with `-` or `_`' => -> (slug) { !%w(- _).include?(slug[0]) },
212
+ 'Cannot end with `-` or `_`' => -> (slug) { !%w(- _).include?(slug[-1]) },
213
+ 'Cannot have multiple `-` in a row' => -> (slug) { !slug.match(/--/) },
214
+ 'Must only contain letters, numbers, hyphens, and underscores' => -> (slug) { !!slug.match(/^[a-z0-9\-\_]+$/) },
215
+ }
216
+
217
+ def self.slug_valid?(slug, rules = DEFAULT_SLUG_RULES)
218
+ rules.values.all? {|rule| rule.call(slug) }
219
+ end
220
+
221
+ def self.violated_slug_rules(slug, rules = DEFAULT_SLUG_RULES)
222
+ rules.reject do |desc, rule|
223
+ rule.call(slug)
224
+ end.map(&:first)
225
+ end
226
+
227
+ def self.verify_adoc_slugs!(adoc_paths, rules = DEFAULT_SLUG_RULES)
228
+ data = adoc_paths.map do |path|
229
+ doc = Asciidoctor.load(File.read(path))
230
+
231
+ slug = doc.attributes['slug']
232
+ if !slug_valid?(slug, rules)
233
+ violations = violated_slug_rules(slug, rules)
234
+ [path, slug, violations]
235
+ end
236
+ end.compact
237
+
238
+ if data.size > 0
239
+ require 'colorize'
240
+ data.each do |path, slug, violations|
241
+ puts 'WARNING!!'.red
242
+ puts "The document #{path.blue} has the #{slug.blue} which in invalid because:"
243
+ violations.each do |violation|
244
+ puts " - #{violation.yellow}"
245
+ end
246
+ end
247
+ raise 'Invalid slugs. Cannot continue'
248
+ end
249
+ end
183
250
  end
@@ -1,3 +1,3 @@
1
1
  module AsciiPress
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.0
4
+ version: 0.2.0
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-04-07 00:00:00.000000000 Z
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubypress
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.7
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.7
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement