Module: Bridgetown

Defined in:
lib/bridgetown-core/url.rb,
lib/bridgetown-core/site.rb,
lib/bridgetown-core/slot.rb,
lib/bridgetown-core/cache.rb,
lib/bridgetown-core/hooks.rb,
lib/bridgetown-core/utils.rb,
lib/bridgetown-core/errors.rb,
lib/bridgetown-core/layout.rb,
lib/bridgetown-core/plugin.rb,
lib/bridgetown-core/reader.rb,
lib/bridgetown-core/tags/l.rb,
lib/bridgetown-core/tags/t.rb,
lib/bridgetown-core/cleaner.rb,
lib/bridgetown-core/current.rb,
lib/bridgetown-core/filters.rb,
lib/bridgetown-core/helpers.rb,
lib/bridgetown-core/version.rb,
lib/bridgetown-core/watcher.rb,
lib/bridgetown-core/tags/dsd.rb,
lib/bridgetown-core/component.rb,
lib/bridgetown-core/converter.rb,
lib/bridgetown-core/generator.rb,
lib/bridgetown-core/rack/boot.rb,
lib/bridgetown-core/tags/find.rb,
lib/bridgetown-core/tags/link.rb,
lib/bridgetown-core/tags/with.rb,
lib/bridgetown-core/utils/aux.rb,
lib/bridgetown-core/collection.rb,
lib/bridgetown-core/deprecator.rb,
lib/bridgetown-core/drops/drop.rb,
lib/bridgetown-core/log_writer.rb,
lib/bridgetown-core/model/base.rb,
lib/bridgetown-core/utils/ansi.rb,
lib/bridgetown-core/log_adapter.rb,
lib/bridgetown-core/rack/logger.rb,
lib/bridgetown-core/rack/routes.rb,
lib/bridgetown-core/static_file.rb,
lib/bridgetown-core/yaml_parser.rb,
lib/bridgetown-core/commands/new.rb,
lib/bridgetown-core/entry_filter.rb,
lib/bridgetown-core/model/origin.rb,
lib/bridgetown-core/commands/base.rb,
lib/bridgetown-core/configuration.rb,
lib/bridgetown-core/resource/base.rb,
lib/bridgetown-core/tags/post_url.rb,
lib/bridgetown-core/commands/apply.rb,
lib/bridgetown-core/commands/build.rb,
lib/bridgetown-core/commands/clean.rb,
lib/bridgetown-core/commands/start.rb,
lib/bridgetown-core/core_ext/psych.rb,
lib/bridgetown-core/drops/url_drop.rb,
lib/bridgetown-core/generated_page.rb,
lib/bridgetown-core/plugin_manager.rb,
lib/bridgetown-core/tags/class_map.rb,
lib/bridgetown-core/tags/highlight.rb,
lib/bridgetown-core/commands/doctor.rb,
lib/bridgetown-core/drops/site_drop.rb,
lib/bridgetown-core/liquid_renderer.rb,
lib/bridgetown-core/tags/asset_path.rb,
lib/bridgetown-core/utils/ruby_exec.rb,
lib/bridgetown-core/commands/console.rb,
lib/bridgetown-core/commands/esbuild.rb,
lib/bridgetown-core/commands/plugins.rb,
lib/bridgetown-core/commands/webpack.rb,
lib/bridgetown-core/liquid_extensions.rb,
lib/bridgetown-core/model/repo_origin.rb,
lib/bridgetown-core/commands/configure.rb,
lib/bridgetown-core/resource/relations.rb,
lib/bridgetown-core/ruby_template_view.rb,
lib/bridgetown-core/utils/require_gems.rb,
lib/bridgetown-core/converters/identity.rb,
lib/bridgetown-core/converters/markdown.rb,
lib/bridgetown-core/drops/resource_drop.rb,
lib/bridgetown-core/filters/from_liquid.rb,
lib/bridgetown-core/filters/url_filters.rb,
lib/bridgetown-core/model/plugin_origin.rb,
lib/bridgetown-core/tags/render_content.rb,
lib/bridgetown-core/concerns/localizable.rb,
lib/bridgetown-core/concerns/publishable.rb,
lib/bridgetown-core/drops/relations_drop.rb,
lib/bridgetown-core/filters/date_filters.rb,
lib/bridgetown-core/frontmatter_defaults.rb,
lib/bridgetown-core/liquid_renderer/file.rb,
lib/bridgetown-core/model/builder_origin.rb,
lib/bridgetown-core/resource/destination.rb,
lib/bridgetown-core/resource/transformer.rb,
lib/bridgetown-core/drops/bridgetown_drop.rb,
lib/bridgetown-core/drops/collection_drop.rb,
lib/bridgetown-core/liquid_renderer/table.rb,
lib/bridgetown-core/readers/layout_reader.rb,
lib/bridgetown-core/utils/loaders_manager.rb,
lib/bridgetown-core/commands/registrations.rb,
lib/bridgetown-core/concerns/prioritizable.rb,
lib/bridgetown-core/concerns/transformable.rb,
lib/bridgetown-core/drops/static_file_drop.rb,
lib/bridgetown-core/resource/taxonomy_term.rb,
lib/bridgetown-core/resource/taxonomy_type.rb,
lib/bridgetown-core/readers/defaults_reader.rb,
lib/bridgetown-core/tags/live_reload_dev_js.rb,
lib/bridgetown-core/utils/ruby_front_matter.rb,
lib/bridgetown-core/converters/erb_templates.rb,
lib/bridgetown-core/filters/grouping_filters.rb,
lib/bridgetown-core/commands/concerns/actions.rb,
lib/bridgetown-core/concerns/layout_placeable.rb,
lib/bridgetown-core/converters/ruby_templates.rb,
lib/bridgetown-core/drops/generated_page_drop.rb,
lib/bridgetown-core/filters/condition_helpers.rb,
lib/bridgetown-core/concerns/liquid_renderable.rb,
lib/bridgetown-core/drops/unified_payload_drop.rb,
lib/bridgetown-core/converters/liquid_templates.rb,
lib/bridgetown-core/converters/serbea_templates.rb,
lib/bridgetown-core/filters/translation_filters.rb,
lib/bridgetown-core/liquid_renderer/file_system.rb,
lib/bridgetown-core/filters/localization_filters.rb,
lib/bridgetown-core/resource/permalink_processor.rb,
lib/bridgetown-core/utils/smarty_pants_converter.rb,
lib/bridgetown-core/commands/concerns/git_helpers.rb,
lib/bridgetown-core/readers/plugin_content_reader.rb,
lib/bridgetown-core/commands/concerns/summarizable.rb,
lib/bridgetown-core/concerns/front_matter_importer.rb,
lib/bridgetown-core/generators/prototype_generator.rb,
lib/bridgetown-core/commands/concerns/build_options.rb,
lib/bridgetown-core/configuration/configuration_dsl.rb,
lib/bridgetown-core/converters/markdown/kramdown_parser.rb,
lib/bridgetown-core/commands/concerns/configuration_overridable.rb,
lib/bridgetown-core.rb,
lib/bridgetown-core.rb

