]> git.ipfire.org Git - thirdparty/bulma.git/commitdiff
Read derived variables
authorJeremy Thomas <bbxdesign@gmail.com>
Sat, 16 Jun 2018 18:01:35 +0000 (19:01 +0100)
committerJeremy Thomas <bbxdesign@gmail.com>
Wed, 20 Jun 2018 00:21:22 +0000 (01:21 +0100)
docs/scripts/.gitignore
docs/scripts/01-read-initial-variables.js [moved from docs/scripts/read-initial-variables.js with 76% similarity]
docs/scripts/02-read-derived-variables.js [new file with mode: 0644]
docs/scripts/read-derived-variables.js [deleted file]
docs/scripts/utils.js
docs/scripts/variables.js

index c465acc16485cc31456ce910fe4850bb9485ac39..dd0cabdbaaf33f8ff67b4a1c0b5f8dd19ee172d1 100644 (file)
@@ -1,3 +1,4 @@
 # Folders
 
+/build
 /output
similarity index 76%
rename from docs/scripts/read-initial-variables.js
rename to docs/scripts/01-read-initial-variables.js
index e562dcdc8f3be78084fef757eb289aa294c2e90e..bd7c6a48900a5f073f83452cff7832c61d37e936 100644 (file)
@@ -20,13 +20,11 @@ function plugin() {
 
         if (variable != false) {
           variables.by_name[variable.name] = variable;
-          variables[file_name].push(variable.name);
+          variables.list.push(variable.name);
         }
       });
-
-      metalsmith.variables = variables;
-
-      utils.writeFile('./output/initial-variables.json', variables);
     });
+
+    utils.writeFile(utils.files.initial_variables, variables);
   };
 }
diff --git a/docs/scripts/02-read-derived-variables.js b/docs/scripts/02-read-derived-variables.js
new file mode 100644 (file)
index 0000000..b8f6278
--- /dev/null
@@ -0,0 +1,34 @@
+module.exports = plugin;
+
+const utils = require('./utils');
+const fs = require('fs');
+
+let initial_variables = JSON.parse(fs.readFileSync(utils.files.initial_variables));
+
+function plugin() {
+  let variables = {
+    by_name: {},
+  };
+
+  return (files, metalsmith, done) => {
+    setImmediate(done);
+
+    Object.keys(files).forEach(file_path => {
+      const {file_name, lines} = utils.getLines(files, file_path);
+
+      variables[file_name] = [];
+
+      lines.forEach(line => {
+        const variable = utils.parseLine(line);
+
+        if (variable != false) {
+          variable.computed_value = utils.getComputedValue(variable.value, variable.type, initial_variables);
+          variables.by_name[variable.name] = variable;
+          variables.list.push(variable.name);
+        }
+      });
+    });
+
+    utils.writeFile(utils.files.derived_variables, variables);
+  };
+}
diff --git a/docs/scripts/read-derived-variables.js b/docs/scripts/read-derived-variables.js
deleted file mode 100644 (file)
index 75c77d9..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-module.exports = plugin;
-
-var utils = require('./utils');
-
-function plugin() {
-  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) {
-          console.log('variable', variable);
-        }
-      });
-
-      metalsmith.variables = variables;
-    });
-  };
-}
index 813b11673e7308a8b4663d46a790297746b68c52..654eae9145093e37238243f29f6f6d10036cfc01 100644 (file)
@@ -1,69 +1,91 @@
 const fs = require('fs');
 
-function 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('px') || value.endsWith('rem')) {
-    return 'size';
-  }
+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';
-}
+    return 'string';
+  },
 
-function parseLine(line) {
-  if (line.startsWith('$') && line.endsWith('!default')) {
-    const colon_index = line.indexOf(':');
-    const variable_name = line.substring(0, colon_index).trim();
+  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();
+      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: getVariableType(variable_value),
-    };
-  }
+      return variable = {
+        name: variable_name,
+        value: variable_value,
+        type: utils.getVariableType(variable_value),
+      };
+    }
 
-  return false;
-}
+    return false;
+  },
 
-function 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);
+  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),
-  }
-}
+    return {
+      file_name,
+      lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
+    }
+  },
+
+  writeFile: (file_path, data) => {
+    const json_data = JSON.stringify(data, null, '  ');
+
+    fs.writeFile(file_path, json_data, err => {
+      if (err) {
+        return console.log(err);
+      }
+
+      console.log('The file was saved!');
+    });
+  },
 
-function writeFile(file_path, data) {
-  const json_data = JSON.stringify(data, null, '  ');
+  getComputedValue: (value, type, initial_variables) => {
+    if (value.startsWith('$') && value in initial_variables.by_name) {
+      const second_value = initial_variables.by_name[value].value;
+      console.log('second_value', second_value);
 
-  fs.writeFile(file_path, json_data, err => {
-    if (err) {
-      return console.log(err);
+      if (second_value.startsWith('$') && second_value in initial_variables) {
+        const third_value = initial_variables.by_name[second_value].value;
+        console.log('third_value', third_value);
+
+        return third_value;
+      }
+
+      return second_value;
     }
 
-    console.log('The file was saved!');
-  });
+    return value;
+  }
 }
 
-let plugin = {};
-plugin.getVariableType = getVariableType;
-plugin.getLines = getLines;
-plugin.parseLine = parseLine;
-plugin.writeFile = writeFile;
+const output_base = './output/';
+
+utils.files = {};
+utils.files.initial_variables = `${output_base}initial-variables.json`;
+utils.files.derived_variables = `${output_base}derived-variables.json`;
 
-module.exports = plugin;
+module.exports = utils;
index bbdd46d819397b907a19c773d8e1755d0504cf61..f378e2103c1bbd958585a6e5f5dd0abaf35b5e5b 100644 (file)
@@ -6,18 +6,21 @@ var writemetadata = require('metalsmith-writemetadata');
 // var regex = '**/*.sass';
 var regex_initial = '**/initial-variables.sass';
 var regex_derived = '**/derived-variables.sass';
-var initial_plugin = require('./read-initial-variables');
-var derived_plugin = require('./read-derived-variables');
+var initial_plugin = require('./01-read-initial-variables');
+var derived_plugin = require('./02-read-derived-variables');
 
 Metalsmith(__dirname)
-  .source('../../sass/utilities')
-  // .destination('./output')
-  // .clean(true)
+  .source('../../sass')
   .use(filter(regex_initial))
   .use(initial_plugin())
-  // .use(filter(regex_derived))
-  // .use(derived_plugin())
-  // .use(writemetadata())
+  .build(function(err) {
+    if (err) throw err;
+  });
+
+Metalsmith(__dirname)
+  .source('../../sass')
+  .use(filter(regex_derived))
+  .use(derived_plugin())
   .build(function(err) {
     if (err) throw err;
   });