bulma/docs/_scripts/plugins/utils.js

214 lines
5.6 KiB
JavaScript
Raw Normal View History

2018-06-16 17:26:55 +00:00
const fs = require('fs');
2018-06-16 18:58:58 +00:00
const path = require('path');
2018-06-16 17:26:55 +00:00
2018-06-16 19:47:52 +00:00
const base_path = '../_data/variables/';
2018-06-17 00:05:48 +00:00
const css_keywords = ['null', 'ease-out', 'optimizeLegibility'];
const regex_unitless = /^([0-9]+\.[0-9]+)$/;
2018-06-16 19:47:52 +00:00
2018-06-16 18:01:35 +00:00
let utils = {
2018-06-17 00:05:48 +00:00
getVariableType: (name, value) => {
2018-06-16 19:47:52 +00:00
if (!value) {
return 'string';
}
2018-06-17 00:05:48 +00:00
if (name == '$sizes') {
return 'map';
}
if (
value.startsWith('hsl') ||
value.startsWith('#') ||
value.startsWith('rgb')
) {
2018-06-16 18:01:35 +00:00
return 'color';
2018-06-17 00:05:48 +00:00
} else if (css_keywords.includes(value)) {
return 'keyword';
} else if (value.startsWith('mergeColorMaps')) {
return 'map';
} else if (value.startsWith('findColorInvert')) {
2018-06-17 00:05:48 +00:00
return 'function';
2018-06-16 18:01:35 +00:00
} else if (value.startsWith('$')) {
return 'variable';
} else if (value.startsWith('BlinkMacSystemFont') || value == 'monospace') {
2018-06-16 18:01:35 +00:00
return 'font-family';
} else if (value == 'true' || value == 'false') {
2018-06-16 18:01:35 +00:00
return 'boolean';
} else if (name.endsWith('shadow')) {
return 'shadow';
2018-06-17 00:05:48 +00:00
} else if (value.endsWith('ms')) {
return 'duration';
2018-06-16 18:01:35 +00:00
} else if (value.includes('+')) {
return 'computed';
} else if (value.endsWith('00') || value == 'normal') {
2018-06-16 18:01:35 +00:00
return 'font-weight';
2018-06-17 00:05:48 +00:00
} else if (
value.endsWith('px') ||
value.endsWith('em') ||
value.includes('px ') ||
value.includes('em ')
) {
2018-06-16 18:01:35 +00:00
return 'size';
2018-06-16 19:47:52 +00:00
} else if (value.includes('$')) {
return 'compound';
2018-06-17 00:05:48 +00:00
} else if (value.match(regex_unitless)) {
return 'unitless';
2018-06-16 18:01:35 +00:00
}
2018-06-16 17:26:55 +00:00
2018-06-16 18:01:35 +00:00
return 'string';
},
2018-06-16 17:26:55 +00:00
2018-06-16 18:01:35 +00:00
parseLine: (line) => {
if (line.startsWith('$') && line.endsWith('!default')) {
const colon_index = line.indexOf(':');
const variable_name = line.substring(0, colon_index).trim();
2018-06-16 17:26:55 +00:00
2018-06-16 18:01:35 +00:00
const default_index = line.indexOf('!default');
let variable_value = line
.substring(colon_index + 1, default_index)
.trim();
2018-06-16 17:26:55 +00:00
return (variable = {
2018-06-16 18:01:35 +00:00
name: variable_name,
value: variable_value,
2018-06-17 00:05:48 +00:00
type: utils.getVariableType(variable_name, variable_value),
});
2018-06-16 18:01:35 +00:00
}
2018-06-16 17:26:55 +00:00
2018-06-16 18:01:35 +00:00
return false;
},
2018-06-16 17:26:55 +00:00
2018-06-16 18:01:35 +00:00
getLines: (files, file_path) => {
const file = files[file_path];
const slash_index = file_path.lastIndexOf('/');
const dot_index = file_path.lastIndexOf('.');
const file_name = file_path.substring(slash_index + 1, dot_index);
2018-06-16 17:26:55 +00:00
2018-06-16 18:01:35 +00:00
return {
file_name,
lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
};
2018-06-16 18:01:35 +00:00
},
writeFile: (file_path, data) => {
const json_data = JSON.stringify(data, null, ' ');
2018-06-16 19:47:52 +00:00
const json_file_path = base_path + file_path.replace('.sass', '.json');
2018-06-16 18:58:58 +00:00
utils.ensureDirectoryExistence(json_file_path);
2018-06-16 18:01:35 +00:00
fs.writeFile(json_file_path, json_data, (err) => {
2018-06-16 18:01:35 +00:00
if (err) {
return console.log(err);
}
2018-06-16 18:58:58 +00:00
console.log(`The file ${json_file_path} was saved!`);
2018-06-16 18:01:35 +00:00
});
},
2018-06-16 17:26:55 +00:00
2018-06-16 18:58:58 +00:00
getInitialValue: (value, type, initial_variables) => {
2018-06-16 18:01:35 +00:00
if (value.startsWith('$') && value in initial_variables.by_name) {
const second_value = initial_variables.by_name[value].value;
2018-06-16 17:26:55 +00:00
if (
second_value.startsWith('$') &&
second_value in initial_variables.by_name
) {
2018-06-16 18:01:35 +00:00
const third_value = initial_variables.by_name[second_value].value;
return third_value;
}
return second_value;
2018-06-16 17:26:55 +00:00
}
2018-06-16 18:01:35 +00:00
return value;
2018-06-16 18:58:58 +00:00
},
getComputedData: (
name,
value,
type,
initial_variables,
derived_variables = {}
) => {
2018-06-16 19:47:52 +00:00
let computed_value = '';
2018-06-16 18:58:58 +00:00
if (value.startsWith('$')) {
let second_value;
if (value in initial_variables.by_name) {
second_value = initial_variables.by_name[value].value;
} else if (
derived_variables.by_name &&
value in derived_variables.by_name
) {
2018-06-16 18:58:58 +00:00
second_value = derived_variables.by_name[value].value;
}
if (second_value && second_value.startsWith('$')) {
let third_value;
if (second_value in initial_variables.by_name) {
third_value = initial_variables.by_name[second_value].value;
} else if (
derived_variables.by_name &&
second_value in derived_variables.by_name
) {
2018-06-16 18:58:58 +00:00
third_value = derived_variables.by_name[second_value].value;
}
2018-06-16 19:47:52 +00:00
computed_value = third_value;
} else {
computed_value = second_value;
2018-06-16 18:58:58 +00:00
}
2018-06-17 00:05:48 +00:00
} else if (value.startsWith('findColorInvert')) {
// e.g. $turquoise-invert -> findColorInvert($turquoise)
if (value.endsWith('($yellow)')) {
computed_value = 'rgba(0, 0, 0, 0.7)';
} else {
computed_value = '#fff';
}
2018-06-16 19:47:52 +00:00
}
2018-06-16 18:58:58 +00:00
2018-06-17 00:05:48 +00:00
if (computed_value && computed_value != '') {
// e.g. $primary-invert -> $turquoise-invert -> findColorInvert($turquoise)
if (computed_value.startsWith('findColorInvert')) {
if (computed_value.endsWith('($yellow)')) {
computed_value = 'rgba(0, 0, 0, 0.7)';
} else {
computed_value = '#fff';
}
}
const computed_type = utils.getVariableType(name, computed_value);
2018-06-16 19:47:52 +00:00
if (computed_value.startsWith('mergeColorMaps')) {
computed_value = 'Bulma colors';
}
2018-06-16 19:47:52 +00:00
return {
computed_type,
computed_value,
};
2018-06-16 18:58:58 +00:00
}
2018-06-16 19:47:52 +00:00
return {};
2018-06-16 18:58:58 +00:00
},
ensureDirectoryExistence: (file_path) => {
var dirname = path.dirname(file_path);
if (fs.existsSync(dirname)) {
return true;
}
utils.ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
},
};
2018-06-16 17:26:55 +00:00
2018-06-16 18:01:35 +00:00
utils.files = {};
2018-06-16 19:47:52 +00:00
utils.files.initial_variables = `${base_path}utilities/initial-variables.json`;
utils.files.derived_variables = `${base_path}utilities/derived-variables.json`;
2018-06-16 17:26:55 +00:00
2018-06-16 18:01:35 +00:00
module.exports = utils;