From 59f4c792b498ef399dad5643576327da8ed122f2 Mon Sep 17 00:00:00 2001 From: Alexandre Flament Date: Tue, 23 Nov 2021 20:18:09 +0100 Subject: [PATCH] [mod] simple theme: use sharp instead of convert to create .png from .svg define a custom grunt task, since grunt-sharp is too old (it can't be installed). in gruntfile.js, the image tasks are moved at the end the build chain. Signed-off-by: Markus Heiser --- manage | 30 ------------------ searx/static/themes/simple/gruntfile.js | 41 +++++++++++++++++++++++++ searx/static/themes/simple/package.json | 1 + 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/manage b/manage index ec8e13c0a..1236cb31c 100755 --- a/manage +++ b/manage @@ -708,42 +708,12 @@ themes.oscar() { themes.simple() { local static="searx/static/themes/simple" ( set -e - convert_if_newer "src/brand/searxng-wordmark.svg" "$static/img/favicon.png" \ - -transparent white -resize 64x64 build_msg GRUNT "theme: simple" npm --prefix searx/static/themes/simple run build ) dump_return $? } -convert_if_newer() { - - # usage: convert_if_newer [, ...] - # - # convert_if_newer "path/to/origin.svg" "path/to/converted.png" -resize 100x100 - # - # Run's ImageMagik' convert comand to generate from , if - # is newer than . The command line is to convert is:: - # - # convert [, ...] - - local src_file="$1" && shift - local dst_file="$1" && shift - - if [[ "${src_file}" -nt "${dst_file}" ]]; then - if ! required_commands convert; then - info_msg "to install build tools use::" - info_msg " sudo -H ./utils/searx.sh install buildhost" - die 1 "install needed build tools first" - fi - build_msg CONVERT "${src_file}" "$@" "${dst_file}" - convert "${src_file}" "$@" "${dst_file}" - else - build_msg CONVERT "${dst_file} (up-to-date)" - fi -} - - PYLINT_FILES=() while IFS= read -r line; do PYLINT_FILES+=("$line") diff --git a/searx/static/themes/simple/gruntfile.js b/searx/static/themes/simple/gruntfile.js index 83989397d..d51316dcf 100644 --- a/searx/static/themes/simple/gruntfile.js +++ b/searx/static/themes/simple/gruntfile.js @@ -2,6 +2,8 @@ module.exports = function(grunt) { + const eachAsync = require('each-async'); + grunt.initConfig({ _brand: '../../../../src/brand', @@ -19,6 +21,7 @@ module.exports = function(grunt) { 'less:development', 'less:production', 'image', + 'svg2png', 'svg2jinja' ] } @@ -148,6 +151,13 @@ module.exports = function(grunt) { } } }, + svg2png: { + favicon: { + files: { + 'img/favicon.png': '<%= _brand %>/searxng-wordmark.svg' + } + } + }, svg2jinja: { all: { src: { @@ -232,6 +242,36 @@ module.exports = function(grunt) { grunt.log.ok(this.data.dest + " created"); }); + grunt.registerMultiTask('svg2png', 'Convert SVG to PNG', function () { + const sharp = require('sharp'), done = this.async(); + eachAsync(this.files, async (file, _index, next) => { + try { + if (file.src.length != 1) { + next("this task supports only one source per destination"); + } + const info = await sharp(file.src[0]) + .png({ + force: true, + compressionLevel: 9, + palette: true, + }) + .toFile(file.dest); + grunt.log.ok(file.dest + ' created (' + info.size + ' bytes, ' + info.width + 'px * ' + info.height + 'px)'); + next(); + } catch (error) { + grunt.fatal(error); + next(error); + } + }, error => { + if (error) { + grunt.fatal(error); + done(error); + } else { + done(); + } + }); + }); + grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-contrib-copy'); grunt.loadNpmTasks('grunt-contrib-uglify'); @@ -254,6 +294,7 @@ module.exports = function(grunt) { 'less:development', 'less:production', 'image', + 'svg2png', 'svg2jinja', ]); }; diff --git a/searx/static/themes/simple/package.json b/searx/static/themes/simple/package.json index 25797b8fe..0cf6c4f64 100644 --- a/searx/static/themes/simple/package.json +++ b/searx/static/themes/simple/package.json @@ -16,6 +16,7 @@ "ionicons": "^6.0.0", "less": "^4.1.1", "less-plugin-clean-css": "^1.5.1", + "sharp": "^0.29.3", "stylelint": "^13.13.1", "stylelint-config-standard": "^22.0.0", "ejs": "^3.1.6",