jekyll-page-boilerplate 4.3.0 → 4.4.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
  SHA256:
3
- metadata.gz: 11506899d111701050ee623e6f1970e1a6ea1028a597c47ddb09e84d2bda8625
4
- data.tar.gz: 79a41ce8ae43e40aab6ece52faab4b0a604f462849a9bb076d8df601cff1409d
3
+ metadata.gz: 65d5d4ea29334a96097f61f901251bd077dd288ef96db98316e79c289db435ea
4
+ data.tar.gz: 44365677835697d6b07f636a14e81a96595be48274d0e3a828c9b09499282d89
5
5
  SHA512:
6
- metadata.gz: 57085fc3192f461a8fbdb1bf6dfe722e5eb9447fe608b4be170922192a17c930780d4e25c253a77220ab6dcd697f94c3c2029b2c34546d7f91e0974d679e224d
7
- data.tar.gz: 587b236f7a440a0a6d274926267b2fd9e45f83eb11724559b89a5bfba76deb27b99a809f651c96a3d7dabc78dcdce647a961e4961aea8f602aac7c006dc67a48
6
+ metadata.gz: 71b662bc22160d2a1550eac1beedb5ebebca1484765d1e3b18062f863a7948f8b3b7d7773c8caf6a639a8937f9609e01cfed01dc1643d0e0bc1a9c3195b7c0a2
7
+ data.tar.gz: fd59775cb1917f121ba79861a9b5aad461b0928d482e1af2a59a7b8236732b2ce397ed3594491d4372516a1611213aa14153847ae45f88db3b1340051743f59f
@@ -6,9 +6,9 @@ A boilerplate is a markdown file you place under the `_boilerplates` folder to g
6
6
  _boilerplate: # boilerplate settings
7
7
  path: _posts # the path to create the new page under.
8
8
  timestamp: true # when true new post/pages will include the date in the filename.
9
- # a custom slug overrides the timestamp setting
10
- slug: '{{ title }}-{{ date }}'
11
- # slug is a template for the filename, it cant take the same tags everything else.
9
+ slug: '{{ title }}-{{ num }}'
10
+ # slug is a template for the filename, it cant take the same tags as everything else.
11
+ num: 0
12
12
  title: {{ boilerplate.title }} # tags like this will be replaced
13
13
  layout: post # everthing else will be copied to the new post.
14
14
  author: John Doe
@@ -27,6 +27,6 @@ Available Tags `{{ boilerplate.xxx }}`:
27
27
  - `.title`, `.name`
28
28
  - `.path`, `.file`, `.suffix`
29
29
  - `.time`, `.date`, `.timestamp`
30
- - `.random_url`
30
+ - `.random`
31
31
  - Custom tags you provide in the command `bplate post custom=1`
32
32
  - And anything you put under the `_boilerplate:` header
@@ -1,10 +1,10 @@
1
- A boilerplate is a markdown file you place under the `_boilerplates/<boilerplate>.md` folder to generate new pages for jekyll.
1
+ A boilerplate is a markdown file you place under the `_boilerplates/` folder to generate new pages for jekyll.
2
2
 
3
3
 
4
4
  It can automatically timestamp and title new pages.
5
5
 
6
6
 
7
- It will also replacing any `{{ boilerplate.xxx }}` tags with content. Available tags include `.time, .title, .date, .random_url`.
7
+ It will also replacing any `{{ boilerplate.xxx }}` tags with content. Available tags include `.time, .title, .date, .random`.
8
8
 
9
9
 
10
10
  You can also provide custom tags with `boilerplate post nav_order=1` > `{{ boilerplate.nav_order }}`.
@@ -2,31 +2,26 @@
2
2
  require 'yaml'
3
3
  require "stringex"
4
4
  require 'securerandom'
5
+ require 'fileutils'
5
6
 
6
7
  class JekyllPageBoilerplate::Page
7
8
 
8
9
  BOILERPLATES_PATH = '_boilerplates'
9
- FILE_DATE_FORMATE = '%Y-%m-%d'
10
10
  READ_CONFIG_REGEX = /[\r\n\s]{0,}^_boilerplate:(\s*^[\t ]{1,2}.+$)+[\r\s\n]{0,}(?![^\r\s\n])/
11
11
  READ_FILE_REGEX = /^-{3}\s*^(?<head>[\s\S]*)^-{3}\s^(?<body>[\s\S]*)/
12
12
  TAGS_REGEX = /\{{2}\s{0,}boilerplate\.([^\{\}\.\s]+)\s{0,}\}{2}/
13
- TAG_SLUG = /\{{2}\s{0,}([^\{\}\.\s]+)\s{0,}\}{2}/
14
13
 
15
14
 
15
+ attr_reader :tags
16
16
 
17
- attr_reader :config
18
-
19
- def self.run boilerplate, options
20
- page = self.new(boilerplate, options)
17
+ def self.run boilerplate, *options
18
+ page = self.new(boilerplate, *options)
21
19
  page.create
