Skip to content

Commit

Permalink
Ajoute la génération des smileys au format PNG depuis le SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D committed Oct 28, 2023
1 parent 0a733a7 commit 52eea3a
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 3 deletions.
31 changes: 30 additions & 1 deletion Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { ESLint } = require('eslint')
const gulp = require('gulp')
const gulpif = require('gulp-if')
const imagemin = require('gulp-imagemin')
const run = require('gulp-run')
const options = require('gulp-options')
const path = require('path')
const postcss = require('gulp-postcss')
Expand Down Expand Up @@ -171,6 +172,17 @@ function images() {
.pipe(gulp.dest('dist/'))
}

// Generate PNG versions of SVG smileys.
// Output files are not optimized for size.
function convertSmileysToPng() {
const pathToScript = 'scripts/convert_smileys_to_svg.py'
const pathToSvgSmileys = 'assets/smileys/svg/'
const pathToPngSmileys = 'dist/smileys/png'
const convertToPng = run(`python ${pathToScript} ${pathToSvgSmileys} ${pathToPngSmileys}`)
convertToPng.exec()
return Promise.resolve('Smileys were converted') // to fit into gulp's asynchronous system
}

function spriteImages() {
return gulp.src(['dist/images/sprite*.png'])
.pipe(gulpif(!fast, imagemin())) // Minify the images
Expand Down Expand Up @@ -210,7 +222,24 @@ function watch() {
}

// Build the front
const build = gulp.parallel(prepareZmd, prepareEasyMde, jsPackages, js, images, errors, gulp.series(spriteCss, gulp.parallel(css, spriteImages)))
const build = gulp.series(
convertSmileysToPng, // automatically converted files may be overwritten with manually converted files afterwards
gulp.parallel(
prepareZmd,
prepareEasyMde,
jsPackages,
js,
images,
errors,
gulp.series(
spriteCss,
gulp.parallel(
css,
spriteImages
)
)
)
)

exports.build = build
exports.watch = gulp.series(build, watch)
Expand Down
Binary file added assets/smileys/png/ninja.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"gulp-imagemin": "7.1.0",
"gulp-options": "1.1.1",
"gulp-postcss": "9.0.1",
"gulp-run": "1.7.1",
"gulp-terser-js": "5.2.2",
"gulp.spritesmith": "6.13.0",
"jquery": "3.7.1",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ social-auth-app-django==5.3.0

# Explicit dependencies (references in code)
beautifulsoup4==4.12.2
cairosvg==2.7.1
django-crispy-forms==1.14.0
django-model-utils==4.3.1
django-munin==0.2.1
Expand Down
46 changes: 46 additions & 0 deletions scripts/convert_smileys_to_svg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import argparse
import pathlib
import os

import cairosvg


def convert_folder_to_svg(src_folder: pathlib.Path, dst_folder: pathlib.Path) -> int:
"""
Convert all SVGs from src_folder into PNGs and write them in dst_folder.
Create dst_folder if needed.
Existing files in dst_fodler are overwritten.
Return the number of converted files.
"""
src_files = src_folder.rglob("*.svg")
os.makedirs(dst_folder, exist_ok=True)
converted_file_count = 0
for src_file in src_files:
dst_file = dst_folder / f"{src_file.stem}.png"
cairosvg.svg2png(url=src_file.as_posix(), write_to=dst_file.as_posix())
converted_file_count += 1
return converted_file_count


def get_cli_args():
"""Get arguments from the CLI."""

# Build parser
parser = argparse.ArgumentParser(description="Convert a folder of SVG files to PNG")
parser.add_argument("source", help="Folder containing the SVG files to be converted")
parser.add_argument("destination", help="Folder in which the PNG files are written")

# Parse
raw_args = parser.parse_args()
processed_args = {
"source": pathlib.Path(raw_args.source),
"destination": pathlib.Path(raw_args.destination),
}

return processed_args


if __name__ == "__main__":
args = get_cli_args()
file_count = convert_folder_to_svg(args["source"], args["destination"])
print(f"{__file__}: {file_count} files converted.")
Loading

0 comments on commit 52eea3a

Please sign in to comment.