# Nick's web site: css_source filter. Add a notice to generated CSS # files where the reader may find the source code. # # Copyright © 2020 Nick Bowler # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . class CssSourceFilter < Nanoc::Filter identifier :css_source def run(content, params = {}) srcuri = if params[:uribase] then params[:uribase] + item_source(@item) end copypattern = /\/\*.*?[Cc]opyright.*?\*\//m css = content.gsub(copypattern, "") css.gsub!(/^@charset.*?$\n?/m, "") begin copymsgs = @item.compiled_content(snapshot: :css_source) rescue Nanoc::Core::Errors::NoSuchSnapshot copymsgs = content end copymsgs = copymsgs.scan(copypattern) ["/*", " * Generated file, do not edit!", " * Compiled from " + item_source(@item) + " on " + Time.now.gmtime.strftime("%Y-%m-%d %H:%M UTC"), if srcuri then " * Source code is available online at <" + srcuri + ">" end, "*/", copymsgs, css].compact.join("\n") end end