mirror of
https://github.com/jgthms/bulma
synced 2026-03-16 18:44:28 -07:00
Split plugins
This commit is contained in:
29
docs/scripts/plugins/01-read-initial-variables.js
Normal file
29
docs/scripts/plugins/01-read-initial-variables.js
Normal file
@@ -0,0 +1,29 @@
|
||||
module.exports = plugin;
|
||||
|
||||
const utils = require('./utils');
|
||||
|
||||
function plugin() {
|
||||
let variables = {
|
||||
by_name: {},
|
||||
list: [],
|
||||
};
|
||||
|
||||
return (files, metalsmith, done) => {
|
||||
setImmediate(done);
|
||||
|
||||
Object.keys(files).forEach(file_path => {
|
||||
const {file_name, lines} = utils.getLines(files, file_path);
|
||||
|
||||
lines.forEach(line => {
|
||||
const variable = utils.parseLine(line);
|
||||
|
||||
if (variable != false) {
|
||||
variables.by_name[variable.name] = variable;
|
||||
variables.list.push(variable.name);
|
||||
}
|
||||
});
|
||||
|
||||
utils.writeFile(file_path, variables);
|
||||
});
|
||||
};
|
||||
}
|
||||
32
docs/scripts/plugins/02-read-derived-variables.js
Normal file
32
docs/scripts/plugins/02-read-derived-variables.js
Normal file
@@ -0,0 +1,32 @@
|
||||
module.exports = plugin;
|
||||
|
||||
const utils = require('./utils');
|
||||
const fs = require('fs');
|
||||
|
||||
let initial_variables = JSON.parse(fs.readFileSync(utils.files.initial_variables));
|
||||
|
||||
function plugin() {
|
||||
return (files, metalsmith, done) => {
|
||||
setImmediate(done);
|
||||
|
||||
Object.keys(files).forEach(file_path => {
|
||||
let variables = {
|
||||
by_name: {},
|
||||
list: [],
|
||||
};
|
||||
const {file_name, lines} = utils.getLines(files, file_path);
|
||||
|
||||
lines.forEach(line => {
|
||||
const variable = utils.parseLine(line);
|
||||
|
||||
if (variable != false) {
|
||||
variable.computed_value = utils.getInitialValue(variable.value, variable.type, initial_variables);
|
||||
variables.by_name[variable.name] = variable;
|
||||
variables.list.push(variable.name);
|
||||
}
|
||||
});
|
||||
|
||||
utils.writeFile(file_path, variables);
|
||||
});
|
||||
};
|
||||
}
|
||||
37
docs/scripts/plugins/03-read-other-variables.js
Normal file
37
docs/scripts/plugins/03-read-other-variables.js
Normal file
@@ -0,0 +1,37 @@
|
||||
module.exports = plugin;
|
||||
|
||||
const utils = require('./utils');
|
||||
const fs = require('fs');
|
||||
|
||||
let initial_variables = JSON.parse(fs.readFileSync(utils.files.initial_variables));
|
||||
let derived_variables = JSON.parse(fs.readFileSync(utils.files.derived_variables));
|
||||
|
||||
function plugin() {
|
||||
return (files, metalsmith, done) => {
|
||||
setImmediate(done);
|
||||
|
||||
Object.keys(files).forEach(file_path => {
|
||||
if (file_path.startsWith('utilities')) {
|
||||
return;
|
||||
}
|
||||
|
||||
let variables = {
|
||||
by_name: {},
|
||||
list: [],
|
||||
};
|
||||
const {file_name, lines} = utils.getLines(files, file_path);
|
||||
|
||||
lines.forEach(line => {
|
||||
const variable = utils.parseLine(line);
|
||||
|
||||
if (variable != false) {
|
||||
variable.computed_value = utils.getComputedValue(variable.value, variable.type, initial_variables, derived_variables);
|
||||
variables.by_name[variable.name] = variable;
|
||||
variables.list.push(variable.name);
|
||||
}
|
||||
});
|
||||
|
||||
utils.writeFile(file_path, variables);
|
||||
});
|
||||
};
|
||||
}
|
||||
130
docs/scripts/plugins/utils.js
Normal file
130
docs/scripts/plugins/utils.js
Normal file
@@ -0,0 +1,130 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
let utils = {
|
||||
getVariableType: (value) => {
|
||||
if (value.startsWith('hsl')) {
|
||||
return 'color';
|
||||
} else if (value.startsWith('$')) {
|
||||
return 'variable';
|
||||
} else if (value.startsWith('BlinkMacSystemFont') || value == 'monospace') {
|
||||
return 'font-family';
|
||||
} else if (value == 'true' || value == 'false') {
|
||||
return 'boolean';
|
||||
} else if (value.includes('+')) {
|
||||
return 'computed';
|
||||
} else if (value.endsWith('00')) {
|
||||
return 'font-weight';
|
||||
} else if (value.endsWith('px') || value.endsWith('rem')) {
|
||||
return 'size';
|
||||
}
|
||||
|
||||
return 'string';
|
||||
},
|
||||
|
||||
parseLine: (line) => {
|
||||
if (line.startsWith('$') && line.endsWith('!default')) {
|
||||
const colon_index = line.indexOf(':');
|
||||
const variable_name = line.substring(0, colon_index).trim();
|
||||
|
||||
const default_index = line.indexOf('!default');
|
||||
const variable_value = line.substring(colon_index + 1, default_index).trim();
|
||||
|
||||
return variable = {
|
||||
name: variable_name,
|
||||
value: variable_value,
|
||||
type: utils.getVariableType(variable_value),
|
||||
};
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
|
||||
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);
|
||||
|
||||
return {
|
||||
file_name,
|
||||
lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
|
||||
}
|
||||
},
|
||||
|
||||
writeFile: (file_path, data) => {
|
||||
const json_data = JSON.stringify(data, null, ' ');
|
||||
const json_file_path = 'variables/' + file_path.replace('.sass', '.json');
|
||||
utils.ensureDirectoryExistence(json_file_path);
|
||||
|
||||
fs.writeFile(json_file_path, json_data, err => {
|
||||
if (err) {
|
||||
return console.log(err);
|
||||
}
|
||||
|
||||
console.log(`The file ${json_file_path} was saved!`);
|
||||
});
|
||||
},
|
||||
|
||||
getInitialValue: (value, type, initial_variables) => {
|
||||
if (value.startsWith('$') && value in initial_variables.by_name) {
|
||||
const second_value = initial_variables.by_name[value].value;
|
||||
|
||||
if (second_value.startsWith('$') && second_value in initial_variables.by_name) {
|
||||
const third_value = initial_variables.by_name[second_value].value;
|
||||
console.log('third_value', third_value);
|
||||
|
||||
return third_value;
|
||||
}
|
||||
|
||||
return second_value;
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
|
||||
getComputedValue: (value, type, initial_variables, derived_variables) => {
|
||||
if (value.startsWith('$')) {
|
||||
let second_value;
|
||||
|
||||
if (value in initial_variables.by_name) {
|
||||
second_value = initial_variables.by_name[value].value;
|
||||
} else if (value in derived_variables.by_name) {
|
||||
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 (second_value in derived_variables.by_name) {
|
||||
third_value = derived_variables.by_name[second_value].value;
|
||||
}
|
||||
|
||||
return third_value;
|
||||
}
|
||||
|
||||
return second_value;
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
|
||||
ensureDirectoryExistence: (file_path) => {
|
||||
var dirname = path.dirname(file_path);
|
||||
|
||||
if (fs.existsSync(dirname)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
utils.ensureDirectoryExistence(dirname);
|
||||
fs.mkdirSync(dirname);
|
||||
}
|
||||
}
|
||||
|
||||
utils.files = {};
|
||||
utils.files.initial_variables = `./variables/utilities/initial-variables.json`;
|
||||
utils.files.derived_variables = `./variables/utilities/derived-variables.json`;
|
||||
|
||||
module.exports = utils;
|
||||
Reference in New Issue
Block a user