File: apply-stylesheet.sh

package info (click to toggle)
plasma-framework 5.28.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,168 kB
  • ctags: 3,945
  • sloc: cpp: 28,840; sh: 534; python: 477; ruby: 117; xml: 110; php: 27; makefile: 5
file content (287 lines) | stat: -rwxr-xr-x 8,498 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash

PARSED_OPTIONS=$(getopt -n "$0"  -o hf: --long "file:,TextFrom:,TextTo:,BackgroundFrom:,BackgroundTo:,HighlightFrom:,HighlightTo:,ViewTextFrom:,ViewTextTo:,ViewBackgroundFrom:,ViewBackgroundTo:,ViewHoverFrom:,ViewHoverTo:,ViewFocusFrom:,ViewFocusTo:,ButtonTextFrom:,ButtonTextTo:,ButtonBackgroundFrom:,ButtonBackgroundTo:,ButtonHoverFrom:,ButtonHoverTo:,ButtonFocusFrom:,ButtonFocusTo:"  -- "$@")

if [ $? -ne 0 ];
then
  exit 1
fi

eval set -- "$PARSED_OPTIONS"

textFrom=\#31363b
backgroundFrom=\#eff0f1
highlightFrom=\#3daee9
viewTextFrom=\#31363b
viewBackgroundFrom=\#fcfcfc
viewHoverFrom=\#93cee9
viewFocusFrom=\#3daee9
buttonTextFrom=\#31363b
buttonBackgroundFrom=\#eff0f1
buttonHoverFrom=\#93cee9
buttonFocusFrom=\#3daee9

textTo=\#31363b
backgroundTo=\#eff0f1
highlightTo=\#3daee9
viewTextTo=\#31363b
viewBackgroundTo=\#fcfcfc
viewHoverTo=\#93cee9
viewFocusTo=\#3daee9
buttonTextTo=\#31363b
buttonBackgroundTo=\#eff0f1
buttonHoverTo=\#93cee9
buttonFocusTo=\#3daee9

file=''

while true;
do
  case "$1" in
 
    -h|--help)
      echo "usage $0 [-h|options] -f file.svgz"
      echo "Where options can be:"
      echo " --TextFrom=color html encoded color to replace with the ColorScheme-Text from the stylesheet"
      echo " --TextTo=color html encoded that the ColorScheme-Text class will have"
      echo
      echo " --BackgroundFrom=color html encoded color to replace with the ColorScheme-Background from the stylesheet"
      echo " --BackgroundTo=color html encoded that the ColorScheme-Background class will have"
      echo
      echo " --HighlightFrom=color html encoded color to replace with the ColorScheme-Highlight from the stylesheet"
      echo " --HighlightTo=color html encoded that the ColorScheme-Highlight class will have"
      echo
      echo " --ViewTextFrom=color html encoded color to replace with the ColorScheme-ViewText from the stylesheet"
      echo " --ViewTextTo=color html encoded that the ColorScheme-ViewText class will have"
      echo
      echo " --ViewBackgroundFrom=color html encoded color to replace with the ColorScheme-ViewBackground from the stylesheet"
      echo " --ViewBackgroundTo=color html encoded that the ColorScheme-ViewBackground class will have"
      echo
      echo " --ViewHoverFrom=color html encoded color to replace with the ColorScheme-ViewHover from the stylesheet"
      echo " --ViewHoverTo=color html encoded that the ColorScheme-ViewHover class will have"
      echo
      echo " --ViewFocusFrom=color html encoded color to replace with the ColorScheme-ViewFocus from the stylesheet"
      echo " --ViewFocusTo=color html encoded that the ColorScheme-ViewFocus class will have"
      echo
      echo " --ButtonTextFrom=color html encoded color to replace with the ColorScheme-ButtonText from the stylesheet"
      echo " --ButtonTextTo=color html encoded that the ColorScheme-ButtonText class will have"
      echo
      echo " --ButtonBackgroundFrom=color html encoded color to replace with the ColorScheme-ButtonBackground from the stylesheet"
      echo " --ButtonBackgroundTo=color html encoded that the ColorScheme-ButtonBackground class will have"
      echo
      echo " --ButtonHoverFrom=color html encoded color to replace with the ColorScheme-ButtonHover from the stylesheet"
      echo " --ButtonHoverTo=color html encoded that the ColorScheme-ButtonHover class will have"
      echo
      echo " --ButtonFocusFrom=color html encoded color to replace with the ColorScheme-ButtonFocus from the stylesheet"
      echo " --ButtonFocusTo=color html encoded that the ColorScheme-ButtonFocus class will have"
      echo
      echo "All the colors have default values conformant to the Breeze color palette"
      echo
      exit
     shift;;
 
    --TextFrom)
      textFrom=$2
      shift 2;;
    --TextTo)
      textTo=$2
      shift 2;;
 
    --BackgroundFrom)
      backgroundFrom=$2
      shift 2;;
    --BackgroundTo)
      backgroundTo=$2
      shift 2;;

    --HighlightFrom)
      highlightFrom=$2
      shift 2;;
    --HighlightTo)
      highlightTo=$2
      shift 2;;

    --ViewTextFrom)
      viewTextFrom=$2
      shift 2;;
    --ViewTextTo)
      viewTextTo=$2
      shift 2;;

    --ViewBackgroundFrom)
      viewBackgroundFrom=$2
      shift 2;;
    --ViewBackgroundTo)
      viewBackgroundTo=$2
      shift 2;;

    --ViewHoverFrom)
      viewHoverFrom=$2
      shift 2;;
    --ViewHoverTo)
      viewHoverTo=$2
      shift 2;;

    --ViewFocusFrom)
      viewFocusFrom=$2
      shift 2;;
    --ViewFocusTo)
      viewFocusTo=$2
      shift 2;;

    --ButtonTextFrom)
      buttonTextFrom=$2
      shift 2;;
    --ButtonTextTo)
      buttonTextTo=$2
      shift 2;;

    --ButtonBackgroundFrom)
      buttonBackgroundFrom=$2
      shift 2;;
    --ButtonBackgroundTo)
      buttonBackgroundTo=$2
      shift 2;;

    --ButtonHoverFrom)
      buttonBackgroundFrom=$2
      shift 2;;
    --ButtonHoverTo)
      buttonHoverTo=$2
      shift 2;;

    --ButtonFocusFrom)
      buttonFocusFrom=$2
      shift 2;;
    --ButtonFocusTo)
      buttonFocusTo=$2
      shift 2;;

    -f|--file)
      file=`echo $2 | cut -d'.' --complement -f2-`
      shift 2;;

    --)
      shift
      break;;
  esac
