Skip to content

Commit

Permalink
Configure sites in subfolders via base_path, not baseurl (#348)
Browse files Browse the repository at this point in the history
* Configure sites in subfolders via base_path, not baseurl

* Add baseurl back onto Site drop

* Restore missing rubocop comments
  • Loading branch information
jaredcwhite authored Jul 18, 2021
1 parent a5c2a19 commit 9eb597f
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def self.extended(klass)
klass.class_option :limit_posts,
type: :numeric,
desc: "Limits the number of posts to parse and publish"
klass.class_option :baseurl,
klass.class_option :base_path,
aliases: "-b",
desc: "Serve the website from the given base URL"
desc: "Serve the website from the given base path"
klass.class_option :force_polling,
type: :boolean,
desc: "Force watch to use polling"
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/commands/serve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def webrick_opts(opts)

def start_up_webrick(opts, destination)
@server = WEBrick::HTTPServer.new(webrick_opts(opts)).tap { |o| o.unmount("") }
@server.mount(opts["baseurl"].to_s, Servlet, destination, file_handler_opts)
@server.mount(opts["base_path"].to_s, Servlet, destination, file_handler_opts)

Bridgetown.logger.info "Server address:", server_address(@server, opts)
launch_browser @server, opts if opts["open_url"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def config=(config)
configure_include_paths
configure_file_read_opts

self.baseurl = config.baseurl
self.permalink_style = (config["permalink"] || "pretty").to_sym

@config
Expand All @@ -34,6 +33,22 @@ def uses_resource?
config[:content_engine] == "resource"
end

# Returns a base path from which the site is served (aka `/cool-site`) or
# `/` if served from root.
#
# @param strip_slash_only [Boolean] set to true if you wish "/" to be returned as ""
# @return [String]
def base_path(strip_slash_only: false)
(config[:base_path] || config[:baseurl]).yield_self do |path|
strip_slash_only ? path.to_s.sub(%r{^/$}, "") : path
end
end

def baseurl
Bridgetown::Deprecator.deprecation_message "Site#baseurl is now Site#base_path"
base_path(strip_slash_only: true).presence
end

def defaults_reader
@defaults_reader ||= Bridgetown::DefaultsReader.new(self)
end
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Configuration < HashWithDotAccess::Hash
"detach" => false, # default to not detaching the server
"port" => "4000",
"host" => "127.0.0.1",
"baseurl" => nil, # this mounts at /, i.e. no subdirectory
"base_path" => "/",
"show_dir_listing" => false,

# Output Configuration
Expand Down
2 changes: 2 additions & 0 deletions bridgetown-core/lib/bridgetown-core/drops/site_drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class SiteDrop < Drop
mutable false

def_delegators :@obj,
:baseurl, # deprecated
:base_path,
:data,
:locale,
:time,
Expand Down
12 changes: 4 additions & 8 deletions bridgetown-core/lib/bridgetown-core/filters/url_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Filters
module URLFilters
extend self

# Produces an absolute URL based on site.url and site.baseurl.
# Produces an absolute URL based on site.url and site.base_path.
#
# input - the URL to make absolute.
#
Expand All @@ -15,7 +15,7 @@ def absolute_url(input)
cache[input] ||= compute_absolute_url(input)
end

# Produces a URL relative to the domain root based on site.baseurl
# Produces a URL relative to the domain root based on site.base_path
# unless it is already an absolute url with an authority (host).
#
# input - the URL to make relative to the domain root
Expand Down Expand Up @@ -70,17 +70,13 @@ def compute_relative_url(input)
input = input.url if input.respond_to?(:url)
return input if Addressable::URI.parse(input.to_s).absolute?

parts = [sanitized_baseurl, input]
site = @context.registers[:site]
parts = [site.base_path.chomp("/"), input]
Addressable::URI.parse(
parts.compact.map { |part| ensure_leading_slash(part.to_s) }.join
).normalize.to_s
end

def sanitized_baseurl
site = @context.registers[:site]
site.config["baseurl"].to_s.chomp("/")
end

def ensure_leading_slash(input)
return input if input.nil? || input.empty? || input.start_with?("/")

Expand Down
4 changes: 3 additions & 1 deletion bridgetown-core/lib/bridgetown-core/resource/destination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def final_ext

def output_path
path = URL.unescape_path(relative_url)
path = path.delete_prefix(resource.site.baseurl) if resource.site.baseurl.present?
if resource.site.base_path.present?
path = path.delete_prefix resource.site.base_path(strip_slash_only: true)
end
path = resource.site.in_dest_dir(path)
path = File.join(path, "index.html") if relative_url.end_with? "/"
path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def transform
# No relative URLs should ever end in /index.html
new_url.sub!(%r{/index$}, "") if final_ext == ".html"

add_baseurl finalize_permalink(new_url, permalink)
add_base_path finalize_permalink(new_url, permalink)
end

def process_segment(segment)
Expand Down Expand Up @@ -96,8 +96,12 @@ def finalize_permalink(new_url, permalink)
end
end

def add_baseurl(permalink)
resource.site.baseurl.present? ? "#{resource.site.baseurl}#{permalink}" : permalink
def add_base_path(permalink)
if resource.site.base_path.present?
return "#{resource.site.base_path(strip_slash_only: true)}#{permalink}"
end

permalink
end

### Default Placeholders Processors
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Site
# is default
alias_method :generated_pages, :pages

attr_accessor :permalink_style, :time, :baseurl, :data,
attr_accessor :permalink_style, :time, :data,
:file_read_opts, :plugin_manager, :converters,
:generators, :reader

Expand Down
5 changes: 3 additions & 2 deletions bridgetown-core/lib/bridgetown-core/static_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ def path
def destination(dest)
dest = site.in_dest_dir(dest)
dest_url = url
dest_url = dest_url.delete_prefix(site.baseurl) if site.uses_resource? &&
site.baseurl.present? && collection
if site.uses_resource? && site.base_path.present? && collection
dest_url = dest_url.delete_prefix site.base_path(strip_slash_only: true)
end
site.in_dest_dir(dest, Bridgetown::URL.unescape_path(dest_url))
end

Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def parse_webpack_manifest_file(site, asset_type)

def static_frontend_path(site, additional_parts = [])
path_parts = [
site.config["baseurl"].to_s.gsub(%r(^/|/$), ""),
site.base_path.gsub(%r(^/|/$), ""),
"_bridgetown/static",
*additional_parts,
]
Expand Down
6 changes: 5 additions & 1 deletion bridgetown-core/lib/site_template/bridgetown.config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
# https://learnxinyminutes.com/docs/yaml/
#

baseurl: "" # OPTIONAL: the subpath of your site, e.g. /blog
url: "" # the base hostname & protocol for your site, e.g. https://example.com

permalink: pretty

# Other options you might want to investigate:
#
# base_path: "/" # the subpath of your site, e.g. /blog
# timezone: America/Los_Angeles
# pagination:
# enabled: true
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
title: Your awesome title
tagline: This site is totally awesome
email: [email protected]
description: >- # this means to ignore newlines until "baseurl:"
description: >-
Write an awesome description for your new site here. It will appear in your document head meta (for Google search results) and in your feed.xml site description.
Loading

0 comments on commit 9eb597f

Please sign in to comment.