bridgetown-core 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +42 -0
  3. data/bridgetown-core.gemspec +46 -0
  4. data/lib/bridgetown-core.rb +202 -0
  5. data/lib/bridgetown-core/cache.rb +190 -0
  6. data/lib/bridgetown-core/cleaner.rb +111 -0
  7. data/lib/bridgetown-core/collection.rb +279 -0
  8. data/lib/bridgetown-core/command.rb +106 -0
  9. data/lib/bridgetown-core/commands/build.rb +96 -0
  10. data/lib/bridgetown-core/commands/clean.rb +43 -0
  11. data/lib/bridgetown-core/commands/console.rb +56 -0
  12. data/lib/bridgetown-core/commands/doctor.rb +172 -0
  13. data/lib/bridgetown-core/commands/help.rb +34 -0
  14. data/lib/bridgetown-core/commands/new.rb +148 -0
  15. data/lib/bridgetown-core/commands/serve.rb +273 -0
  16. data/lib/bridgetown-core/commands/serve/servlet.rb +68 -0
  17. data/lib/bridgetown-core/configuration.rb +323 -0
  18. data/lib/bridgetown-core/converter.rb +54 -0
  19. data/lib/bridgetown-core/converters/identity.rb +39 -0
  20. data/lib/bridgetown-core/converters/markdown.rb +108 -0
  21. data/lib/bridgetown-core/converters/markdown/kramdown_parser.rb +132 -0
  22. data/lib/bridgetown-core/converters/smartypants.rb +69 -0
  23. data/lib/bridgetown-core/convertible.rb +237 -0
  24. data/lib/bridgetown-core/deprecator.rb +50 -0
  25. data/lib/bridgetown-core/document.rb +475 -0
  26. data/lib/bridgetown-core/drops/bridgetown_drop.rb +32 -0
  27. data/lib/bridgetown-core/drops/collection_drop.rb +20 -0
  28. data/lib/bridgetown-core/drops/document_drop.rb +69 -0
  29. data/lib/bridgetown-core/drops/drop.rb +215 -0
  30. data/lib/bridgetown-core/drops/excerpt_drop.rb +19 -0
  31. data/lib/bridgetown-core/drops/page_drop.rb +14 -0
  32. data/lib/bridgetown-core/drops/site_drop.rb +62 -0
  33. data/lib/bridgetown-core/drops/static_file_drop.rb +14 -0
  34. data/lib/bridgetown-core/drops/unified_payload_drop.rb +26 -0
  35. data/lib/bridgetown-core/drops/url_drop.rb +132 -0
  36. data/lib/bridgetown-core/entry_filter.rb +108 -0
  37. data/lib/bridgetown-core/errors.rb +20 -0
  38. data/lib/bridgetown-core/excerpt.rb +202 -0
  39. data/lib/bridgetown-core/external.rb +62 -0
  40. data/lib/bridgetown-core/filters.rb +467 -0
  41. data/lib/bridgetown-core/filters/date_filters.rb +110 -0
  42. data/lib/bridgetown-core/filters/grouping_filters.rb +64 -0
  43. data/lib/bridgetown-core/filters/url_filters.rb +79 -0
  44. data/lib/bridgetown-core/frontmatter_defaults.rb +238 -0
  45. data/lib/bridgetown-core/generator.rb +5 -0
  46. data/lib/bridgetown-core/hooks.rb +103 -0
  47. data/lib/bridgetown-core/layout.rb +57 -0
  48. data/lib/bridgetown-core/liquid_extensions.rb +22 -0
  49. data/lib/bridgetown-core/liquid_renderer.rb +71 -0
  50. data/lib/bridgetown-core/liquid_renderer/file.rb +67 -0
  51. data/lib/bridgetown-core/liquid_renderer/table.rb +75 -0
  52. data/lib/bridgetown-core/log_adapter.rb +151 -0
  53. data/lib/bridgetown-core/log_writer.rb +60 -0
  54. data/lib/bridgetown-core/mime.types +867 -0
  55. data/lib/bridgetown-core/page.rb +214 -0
  56. data/lib/bridgetown-core/page_without_a_file.rb +14 -0
  57. data/lib/bridgetown-core/path_manager.rb +31 -0
  58. data/lib/bridgetown-core/plugin.rb +80 -0
  59. data/lib/bridgetown-core/plugin_manager.rb +60 -0
  60. data/lib/bridgetown-core/publisher.rb +23 -0
  61. data/lib/bridgetown-core/reader.rb +185 -0
  62. data/lib/bridgetown-core/readers/collection_reader.rb +22 -0
  63. data/lib/bridgetown-core/readers/data_reader.rb +75 -0
  64. data/lib/bridgetown-core/readers/layout_reader.rb +48 -0
  65. data/lib/bridgetown-core/readers/page_reader.rb +24 -0
  66. data/lib/bridgetown-core/readers/post_reader.rb +74 -0
  67. data/lib/bridgetown-core/readers/static_file_reader.rb +24 -0
  68. data/lib/bridgetown-core/regenerator.rb +195 -0
  69. data/lib/bridgetown-core/related_posts.rb +52 -0
  70. data/lib/bridgetown-core/renderer.rb +261 -0
  71. data/lib/bridgetown-core/site.rb +469 -0
  72. data/lib/bridgetown-core/static_file.rb +205 -0
  73. data/lib/bridgetown-core/tags/component.rb +34 -0
  74. data/lib/bridgetown-core/tags/highlight.rb +111 -0
  75. data/lib/bridgetown-core/tags/include.rb +220 -0
  76. data/lib/bridgetown-core/tags/link.rb +41 -0
  77. data/lib/bridgetown-core/tags/post_url.rb +107 -0
  78. data/lib/bridgetown-core/url.rb +164 -0
  79. data/lib/bridgetown-core/utils.rb +367 -0
  80. data/lib/bridgetown-core/utils/ansi.rb +57 -0
  81. data/lib/bridgetown-core/utils/exec.rb +26 -0
  82. data/lib/bridgetown-core/utils/internet.rb +37 -0
  83. data/lib/bridgetown-core/utils/platforms.rb +80 -0
  84. data/lib/bridgetown-core/utils/thread_event.rb +31 -0
  85. data/lib/bridgetown-core/utils/win_tz.rb +75 -0
  86. data/lib/bridgetown-core/version.rb +5 -0
  87. data/lib/bridgetown-core/watcher.rb +139 -0
  88. data/lib/site_template/.gitignore +6 -0
  89. data/lib/site_template/bridgetown.config.yml +21 -0
  90. data/lib/site_template/frontend/javascript/index.js +3 -0
  91. data/lib/site_template/frontend/styles/index.scss +17 -0
  92. data/lib/site_template/package.json +23 -0
  93. data/lib/site_template/src/404.html +9 -0
  94. data/lib/site_template/src/_data/site_metadata.yml +11 -0
  95. data/lib/site_template/src/_includes/footer.html +3 -0
  96. data/lib/site_template/src/_includes/head.html +9 -0
  97. data/lib/site_template/src/_includes/navbar.html +4 -0
  98. data/lib/site_template/src/_layouts/default.html +15 -0
  99. data/lib/site_template/src/_layouts/home.html +7 -0
  100. data/lib/site_template/src/_layouts/page.html +7 -0
  101. data/lib/site_template/src/_layouts/post.html +7 -0
  102. data/lib/site_template/src/_posts/0000-00-00-welcome-to-bridgetown.md.erb +26 -0
  103. data/lib/site_template/src/about.md +11 -0
  104. data/lib/site_template/src/index.md +7 -0
  105. data/lib/site_template/webpack.config.js +60 -0
  106. data/rake/release.rake +30 -0
  107. metadata +106 -1
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class BridgetownDrop < Liquid::Drop
6
+ class << self
7
+ def global
8
+ @global ||= BridgetownDrop.new
9
+ end
10
+ end
11
+
12
+ def version
13
+ Bridgetown::VERSION
14
+ end
15
+
16
+ def environment
17
+ Bridgetown.env
18
+ end
19
+
20
+ def to_h
21
+ @to_h ||= {
22
+ "version" => version,
23
+ "environment" => environment,
24
+ }
25
+ end
26
+
27
+ def to_json(state = nil)
28
+ JSON.generate(to_h, state)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class CollectionDrop < Drop
6
+ extend Forwardable
7
+
8
+ mutable false
9
+
10
+ def_delegator :@obj, :write?, :output
11
+ def_delegators :@obj, :label, :docs, :files, :directory, :relative_directory
12
+
13
+ private def_delegator :@obj, :metadata, :fallback_data
14
+
15
+ def to_s
16
+ docs.to_s
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class DocumentDrop < Drop
6
+ extend Forwardable
7
+
8
+ NESTED_OBJECT_FIELD_BLACKLIST = %w(
9
+ content output excerpt next previous
10
+ ).freeze
11
+
12
+ mutable false
13
+
14
+ def_delegator :@obj, :relative_path, :path
15
+ def_delegators :@obj, :id, :output, :content, :to_s, :relative_path, :url, :date
16
+
17
+ private def_delegator :@obj, :data, :fallback_data
18
+
19
+ def collection
20
+ @obj.collection.label
21
+ end
22
+
23
+ def excerpt
24
+ fallback_data["excerpt"].to_s
25
+ end
26
+
27
+ def <=>(other)
28
+ return nil unless other.is_a? DocumentDrop
29
+
30
+ cmp = self["date"] <=> other["date"]
31
+ cmp = self["path"] <=> other["path"] if cmp.nil? || cmp.zero?
32
+ cmp
33
+ end
34
+
35
+ def previous
36
+ @obj.previous_doc.to_liquid
37
+ end
38
+
39
+ def next
40
+ @obj.next_doc.to_liquid
41
+ end
42
+
43
+ # Generate a Hash for use in generating JSON.
44
+ # This is useful if fields need to be cleared before the JSON can generate.
45
+ #
46
+ # state - the JSON::State object which determines the state of current processing.
47
+ #
48
+ # Returns a Hash ready for JSON generation.
49
+ def hash_for_json(state = nil)
50
+ to_h.tap do |hash|
51
+ if state && state.depth >= 2
52
+ hash["previous"] = collapse_document(hash["previous"]) if hash["previous"]
53
+ hash["next"] = collapse_document(hash["next"]) if hash["next"]
54
+ end
55
+ end
56
+ end
57
+
58
+ # Generate a Hash which breaks the recursive chain.
59
+ # Certain fields which are normally available are omitted.
60
+ #
61
+ # Returns a Hash with only non-recursive fields present.
62
+ def collapse_document(doc)
63
+ doc.keys.each_with_object({}) do |(key, _), result|
64
+ result[key] = doc[key] unless NESTED_OBJECT_FIELD_BLACKLIST.include?(key)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,215 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class Drop < Liquid::Drop
6
+ include Enumerable
7
+
8
+ NON_CONTENT_METHODS = [:fallback_data, :collapse_document].freeze
9
+
10
+ # Get or set whether the drop class is mutable.
11
+ # Mutability determines whether or not pre-defined fields may be
12
+ # overwritten.
13
+ #
14
+ # is_mutable - Boolean set mutability of the class (default: nil)
15
+ #
16
+ # Returns the mutability of the class
17
+ def self.mutable(is_mutable = nil)
18
+ @is_mutable = is_mutable || false
19
+ end
20
+
21
+ def self.mutable?
22
+ @is_mutable
23
+ end
24
+
25
+ # Create a new Drop
26
+ #
27
+ # obj - the Bridgetown Site, Collection, or Document required by the
28
+ # drop.
29
+ #
30
+ # Returns nothing
31
+ def initialize(obj)
32
+ @obj = obj
33
+ end
34
+
35
+ # Access a method in the Drop or a field in the underlying hash data.
36
+ # If mutable, checks the mutations first. Then checks the methods,
37
+ # and finally check the underlying hash (e.g. document front matter)
38
+ # if all the previous places didn't match.
39
+ #
40
+ # key - the string key whose value to fetch
41
+ #
42
+ # Returns the value for the given key, or nil if none exists
43
+ def [](key)
44
+ if self.class.mutable? && mutations.key?(key)
45
+ mutations[key]
46
+ elsif self.class.invokable? key
47
+ public_send key
48
+ else
49
+ fallback_data[key]
50
+ end
51
+ end
52
+ alias_method :invoke_drop, :[]
53
+
54
+ # Set a field in the Drop. If mutable, sets in the mutations and
55
+ # returns. If not mutable, checks first if it's trying to override a
56
+ # Drop method and raises a DropMutationException if so. If not
57
+ # mutable and the key is not a method on the Drop, then it sets the
58
+ # key to the value in the underlying hash (e.g. document front
59
+ # matter)
60
+ #
61
+ # key - the String key whose value to set
62
+ # val - the Object to set the key's value to
63
+ #
64
+ # Returns the value the key was set to unless the Drop is not mutable
65
+ # and the key matches a method in which case it raises a
66
+ # DropMutationException.
67
+ def []=(key, val)
68
+ setter = "#{key}="
69
+ if respond_to?(setter)
70
+ public_send(setter, val)
71
+ elsif respond_to?(key.to_s)
72
+ if self.class.mutable?
73
+ mutations[key] = val
74
+ else
75
+ raise Errors::DropMutationException, "Key #{key} cannot be set in the drop."
76
+ end
77
+ else
78
+ fallback_data[key] = val
79
+ end
80
+ end
81
+
82
+ # Generates a list of strings which correspond to content getter
83
+ # methods.
84
+ #
85
+ # Returns an Array of strings which represent method-specific keys.
86
+ def content_methods
87
+ @content_methods ||= (
88
+ self.class.instance_methods \
89
+ - Bridgetown::Drops::Drop.instance_methods \
90
+ - NON_CONTENT_METHODS
91
+ ).map(&:to_s).reject do |method|
92
+ method.end_with?("=")
93
+ end
94
+ end
95
+
96
+ # Check if key exists in Drop
97
+ #
98
+ # key - the string key whose value to fetch
99
+ #
100
+ # Returns true if the given key is present
101
+ def key?(key)
102
+ return false if key.nil?
103
+ return true if self.class.mutable? && mutations.key?(key)
104
+
105
+ respond_to?(key) || fallback_data.key?(key)
106
+ end
107
+
108
+ # Generates a list of keys with user content as their values.
109
+ # This gathers up the Drop methods and keys of the mutations and
110
+ # underlying data hashes and performs a set union to ensure a list
111
+ # of unique keys for the Drop.
112
+ #
113
+ # Returns an Array of unique keys for content for the Drop.
114
+ def keys
115
+ (content_methods |
116
+ mutations.keys |
117
+ fallback_data.keys).flatten
118
+ end
119
+
120
+ # Generate a Hash representation of the Drop by resolving each key's
121
+ # value. It includes Drop methods, mutations, and the underlying object's
122
+ # data. See the documentation for Drop#keys for more.
123
+ #
124
+ # Returns a Hash with all the keys and values resolved.
125
+ def to_h
126
+ keys.each_with_object({}) do |(key, _), result|
127
+ result[key] = self[key]
128
+ end
129
+ end
130
+ alias_method :to_hash, :to_h
131
+
132
+ # Inspect the drop's keys and values through a JSON representation
133
+ # of its keys and values.
134
+ #
135
+ # Returns a pretty generation of the hash representation of the Drop.
136
+ def inspect
137
+ JSON.pretty_generate to_h
138
+ end
139
+
140
+ # Generate a Hash for use in generating JSON.
141
+ # This is useful if fields need to be cleared before the JSON can generate.
142
+ #
143
+ # Returns a Hash ready for JSON generation.
144
+ def hash_for_json(*)
145
+ to_h
146
+ end
147
+
148
+ # Generate a JSON representation of the Drop.
149
+ #
150
+ # state - the JSON::State object which determines the state of current processing.
151
+ #
152
+ # Returns a JSON representation of the Drop in a String.
153
+ def to_json(state = nil)
154
+ JSON.generate(hash_for_json(state), state)
155
+ end
156
+
157
+ # Collects all the keys and passes each to the block in turn.
158
+ #
159
+ # block - a block which accepts one argument, the key
160
+ #
161
+ # Returns nothing.
162
+ def each_key(&block)
163
+ keys.each(&block)
164
+ end
165
+
166
+ def each
167
+ each_key.each do |key|
168
+ yield key, self[key]
169
+ end
170
+ end
171
+
172
+ def merge(other, &block)
173
+ dup.tap do |me|
174
+ if block.nil?
175
+ me.merge!(other)
176
+ else
177
+ me.merge!(other, block)
178
+ end
179
+ end
180
+ end
181
+
182
+ def merge!(other)
183
+ other.each_key do |key|
184
+ if block_given?
185
+ self[key] = yield key, self[key], other[key]
186
+ else
187
+ if Utils.mergable?(self[key]) && Utils.mergable?(other[key])
188
+ self[key] = Utils.deep_merge_hashes(self[key], other[key])
189
+ next
190
+ end
191
+
192
+ self[key] = other[key] unless other[key].nil?
193
+ end
194
+ end
195
+ end
196
+
197
+ # Imitate Hash.fetch method in Drop
198
+ #
199
+ # Returns value if key is present in Drop, otherwise returns default value
200
+ # KeyError is raised if key is not present and no default value given
201
+ def fetch(key, default = nil, &block)
202
+ return self[key] if key?(key)
203
+ raise KeyError, %(key not found: "#{key}") if default.nil? && block.nil?
204
+ return yield(key) unless block.nil?
205
+ return default unless default.nil?
206
+ end
207
+
208
+ private
209
+
210
+ def mutations
211
+ @mutations ||= {}
212
+ end
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class ExcerptDrop < DocumentDrop
6
+ def layout
7
+ @obj.doc.data["layout"]
8
+ end
9
+
10
+ def date
11
+ @obj.doc.date
12
+ end
13
+
14
+ def excerpt
15
+ nil
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class PageDrop < Drop
6
+ extend Forwardable
7
+
8
+ mutable false
9
+
10
+ def_delegators :@obj, :content, :dir, :name, :path, :url
11
+ private def_delegator :@obj, :data, :fallback_data
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class SiteDrop < Drop
6
+ extend Forwardable
7
+
8
+ mutable false
9
+
10
+ def_delegator :@obj, :site_data, :data
11
+ def_delegators :@obj, :time, :pages, :static_files, :tags, :categories
12
+
13
+ private def_delegator :@obj, :config, :fallback_data
14
+
15
+ attr_writer :current_document
16
+
17
+ def [](key)
18
+ if key != "posts" && @obj.collections.key?(key)
19
+ @obj.collections[key].docs
20
+ else
21
+ super(key)
22
+ end
23
+ end
24
+
25
+ def key?(key)
26
+ (key != "posts" && @obj.collections.key?(key)) || super
27
+ end
28
+
29
+ def posts
30
+ @site_posts ||= @obj.posts.docs.sort { |a, b| b <=> a }
31
+ end
32
+
33
+ def html_pages
34
+ @site_html_pages ||= @obj.pages.select do |page|
35
+ page.html? || page.url.end_with?("/")
36
+ end
37
+ end
38
+
39
+ def collections
40
+ @site_collections ||= @obj.collections.values.sort_by(&:label).map(&:to_liquid)
41
+ end
42
+
43
+ # `Site#documents` cannot be memoized so that `Site#docs_to_write` can access the
44
+ # latest state of the attribute.
45
+ #
46
+ # Since this method will be called after `Site#pre_render` hook, the `Site#documents`
47
+ # array shouldn't thereafter change and can therefore be safely memoized to prevent
48
+ # additional computation of `Site#documents`.
49
+ def documents
50
+ @documents ||= @obj.documents
51
+ end
52
+
53
+ # TODO: provide a way to set BRIDGETOWN_ENV-specific metadata
54
+ def metadata
55
+ @site_metadata ||= @obj.data["site_metadata"]
56
+ end
57
+
58
+ # return nil for `{{ site.config }}` even if --config was passed via CLI
59
+ def config; end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Drops
5
+ class StaticFileDrop < Drop
6
+ extend Forwardable
7
+ def_delegators :@obj, :name, :extname, :modified_time, :basename
8
+ def_delegator :@obj, :relative_path, :path
9
+ def_delegator :@obj, :type, :collection
10
+
11
+ private def_delegator :@obj, :data, :fallback_data
12
+ end
13
+ end
14
+ end