X-Git-Url: https://git.draconx.ca/gitweb/homepage.git/blobdiff_plain/ff1ac7e6d5d6acb82977353b9155a276a8077980..b585d7ee7724970904ba0bd77d84c2337159b4b3:/lib/css-source.rb diff --git a/lib/css-source.rb b/lib/css-source.rb new file mode 100644 index 0000000..211c155 --- /dev/null +++ b/lib/css-source.rb @@ -0,0 +1,44 @@ +# 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