spritesh 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/bin/spritesh +1 -1
  3. data/bin/spritesh.js +126 -0
  4. metadata +5 -5
  5. data/bin/sprite.sh +0 -96
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a5c06e3bdb54bff025695dbab4b8d21ebb55a8d
4
- data.tar.gz: c0a780093d2807427601d99db6aab285288dee82
3
+ metadata.gz: c13c15fa2554bd54dc4bea931eb5ffbefee2449d
4
+ data.tar.gz: 79c8e42718a8ab903571f5f47ef5982e3c28a8de
5
5
  SHA512:
6
- metadata.gz: 1a739742c4fbad686fcd49ad2bf017bca4450a5bc20a2764212c11d8db91a88fadc4514b7821f88d2ce56b19063d79135dfb76b50b9af20cb660bfc89fd483c4
7
- data.tar.gz: bd09f1b12d6a1ace548fc1cd85967bd7f7a218a291c0c90d8faa8c56c38eb488ce9aec7795e116d451b20afdb2bdac91d8931e2368663f28763259f0a568477f
6
+ metadata.gz: 55bca2537e5582d2a5109206efa3b4b19e97b30f8b5364dcc2e511259d92062c658e6113065958f42225ebd53312289fba4b2b901523282cc3bd523374a7eda9
7
+ data.tar.gz: 0625db59003a7cb6b6a92490bbf5c838b2a59ee85cb57a4b9ce92c31e1da27353a9fe24193703f742b0f1142bedc9cbdbacd1cb5c82e355061395cc8bd240952
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  bin_dir = File.expand_path(File.dirname(__FILE__))
4
- shell_script_path = File.join(bin_dir, 'sprite.sh')
4
+ shell_script_path = File.join(bin_dir, 'spritesh.js')
5
5
 
6
6
  system("#{shell_script_path} #{ARGV.join(' ')}")
