Skip to content

Commit

Permalink
- Add support for named directory cover pages
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Mar 26, 2024
1 parent 6fa2afb commit 19ae5c5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .envrc.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# for git-changelog (https://github.com/dannyben/git-changelog)
export CHANGELOG_COMMIT_URL=https://github.com/DannyBen/madness/commit/%h
export CHANGELOG_COMPARE_URL=https://github.com/dannyben/madness/compare/%s

# enable debugging with the debug gem
export DEBUGGER=1
7 changes: 4 additions & 3 deletions Runfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'madness'
require "sasstool"
require "slim"
require "pretty_trace/enable-trim"
require 'sasstool'
require 'slim'
require 'pretty_trace/enable-trim'
require 'debug' rescue nil

title "Madness Runfile"
summary "Runfile tasks for building the Madness gem"
Expand Down
2 changes: 2 additions & 0 deletions lib/madness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
requires 'madness/settings'
requires 'madness/server_helper'
requires 'madness'

require 'debug' rescue nil if ENV['DEBUGGER']
26 changes: 19 additions & 7 deletions lib/madness/directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ def list
private

def files
result = Dir["#{dir}/#{config.dir_glob}"]
result.reject! do |f|
['README.md', 'index.md'].include? File.basename(f)
@files ||= begin
result = Dir["#{dir}/#{config.dir_glob}"]
result.reject! do |f|
['README.md', 'index.md'].include? File.basename(f)
end
result.nat_sort.map { |path| Item.new path, :file }
end
result.nat_sort.map { |path| Item.new path, :file }
end

def dirs
result = Dir["#{dir}/*"].select { |f| File.directory? f }
result.reject! { |f| exclude? f }
result.nat_sort.map { |path| Item.new path, :dir }
@dirs ||= begin
result = Dir["#{dir}/*"].select { |f| File.directory? f }
result.reject! { |f| exclude?(f) || has_named_cover_page?(f) }
result.nat_sort.map { |path| Item.new path, :dir }
end
end

def paths
@paths ||= files.map(&:path)
end

def has_named_cover_page?(path)
paths.include? "#{path}.md"
end

def exclude?(path)
Expand Down
26 changes: 22 additions & 4 deletions lib/madness/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ def set_base_attributes_for_directory
@dir = base
@type = :readme

if File.exist? "#{base}/index.md"
@file = "#{base}/index.md"
elsif File.exist? "#{base}/README.md"
@file = "#{base}/README.md"
if cover_page
@file = cover_page
else
@type = :empty
end
Expand All @@ -79,5 +77,25 @@ def md_file?
def md_filename
File.extname(base) == '.md' ? base : "#{base}.md"
end

def cover_page
@cover_page ||= cover_page!
end

def cover_page!
cover_page_candidates.each do |candidate|
return candidate if File.exist? candidate
end

nil
end

def cover_page_candidates
@cover_page_candidates ||= [
File.expand_path('index.md', base),
File.expand_path("../#{File.basename(base)}.md", base),
File.expand_path('README.md', base),
]
end
end
end

0 comments on commit 19ae5c5

Please sign in to comment.