]> git.draconx.ca Git - homepage.git/blob - lib/css-source.rb
cdecl99-1.3 bash-5 hotfix
[homepage.git] / lib / css-source.rb
1 # Nick's web site: css_source filter.  Add a notice to generated CSS
2 # files where the reader may find the source code.
3 #
4 # Copyright © 2020-2021 Nick Bowler
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19 class CssSourceFilter < Nanoc::Filter
20     identifier :css_source
21
22     def run(content, params = {})
23         srcuri = if params[:uribase]
24             then params[:uribase] + item_source(@item) end
25         copypattern = /\/\*.*?[Cc]opyright.*?\*\//m
26         css = content.gsub(copypattern, "")
27         css.gsub!(/^@charset.*?$\n?/m, "")
28
29         begin
30             copymsgs = @item.compiled_content(snapshot: :css_source)
31         rescue Nanoc::Core::Errors::NoSuchSnapshot
32             copymsgs = content
33         end
34         copymsgs = copymsgs.scan(copypattern)
35
36         ["/*!",
37          " * Generated file, do not edit!",
38          " * Compiled from " + item_source(@item) + " on " +
39             Time.now.gmtime.strftime("%Y-%m-%d %H:%M UTC"),
40          if srcuri then
41             " * Source code is available online at <" + srcuri + ">"
42          end, "*/", copymsgs, css].compact.join("\n")
43     end
44 end