@@ -0,0 +1,126 @@
1
+ #!/usr/bin/env node
2
+
3
+ const path = require('path');
4
+ const fs = require('fs-promise');
5
+ const program = require('commander');
6
+ const cheerio = require('cheerio');
7
+
8
+ program
9
+ .option('-i, --input [input]', 'Specifies input dir (current dir by default)')
10
+ .option('-o, --output [output]', 'Specifies output file ("./sprite.svg" by default)')
11
+ .option('-v, --viewbox [viewbox]', 'Specifies viewBox attribute (parsed by default)')
12
+ .option('-p, --prefix [prefix]', 'Specifies prefix for id attribute (none by default)')
13
+ .option('-q, --quiet', 'Disable informative output')
14
+ .parse(process.argv);
15
+
16
+ const SRC_FOLDER = program.input || '.';
17
+ const DEST_FILE = program.output || 'sprite.svg';
18
+ const ID_PREFIX = program.prefix || '';
19
+ const VIEWBOX = program.viewbox || null;
20
+ const QUIET = program.quiet || false;
21
+
22
+ const log = (message) => {
23
+ if (!QUIET) console.log(message);
24
+ };
25
+
26
+ const getSvgElement = (content) => {
27
+ const $ = cheerio.load(content);
28
+ return $('svg').first();
29
+ };
30
+
31
+ const getViewbox = (content) => {
32
+ return VIEWBOX || getSvgElement(content).attr('viewbox');
33
+ };
34
+
35
+ const getPreserveAspectRatio = (content) => {
36
+ return getSvgElement(content).attr('preserveaspectratio');
37
+ };
38
+
39
+ const constructId = (fileName) => {
40
+ return (ID_PREFIX + fileName).replace(' ', '-');
41
+ };
42
+
43
+ const constructAttributesString = (attributes) => {
44
+ return Object.keys(attributes).reduce((acc, key) => {
45
+ const value = attributes[key]
46
+ return value
47
+ ? `${acc} ${key}='${value}'`
48
+ : acc;
49
+ }, '');
50
+ };
51
+
52
+ const getSvgContent = (content) => {
53
+ return getSvgElement(content).html();
54
+ };
55
+
56
+ const createSymbol = (content, attributes) => {
57
+ return `<symbol ${constructAttributesString(attributes)}>
58
+ ${getSvgContent(content)}
59
+ </symbol>`;
60
+ };
61
+
62
+ const wrapFile = (fileName, content) => {
63
+ const attributes = {
64
+ viewBox: getViewbox(content),
65
+ id: constructId(fileName),
66
+ preserveAspectRatio: getPreserveAspectRatio(content)
67
+ };
68
+
69
+ log(`Processing ‘${fileName}’ (viewBox ‘${attributes.viewBox}’)…`);
70
+
71
+ return createSymbol(content, attributes);
72
+ };
73
+
74
+ const processFile = (file) => {
75
+ const filePath = path.resolve(SRC_FOLDER, file);
76
+ const fileName = path.basename(file, path.extname(file));
77
+ const wrapContent = wrapFile.bind(null, fileName);
78
+
79
+ return fs.readFile(filePath, 'utf8').then(wrapContent);
80
+ };
81
+
82
+ const removeDestFile = () => {
83
+ return fs.remove(DEST_FILE);
84
+ };
85
+
86
+ const readSrcFolder = (foo) => {
87
+ return fs.readdir(SRC_FOLDER);
88
+ };
89
+
90
+ const processFiles = (files) => {
91
+ const processedFiles = files
92
+ .filter(filterFile)
93
+ .map(processFile);
94
+
95
+ return Promise.all(processedFiles);
96
+ };
97
+
98
+ const filterFile = (file) => {
99
+ return path.extname(file) === '.svg';
100
+ };
101
+
102
+ const getSpriteContent = (contents) => {
103
+ return '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none">'
104
+ + contents.join('')
105
+ + '</svg>';
106
+ };
107
+
108
+ const writeDestFile = (content) => {
109
+ return fs.writeFile(DEST_FILE, content, 'utf8');
110
+ };
111
+
112
+ const printFinish = () => {
113
+ log(`File ‘${DEST_FILE}’ successfully generated.`);
114
+ };
115
+
116
+ const catchErrors = (err) => {
117
+ throw err;
118
+ };
119
+
120
+ removeDestFile()
121
+ .then(readSrcFolder)
122
+ .then(processFiles)
123
+ .then(getSpriteContent)
124
+ .then(writeDestFile)
125
+ .then(printFinish)
126
+ .catch(catchErrors);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spritesh
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hugo Giraudel
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-02 00:00:00.000000000 Z
12
+ date: 2016-05-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: A Bash script to build a SVG sprite from a folder of SVG files (typically
14
+ description: A script to build a SVG sprite from a folder of SVG files (typically
15
15
  icons).
16
16
  email:
17
17
  - h.giraudel@de.edenspiekermann.com
@@ -21,8 +21,8 @@ executables:
21
21
  extensions: []
22
22
  extra_rdoc_files: []
23
23
  files:
24
- - bin/sprite.sh
25
24
  - bin/spritesh
25
+ - bin/spritesh.js
26
26
  homepage: https://github.com/edenspiekermann/sprite.sh
27
27
  licenses:
28
28
  - MIT
@@ -43,7 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  version: '0'
44
44
  requirements: []
45
45
  rubyforge_project:
46
- rubygems_version: 2.5.2
46
+ rubygems_version: 2.4.3
47
47
  signing_key:
48
48
  specification_version: 4
49
49
  summary: Build an SVG sprite from a folder of SVG files.
@@ -1,96 +0,0 @@
1
- #!/bin/bash
2
-
3
- PROGNAME="${0##*/}"
4
- SRC_FOLDER="."
5
- DEST_FILE="sprite.svg"
6
- ID_PREFIX=""
7
- QUIET="0"
8
-
9
- usage()
10
- {
11
- cat <<EO
12
- Usage: $PROGNAME [options]
13
- Script to build a SVG sprite from a folder of SVG files.
14
- Options:
15
- EO
16
- cat <<EO | column -s\& -t
17
- -h, --help & Shows this help
18
- -q, --quiet & Disables informative output
19
- -i, --input [dir] & Specifies input dir (current dir by default)
20
- -o, --output [file] & Specifies output file ("./sprite.svg" by default)
21
- -v, --viewbox [str] & Specifies viewBox attribute (parsed by default)
22
- -p, --prefix [str] & Specifies prefix for id attribute (none by default)
23
- EO
24
- }
25
-
26
- echo_verbose ()
27
- {
28
- if [ "$QUIET" == "0" ]; then
29
- echo "$1"
30
- fi
31
- }
32
-
33
- clean ()
34
- {
35
- rm -rf "$DEST_FILE"
36
- }
37
-
38
- main ()
39
- {
40
- for f in "$SRC_FOLDER"/*.svg; do
41
- if [ -f "$f" ]; then
42
- NAME="$(basename "$f" .svg)"
43
- VIEWBOX="${VIEWBOX_SIZE:-$(sed -n -E 's/.*viewBox="([^"]+)".*/\1/p' "$f")}"
44
- ID="$(echo "$ID_PREFIX$NAME" | tr ' ' '-')"
45
-
46
- echo_verbose "Processing \`$f\` (viewBox \`$VIEWBOX\`)…"
47
- echo "<symbol id='$ID' viewBox='$VIEWBOX'>$(perl -pe 's/\s*?<\/??svg.*?>\s*?\n//gi' "$f")</symbol>" >> "$DEST_FILE"
48
- fi
49
- done
50
-
51
- if [ -f "$DEST_FILE" ]; then
52
- awk 'BEGIN{print "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" style=\"display:none\">"}{print}END{print "</svg>"}' "$DEST_FILE" > .spritesh && mv .spritesh "$DEST_FILE"
53
- fi
54
- }
55
-
56
- # Grabbing options
57
- while [[ $# > 0 ]]; do
58
- key="$1"
59
- case "$key" in
60
- -h|--help)
61
- usage
62
- exit 0
63
- ;;
64
- -i|--input)
65
- SRC_FOLDER="$2"
66
- shift
67
- ;;
68
- -o|--output)
69
- DEST_FILE="$2"
70
- shift
71
- ;;
72
- -v|--viewbox)
73
- VIEWBOX_SIZE="$2"
74
- shift
75
- ;;
76
- -p|--prefix)
77
- ID_PREFIX="$2"
78
- shift
79
- ;;
80
- -q|--quiet)
81
- QUIET="1"
82
- ;;
83
- *)
84
- ;;
85
- esac
86
- shift
87
- done
88
-
89
- clean
90
- main
91
-
92
- if [ -f "$DEST_FILE" ]; then
93
- echo_verbose "File \`$DEST_FILE\` successfully generated."
94
- else
95
- echo_verbose "Could not generated \`$DEST_FILE\`. Are there any SVG file in \`$SRC_FOLDER\` folder?"
96
- fi