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,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Filters
5
+ module DateFilters
6
+ # Format a date in short format e.g. "27 Jan 2011".
7
+ # Ordinal format is also supported, in both the UK
8
+ # (e.g. "27th Jan 2011") and US ("e.g. Jan 27th, 2011") formats.
9
+ # UK format is the default.
10
+ #
11
+ # date - the Time to format.
12
+ # type - if "ordinal" the returned String will be in ordinal format
13
+ # style - if "US" the returned String will be in US format.
14
+ # Otherwise it will be in UK format.
15
+ #
16
+ # Returns the formatting String.
17
+ def date_to_string(date, type = nil, style = nil)
18
+ stringify_date(date, "%b", type, style)
19
+ end
20
+
21
+ # Format a date in long format e.g. "27 January 2011".
22
+ # Ordinal format is also supported, in both the UK
23
+ # (e.g. "27th January 2011") and US ("e.g. January 27th, 2011") formats.
24
+ # UK format is the default.
25
+ #
26
+ # date - the Time to format.
27
+ # type - if "ordinal" the returned String will be in ordinal format
28
+ # style - if "US" the returned String will be in US format.
29
+ # Otherwise it will be in UK format.
30
+ #
31
+ # Returns the formatted String.
32
+ def date_to_long_string(date, type = nil, style = nil)
33
+ stringify_date(date, "%B", type, style)
34
+ end
35
+
36
+ # Format a date for use in XML.
37
+ #
38
+ # date - The Time to format.
39
+ #
40
+ # Examples
41
+ #
42
+ # date_to_xmlschema(Time.now)
43
+ # # => "2011-04-24T20:34:46+08:00"
44
+ #
45
+ # Returns the formatted String.
46
+ def date_to_xmlschema(date)
47
+ return date if date.to_s.empty?
48
+
49
+ time(date).xmlschema
50
+ end
51
+
52
+ # Format a date according to RFC-822
53
+ #
54
+ # date - The Time to format.
55
+ #
56
+ # Examples
57
+ #
58
+ # date_to_rfc822(Time.now)
59
+ # # => "Sun, 24 Apr 2011 12:34:46 +0000"
60
+ #
61
+ # Returns the formatted String.
62
+ def date_to_rfc822(date)
63
+ return date if date.to_s.empty?
64
+
65
+ time(date).rfc822
66
+ end
67
+
68
+ private
69
+
70
+ # month_type: Notations that evaluate to 'Month' via `Time#strftime` ("%b", "%B")
71
+ # type: nil (default) or "ordinal"
72
+ # style: nil (default) or "US"
73
+ #
74
+ # Returns a stringified date or the empty input.
75
+ def stringify_date(date, month_type, type = nil, style = nil)
76
+ return date if date.to_s.empty?
77
+
78
+ time = time(date)
79
+ if type == "ordinal"
80
+ day = time.day
81
+ ordinal_day = "#{day}#{ordinal(day)}"
82
+ return time.strftime("#{month_type} #{ordinal_day}, %Y") if style == "US"
83
+
84
+ return time.strftime("#{ordinal_day} #{month_type} %Y")
85
+ end
86
+ time.strftime("%d #{month_type} %Y")
87
+ end
88
+
89
+ def ordinal(number)
90
+ return "th" if (11..13).cover?(number)
91
+
92
+ case number % 10
93
+ when 1 then "st"
94
+ when 2 then "nd"
95
+ when 3 then "rd"
96
+ else "th"
97
+ end
98
+ end
99
+
100
+ def time(input)
101
+ date = Liquid::Utils.to_date(input)
102
+ unless date.respond_to?(:to_time)
103
+ raise Errors::InvalidDateError,
104
+ "Invalid Date: '#{input.inspect}' is not a valid datetime."
105
+ end
106
+ date.to_time.dup.localtime
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Filters
5
+ module GroupingFilters
6
+ # Group an array of items by a property
7
+ #
8
+ # input - the inputted Enumerable
9
+ # property - the property
10
+ #
11
+ # Returns an array of Hashes, each looking something like this:
12
+ # {"name" => "larry"
13
+ # "items" => [...] } # all the items where `property` == "larry"
14
+ def group_by(input, property)
15
+ if groupable?(input)
16
+ groups = input.group_by { |item| item_property(item, property).to_s }
17
+ grouped_array(groups)
18
+ else
19
+ input
20
+ end
21
+ end
22
+
23
+ # Group an array of items by an expression
24
+ #
25
+ # input - the object array
26
+ # variable - the variable to assign each item to in the expression
27
+ # expression -a Liquid comparison expression passed in as a string
28
+ #
29
+ # Returns the filtered array of objects
30
+ def group_by_exp(input, variable, expression)
31
+ return input unless groupable?(input)
32
+
33
+ parsed_expr = parse_expression(expression)
34
+ @context.stack do
35
+ groups = input.group_by do |item|
36
+ @context[variable] = item
37
+ parsed_expr.render(@context)
38
+ end
39
+ grouped_array(groups)
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def parse_expression(str)
46
+ Liquid::Variable.new(str, Liquid::ParseContext.new)
47
+ end
48
+
49
+ def groupable?(element)
50
+ element.respond_to?(:group_by)
51
+ end
52
+
53
+ def grouped_array(groups)
54
+ groups.each_with_object([]) do |item, array|
55
+ array << {
56
+ "name" => item.first,
57
+ "items" => item.last,
58
+ "size" => item.last.size,
59
+ }
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Filters
5
+ module URLFilters
6
+ # Produces an absolute URL based on site.url and site.baseurl.
7
+ #
8
+ # input - the URL to make absolute.
9
+ #
10
+ # Returns the absolute URL as a String.
11
+ def absolute_url(input)
12
+ cache = (@context.registers[:cached_absolute_urls] ||= {})
13
+ cache[input] ||= compute_absolute_url(input)
14
+ end
15
+
16
+ # Produces a URL relative to the domain root based on site.baseurl
17
+ # unless it is already an absolute url with an authority (host).
18
+ #
19
+ # input - the URL to make relative to the domain root
20
+ #
21
+ # Returns a URL relative to the domain root as a String.
22
+ def relative_url(input)
23
+ cache = (@context.registers[:cached_relative_urls] ||= {})
24
+ cache[input] ||= compute_relative_url(input)
25
+ end
26
+
27
+ # Strips trailing `/index.html` from URLs to create pretty permalinks
28
+ #
29
+ # input - the URL with a possible `/index.html`
30
+ #
31
+ # Returns a URL with the trailing `/index.html` removed
32
+ def strip_index(input)
33
+ return if input.nil? || input.to_s.empty?
34
+
35
+ input.sub(%r!/index\.html?$!, "/")
36
+ end
37
+
38
+ private
39
+
40
+ def compute_absolute_url(input)
41
+ return if input.nil?
42
+
43
+ input = input.url if input.respond_to?(:url)
44
+ return input if Addressable::URI.parse(input.to_s).absolute?
45
+
46
+ site = @context.registers[:site]
47
+ site_url = site.config["url"]
48
+ return relative_url(input) if site_url.nil? || site_url == ""
49
+
50
+ Addressable::URI.parse(
51
+ site_url.to_s + relative_url(input)
52
+ ).normalize.to_s
53
+ end
54
+
55
+ def compute_relative_url(input)
56
+ return if input.nil?
57
+
58
+ input = input.url if input.respond_to?(:url)
59
+ return input if Addressable::URI.parse(input.to_s).absolute?
60
+
61
+ parts = [sanitized_baseurl, input]
62
+ Addressable::URI.parse(
63
+ parts.compact.map { |part| ensure_leading_slash(part.to_s) }.join
64
+ ).normalize.to_s
65
+ end
66
+
67
+ def sanitized_baseurl
68
+ site = @context.registers[:site]
69
+ site.config["baseurl"].to_s.chomp("/")
70
+ end
71
+
72
+ def ensure_leading_slash(input)
73
+ return input if input.nil? || input.empty? || input.start_with?("/")
74
+
75
+ "/#{input}"
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,238 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ # This class handles custom defaults for YAML frontmatter settings.
5
+ # These are set in bridgetown.config.yml and apply both to internal use (e.g. layout)
6
+ # and the data available to liquid.
7
+ #
8
+ # It is exposed via the frontmatter_defaults method on the site class.
9
+ class FrontmatterDefaults
10
+ # Initializes a new instance.
11
+ def initialize(site)
12
+ @site = site
13
+ end
14
+
15
+ def reset
16
+ @glob_cache = {} if @glob_cache
17
+ end
18
+
19
+ def update_deprecated_types(set)
20
+ return set unless set.key?("scope") && set["scope"].key?("type")
21
+
22
+ set["scope"]["type"] =
23
+ case set["scope"]["type"]
24
+ when "page"
25
+ Deprecator.defaults_deprecate_type("page", "pages")
26
+ "pages"
27
+ when "post"
28
+ Deprecator.defaults_deprecate_type("post", "posts")
29
+ "posts"
30
+ else
31
+ set["scope"]["type"]
32
+ end
33
+
34
+ set
35
+ end
36
+
37
+ def ensure_time!(set)
38
+ return set unless set.key?("values") && set["values"].key?("date")
39
+ return set if set["values"]["date"].is_a?(Time)
40
+
41
+ set["values"]["date"] = Utils.parse_date(
42
+ set["values"]["date"],
43
+ "An invalid date format was found in a front-matter default set: #{set}"
44
+ )
45
+ set
46
+ end
47
+
48
+ # Finds a default value for a given setting, filtered by path and type
49
+ #
50
+ # path - the path (relative to the source) of the page or
51
+ # post the default is used in
52
+ # type - a symbol indicating whether a :page,
53
+ # a :post calls this method
54
+ #
55
+ # Returns the default value or nil if none was found
56
+ def find(path, type, setting)
57
+ value = nil
58
+ old_scope = nil
59
+
60
+ matching_sets(path, type).each do |set|
61
+ if set["values"].key?(setting) && has_precedence?(old_scope, set["scope"])
62
+ value = set["values"][setting]
63
+ old_scope = set["scope"]
64
+ end
65
+ end
66
+ value
67
+ end
68
+
69
+ # Collects a hash with all default values for a page or post
70
+ #
71
+ # path - the relative path of the page or post
72
+ # type - a symbol indicating the type (:post or :page)
73
+ #
74
+ # Returns a hash with all default values (an empty hash if there are none)
75
+ def all(path, type)
76
+ defaults = {}
77
+ old_scope = nil
78
+ matching_sets(path, type).each do |set|
79
+ if has_precedence?(old_scope, set["scope"])
80
+ defaults = Utils.deep_merge_hashes(defaults, set["values"])
81
+ old_scope = set["scope"]
82
+ else
83
+ defaults = Utils.deep_merge_hashes(set["values"], defaults)
84
+ end
85
+ end
86
+ defaults
87
+ end
88
+
89
+ private
90
+
91
+ # Checks if a given default setting scope matches the given path and type
92
+ #
93
+ # scope - the hash indicating the scope, as defined in bridgetown.config.yml
94
+ # path - the path to check for
95
+ # type - the type (:post or :page) to check for
96
+ #
97
+ # Returns true if the scope applies to the given type and path
98
+ def applies?(scope, path, type)
99
+ applies_type?(scope, type) && applies_path?(scope, path)
100
+ end
101
+
102
+ def applies_path?(scope, path)
103
+ rel_scope_path = scope["path"]
104
+ return true if !rel_scope_path.is_a?(String) || rel_scope_path.empty?
105
+
106
+ sanitized_path = sanitize_path(path)
107
+
108
+ if rel_scope_path.include?("*")
109
+ glob_scope(sanitized_path, rel_scope_path)
110
+ else
111
+ path_is_subpath?(sanitized_path, strip_collections_dir(rel_scope_path))
112
+ end
113
+ end
114
+
115
+ def glob_scope(sanitized_path, rel_scope_path)
116
+ site_source = Pathname.new(@site.source)
117
+ abs_scope_path = site_source.join(rel_scope_path).to_s
118
+
119
+ glob_cache(abs_scope_path).each do |scope_path|
120
+ scope_path = Pathname.new(scope_path).relative_path_from(site_source).to_s
121
+ scope_path = strip_collections_dir(scope_path)
122
+ Bridgetown.logger.debug "Globbed Scope Path:", scope_path
123
+ return true if path_is_subpath?(sanitized_path, scope_path)
124
+ end
125
+ false
126
+ end
127
+
128
+ def glob_cache(path)
129
+ @glob_cache ||= {}
130
+ @glob_cache[path] ||= Dir.glob(path)
131
+ end
132
+
133
+ def path_is_subpath?(path, parent_path)
134
+ path.start_with?(parent_path)
135
+ end
136
+
137
+ def strip_collections_dir(path)
138
+ collections_dir = @site.config["collections_dir"]
139
+ slashed_coll_dir = collections_dir.empty? ? "/" : "#{collections_dir}/"
140
+ return path if collections_dir.empty? || !path.to_s.start_with?(slashed_coll_dir)
141
+
142
+ path.sub(slashed_coll_dir, "")
143
+ end
144
+
145
+ # Determines whether the scope applies to type.
146
+ # The scope applies to the type if:
147
+ # 1. no 'type' is specified
148
+ # 2. the 'type' in the scope is the same as the type asked about
149
+ #
150
+ # scope - the Hash defaults set being asked about application
151
+ # type - the type of the document being processed / asked about
152
+ # its defaults.
153
+ #
154
+ # Returns true if either of the above conditions are satisfied,
155
+ # otherwise returns false
156
+ def applies_type?(scope, type)
157
+ !scope.key?("type") || scope["type"].eql?(type.to_s)
158
+ end
159
+
160
+ # Checks if a given set of default values is valid
161
+ #
162
+ # set - the default value hash, as defined in bridgetown.config.yml
163
+ #
164
+ # Returns true if the set is valid and can be used in this class
165
+ def valid?(set)
166
+ set.is_a?(Hash) && set["values"].is_a?(Hash)
167
+ end
168
+
169
+ # Determines if a new scope has precedence over an old one
170
+ #
171
+ # old_scope - the old scope hash, or nil if there's none
172
+ # new_scope - the new scope hash
173
+ #
174
+ # Returns true if the new scope has precedence over the older
175
+ # rubocop: disable Naming/PredicateName
176
+ def has_precedence?(old_scope, new_scope)
177
+ return true if old_scope.nil?
178
+
179
+ new_path = sanitize_path(new_scope["path"])
180
+ old_path = sanitize_path(old_scope["path"])
181
+
182
+ if new_path.length != old_path.length
183
+ new_path.length >= old_path.length
184
+ elsif new_scope.key?("type")
185
+ true
186
+ else
187
+ !old_scope.key? "type"
188
+ end
189
+ end
190
+ # rubocop: enable Naming/PredicateName
191
+
192
+ # Collects a list of sets that match the given path and type
193
+ #
194
+ # Returns an array of hashes
195
+ def matching_sets(path, type)
196
+ @matched_set_cache ||= {}
197
+ @matched_set_cache[path] ||= {}
198
+ @matched_set_cache[path][type] ||= begin
199
+ valid_sets.select do |set|
200
+ !set.key?("scope") || applies?(set["scope"], path, type)
201
+ end
202
+ end
203
+ end
204
+
205
+ # Returns a list of valid sets
206
+ #
207
+ # This is not cached to allow plugins to modify the configuration
208
+ # and have their changes take effect
209
+ #
210
+ # Returns an array of hashes
211
+ def valid_sets
212
+ sets = @site.config["defaults"]
213
+ return [] unless sets.is_a?(Array)
214
+
215
+ sets.map do |set|
216
+ if valid?(set)
217
+ ensure_time!(update_deprecated_types(set))
218
+ else
219
+ Bridgetown.logger.warn "Defaults:", "An invalid front-matter default set was found:"
220
+ Bridgetown.logger.warn set.to_s
221
+ nil
222
+ end
223
+ end.compact
224
+ end
225
+
226
+ # Sanitizes the given path by removing a leading and adding a trailing slash
227
+
228
+ SANITIZATION_REGEX = %r!\A/|(?<=[^/])\z!.freeze
229
+
230
+ def sanitize_path(path)
231
+ if path.nil? || path.empty?
232
+ ""
233
+ else
234
+ path.gsub(SANITIZATION_REGEX, "")
235
+ end
236
+ end
237
+ end
238
+ end