22
- return "Created %s/%s" % [page.config['path'], page.config['file']]
20
+ return "Created "+ File.join(page.tags['path'], page.tags['file'])
23
21
  end
24
22
 
25
- def initialize boilerplate, options
26
- options.compact!
27
- options.transform_keys!(&:to_s)
28
- plate_path = get_boilerplate_path(boilerplate).to_s
29
-
23
+ def initialize boilerplate, *options, **params
24
+ plate_path = get_boilerplate_path(boilerplate)
30
25
  abort_unless_file_exists( plate_path )
31
26
 
32
27
  parsed_file = {}
@@ -34,45 +29,45 @@ class JekyllPageBoilerplate::Page
34
29
  parsed_file = file.read.match(READ_FILE_REGEX).named_captures
35
30
  end
36
31
 
37
- @config = get_config(parsed_file['head']).merge(options)
38
- @config['suffix'] ||= plate_path[/\.\w+$/]
39
- @config['name'] ||= plate_path[/.*(?=\.)/] || plate_path
40
- @config['basename'] = File.basename(plate_path, '.*')
41
- @config['title'] ||= @config['basename']
42
-
43
- unless @config['slug']
44
- if @config['timestamp']
45
- @config['slug'] = '{{ date }}-{{ title }}'
46
- else
47
- @config['slug'] = '{{ title }}'
48
- end
49
- end
32
+ @tags = JekyllPageBoilerplate::Tags.new(
33
+ defaults(plate_path),
34
+ get_header_config(parsed_file['head']),
35
+ *options,
36
+ params
37
+ )
38
+ @tags[:file] = '{{ date }}-{{ slug }}{{ suffix }}' if @tags.timestamp
39
+ @tags.fill(:slug, :file, :path, safe: true)
40
+
50
41
  @head = get_head(parsed_file['head'])
51
- @body = get_body(parsed_file['body'])
42
+ @body = parsed_file['body']
52
43
  end
53
44
 
54
45
  def create
55
- @config['time'] ||= Time.now.to_s
56
- @config['date'] ||= Time.now.strftime(FILE_DATE_FORMATE)
57
-
58
- abort_unless_file_exists(@config['path'])
59
-
60
- # puts @config['slug']
61
- scan_slug
62
- @config['file'] = @config['slug']+@config['suffix']
63
- # puts @config['file']
64
- # puts @config['title'].inspect
46
+ FileUtils.mkdir_p(@tags.path)
65
47
 
66
48
  scan_template :@body
67
49
  scan_template :@head
68
50
 
69
- create_new_page @config['file']
51
+ create_new_page
70
52
  end
71
-
53
+
72
54
  private
73
55
 
74
- def create_new_page filename
75
- new_file_path = File.join( @config['path'], filename )
56
+ def defaults(plate_path, timestamp: false)
57
+ basename = File.basename(plate_path, '.*')
58
+ {
59
+ suffix: plate_path[/\.\w+$/],
60
+ name: plate_path[/.*(?=\.)/] || plate_path,
61
+ basename: basename,
62
+ title: basename,
63
+ slug: '{{ title }}',
64
+ path: '_posts/',
65
+ file: '{{ slug }}{{ suffix }}',
66
+ }
67
+ end
68
+
69
+ def create_new_page
70
+ new_file_path = File.join( @tags.path, @tags.file )
76
71
 
77
72
  abort_if_file_exists(new_file_path)
78
73
 
@@ -87,43 +82,11 @@ class JekyllPageBoilerplate::Page
87
82
 
88
83
  def scan_template var
89
84
  instance_variable_get(var).scan(TAGS_REGEX).flatten.uniq.each do |tag|
