Skip to content

Custom function in list scss for only sass_src #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}<link rel="stylesheet" href="{% sass_src 'bs4demo/css/main.scss' %}" type="text/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

Expand Down
27 changes: 26 additions & 1 deletion sass_processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down