Commit 40db6294283c24a5abf19a3f09ba49d6671aa16f

Authored by William Daniau
1 parent f50b33c8fb
Exists in master

Correction de nom de routine dans wize.rb

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

#!/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.html", "r") 124 124 printTemplate = File.new("Wize_Print_Template.html", "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")
end 151 151 end
152 152
# Create style-name.conf according to common-name/zim-variables.css 153 153 # Create style-name.conf according to common-name/zim-variables.css
def createStyleFile(name) 154 154 def createStyleFile(name)
lfTags = ["--main-header-color", 155 155 lfTags = ["--main-header-color",
"--main-menu-u-decoration", 156 156 "--main-menu-u-decoration",
"--main-menu-u-background-color", 157 157 "--main-menu-u-background-color",
"--main-strike-color", 158 158 "--main-strike-color",
"--main-tt-color", 159 159 "--main-tt-color",
"--main-pre-color", 160 160 "--main-pre-color",
"--main-link-color"] 161 161 "--main-link-color"]
162 162
# First we load the css file and parse it 163 163 # First we load the css file and parse it
Dir.chdir($zimHtmlTemplateDir) 164 164 Dir.chdir($zimHtmlTemplateDir)
begin 165 165 begin
nameCSS = File.new("common-#{name}/zim-variables.css", "r") 166 166 nameCSS = File.new("common-#{name}/zim-variables.css", "r")
rescue 167 167 rescue
puts "cannot open common-#{name}/zim-variables.css for reading" 168 168 puts "cannot open common-#{name}/zim-variables.css for reading"
exit 169 169 exit
end 170 170 end
sCSS = nameCSS.read 171 171 sCSS = nameCSS.read
regComment = /\/\*.*?\*\//m 172 172 regComment = /\/\*.*?\*\//m
sCSS.gsub!(regComment, '') 173 173 sCSS.gsub!(regComment, '')
sTags = {} 174 174 sTags = {}
lfTags.each { |tag| 175 175 lfTags.each { |tag|
var = Regexp.escape(tag) 176 176 var = Regexp.escape(tag)
reg = /#{var}\s*:\s*(\S+)\s*;/ 177 177 reg = /#{var}\s*:\s*(\S+)\s*;/
sCSS =~ reg 178 178 sCSS =~ reg
sTags[tag] = $1 179 179 sTags[tag] = $1
# On traite le cas particulier du u-decoration 180 180 # On traite le cas particulier du u-decoration
if tag == "--main-menu-u-decoration" 181 181 if tag == "--main-menu-u-decoration"
sTags[tag] = sTags[tag] == 'underline' ? 'PANGO_UNDERLINE_SINGLE' : '' 182 182 sTags[tag] = sTags[tag] == 'underline' ? 'PANGO_UNDERLINE_SINGLE' : ''
end 183 183 end
} 184 184 }
185 185
# know read the style template 186 186 # know read the style template
Dir.chdir($zimShareDir) 187 187 Dir.chdir($zimShareDir)
begin 188 188 begin
styleTemplate = File.new("style-template.conf", "r") 189 189 styleTemplate = File.new("style-template.conf", "r")
rescue 190 190 rescue
puts "cannot open style-template.conf for reading" 191 191 puts "cannot open style-template.conf for reading"
exit 192 192 exit
end 193 193 end
styleTemplateString = styleTemplate.read 194 194 styleTemplateString = styleTemplate.read
lfTags.each { |tag| 195 195 lfTags.each { |tag|
styleTemplateString.gsub!("%%#{tag}%%", sTags[tag]) 196 196 styleTemplateString.gsub!("%%#{tag}%%", sTags[tag])
} 197 197 }
# Now write the file 198 198 # Now write the file
begin 199 199 begin
styleOutput = File.new("style-#{name}.conf", "w") 200 200 styleOutput = File.new("style-#{name}.conf", "w")
rescue 201 201 rescue
puts "cannot open style-#{name}.conf for writing" 202 202 puts "cannot open style-#{name}.conf for writing"
exit 203 203 exit
end 204 204 end
styleOutput.write styleTemplateString 205 205 styleOutput.write styleTemplateString
styleOutput.close 206 206 styleOutput.close
# Now create the symbolic link 207 207 # Now create the symbolic link
system("rm style.conf") 208 208 system("rm style.conf")
system("ln -s style-#{name}.conf style.conf") 209 209 system("ln -s style-#{name}.conf style.conf")
end 210 210 end
211 211
# Parse arguments using the class and get results in a hash 212 212 # Parse arguments using the class and get results in a hash
options = ParseOptions.parse(ARGV) 213 213 options = ParseOptions.parse(ARGV)
if options.length == 0 214 214 if options.length == 0
puts "Utiliser -h pour l'aide" 215 215 puts "Utiliser -h pour l'aide"
exit 216 216 exit
end 217 217 end
218 218
# get existing configs 219 219 # get existing configs
configs = listConfigs 220 220 configs = listConfigs
221 221
# Installation 222 222 # Installation
if options[:install] 223 223 if options[:install]
installPrintFile 224 224 installWize
exit 225 225 exit
end 226 226 end
227 227
# List 228 228 # List
if options[:list] 229 229 if options[:list]