]> git.draconx.ca Git - homepage.git/blob - lib/deadlinks.rb
Fix media queries that disable justification.
[homepage.git] / lib / deadlinks.rb
1 # Nick's web site: Workarounds for Nanoc's busted handling of broken symlinks.
2 #
3 # Copyright © 2020 Nick Bowler
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18 # Monkey patch the filesystem data source to adjust some strange behavoiur.
19 class Nanoc::DataSources::Filesystem < Nanoc::DataSource
20     # The filesystem mtime_of crashes on broken symlinks.  Let's not do that,
21     # instead we'll fall back to the link mtime for broken links.
22     def mtime_of(*args)
23         mtimes = args.compact.map do |f|
24             File.stat(f).mtime
25         rescue Errno::ENOENT
26             File.lstat(f).mtime
27         end
28         mtimes.max
29     end
30
31     module Tools
32         @dummy_file = Tempfile.new('dummy')
33
34         # The original resolve_symlink helper uses a readlink loop which
35         # crashes on broken links.  I'm also fairly sure symlinks in path
36         # components are not correctly resolved.  Let's just use realpath
37         # instead and hope ruby itself doesn't have these bugs.
38         def resolve_symlink(filename, recursion_limit = nil)
39             File.realpath(filename)
40         rescue Errno::ENOENT
41             # Dead link.  Return a valid filename otherwise the filesystem
42             # source does bizarre things.  The actual items are created with
43             # the original filename so this dummy file does not appear to
44             # leak outside of the filesystem data source implementation.
45             @dummy_file.path
46         end
47         module_function :resolve_symlink
48     end
49 end