Created
May 23, 2012 15:36
-
-
Save knalli/2775940 to your computer and use it in GitHub Desktop.
SASS Extension for dynamic absolute paths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This extension makes the SASS function "application_resources_url" available, which will return the absolute path within a spring mvc managed application. | |
# | |
# The implementation relays on a central java properties file located at src/main/resources/application.properties. It should contain at least following keys | |
# webapp.version -- a string, e.g. 1.0.0 | |
# webapp.build -- a string, e.g. 45 | |
# webapp.context -- a string, e.g. app (optional, if empty only / will be used) | |
# | |
# Version 1.0 (2012-05-23, Jan Philipp <[email protected]>) | |
# Under MIT License | |
# | |
# Helper for application_resources_url() | |
class JavaProperties | |
def self.load(path) | |
File.open(path) | |
end | |
def self.get(path, key) | |
result=nil | |
File.open(path) { |file| opts=YAML.load(file); result = opts[key]} | |
return result | |
end | |
end | |
# Usage, e.g.: Write: $pie-behavior: url(application_resources_url() + "/stylesheets/PIE.htc"); | |
# Result: url("/app/resources-1.0.0.0/stylesheets/PIE.htc"); | |
module Sass::Script::Functions | |
def application_resources_url() | |
context = JavaProperties.get 'src/main/resources/application.properties', 'webapp.context' | |
version = JavaProperties.get 'src/main/resources/application.properties', 'webapp.version' | |
build = JavaProperties.get 'src/main/resources/application.properties', 'webapp.build' | |
path = "/" | |
path += "#{context}/" if context | |
path += "resources" | |
path += "-#{version}.#{build}" if version | |
Sass::Script::String.new(path) | |
end | |
declare :application_resources_url, :args => [] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment