Commit d3cf595986ce0cee9e6f12282d6b7289a2ce7181
1 parent
d983184f3a
Exists in
master
Change wize.rb to apply config on install
Showing 1 changed file with 3 additions and 0 deletions Inline Diff
wize.rb
| #!/usr/bin/env ruby | 1 | 1 | #!/usr/bin/env ruby | |
| 2 | 2 | |||
| require 'optparse' | 3 | 3 | require 'optparse' | |
| 4 | 4 | |||
| # Variables globales $version, $auteur et $date | 5 | 5 | # Variables globales $version, $auteur et $date | |
| $version = "0.1" | 6 | 6 | $version = "0.1" | |
| $auteur = "William Daniau" | 7 | 7 | $auteur = "William Daniau" | |
| $date = "2019 10 11" | 8 | 8 | $date = "2019 10 11" | |
| 9 | 9 | |||
| # Global directory definitions | 10 | 10 | # Global directory definitions | |
| $zimShareDir = "#{ENV['HOME']}/.local/share/zim" | 11 | 11 | $zimShareDir = "#{ENV['HOME']}/.local/share/zim" | |
| $zimHtmlTemplateDir = "#{$zimShareDir}/templates/html" | 12 | 12 | $zimHtmlTemplateDir = "#{$zimShareDir}/templates/html" | |
| $zimConfigDir = "#{ENV['HOME']}/.config/zim" | 13 | 13 | $zimConfigDir = "#{ENV['HOME']}/.config/zim" | |
| 14 | 14 | |||
| # Parse arguments class | 15 | 15 | # Parse arguments class | |
| class ParseOptions | 16 | 16 | class ParseOptions | |
| def self.parse(args) | 17 | 17 | def self.parse(args) | |
| options = Hash.new | 18 | 18 | options = Hash.new | |
| 19 | 19 | |||
| opt_parser = OptionParser.new do |opts| | 20 | 20 | opt_parser = OptionParser.new do |opts| | |
| opts.banner = "\nUtilisation : wize.rb [options]" | 21 | 21 | opts.banner = "\nUtilisation : wize.rb [options]" | |
| opts.separator "" | 22 | 22 | opts.separator "" | |
| whatIDo = <<-AFP | 23 | 23 | whatIDo = <<-AFP | |
| 24 | 24 | |||
| liste ou applique une configuration de template wize pour zim | 25 | 25 | liste ou applique une configuration de template wize pour zim | |
| 26 | 26 | |||
| AFP | 27 | 27 | AFP | |
| 28 | 28 | |||
| opts.separator whatIDo | 29 | 29 | opts.separator whatIDo | |
| 30 | 30 | |||
| opts.on("-i", | 31 | 31 | opts.on("-i", | |
| "--install", | 32 | 32 | "--install", | |
| "modifie le fichier Wize_Print.html en remplaçant", | 33 | 33 | "modifie le fichier Wize_Print.html en remplaçant", | |
| "%%HOME%% par la valeur de $HOME. Installe également", | 34 | 34 | "%%HOME%% par la valeur de $HOME. Installe également", | |
| "un lien symbolique dans le dossier de config de zim.", | 35 | 35 | "un lien symbolique dans le dossier de config de zim.", | |
| "A éxécuter une seule fois a priori.") do |l| | 36 | 36 | "A éxécuter une seule fois a priori.") do |l| | |
| options[:install] = l | 37 | 37 | options[:install] = l | |
| end | 38 | 38 | end | |
| 39 | 39 | |||
| opts.on("-l", | 40 | 40 | opts.on("-l", | |
| "--list", | 41 | 41 | "--list", | |
| "Liste les configurations disponibles") do |l| | 42 | 42 | "Liste les configurations disponibles") do |l| | |
| options[:list] = l | 43 | 43 | options[:list] = l | |
| end | 44 | 44 | end | |
| 45 | 45 | |||
| opts.on("-p", | 46 | 46 | opts.on("-p", | |
| "--print", | 47 | 47 | "--print", | |
| "Affiche la configuration actuelle") do |p| | 48 | 48 | "Affiche la configuration actuelle") do |p| | |
| options[:print] = p | 49 | 49 | options[:print] = p | |
| end | 50 | 50 | end | |
| 51 | 51 | |||
| opts.on("-a", | 52 | 52 | opts.on("-a", | |
| "--apply CONF", | 53 | 53 | "--apply CONF", | |
| "applique la config CONF") do |conf| | 54 | 54 | "applique la config CONF") do |conf| | |
| options[:conf] = conf | 55 | 55 | options[:conf] = conf | |
| end | 56 | 56 | end | |
| 57 | 57 | |||
| opts.separator "" | 58 | 58 | opts.separator "" | |
| opts.separator " Options standard:" | 59 | 59 | opts.separator " Options standard:" | |
| opts.on_tail("-h", "--help", "Affiche ce message") do | 60 | 60 | opts.on_tail("-h", "--help", "Affiche ce message") do | |
| puts opts | 61 | 61 | puts opts | |
| puts "" | 62 | 62 | puts "" | |
| exit | 63 | 63 | exit | |
| end | 64 | 64 | end | |
| opts.on_tail("-v", "--version", "Affiche la version") do | 65 | 65 | opts.on_tail("-v", "--version", "Affiche la version") do | |
| puts "Version : " + $version | 66 | 66 | puts "Version : " + $version | |
| puts "Auteur : " + $auteur | 67 | 67 | puts "Auteur : " + $auteur | |
| puts "Date : " + $date | 68 | 68 | puts "Date : " + $date | |
| puts "" | 69 | 69 | puts "" | |
| exit | 70 | 70 | exit | |
| end | 71 | 71 | end | |
| end | 72 | 72 | end | |
| 73 | 73 | |||
| opt_parser.parse!(args) | 74 | 74 | opt_parser.parse!(args) | |
| options | 75 | 75 | options | |
| end | 76 | 76 | end | |
| end | 77 | 77 | end | |
| 78 | 78 | |||
| # Get the config list | 79 | 79 | # Get the config list | |
| def listConfigs | 80 | 80 | def listConfigs | |
| Dir.chdir($zimHtmlTemplateDir) | 81 | 81 | Dir.chdir($zimHtmlTemplateDir) | |
| a = `ls`.split(/\n/) | 82 | 82 | a = `ls`.split(/\n/) | |
| names = [] | 83 | 83 | names = [] | |
| r = /^common-(.+)$/ | 84 | 84 | r = /^common-(.+)$/ | |
| a.each { |f| | 85 | 85 | a.each { |f| | |
| if f =~ r | 86 | 86 | if f =~ r | |
| names.push($1) | 87 | 87 | names.push($1) | |
| end | 88 | 88 | end | |
| } | 89 | 89 | } | |
| names | 90 | 90 | names | |
| end | 91 | 91 | end | |
| 92 | 92 | |||
| # Get the current config by looking at print-common | 93 | 93 | # Get the current config by looking at print-common | |
| def getCurrent | 94 | 94 | def getCurrent | |
| Dir.chdir($zimHtmlTemplateDir) | 95 | 95 | Dir.chdir($zimHtmlTemplateDir) | |
| File.readlink("print-common") =~ /^common-(.+)$/ | 96 | 96 | File.readlink("print-common") =~ /^common-(.+)$/ | |
| $1 | 97 | 97 | $1 | |
| end | 98 | 98 | end | |
| 99 | 99 | |||
| # Apply a config | 100 | 100 | # Apply a config | |
| def applyConfig(name) | 101 | 101 | def applyConfig(name) | |
| Dir.chdir($zimHtmlTemplateDir) | 102 | 102 | Dir.chdir($zimHtmlTemplateDir) | |
| list = ["Wize", "Wize_with_index", "Wize_with_index_sod"] | 103 | 103 | list = ["Wize", "Wize_with_index", "Wize_with_index_sod"] | |
| list.each { |l| | 104 | 104 | list.each { |l| | |
| com = "rm -f #{l}/common-spec" | 105 | 105 | com = "rm -f #{l}/common-spec" | |
| system(com) | 106 | 106 | system(com) | |
| com = "ln -s ../common-#{name} #{l}/common-spec" | 107 | 107 | com = "ln -s ../common-#{name} #{l}/common-spec" | |
| system(com) | 108 | 108 | system(com) | |
| } | 109 | 109 | } | |
| system("rm -f print-common") | 110 | 110 | system("rm -f print-common") | |
| system("ln -s common-#{name} print-common") | 111 | 111 | system("ln -s common-#{name} print-common") | |
| end | 112 | 112 | end | |
| 113 | 113 | |||
| # Is the name a valid config? | 114 | 114 | # Is the name a valid config? | |
| def configInList?(name, names) | 115 | 115 | def configInList?(name, names) | |
| names.include? name | 116 | 116 | names.include? name | |
| end | 117 | 117 | end | |
| 118 | 118 | |||
| # Re-create Wize_Print.html from template | 119 | 119 | # Re-create Wize_Print.html from template | |
| # and initialize link into zim config directory | 120 | 120 | # and initialize link into zim config directory | |
| def installWize | 121 | 121 | def installWize | |
| Dir.chdir($zimHtmlTemplateDir) | 122 | 122 | Dir.chdir($zimHtmlTemplateDir) | |
| begin | 123 | 123 | begin | |
| printTemplate = File.new("Wize_Print_Template.tpl", "r") | 124 | 124 | printTemplate = File.new("Wize_Print_Template.tpl", "r") | |
| rescue | 125 | 125 | rescue | |
| puts "cannot open print file for reading" | 126 | 126 | puts "cannot open print file for reading" | |
| exit | 127 | 127 | exit | |
| end | 128 | 128 | end | |
| tabfile = [] | 129 | 129 | tabfile = [] | |
| stor = '%%HOME%%' | 130 | 130 | stor = '%%HOME%%' | |
| rby = ENV['HOME'] | 131 | 131 | rby = ENV['HOME'] | |
| printTemplate.readlines.each { |line| | 132 | 132 | printTemplate.readlines.each { |line| | |
| line.gsub!(stor,rby) | 133 | 133 | line.gsub!(stor,rby) | |
| tabfile.push(line) | 134 | 134 | tabfile.push(line) | |
| } | 135 | 135 | } | |
| printTemplate.close | 136 | 136 | printTemplate.close | |
| begin | 137 | 137 | begin | |
| printFile = File.new("Wize_Print.html", "w") | 138 | 138 | printFile = File.new("Wize_Print.html", "w") | |
| rescue | 139 | 139 | rescue | |
| puts "cannot open print file for writing" | 140 | 140 | puts "cannot open print file for writing" | |
| exit | 141 | 141 | exit | |
| end | 142 | 142 | end | |
| tabfile.each { |line| | 143 | 143 | tabfile.each { |line| | |
| printFile.write line | 144 | 144 | printFile.write line | |
| } | 145 | 145 | } | |
| printFile.close | 146 | 146 | printFile.close | |
| 147 | 147 | |||
| Dir.chdir($zimConfigDir) | 148 | 148 | Dir.chdir($zimConfigDir) | |
| system("rm -f style.conf") | 149 | 149 | system("rm -f style.conf") | |
| system("ln -s #{$zimShareDir}/style.conf style.conf") | 150 | 150 | system("ln -s #{$zimShareDir}/style.conf style.conf") | |
| 151 | ||||
| 152 | # Now apply wize theme | |||
| 153 | applyConfig("wize") | |||
| end | 151 | 154 | end | |
| 152 | 155 | |||
| # Create style-name.conf according to common-name/zim-variables.css | 153 | 156 | # Create style-name.conf according to common-name/zim-variables.css | |
| def createStyleFile(name) | 154 | 157 | def createStyleFile(name) | |
| lfTags = ["--main-header-color", | 155 | 158 | lfTags = ["--main-header-color", | |
| "--main-menu-u-decoration", | 156 | 159 | "--main-menu-u-decoration", | |
| "--main-menu-u-background-color", | 157 | 160 | "--main-menu-u-background-color", | |
| "--main-strike-color", | 158 | 161 | "--main-strike-color", | |
| "--main-tt-color", | 159 | 162 | "--main-tt-color", | |
| "--main-pre-color", | 160 | 163 | "--main-pre-color", | |
| "--main-link-color"] | 161 | 164 | "--main-link-color"] | |
| 162 | 165 | |||
| # First we load the css file and parse it | 163 | 166 | # First we load the css file and parse it | |
| Dir.chdir($zimHtmlTemplateDir) | 164 | 167 | Dir.chdir($zimHtmlTemplateDir) | |
| begin | 165 | 168 | begin | |
| nameCSS = File.new("common-#{name}/zim-variables.css", "r") | 166 | 169 | nameCSS = File.new("common-#{name}/zim-variables.css", "r") | |
| rescue | 167 | 170 | rescue | |
| puts "cannot open common-#{name}/zim-variables.css for reading" | 168 | 171 | puts "cannot open common-#{name}/zim-variables.css for reading" | |
| exit | 169 | 172 | exit | |
| end | 170 | 173 | end | |
| sCSS = nameCSS.read | 171 | 174 | sCSS = nameCSS.read | |
| regComment = /\/\*.*?\*\//m | 172 | 175 | regComment = /\/\*.*?\*\//m | |
| sCSS.gsub!(regComment, '') | 173 | 176 | sCSS.gsub!(regComment, '') | |
| sTags = {} | 174 | 177 | sTags = {} | |
| lfTags.each { |tag| | 175 | 178 | lfTags.each { |tag| | |
| var = Regexp.escape(tag) | 176 | 179 | var = Regexp.escape(tag) | |
| reg = /#{var}\s*:\s*(\S+)\s*;/ | 177 | 180 | reg = /#{var}\s*:\s*(\S+)\s*;/ | |
| sCSS =~ reg | 178 | 181 | sCSS =~ reg | |
| sTags[tag] = $1 | 179 | 182 | sTags[tag] = $1 | |
| # On traite le cas particulier du u-decoration | 180 | 183 | # On traite le cas particulier du u-decoration | |
| if tag == "--main-menu-u-decoration" | 181 | 184 | if tag == "--main-menu-u-decoration" | |
| sTags[tag] = sTags[tag] == 'underline' ? 'PANGO_UNDERLINE_SINGLE' : '' | 182 | 185 | sTags[tag] = sTags[tag] == 'underline' ? 'PANGO_UNDERLINE_SINGLE' : '' | |
| end | 183 | 186 | end | |
| } | 184 | 187 | } | |
| 185 | 188 | |||
| # know read the style template | 186 | 189 | # know read the style template | |
| Dir.chdir($zimShareDir) | 187 | 190 | Dir.chdir($zimShareDir) | |
| begin | 188 | 191 | begin | |
| styleTemplate = File.new("style-template.conf", "r") | 189 | 192 | styleTemplate = File.new("style-template.conf", "r") | |
| rescue | 190 | 193 | rescue | |
| puts "cannot open style-template.conf for reading" | 191 | 194 | puts "cannot open style-template.conf for reading" | |
| exit | 192 | 195 | exit | |
| end | 193 | 196 | end | |
| styleTemplateString = styleTemplate.read | 194 | 197 | styleTemplateString = styleTemplate.read | |
| lfTags.each { |tag| | 195 | 198 | lfTags.each { |tag| | |
| styleTemplateString.gsub!("%%#{tag}%%", sTags[tag]) | 196 | 199 | styleTemplateString.gsub!("%%#{tag}%%", sTags[tag]) | |
| } | 197 | 200 | } | |
| # Now write the file | 198 | 201 | # Now write the file | |
| begin | 199 | 202 | begin | |
| styleOutput = File.new("style-#{name}.conf", "w") | 200 | 203 | styleOutput = File.new("style-#{name}.conf", "w") | |
| rescue | 201 | 204 | rescue | |
| puts "cannot open style-#{name}.conf for writing" | 202 | 205 | puts "cannot open style-#{name}.conf for writing" | |
| exit | 203 | 206 | exit | |
| end | 204 | 207 | end | |
| styleOutput.write styleTemplateString | 205 | 208 | styleOutput.write styleTemplateString | |
| styleOutput.close | 206 | 209 | styleOutput.close | |
| # Now create the symbolic link | 207 | 210 | # Now create the symbolic link | |
| system("rm style.conf") | 208 | 211 | system("rm style.conf") | |
| system("ln -s style-#{name}.conf style.conf") | 209 | 212 | system("ln -s style-#{name}.conf style.conf") | |
| end | 210 | 213 | end | |
| 211 | 214 | |||
| # Parse arguments using the class and get results in a hash | 212 | 215 | # Parse arguments using the class and get results in a hash | |
| options = ParseOptions.parse(ARGV) | 213 | 216 | options = ParseOptions.parse(ARGV) | |
| if options.length == 0 | 214 | 217 | if options.length == 0 | |
| puts "Utiliser -h pour l'aide" | 215 | 218 | puts "Utiliser -h pour l'aide" | |
| exit | 216 | 219 | exit | |
| end | 217 | 220 | end | |
| 218 | 221 | |||
| # get existing configs | 219 | 222 | # get existing configs | |
| configs = listConfigs | 220 | 223 | configs = listConfigs | |
| 221 | 224 | |||
| # Installation | 222 | 225 | # Installation | |
| if options[:install] | 223 | 226 | if options[:install] | |
| installWize | 224 | 227 | installWize | |
| exit | 225 | 228 | exit | |
| end | 226 | 229 | end | |
| 227 | 230 | |||
| # List | 228 | 231 | # List |