Overview

Public: Methods that generate a URL for GeneratedPage.

Examples

URL.new({ :template => /:categories/:title.html", :placeholders => => "ruby", :title => "something" }).to_s

Defined Under Namespace

Modules: Commands, ConsoleMethods, Converters, CoreExt, Deprecator, Drops, ERBCapture, Errors, Filters, FrontMatterImporter, Hooks, LayoutPlaceable, LiquidExtensions, LiquidRenderable, Localizable, Model, Prioritizable, Publishable, Rack, Resource, Tags, Transformable, Utils, Watcher Classes: Cache, Cleaner, Collection, Component, Configuration, Converter, Current, DefaultsReader, ERBEngine, ERBView, EntryFilter, FrontmatterDefaults, GeneratedPage, Generator, Layout, LayoutReader, LiquidRenderer, LogAdapter, LogWriter, OutputBuffer, Plugin, PluginContentReader, PluginManager, PrototypeGenerator, PrototypePage, Reader, RubyTemplateView, SerbeaView, Site, Slot, StaticFile, URL, YAMLParser

Constant Summary collapse

VERSION =
"1.3.4"
CODE_NAME =
"Kelly Butte"

Class Method Summary collapse

Class Method Details

.begin!Object

Set up the Bridgetown execution environment before attempting to load any plugins or gems prior to a site build



141
142
143
144
145
146
# File 'lib/bridgetown-core.rb', line 141

def begin!
  ENV["RACK_ENV"] ||= environment

  Bridgetown::Current.preloaded_configuration = Bridgetown::Configuration::Preflight.new
  Bridgetown::PluginManager.setup_bundler
end

.build_errors_pathString

When there's a build error, error details will be logged to a file which the dev server can read and pass along to the browser.

Returns:

  • (String)

    the path to the cached errors file



385
386
387
388
389
390
391
# File 'lib/bridgetown-core.rb', line 385

def build_errors_path
  File.join(
    (Bridgetown::Current.site&.config || Bridgetown::Current.preloaded_configuration).root_dir,
    ".bridgetown-cache",
    "build_errors.txt"
  )
end

.configuration(override = {}) ⇒ Hash

Generate a Bridgetown configuration hash by merging the default options with anything in bridgetown.config.yml, and adding the given options on top.

Parameters:

  • override (Hash) (defaults to: {})
    • A an optional hash of config directives that override any options in both the defaults and the config file. See Bridgetown::Configuration::DEFAULTS for a list of option names and their defaults.

Returns:

  • (Hash)

    The final configuration hash.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/bridgetown-core.rb', line 158

def configuration(override = {})
  config = Configuration.new
  override = Configuration.new(override)
  unless override.delete("skip_config_files")
    config = config.read_config_files(config.config_files(override))
  end

  # Merge DEFAULTS < bridgetown.config.yml < override
  # @param obj [Bridgetown::Configuration]
  Configuration.from(Utils.deep_merge_hashes(config, override)).tap do |obj|
    set_timezone(obj["timezone"]) if obj["timezone"]

    # Copy "global" source manifests and initializers into this new configuration
    if Bridgetown::Current.preloaded_configuration.is_a?(Bridgetown::Configuration::Preflight)
      obj.source_manifests = Bridgetown::Current.preloaded_configuration.source_manifests

      if Bridgetown::Current.preloaded_configuration.initializers
        obj.initializers = Bridgetown::Current.preloaded_configuration.initializers
      end
    end

    Bridgetown::Current.preloaded_configuration = obj
  end
end

.configure(&block) ⇒ Object



255
256
257
# File 'lib/bridgetown-core.rb', line 255

def configure(&block)
  initializer :init, &block
end

.environmentObject Also known as: env

Tells you which Bridgetown environment you are building in so you can skip tasks if you need to.



134
135
136
# File 'lib/bridgetown-core.rb', line 134

def environment
  (ENV["BRIDGETOWN_ENV"] || "development").inquiry
end

.initializer(name, prepend: false, replace: false, &block) ⇒ Object

rubocop:todo Metrics



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/bridgetown-core.rb', line 215

def initializer(name, prepend: false, replace: false, &block) # rubocop:todo Metrics
  unless Bridgetown::Current.preloaded_configuration
    raise "The `#{name}' initializer in #{block.source_location[0]} was called " \
          "without a preloaded configuration"
  end

  Bridgetown::Current.preloaded_configuration.initializers ||= {}

  if Bridgetown::Current.preloaded_configuration.initializers.key?(name.to_sym)
    if replace
      Bridgetown.logger.warn(
        "Initializing:",
        "The previous `#{name}' initializer was replaced by a new initializer"
      )
    else
      prev_block = Bridgetown::Current.preloaded_configuration.initializers[name.to_sym].block
      new_block = block
      block = if prepend
                proc do |*args, **kwargs|
                  new_block.(*args, **kwargs)
                  prev_block.(*args, **kwargs)
                end
              else
                proc do |*args, **kwargs|
                  prev_block.(*args, **kwargs)
                  new_block.(*args, **kwargs)
                end
              end
    end
  end

  Bridgetown::Current.preloaded_configuration.initializers[name.to_sym] =
    Bridgetown::Configuration::Initializer.new(
      name: name.to_sym,
      block: block,
      completed: false
    )
end

.load_dotenv(root:) ⇒ Object

Loads ENV configuration via dotenv gem, if available

Parameters:

  • root (String)

    root of Bridgetown site



282
283
284
285
286
287
288
289
290
# File 'lib/bridgetown-core.rb', line 282

def load_dotenv(root:)
  dotenv_files = [
    File.join(root, ".env.#{Bridgetown.env}.local"),
    (File.join(root, ".env.local") unless Bridgetown.env.test?),
    File.join(root, ".env.#{Bridgetown.env}"),
    File.join(root, ".env"),
  ].compact
  Dotenv.load(*dotenv_files)
end

.load_tasksObject



266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/bridgetown-core.rb', line 266

def load_tasks
  require "bridgetown-core/commands/base"
  unless Bridgetown::Current.preloaded_configuration
    Bridgetown::Current.preloaded_configuration = Bridgetown::Configuration::Preflight.new
  end
  Bridgetown::PluginManager.setup_bundler(skip_yarn: true)

  if Bridgetown::Current.preloaded_configuration.is_a?(Bridgetown::Configuration::Preflight)
    Bridgetown::Current.preloaded_configuration = Bridgetown.configuration
  end
  load File.expand_path("bridgetown-core/tasks/bridgetown_tasks.rake", __dir__)
end

.loggerLogAdapter

Fetch the logger instance for this Bridgetown process.

Returns:



326
327
328
# File 'lib/bridgetown-core.rb', line 326

def logger
  @logger ||= LogAdapter.new(LogWriter.new, (ENV["BRIDGETOWN_LOG_LEVEL"] || :info).to_sym)
end

.logger=(writer) ⇒ LogAdapter

Set the log writer. New log writer must respond to the same methods as Ruby's internal Logger.

Parameters:

  • writer (Object)

    the new Logger-compatible log transport

Returns:



336
337
338
# File 'lib/bridgetown-core.rb', line 336

def logger=(writer)
  @logger = LogAdapter.new(writer, (ENV["BRIDGETOWN_LOG_LEVEL"] || :info).to_sym)
end

.register_command(&block) ⇒ Object

Convenience method to register a new Thor command



262
263
264
# File 'lib/bridgetown-core.rb', line 262

def register_command(&block)
  Bridgetown::Commands::Registrations.register(&block)
end

.reset_configuration!Bridgetown::Configuration::Preflight

Initialize a preflight configuration object, copying initializers and source manifests from a previous standard configuration if necessary. Typically only needed in test suites to reset before a new test.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/bridgetown-core.rb', line 188

def reset_configuration! # rubocop:disable Metrics/AbcSize
  if Bridgetown::Current.preloaded_configuration.nil?
    return Bridgetown::Current.preloaded_configuration =
             Bridgetown::Configuration::Preflight.new
  end

  return unless Bridgetown::Current.preloaded_configuration.is_a?(Bridgetown::Configuration)

  previous_config = Bridgetown::Current.preloaded_configuration
  new_config = Bridgetown::Configuration::Preflight.new
  new_config.initializers = previous_config.initializers
  new_config.source_manifests = previous_config.source_manifests
  if new_config.initializers
    new_config.initializers.delete(:init)
    new_config.initializers.select! do |_k, initializer|
      next false if initializer.block.source_location[0].start_with?(
        File.join(previous_config.root_dir, "config")
      )

      initializer.completed = false
      true
    end
  end

  Bridgetown::Current.preloaded_configuration = new_config
end

.sanitized_path(base_directory, questionable_path) ⇒ String

Ensures the questionable path is prefixed with the base directory and prepends the questionable path with the base directory if false.

Parameters:

  • base_directory (String)

    the directory with which to prefix the questionable path

  • questionable_path (String)

    the path we're unsure about, and want prefixed

Returns:

  • (String)

    the sanitized path



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/bridgetown-core.rb', line 360

def sanitized_path(base_directory, questionable_path)
  return base_directory if base_directory.eql?(questionable_path)

  clean_path = questionable_path.dup
  clean_path.insert(0, "/") if clean_path.start_with?("~")
  clean_path = File.expand_path(clean_path, "/")

  return clean_path if clean_path.eql?(base_directory)

  # remove any remaining extra leading slashes not stripped away by calling
  # `File.expand_path` above.
  clean_path.squeeze!("/")

  if clean_path.start_with?(base_directory.sub(%r!\z!, "/"))
    clean_path
  else
    clean_path.sub!(%r!\A\w:/!, "/")
    File.join(base_directory, clean_path)
  end
end

.set_timezone(timezone) ⇒ void

This method returns an undefined value.

Set the TZ environment variable to use the timezone specified

rubocop:disable Naming/AccessorMethodName

Parameters:

  • timezone (String)

    the IANA Time Zone



310
311
312
# File 'lib/bridgetown-core.rb', line 310

def set_timezone(timezone)
  ENV["TZ"] = timezone
end

.sitesArray<Bridgetown::Site>

Deprecated. Now using the Current site.

Returns:



343
344
345
346
347
348
349
# File 'lib/bridgetown-core.rb', line 343

def sites
  Deprecator.deprecation_message(
    "Bridgetown.sites will be removed in the next version. Use Bridgetown::Current.sites" \
    "instead"
  )
  [Bridgetown::Current.site].compact
end

.timezoneString

Get the current TZ environment variable

Returns:

  • (String)


317
318
319
# File 'lib/bridgetown-core.rb', line 317

def timezone
  ENV["TZ"]
end

.with_unbundled_env(&block) ⇒ void

This method returns an undefined value.

Determines the correct Bundler environment block method to use and passes the block on to it.



296
297
298
299
300
301
302
# File 'lib/bridgetown-core.rb', line 296

def with_unbundled_env(&block)
  if Bundler.bundler_major_version >= 2
    Bundler.method(:with_unbundled_env).call(&block)
  else
    Bundler.method(:with_clean_env).call(&block)
  end
end