Skip to content

Commit

Permalink
Make urls generate based on the path given at write time
Browse files Browse the repository at this point in the history
  • Loading branch information
waferbaby committed Feb 7, 2024
1 parent ed85973 commit c7b4f65
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
10 changes: 6 additions & 4 deletions lib/dimples/sources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ def parse_metadata(contents)
end

def write(output_path = nil, metadata = {})
output = render(metadata)

output_path = File.join(output_directory, filename) if output_path.nil?
output_dir = File.dirname(output_path)

@metadata[:url] = url_for(output_dir)

output = render(metadata)

FileUtils.mkdir_p(output_dir) unless File.directory?(output_dir)
File.write(output_path, output)
end
Expand All @@ -54,8 +56,8 @@ def output_directory
@site.config[:output][:root]
end

def url
@metadata[:url] || output_directory.gsub(@site.config[:output][:root], '')
def url_for(path)
path.gsub(@site.config[:output][:root], '').concat('/')
end

def template
Expand Down
2 changes: 1 addition & 1 deletion lib/dimples/sources/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def output_directory
end

def url
@metadata[:url] || super.tap do |url|
super.tap do |url|
url.concat(filename) unless filename == 'index.html'
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/dimples/sources/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def output_directory
@output_directory ||= File.dirname(@path).gsub(
@site.config[:sources][:posts],
@site.config[:output][:posts]
).concat("/#{@metadata[:slug]}/")
).concat("/#{slug}/")
end

def slug
File.basename(@path)
end

def template
Expand Down

0 comments on commit c7b4f65

Please sign in to comment.