Blame view
wize.rb
6.15 KB
17b522613 Ajout d'un script... |
1 2 3 4 5 6 7 8 |
#!/usr/bin/env ruby require 'optparse' # Variables globales $version, $auteur et $date $version = "0.1" $auteur = "William Daniau" $date = "2019 10 11" |
2623019d5 Amélioration de w... |
9 10 11 12 |
# Global directory definitions $zimShareDir = "#{ENV['HOME']}/.local/share/zim" $zimHtmlTemplateDir = "#{$zimShareDir}/templates/html" $zimConfigDir = "#{ENV['HOME']}/.config/zim" |
17b522613 Ajout d'un script... |
13 14 15 16 17 18 19 20 21 22 |
# Parse arguments class class ParseOptions def self.parse(args) options = Hash.new opt_parser = OptionParser.new do |opts| opts.banner = " Utilisation : wize.rb [options]" opts.separator "" whatIDo = <<-AFP |
7688b13e4 Ajout de l'option... |
23 |
liste ou applique une configuration de template wize pour zim |
17b522613 Ajout d'un script... |
24 25 26 27 |
AFP opts.separator whatIDo |
2623019d5 Amélioration de w... |
28 29 |
opts.on("-i", "--install", |
ad9c10b6d Affichage de l'ai... |
30 31 32 33 34 35 36 |
" - 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| |
2623019d5 Amélioration de w... |
37 38 |
options[:install] = l end |
17b522613 Ajout d'un script... |
39 40 41 42 43 |
opts.on("-l", "--list", "Liste les configurations disponibles") do |l| options[:list] = l end |
7688b13e4 Ajout de l'option... |
44 45 46 47 48 |
opts.on("-p", "--print", "Affiche la configuration actuelle") do |p| options[:print] = p end |
17b522613 Ajout d'un script... |
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 |
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 |
2623019d5 Amélioration de w... |
75 |
# Get the config list |
17b522613 Ajout d'un script... |
76 |
def listConfigs |
2623019d5 Amélioration de w... |
77 |
Dir.chdir($zimHtmlTemplateDir) |
17b522613 Ajout d'un script... |
78 79 80 81 82 83 84 85 86 87 88 |
a = `ls`.split(/ /) names = [] r = /^common-(.+)$/ a.each { |f| if f =~ r names.push($1) end } names end |
7688b13e4 Ajout de l'option... |
89 90 91 92 93 94 |
# Get the current config by looking at print-common def getCurrent Dir.chdir($zimHtmlTemplateDir) File.readlink("print-common") =~ /^common-(.+)$/ $1 end |
2623019d5 Amélioration de w... |
95 |
# Apply a config |
17b522613 Ajout d'un script... |
96 |
def applyConfig(name) |
2623019d5 Amélioration de w... |
97 |
Dir.chdir($zimHtmlTemplateDir) |
4f3a614a5 Premier essai de ... |
98 |
list = ["Wize", "Wize_with_index", "Wize_with_index_sod", "Wize_S5"] |
17b522613 Ajout d'un script... |
99 |
list.each { |l| |
caac8ebc7 On copie au lieu ... |
100 101 102 103 104 105 |
# 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") |
17b522613 Ajout d'un script... |
106 |
} |
caac8ebc7 On copie au lieu ... |
107 |
# For print |
17b522613 Ajout d'un script... |
108 109 110 |
system("rm -f print-common") system("ln -s common-#{name} print-common") end |
2623019d5 Amélioration de w... |
111 |
# Is the name a valid config? |
17b522613 Ajout d'un script... |
112 113 114 |
def configInList?(name, names) names.include? name end |
2623019d5 Amélioration de w... |
115 116 117 118 119 |
# Re-create Wize_Print.html from template # and initialize link into zim config directory def installWize Dir.chdir($zimHtmlTemplateDir) begin |
a7fb3dffa + On renomme Wize... |
120 |
printTemplate = File.new("Wize_Print_Template.tpl", "r") |
2623019d5 Amélioration de w... |
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 |
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") |
d3cf59598 Change wize.rb to... |
147 148 149 |
# Now apply wize theme applyConfig("wize") |
e8d89d4eb Lors de l'install... |
150 |
createStyleFile("wize") |
2623019d5 Amélioration de w... |
151 152 153 154 155 |
end # Create style-name.conf according to common-name/zim-variables.css def createStyleFile(name) lfTags = ["--main-header-color", |
a6614bd01 Changement de nom... |
156 157 |
"--main-u-decoration", "--main-u-background-color", |
2623019d5 Amélioration de w... |
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
"--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 |
a6614bd01 Changement de nom... |
181 |
if tag == "--main-u-decoration" |
2623019d5 Amélioration de w... |
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 |
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 |
17b522613 Ajout d'un script... |
211 212 213 214 215 216 |
# 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 |
17b522613 Ajout d'un script... |
217 218 |
# get existing configs configs = listConfigs |
2623019d5 Amélioration de w... |
219 220 |
# Installation if options[:install] |
40db62942 Correction de nom... |
221 |
installWize |
2623019d5 Amélioration de w... |
222 223 |
exit end |
17b522613 Ajout d'un script... |
224 225 226 227 228 229 230 |
# List if options[:list] configs.each { |c| puts c } exit end |
7688b13e4 Ajout de l'option... |
231 232 233 234 235 |
# Print if options[:print] puts getCurrent exit end |
17b522613 Ajout d'un script... |
236 237 238 239 240 241 242 243 244 245 |
# 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) |
2623019d5 Amélioration de w... |
246 247 248 249 250 |
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" |
17b522613 Ajout d'un script... |
251 |
end |