diff --git a/README.md b/README.md index 6207481..60f0fea 100644 --- a/README.md +++ b/README.md @@ -421,7 +421,43 @@ documentation. Such customized functions must accept parameters explicilty, otherwise `sass_processor` does not know how to map them. Variable argument lists therefore can not be used. +Starting from the fact that libsass can not use the custom_functions in a list scss natively. An action has been implemented to overcome this and concerns only the source sass_src file. +The conditions are as follows: ++ the name of the function must have "presass" in its name. ++ must not have args in parameter. + +Its operation is as follows: +``` +{% addtoblock "css" %}{% endaddtoblock %} +``` +From the main.scss files, a main__presass.scss file is created which has hard values in the files. +Then, to check if main.scss has been modified, the file css.map is scrapper to restore its source origin. + +Example: +settings.py +``` +SASS_PROCESSOR_CUSTOM_FUNCTIONS = { + 'get-color-primary-presass': 'cmsplugin_cascade.theme.utils.get_color_primary', + 'get-color-secondary-presass': 'cmsplugin_cascade.theme.utils.get_color_secondary', + 'get-color-success-presass': 'cmsplugin_cascade.theme.utils.get_color_success', + 'get-color-warning-presass': 'cmsplugin_cascade.theme.utils.get_color_warning', + 'get-color-danger-presass': 'cmsplugin_cascade.theme.utils.get_color_danger', + 'get-color-info-presass': 'cmsplugin_cascade.theme.utils.get_color_info', + 'get-color-light-presass': 'cmsplugin_cascade.theme.utils.get_color_light', + 'get-color-dark-presass': 'cmsplugin_cascade.theme.utils.get_color_dark', +} +``` + +main.scss: +``` +@import "variables"; +$theme-colors: ( + primary: get-color-primary-presass() +); +@import "bootstrap/scss/bootstrap"; +@import "footer"; +``` ## Error reporting diff --git a/sass_processor/processor.py b/sass_processor/processor.py index 0d80914..8d04168 100644 --- a/sass_processor/processor.py +++ b/sass_processor/processor.py @@ -72,8 +72,28 @@ def __call__(self, path): # otherwise compile the SASS/SCSS file into .css and store it filename_map = filename.replace(ext, '.css.map') + + # 1 fake custom_function in list for only master source scss + custom_function_presass = {} + for custom_function in get_custom_functions() : + #check if tuple index out of range + if 'presass' in custom_function.name: + custom_function_presass.update({ custom_function.name :custom_function() }) + filename_scss = filename.replace(ext, '__presass.scss') + with open(filename, "rt") as fin: + with open(filename_scss , "wt") as fout: + for line in fin: + linem = None + for key , value in custom_function_presass.items(): + if key in line: + linem = line.replace('{}{}'.format(key, '()'), value) + if linem: + fout.write(linem) + else: + fout.write(line) + compile_kwargs = { - 'filename': filename, + 'filename': filename_scss, 'source_map_filename': filename_map, 'include_paths': self.include_paths + APPS_INCLUDE_DIRS, 'custom_functions': get_custom_functions(), @@ -131,10 +151,15 @@ def is_latest(self, sourcemap_filename, base): sourcemap_file = find_file(sourcemap_filename) if not sourcemap_file or not os.path.isfile(sourcemap_file): return False + sourcemap_mtime = os.stat(sourcemap_file).st_mtime + with open(sourcemap_file, 'r') as fp: sourcemap = json.load(fp) for srcfilename in sourcemap.get('sources'): + # 2 fake custom_function in list for master source scss + srcfilename = srcfilename.replace('__presass', '') + srcfilename = os.path.join(base, srcfilename) if not os.path.isfile(srcfilename) or os.stat(srcfilename).st_mtime > sourcemap_mtime: # at least one of the source is younger that the sourcemap referring it