From 59e4b3bee4d2d268c7876f8c1d8b144f06fce82d Mon Sep 17 00:00:00 2001 From: nocd5 Date: Fri, 8 Jan 2021 23:52:37 +0900 Subject: [PATCH] Replace a script for generating `assetes.go` assetes.go.rb -> assetes.go.js --- assets.go.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ assets.go.rb | 68 -------------------------------------------------- 2 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 assets.go.js delete mode 100644 assets.go.rb diff --git a/assets.go.js b/assets.go.js new file mode 100644 index 0000000..5707d76 --- /dev/null +++ b/assets.go.js @@ -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 += '\n'; + } + else { + mathjax += '\n'; + } + } + else { + js += '\n'; + } +}); + +files.filter(f => path.extname(f) == '.css').forEach(f => { + css += '\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); + } +}); + diff --git a/assets.go.rb b/assets.go.rb deleted file mode 100644 index a13f56a..0000000 --- a/assets.go.rb +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env ruby - -parent = File.expand_path(File.dirname(__FILE__)) - -js = '' -mathjax = '' -mathjax_cfg = '' -Dir.glob(parent + '/assets/*.js').each do |f| - if File.basename(f) =~ /mathjax/i then - if File.basename(f) =~ /config/i then - mathjax_cfg += '' + "\n" - else - mathjax += '' + "\n" - end - else - js += '' + "\n" - end -end -js_bytes = js.unpack('C*') - .map { |b| format('0x%02X', b) } - .join(', ') - .gsub(/(?:0x\w{2},\s*){16}/, "\\0\n") - .gsub(/\s+\n/, "\n") -mathjax_bytes = mathjax.unpack('C*') - .map { |b| format('0x%02X', b) } - .join(', ') - .gsub(/(?:0x\w{2},\s*){16}/, "\\0\n") - .gsub(/\s+\n/, "\n") -mathjax_cfg_bytes = mathjax_cfg.unpack('C*') - .map { |b| format('0x%02X', b) } - .join(', ') - .gsub(/(?:0x\w{2},\s*){16}/, "\\0\n") - .gsub(/\s+\n/, "\n") - -css = '' -Dir.glob(parent + '/assets/*.css').each do |f| - css += '' + "\n" -end -css_bytes = css.unpack('C*') - .map { |b| format('0x%02X', b) } - .join(', ') - .gsub(/((?:0x\w{2},\s*){16})/, "\\1\n") - .gsub(/\s+\n/, "\n") - -File.open(parent + '/assets.go', 'wb') do |f| - f.puts "package main - -var js_bytes = [...]byte{ - #{js_bytes.gsub(/\n/, "\n\t")}, -} -var mathjax_cfg_bytes = [...]byte{ - #{mathjax_cfg_bytes.gsub(/\n/, "\n\t")}, -} -var mathjax_bytes = [...]byte{ - #{mathjax_bytes.gsub(/\n/, "\n\t")}, -} -var css_bytes = [...]byte{ - #{css_bytes.gsub(/\n/, "\n\t")}, -}" -end