Skip to content

Commit

Permalink
Replace a script for generating assetes.go
Browse files Browse the repository at this point in the history
assetes.go.rb -> assetes.go.js
  • Loading branch information
nocd5 committed Jan 8, 2021
1 parent 388d78c commit 59e4b3b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 68 deletions.
70 changes: 70 additions & 0 deletions assets.go.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const fs = require('fs');
const path = require('path');

let js = '';
let mathjax = '';
let mathjax_cfg = '';
let css = '';

const files = fs.readdirSync(path.join(__dirname, 'assets'))
.map(f => path.join(__dirname, 'assets', f));

files.filter(f => path.extname(f) == '.js').forEach(f => {
if (path.basename(f).match(/mathjax/i)) {
if (path.basename(f).match(/config/i)) {
mathjax_cfg += '<script type="text/x-mathjax-config">\n';
mathjax_cfg += fs.readFileSync(f, { encoding: 'utf8' });
mathjax_cfg += '\n</script>\n';
}
else {
mathjax += '<script type="text/javascript">\n';
mathjax += fs.readFileSync(f, { encoding: 'utf8' });
mathjax += '\n</script>\n';
}
}
else {
js += '<script type="text/javascript">\n';
js += fs.readFileSync(f, { encoding: 'utf8' });
js += '\n</script>\n';
}
});

files.filter(f => path.extname(f) == '.css').forEach(f => {
css += '<style type="text/css">\n';
css += fs.readFileSync(f, { encoding: 'utf8' });
css += '\n</style>\n';
});

const str2bytes = str => [...Buffer.from(str)]
.map(b => '0x' + b.toString(16).toUpperCase().padStart(2, '0'))
.join(', ')
.replace(/(?:0x\w{2},\s*){16}/g, '$&\n')
.replace(/\s+\n/g, '\n');

let js_bytes = str2bytes(js);
let mathjax_bytes = str2bytes(mathjax);
let mathjax_cfg_bytes = str2bytes(mathjax_cfg);
let css_bytes = str2bytes(css);

const content = `package main
var js_bytes = [...]byte{
${js_bytes.replace(/\n/g, '\n\t')},
}
var mathjax_cfg_bytes = [...]byte{
${mathjax_cfg_bytes.replace(/\n/g, '\n\t')},
}
var mathjax_bytes = [...]byte{
${mathjax_bytes.replace(/\n/g, '\n\t')},
}
var css_bytes = [...]byte{
${css_bytes.replace(/\n/g, '\n\t')},
}
`;

fs.writeFile(path.join(__dirname, 'assets.go'), content, err => {
if (err) {
console.log(err.message);
}
});

68 changes: 0 additions & 68 deletions assets.go.rb

This file was deleted.

0 comments on commit 59e4b3b

Please sign in to comment.