-
et ajout d'une variable pour la largeur de texte en sod
wize.rb
6.15 KB
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
#!/usr/bin/env ruby
require 'optparse'
# Variables globales $version, $auteur et $date
$version = "0.1"
$auteur = "William Daniau"
$date = "2019 10 11"
# Global directory definitions
$zimShareDir = "#{ENV['HOME']}/.local/share/zim"
$zimHtmlTemplateDir = "#{$zimShareDir}/templates/html"
$zimConfigDir = "#{ENV['HOME']}/.config/zim"
# Parse arguments class
class ParseOptions
def self.parse(args)
options = Hash.new
opt_parser = OptionParser.new do |opts|
opts.banner = "\nUtilisation : wize.rb [options]"
opts.separator ""
whatIDo = <<-AFP
liste ou applique une configuration de template wize pour zim
AFP
opts.separator whatIDo
opts.on("-i",
"--install",
" - crée le fichier Wize_Print.html à partir",
"du fichier template correspondant en",
"remplaçant %%HOME%% par la valeur de $HOME.",
" - Installe un lien symbolique dans",
"le dossier de config de zim.",
" - Applique le thème wize de base",
"A éxécuter a priori une seule fois .") do |l|
options[:install] = l
end
opts.on("-l",
"--list",
"Liste les configurations disponibles") do |l|
options[:list] = l
end
opts.on("-p",
"--print",
"Affiche la configuration actuelle") do |p|
options[:print] = p
end
opts.on("-a",
"--apply CONF",
"applique la config CONF") do |conf|
options[:conf] = conf
end
opts.separator ""
opts.separator " Options standard:"
opts.on_tail("-h", "--help", "Affiche ce message") do
puts opts
puts ""
exit
end
opts.on_tail("-v", "--version", "Affiche la version") do
puts "Version : " + $version
puts "Auteur : " + $auteur
puts "Date : " + $date
puts ""
exit
end
end
opt_parser.parse!(args)
options
end
end
# Get the config list
def listConfigs
Dir.chdir($zimHtmlTemplateDir)
a = `ls`.split(/\n/)
names = []
r = /^common-(.+)$/
a.each { |f|
if f =~ r
names.push($1)
end
}
names
end
# Get the current config by looking at print-common
def getCurrent
Dir.chdir($zimHtmlTemplateDir)
File.readlink("print-common") =~ /^common-(.+)$/
$1
end
# Apply a config
def applyConfig(name)
Dir.chdir($zimHtmlTemplateDir)
list = ["Wize", "Wize_with_index", "Wize_with_index_sod", "Wize_S5"]
list.each { |l|
# Removing previous content
system("rm -rf #{l}/common")
system("rm -rf #{l}/common-spec")
# Copying new content
system("cp -r common #{l}/common")
system("cp -r common-#{name} #{l}/common-spec")
}
# For print
system("rm -f print-common")
system("ln -s common-#{name} print-common")
end
# Is the name a valid config?
def configInList?(name, names)
names.include? name
end
# Re-create Wize_Print.html from template
# and initialize link into zim config directory
def installWize
Dir.chdir($zimHtmlTemplateDir)
begin
printTemplate = File.new("Wize_Print_Template.tpl", "r")
rescue
puts "cannot open print file for reading"
exit
end
tabfile = []
stor = '%%HOME%%'
rby = ENV['HOME']
printTemplate.readlines.each { |line|
line.gsub!(stor,rby)
tabfile.push(line)
}
printTemplate.close
begin
printFile = File.new("Wize_Print.html", "w")
rescue
puts "cannot open print file for writing"
exit
end
tabfile.each { |line|
printFile.write line
}
printFile.close
Dir.chdir($zimConfigDir)
system("rm -f style.conf")
system("ln -s #{$zimShareDir}/style.conf style.conf")
# Now apply wize theme
applyConfig("wize")
createStyleFile("wize")
end
# Create style-name.conf according to common-name/zim-variables.css
def createStyleFile(name)
lfTags = ["--main-header-color",
"--main-u-decoration",
"--main-u-background-color",
"--main-strike-color",
"--main-tt-color",
"--main-pre-color",
"--main-link-color"]
# First we load the css file and parse it
Dir.chdir($zimHtmlTemplateDir)
begin
nameCSS = File.new("common-#{name}/zim-variables.css", "r")
rescue
puts "cannot open common-#{name}/zim-variables.css for reading"
exit
end
sCSS = nameCSS.read
regComment = /\/\*.*?\*\//m
sCSS.gsub!(regComment, '')
sTags = {}
lfTags.each { |tag|
var = Regexp.escape(tag)
reg = /#{var}\s*:\s*(\S+)\s*;/
sCSS =~ reg
sTags[tag] = $1
# On traite le cas particulier du u-decoration
if tag == "--main-u-decoration"
sTags[tag] = sTags[tag] == 'underline' ? 'PANGO_UNDERLINE_SINGLE' : ''
end
}
# know read the style template
Dir.chdir($zimShareDir)
begin
styleTemplate = File.new("style-template.conf", "r")
rescue
puts "cannot open style-template.conf for reading"
exit
end
styleTemplateString = styleTemplate.read
lfTags.each { |tag|
styleTemplateString.gsub!("%%#{tag}%%", sTags[tag])
}
# Now write the file
begin
styleOutput = File.new("style-#{name}.conf", "w")
rescue
puts "cannot open style-#{name}.conf for writing"
exit
end
styleOutput.write styleTemplateString
styleOutput.close
# Now create the symbolic link
system("rm style.conf")
system("ln -s style-#{name}.conf style.conf")
end
# Parse arguments using the class and get results in a hash
options = ParseOptions.parse(ARGV)
if options.length == 0
puts "Utiliser -h pour l'aide"
exit
end
# get existing configs
configs = listConfigs
# Installation
if options[:install]
installWize
exit
end
# List
if options[:list]
configs.each { |c|
puts c
}
exit
end
# Print
if options[:print]
puts getCurrent
exit
end
# Apply
if options[:conf]
conf = options[:conf]
# is it a valid config?
if !configInList?(conf, configs)
puts "#{conf} n'est pas une configuration valide"
exit
end
# we have a valid config
applyConfig(conf)
createStyleFile(conf)
#
puts "All done!"
puts "Les modifications de templates html sont appliquée directement,"
puts "mais il faut redémarrer zim pour que les modifications y aapparaissent"
end