-
Notifications
You must be signed in to change notification settings - Fork 3
/
slasync
231 lines (206 loc) · 7.1 KB
/
slasync
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
#!/bin/bash
# set -x # for a detailed trace
# use : slasync options goal master dest
# options : 0, 1 or more options
# goal : what parts are to be synchonised ?
# master : sla master file, defines elements to be used (specified without .sla extension)
# dest : sla file to be synchronised (specified without .sla extension)
# ext=${filename##*\.} donne l'extension si on voulait gérer $1 "avec ou sans" extension SLA
. ~/bin/shellcolors
# default <goal>
nodes="STYLE|CHARSTYLE|NotesStyles|COLOR"
insert_after_tag="DOCUMENT"
while [ "$stopoptions" = "" ]
do
stopoptions=true
if [ "$1" = '-?' ] || [ "$1" = '-h' ] ; then
echo "use : 'slasync [options] <goal> masterfile chapterfile'"
echo "Replaces paragraph styles, character styles, notes styles, color and/or masterpages of chapterfile.sla by those in masterfile.sla"
echo
echo "Possible options :"
echo "-h or -? : get this help"
echo "-keeps <str> : keeps paragraph and character style definitions whose NAME contains <str> string in chapter. These MUST NOT be defined in master. Only works with -s or -a goal"
echo "-trace : displays traces of operations, styles kept in chapter and nodes import-forced from master, and keeps intermediate file versions"
echo "-tmp : dont delete intermediate useless temporary files after proceeding"
echo "-notes: to be documented"
echo
echo "Possible goals :"
echo "-ps : replaces paragraph styles only"
echo "-cs : replaces character styles only"
echo "-s : replaces both paragraph and character styles"
echo "-ns : replaces notes styles"
echo "-m : replaces masterpages - cannot be used along with styles or color sync"
echo "-c : replaces colors"
echo "-a : replaces paragraph, character and notes styles + colors but not masterpages (DEFAULT)"
echo "-nodes <node1|node2|node3> : replaces all nodes specified in pipe separated list"
echo
echo "masterfile and chapterfile names should NOT include '.sla' : it will be automaticaly added"
echo
exit
elif [ "$1" = '-keeps' ]; then
shift
if [ "$1" = "" ]; then
echo -e "${ESC_error} missing <part> after '-keeps' option. Read the doc with -h option"
exit
fi
keeps="$1"
echo "keeps char and par styles with NAME containing '$1'"
shift
stopoptions=''
elif [ "$1" = "-trace" ]; then
trace=true
shift
stopoptions=''
elif [ "$1" = "-tmp" ]; then
keeptmp=true
shift
stopoptions=''
elif [ "$1" = '-nodes' ]; then
shift
if [ "$1" = "" ]; then
echo -e "${ESC_error} missing <node> after -nodes option"
exit
fi
nodes=$1
elif [ "$1" = "-s" ]; then
nodes="STYLE|CHARSTYLE"
shift
elif [ "$1" = "-ps" ]; then
nodes=STYLE
shift
elif [ "$1" = "-cs" ]; then
nodes=CHARSTYLE
shift
elif [ "$1" = "-ns" ]; then
nodes=NotesStyles
shift
elif [ "$1" = "-m" ]; then
nodes="MASTERPAGE|MASTEROBJECT"
insert_after_tag="\/Section"
shift
elif [ "$1" = "-c" ]; then
nodes=COLOR
shift
elif [ "$1" = "-a" ]; then
nodes="STYLE|CHARSTYLE|NotesStyles|COLOR"
shift
fi
done
nodesregexp=$nodes
nodes=`echo $nodes | tr '|' '_'`
if [ "$1" = "" ]; then
echo -e "${ESC_error} missing masterpage in slasync command. Read doc using -h"
exit
elif [ ! -f "$1.sla" ]; then
echo -e "${ESC_error} in slasync command : file \"$1.sla\" doesnt exist. Read doc using -?"
exit
elif [ "$2" = "" ] ; then
echo -e "${ESC_error} missing chapter file in slasync command. Read doc using -?"
exit
elif [ ! -f "$2.sla" ]; then
echo -e "${ESC_error} in slasync command : file \"$2.sla\" doesnt exist. Read doc using -?"
exit
elif [ "$2" = "$3" ]; then
echo -e "${ESC_error} chapter and master are identical"
exit
fi
cp -f $2.sla $2.bak.sla
master=$1.sla
chapter=$2.sla
if [ "$trace" != "" ]; then
echo
echo "Origin : $chapter is " `< $chapter wc -l` " lines long"
ls -la "$chapter"
fi
# xmlstarlet ed -u ... -v ... : update value of path to be value (-v) or other xpath value (-x)
# ne change pas les attributs des xpaths mais affecte leurs valeurs
#
# xmlstarlet ed -d ... : delete ; -i -t ... -v ... : insert
#
# xmlstarlet ed -d "SCRIBUSUTF8NEW/DOCUMENT/STYLE" fic.sla : retire toutes les définitions de parstyles
#
#
# xmlstarlet sel -t -v "count(/SCRIBUSUTF8NEW/DOCUMENT/COLOR)" vide.sla
# renvoie le nombre de couleurs définies
# xmlstarlet sel -t -m "SCRIBUSUTF8NEW/DOCUMENT/COLOR" -c . -n vide.sla
# renvoie la liste de toutes les couleurs : <COLOR NAME="Black" SPACE="CMYK" C="0" M="0" Y="0" K="100"/>\n<COLOR NAME="Blue"... etc
# "-m" : match XPATH ; "-c ." : echoes copy of XPATH expression et "-n" : echoes EOL
# Insérer des composants sur mesure :
# echo "<x/>" | xml sel -t -m / -e xml -e child -a data -o value
# produces :
# <xml><child data="value"/></xml>
# -m / : se mettre à la racine
# -e xml : output <xml> et se mettre dedans
# -e child : output <child> et se mettre dedans
# -a data -o value : ajouter un attribut data avec la valeur value
#
# autres xpaths :
# '/xml/block/el[@name="b"]' : ceux dont l'attribut 'name' a la valeur 'b'
# '/xml/block/el[not(@name)]' : ceux qui n'ont pas d'attribut 'name'
# Capture chapter's kept nodes
if [ "$keeps" != "" ]; then
xmlstarlet sel -t -m "SCRIBUSUTF8NEW/DOCUMENT/STYLE[contains(@NAME,'$keeps')]" -c . -n $chapter > "$chapter.kept_$nodes.xml"
xmlstarlet sel -t -m "SCRIBUSUTF8NEW/DOCUMENT/CHARSTYLE[contains(@CNAME,'$keeps')]" -c . -n $chapter >> "$chapter.kept_$nodes.xml"
if [ "$trace" != "" ]; then
echo
echo Keeping `< "$chapter.kept_$nodes.xml" wc -l` " lines in $chapter :"
cat "$chapter.kept_$nodes.xml"
fi
fi
# Capture master's nodes
xmlstarlet sel -t -m "SCRIBUSUTF8NEW/DOCUMENT" -m $nodesregexp -c . -n "$master" > "$master.$nodes.xml"
if [ "$trace" != "" ]; then
echo
echo "master"
ls -la "$master"
echo Found `< "$master.$nodes.xml" wc -l` " lines of $nodes in master :"
ls -la "$master.$nodes.xml"
cat "$master.$nodes.xml"
echo
fi
# Empty chapter nodes
cp "$chapter" "$chapter.without_$nodes.xml" # required because > empties the destination file first
IFS=$'_'
for anode in $nodes
do
if [ "$trace" != "" ]; then
echo "Emptying $anode nodes"
fi
xmlstarlet ed -d "SCRIBUSUTF8NEW/DOCUMENT/$anode" "$chapter.without_$nodes.xml" > "$chapter.without_$nodes.tmp.xml"
cp "$chapter.without_$nodes.tmp.xml" "$chapter.without_$nodes.xml"
done
rm "$chapter.without_$nodes.tmp.xml"
if [ "$trace" != "" ]; then
echo
echo "Without $nodes : `< "$chapter.without_$nodes.xml" wc -l` lines"
ls -la "$chapter.without_$nodes.xml"
fi
# Insert master nodes at the begining of the <DOCUMENT> previously emptied of nodes
sed "/<${insert_after_tag}.*>/ r $master.$nodes.xml" "$chapter.without_$nodes.xml" > result.soon.sla
if [ "$trace" != "" ]; then
echo
echo "With master's $nodes :"
ls -la "result.soon.sla"
fi
if [ "$keeps" != "" ]; then
# Insert kept nodes
sed "/<DOCUMENT.*>/ r $chapter.kept_$nodes.xml" result.soon.sla > "$chapter"
if [ "$trace" != "" ]; then
echo
echo "With kept $nodes :"
fi
else
mv result.soon.sla "$chapter"
fi
if [ "$keeptmp" = "" ]; then
rm "$chapter.without_$nodes.xml"
rm "$master.$nodes.xml"
if [ -f result.soon.sla ]; then
rm result.soon.sla
fi
fi
if [ "$trace" != "" ]; then
echo
echo "Result : $chapter is " `< "$chapter" wc -l` "lines long"
fi
ls -la "$chapter"