ghost_parser 0.0.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 +7 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +106 -0
- data/Rakefile +8 -0
- data/lib/ghost_parser/json_parser.rb +13 -0
- data/lib/ghost_parser.rb +91 -0
- data/sig/ghost_parser.rbs +4 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 618f9df2adceb51cb2af2435782af06a9366418cdf3777dd77e4c27727d5757e
|
4
|
+
data.tar.gz: d408b8924f7122a8fc9910b22a86db11562ad31c90200d825a2afdf71910ecfd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a931262944cf3f4606b2d66286788f8e9ebc0318115df8479c15aabdac6e037029e1726e5aee8e45ab12d41ec71538f9e8c0f519e15393e2c1741a58c465646
|
7
|
+
data.tar.gz: 83b33e59cd6de1de67b2889e6e772de51404f087e05f4f6c1427884e76279e29715af29a50a35f450b1d6b2ecb5ccf24212645fbb09c0997a977a9f07cf667e3
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ghost_parser
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.3
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ghost_parser (0.0.1)
|
5
|
+
nokogiri
|
6
|
+
rake (~> 13.0)
|
7
|
+
rspec (~> 3.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
nokogiri (1.14.2-x86_64-linux)
|
14
|
+
racc (~> 1.4)
|
15
|
+
racc (1.6.2)
|
16
|
+
rake (13.0.6)
|
17
|
+
rspec (3.12.0)
|
18
|
+
rspec-core (~> 3.12.0)
|
19
|
+
rspec-expectations (~> 3.12.0)
|
20
|
+
rspec-mocks (~> 3.12.0)
|
21
|
+
rspec-core (3.12.1)
|
22
|
+
rspec-support (~> 3.12.0)
|
23
|
+
rspec-expectations (3.12.2)
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
25
|
+
rspec-support (~> 3.12.0)
|
26
|
+
rspec-mocks (3.12.4)
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
28
|
+
rspec-support (~> 3.12.0)
|
29
|
+
rspec-support (3.12.0)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
x86_64-linux
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
ghost_parser!
|
36
|
+
nokogiri
|
37
|
+
rake (~> 13.0)
|
38
|
+
rspec (~> 3.0)
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
2.4.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Mathew Thomas
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# GhostParser
|
2
|
+
|
3
|
+
A ruby plugin used to help evaluate the exported [Ghost](https://ghost.org/) posts
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add ghost_parser
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem installghost_parser
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
To parse the export:
|
18
|
+
```ruby
|
19
|
+
parser = GhostParser.new("path/to/export.json").parse
|
20
|
+
|
21
|
+
[
|
22
|
+
{
|
23
|
+
id: "id",
|
24
|
+
title: "title",
|
25
|
+
slug: "slug",
|
26
|
+
html: "html",
|
27
|
+
feature_image: "feature_image",
|
28
|
+
created_at: "created_at",
|
29
|
+
updated_at: "updated_at",
|
30
|
+
published_at: "published_at",
|
31
|
+
status: "status"
|
32
|
+
}, ......
|
33
|
+
]
|
34
|
+
```
|
35
|
+
|
36
|
+
Ghost export content will have their links modified like `__GHOST_URL__/image.jpg`. If you want the content with active links provide your ghost url.
|
37
|
+
```ruby
|
38
|
+
GhostParser.set_ghost_url("your ghost url")
|
39
|
+
```
|
40
|
+
|
41
|
+
Instead of getting all the fields we can define the necessary params with necessary keys
|
42
|
+
```ruby
|
43
|
+
GhostParser.set_config({
|
44
|
+
new_id: "id",
|
45
|
+
name: "title",
|
46
|
+
new_slug: "slug"
|
47
|
+
})
|
48
|
+
|
49
|
+
parser = GhostParser.new("path/to/export.json").parse
|
50
|
+
|
51
|
+
[
|
52
|
+
{
|
53
|
+
new_id: "id",
|
54
|
+
name: "title",
|
55
|
+
new_slug: "slug"
|
56
|
+
}, ......
|
57
|
+
]
|
58
|
+
|
59
|
+
GhostParser.unset_config # Reverts back to default
|
60
|
+
```
|
61
|
+
|
62
|
+
Also supports the option of adding extra fields to parsed data
|
63
|
+
```ruby
|
64
|
+
|
65
|
+
# Using blocks
|
66
|
+
parser = GhostParser.new(file_path).parse do |t|
|
67
|
+
t[:user_id] = 12
|
68
|
+
end
|
69
|
+
|
70
|
+
[
|
71
|
+
{
|
72
|
+
id: "id",
|
73
|
+
title: "title",
|
74
|
+
slug: "slug",
|
75
|
+
html: "html",
|
76
|
+
feature_image: "feature_image",
|
77
|
+
created_at: "created_at",
|
78
|
+
updated_at: "updated_at",
|
79
|
+
published_at: "published_at",
|
80
|
+
status: "status",
|
81
|
+
user_id: 12
|
82
|
+
}, ......
|
83
|
+
]
|
84
|
+
|
85
|
+
# Inline
|
86
|
+
parser = GhostParser.new(file_path).parse(
|
87
|
+
{param_key: "123"}
|
88
|
+
)
|
89
|
+
|
90
|
+
[
|
91
|
+
{
|
92
|
+
id: "id",
|
93
|
+
title: "title",
|
94
|
+
slug: "slug",
|
95
|
+
html: "html",
|
96
|
+
feature_image: "feature_image",
|
97
|
+
created_at: "created_at",
|
98
|
+
updated_at: "updated_at",
|
99
|
+
published_at: "published_at",
|
100
|
+
status: "status",
|
101
|
+
param_key: "123"
|
102
|
+
}, ......
|
103
|
+
]
|
104
|
+
|
105
|
+
```
|
106
|
+
|
data/Rakefile
ADDED
data/lib/ghost_parser.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "ghost_parser/json_parser"
|
4
|
+
require "nokogiri"
|
5
|
+
|
6
|
+
class GhostParser
|
7
|
+
attr_reader :file_path
|
8
|
+
@@ghost_url = ""
|
9
|
+
|
10
|
+
Default_Config = {
|
11
|
+
id: "id",
|
12
|
+
title: "title",
|
13
|
+
slug: "slug",
|
14
|
+
html: "html",
|
15
|
+
feature_image: "feature_image",
|
16
|
+
created_at: "created_at",
|
17
|
+
updated_at: "updated_at",
|
18
|
+
published_at: "published_at",
|
19
|
+
status: "status"
|
20
|
+
}
|
21
|
+
|
22
|
+
def initialize(file_path)
|
23
|
+
@file_path = file_path
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse(attributes = nil, options = {}, &block)
|
27
|
+
raise "File not found" if !File.exist?(file_path)
|
28
|
+
file = File.read(file_path)
|
29
|
+
json_array = JsonParser.new(file).call
|
30
|
+
result_array = []
|
31
|
+
json_array.each do |json|
|
32
|
+
result = {}
|
33
|
+
config.each do |k, v|
|
34
|
+
result[k] = json[v]
|
35
|
+
end
|
36
|
+
result.merge!(attributes) if !attributes.nil?
|
37
|
+
yield(result) if block_given?
|
38
|
+
result_array << result
|
39
|
+
end
|
40
|
+
return format_result(result_array)
|
41
|
+
end
|
42
|
+
|
43
|
+
def config
|
44
|
+
@@config ||= Default_Config
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.set_config(params = nil)
|
48
|
+
@@config = params
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.set_ghost_url(url)
|
52
|
+
@@ghost_url = url
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.unset_config
|
56
|
+
@@config = Default_Config
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def format_result(result_array)
|
62
|
+
if (@@ghost_url.nil? || @@ghost_url.empty?)
|
63
|
+
Warning.warn "Ghost url has not been initialized, urls in the export will not be valid"
|
64
|
+
else
|
65
|
+
result_array.each do |result|
|
66
|
+
result[config.key("html")] = format_html_content(result[config.key("html")]) if config.key("html")
|
67
|
+
result[config.key("feature_image")] = format_feature_image(result[config.key("feature_image")]) if config.key("feature_image")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
result_array
|
71
|
+
end
|
72
|
+
|
73
|
+
def format_html_content(content)
|
74
|
+
doc = Nokogiri::HTML.fragment(content)
|
75
|
+
doc.css("a[href]").each do |link|
|
76
|
+
link['href'] = replace_url(link['href'])
|
77
|
+
end
|
78
|
+
doc.css("img[src]").each do |img|
|
79
|
+
img['src'] = replace_url(img['src'])
|
80
|
+
end
|
81
|
+
return doc.to_html
|
82
|
+
end
|
83
|
+
|
84
|
+
def format_feature_image(image)
|
85
|
+
replace_url(image)
|
86
|
+
end
|
87
|
+
|
88
|
+
def replace_url(url)
|
89
|
+
return url&.gsub("__GHOST_URL__", @@ghost_url)
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ghost_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mathew Thomas
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-23 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- mathew1.9thomas@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- ".ruby-gemset"
|
36
|
+
- ".ruby-version"
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/ghost_parser.rb
|
43
|
+
- lib/ghost_parser/json_parser.rb
|
44
|
+
- sig/ghost_parser.rbs
|
45
|
+
homepage: https://github.com/betacraft/ghost_parser
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
homepage_uri: https://github.com/betacraft/ghost_parser
|
50
|
+
source_code_uri: https://github.com/betacraft/ghost_parser
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.6.0
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.3.26
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: A ruby plugin used to help evaluate the exported Ghost posts
|
70
|
+
test_files: []
|