1 # Nick's web site: css_source filter. Add a notice to generated CSS
2 # files where the reader may find the source code.
4 # Copyright © 2020-2021 Nick Bowler
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.
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.
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/>.
19 class CssSourceFilter < Nanoc::Filter
20 identifier :css_source
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, "")
30 copymsgs = @item.compiled_content(snapshot: :css_source)
31 rescue Nanoc::Core::Errors::NoSuchSnapshot
34 copymsgs = copymsgs.scan(copypattern)
37 " * Generated file, do not edit!",
38 " * Compiled from " + item_source(@item) + " on " +
39 Time.now.gmtime.strftime("%Y-%m-%d %H:%M UTC"),
41 " * Source code is available online at <" + srcuri + ">"
42 end, "*/", copymsgs, css].compact.join("\n")