]> git.draconx.ca Git - homepage.git/blobdiff - lib/scss-var.rb
Let's start a blog!
[homepage.git] / lib / scss-var.rb
diff --git a/lib/scss-var.rb b/lib/scss-var.rb
new file mode 100644 (file)
index 0000000..c6cf654
--- /dev/null
@@ -0,0 +1,54 @@
+# Nick's web site: Helper to retrieve global style variables in ruby.
+#
+# 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 <https://www.gnu.org/licenses/>.
+
+require 'sass'
+
+class GetSCSSGlobals < Sass::Tree::Visitors::Perform
+    def self.visit(root, name = nil)
+        x = new(nil)
+        x.send(:visit, root)
+        result = x.instance_variable_get(:@globals)
+        if name.nil?
+            return result.freeze
+        else
+            return result[name]
+        end
+    end
+
+    protected
+
+    def initialize(environment)
+        @globals = {}
+        @globals.default_proc = proc { |h, k|
+            if h.key? k.to_s then h[k.to_s] end }
+        super
+    end
+
+    def visit_variable(node)
+        super
+        
+        x = @environment.global_env.var(node.name)
+        if !x.nil?
+            @globals[node.name] = x
+        end
+    end
+end
+
+def scss_get_var(variable, item = @items["/style.scss"])
+    engine = Sass::Engine.for_file(item.raw_filename, { :syntax => :scss })
+    return GetSCSSGlobals.visit(engine.to_tree, variable)
+end