90
- instance_variable_get(var).gsub! /\{{2}\s{0,}boilerplate\.#{tag}\s{0,}\}{2}/, get_tag_value(tag)
91
- end
92
- end
93
-
94
- def scan_slug
95
- @config['slug'].scan(TAG_SLUG).flatten.uniq.each do |tag|
96
- @config['slug'].gsub! /\{{2}\s{0,}#{tag}\s{0,}\}{2}/, get_tag_value(tag)
97
- end
98
-
99
- @config['slug'].gsub!(/[^0-9A-Za-z\.\-_]/, '-')
100
- @config['slug'].downcase!
101
- end
102
-
103
- def get_tag_value(key)
104
- return @config[key].to_s if @config[key]
105
- key = key.split('=')
106
- return Tag.send(key[0].to_sym, *key[1]&.split(','))
107
- end
108
-
109
- class Tag
110
- class << self
111
- def method_missing *args
112
- ''
113
- end
114
-
115
- def random_url length = nil
116
- length && length = length.to_i
117
- SecureRandom.urlsafe_base64(length)
118
- end
85
+ instance_variable_get(var).gsub! /\{{2}\s{0,}boilerplate\.#{tag}\s{0,}\}{2}/, @tags[tag].to_s
119
86
  end
120
87
  end
121
88
 
122
- def get_body markdown
123
- return markdown
124
- end
125
-
126
- def get_config head
89
+ def get_header_config head
127
90
  return YAML.load(head.match(READ_CONFIG_REGEX).to_s)['_boilerplate']
128
91
  end
129
92
 
@@ -131,15 +94,10 @@ class JekyllPageBoilerplate::Page
131
94
  return head.gsub( READ_CONFIG_REGEX, '')
132
95
  end
133
96
 
134
-
135
97
  def get_boilerplate_path plate_name
136
- return Dir.glob(
137
- "#{File.join(BOILERPLATES_PATH, plate_name)}*"
138
- ).first
98
+ return Dir.glob( "#{File.join(BOILERPLATES_PATH, plate_name)}*" ).first.to_s
139
99
  end
140
100
 
141
-
142
-
143
101
  def abort_if_file_exists(file_path)
144
102
  if File.exist?(file_path)
145
103
  raise "#{file_path} already exists!"
@@ -0,0 +1,70 @@
1
+
2
+ class JekyllPageBoilerplate::Tags
3
+
4
+ FILE_DATE_FORMATE = '%Y-%m-%d'
5
+ FILL_SCAN = /\{{2}\s{0,}([^\{\}\.\s]+)\s{0,}\}{2}/
6
+
7
+ def self.[] *args
8
+ self.new *args
9
+ end
10
+
11
+ def initialize *args, **params
12
+ @tags = {}
13
+ add(*args, params)
14
+ end
15
+
16
+ def []= key, val
17
+ @tags[key.to_s] = val
18
+ end
19
+
20
+ def add *args, **params
21
+ args.map! {|h| h.transform_keys(&:to_s).compact }
22
+ params.transform_keys!(&:to_s).compact!
23
+ @tags.merge!(*args, params)
24
+ self
25
+ end
26
+
27
+ def [] key
28
+ key = key.to_s
29
+ @tags[key] ||= fetch(*key.split(/\s*=\s*|\s*,\s*/))
30
+ end
31
+
32
+ def fill *keys, safe: false
33
+ keys.map!(&:to_s)
34
+ keys.each do |k|
35
+ @tags[k].scan(FILL_SCAN).flatten.uniq.each do |tag|
36
+ @tags[k].gsub! /\{{2}\s{0,}#{tag.to_s}\s{0,}\}{2}/, self[tag].to_s
37
+ end
38
+ end
39
+ if safe
40
+ keys.each do |k|
41
+ @tags[k] = safe(k)
42
+ end
43
+ end
44
+ self
45
+ end
46
+
47
+ def safe key
48
+ self[key].to_s.downcase.gsub(/[^0-9a-z\.\-\/]+/, '-')
49
+ end
50
+
51
+ def fetch key, *params
52
+ case key
53
+ when 'safe'
54
+ safe(params.join(','))
55
+ when 'time'
56
+ Time.now.to_s
57
+ when 'date'
58
+ Time.now.strftime(FILE_DATE_FORMATE)
59
+ when 'random'
60
+ SecureRandom.hex(*params.map(&:to_i))
61
+ else
62
+ ''
63
+ end
64
+ end
65
+
66
+
67
+ def method_missing key
68
+ @tags[key.to_s]
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module JekyllPageBoilerplate
2
- VERSION = "4.3.0"
2
+ VERSION = "4.4.0"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require "jekyll_page_boilerplate/version"
2
2
  require "jekyll_page_boilerplate/msg"
3
+ require "jekyll_page_boilerplate/tags"
3
4
  require "jekyll_page_boilerplate/page"
4
5
  require "jekyll_page_boilerplate/init"
5
6
  require "jekyll_page_boilerplate/list"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-page-boilerplate
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.3.0
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Ferney
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-14 00:00:00.000000000 Z
11
+ date: 2022-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bales
@@ -45,13 +45,13 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: 2.8.5
47
47
  description: |+
48
- A boilerplate is a markdown file you place under the `_boilerplates/<boilerplate>.md` folder to generate new pages for jekyll.
48
+ A boilerplate is a markdown file you place under the `_boilerplates/` folder to generate new pages for jekyll.
49
49
 
50
50
 
51
51
  It can automatically timestamp and title new pages.
52
52
 
53
53
 
54
- It will also replacing any `{{ boilerplate.xxx }}` tags with content. Available tags include `.time, .title, .date, .random_url`.
54
+ It will also replacing any `{{ boilerplate.xxx }}` tags with content. Available tags include `.time, .title, .date, .random`.
55
55
 
56
56
 
57
57
  You can also provide custom tags with `boilerplate post nav_order=1` > `{{ boilerplate.nav_order }}`.
@@ -78,6 +78,7 @@ files:
78
78
  - lib/jekyll_page_boilerplate/msg/description.md
79
79
  - lib/jekyll_page_boilerplate/msg/gem_description.md
80
80
  - lib/jekyll_page_boilerplate/page.rb
81
+ - lib/jekyll_page_boilerplate/tags.rb
81
82
  - lib/jekyll_page_boilerplate/version.rb
82
83
  - lib/jekyll_page_boilerplate_cli.rb
83
84
  homepage: https://github.com/CodeKarmaDev/jekyll-page-boilerplate