Commit e8d89d4eb3a0673125efaccebce8c1fcae5e80a9

Authored by William Daniau
1 parent 659c7d608b
Exists in master

Lors de l'install on crée aussi style.conf

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