done


if [ -z "$file" ]; 
    then echo missing svg file
    exit 1
fi

isSvgz=0

if [ ! -f $file.svgz ] && [ ! -f $file.svg ]; then
    echo "you must specify a valid svg"
    exit 1
fi

if [ -f $file.svgz ]; then
    isSvgz=1
fi


if [ $isSvgz = 1 ]; then
    mv $file.svgz $file.svg.gz
    gunzip $file.svg.gz
fi

echo Processing $file

stylesheet="
      .ColorScheme-Text {
        color:$textTo;
      }
      .ColorScheme-Background {
        color:$backgroundTo;
      }
      .ColorScheme-Highlight {
        color:$highlightTo;
      }
      .ColorScheme-ViewText {
        color:$viewTextTo;
      }
      .ColorScheme-ViewBackground {
        color:$viewBackgroundTo;
      }
      .ColorScheme-ViewHover {
        color:$viewHoverTo;
      }
      .ColorScheme-ViewFocus{
        color:$viewFocusTo;
      }
      .ColorScheme-ButtonText {
        color:$buttonTextTo;
      }
      .ColorScheme-ButtonBackground {
        color:$buttonBackgroundTo;
      }
      .ColorScheme-ButtonHover {
        color:$buttonHoverTo;
      }
      .ColorScheme-ButtonFocus{
        color:$buttonFocusTo;
      }
      "
colors=($textFrom $backgroundFrom $highlightFrom $viewTextFrom $viewBackgroundFrom $viewHoverFrom $viewFocusFrom $buttonTextFrom $buttonBackgroundFrom $buttonHoverFrom $buttonFocusFrom)
colorNames=(ColorScheme-Text ColorScheme-Background ColorScheme-Highlight ColorScheme-ViewText ColorScheme-ViewBackground ColorScheme-ViewHover ColorScheme-ViewFocus ColorScheme-ButtonText ColorScheme-ButtonBackground ColorScheme-ButtonHover ColorScheme-ButtonFocus)

reorderXslt='
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:svg="http://www.w3.org/2000/svg">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="svg:defs">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <xsl:apply-templates select="*">
        <xsl:sort select="name()" data-type="text" order="descending"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
'
echo $reorderXslt > transform.xsl

if grep -q '"current-color-scheme"' $file.svg; then
    echo replacing the stylesheet
    xmlstarlet ed --update "/svg:svg/svg:defs/_:style" -v "$stylesheet" $file.svg > temp.svg
else
    echo adding the stylesheet
xmlstarlet ed --subnode "/svg:svg/svg:defs" -t elem -n "style" -v "$stylesheet"\
       --subnode "/svg:svg/svg:defs/style" -t attr -n "type" -v "text/css"\
       --subnode "/svg:svg/svg:defs/style" -t attr -n "id" -v "current-color-scheme" $file.svg > temp.svg
fi

xmlstarlet tr transform.xsl temp.svg > temp2.svg
mv temp2.svg temp.svg

for i in {0..4}
do
  xmlstarlet ed --subnode "//*/*[contains(@style, '${colors[i]}') and not (@class)]" -t attr -n "class" -v "${colorNames[i]}" temp.svg > temp2.svg

  mv temp2.svg temp.svg

  sed -i 's/\(style=".*\)fill:'${colors[i]}'/\1fill:currentColor/g' temp.svg
  sed -i 's/\(style=".*\)stop-color:'${colors[i]}'/\1stop-color:currentColor/g' temp.svg
done

rm transform.xsl

mv temp.svg $file.svg
if [ $isSvgz = 1 ]; then
    gzip $file.svg
    mv $file.svg.gz $file.svgz
fi