Blame view
wize.rb
5.84 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 30 |
opts.on("-i", "--install", "modifie le fichier Wize_Print.html en remplaçant", |
7688b13e4 Ajout de l'option... |
31 32 33 |
"%%HOME%% par la valeur de $HOME. Installe également", "un lien symbolique dans le dossier de config de zim.", "A éxécuter une seule fois a priori.") do |l| |
2623019d5 Amélioration de w... |
34 35 |
options[:install] = l end |
17b522613 Ajout d'un script... |
36 37 38 39 40 |
opts.on("-l", "--list", "Liste les configurations disponibles") do |l| options[:list] = l end |
7688b13e4 Ajout de l'option... |
41 42 43 44 45 |
opts.on("-p", "--print", "Affiche la configuration actuelle") do |p| options[:print] = p end |
17b522613 Ajout d'un script... |
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 |
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... |
72 |
# Get the config list |
17b522613 Ajout d'un script... |
73 |
def listConfigs |
2623019d5 Amélioration de w... |
74 |
Dir.chdir($zimHtmlTemplateDir) |
17b522613 Ajout d'un script... |
75 76 77 78 79 80 81 82 83 84 85 |
a = `ls`.split(/ /) names = [] r = /^common-(.+)$/ a.each { |f| if f =~ r names.push($1) end } names end |
7688b13e4 Ajout de l'option... |
86 87 88 89 90 91 |
# 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... |
92 |
# Apply a config |
17b522613 Ajout d'un script... |
93 |
def applyConfig(name) |
2623019d5 Amélioration de w... |
94 |
Dir.chdir($zimHtmlTemplateDir) |
17b522613 Ajout d'un script... |
95 96 97 98 99 100 101 102 103 104 |
list = ["Wize", "Wize_with_index", "Wize_with_index_sod"] list.each { |l| com = "rm -f #{l}/common-spec" system(com) com = "ln -s ../common-#{name} #{l}/common-spec" system(com) } system("rm -f print-common") system("ln -s common-#{name} print-common") end |
2623019d5 Amélioration de w... |
105 |
# Is the name a valid config? |
17b522613 Ajout d'un script... |
106 107 108 |
def configInList?(name, names) names.include? name end |
2623019d5 Amélioration de w... |
109 110 111 112 113 |
# Re-create Wize_Print.html from template # and initialize link into zim config directory def installWize Dir.chdir($zimHtmlTemplateDir) begin |
a7fb3dffa + On renomme Wize... |
114 |
printTemplate = File.new("Wize_Print_Template.tpl", "r") |
2623019d5 Amélioration de w... |
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 |
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") end # Create style-name.conf according to common-name/zim-variables.css def createStyleFile(name) lfTags = ["--main-header-color", "--main-menu-u-decoration", "--main-menu-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-menu-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 |
17b522613 Ajout d'un script... |
201 202 203 204 205 206 |
# 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... |
207 208 |
# get existing configs configs = listConfigs |
2623019d5 Amélioration de w... |
209 210 |
# Installation if options[:install] |
40db62942 Correction de nom... |
211 |
installWize |
2623019d5 Amélioration de w... |
212 213 |
exit end |
17b522613 Ajout d'un script... |
214 215 216 217 218 219 220 |
# List if options[:list] configs.each { |c| puts c } exit end |
7688b13e4 Ajout de l'option... |
221 222 223 224 225 |
# Print if options[:print] puts getCurrent exit end |
17b522613 Ajout d'un script... |
226 227 228 229 230 231 232 233 234 235 |
# 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... |
236 237 238 239 240 |
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... |
241 |
end |