Commit caac8ebc7a4877b7832c48db66bd94044edaaeeb
1 parent
fc7eccbc7c
Exists in
master
On copie au lieu de faire des liens symboliques.
Showing 1 changed file with 7 additions and 4 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 | # Removing previous content | |
system(com) | 106 | 106 | system("rm -rf #{l}/common") | |
com = "ln -s ../common-#{name} #{l}/common-spec" | 107 | 107 | system("rm -rf #{l}/common-spec") | |
system(com) | 108 | 108 | # Copying new content | |
109 | system("cp -r common #{l}/common") | |||
110 | system("cp -r common-#{name} #{l}/common-spec") | |||
} | 109 | 111 | } | |
112 | # For print | |||
system("rm -f print-common") | 110 | 113 | system("rm -f print-common") | |
system("ln -s common-#{name} print-common") | 111 | 114 | system("ln -s common-#{name} print-common") | |
end | 112 | 115 | end | |
113 | 116 | |||
# Is the name a valid config? | 114 | 117 | # Is the name a valid config? | |
def configInList?(name, names) | 115 | 118 | def configInList?(name, names) | |
names.include? name | 116 | 119 | names.include? name | |
end | 117 | 120 | end | |
118 | 121 | |||
# Re-create Wize_Print.html from template | 119 | 122 | # Re-create Wize_Print.html from template | |
# and initialize link into zim config directory | 120 | 123 | # and initialize link into zim config directory | |
def installWize | 121 | 124 | def installWize | |
Dir.chdir($zimHtmlTemplateDir) | 122 | 125 | Dir.chdir($zimHtmlTemplateDir) | |
begin | 123 | 126 | begin | |
printTemplate = File.new("Wize_Print_Template.tpl", "r") | 124 | 127 | printTemplate = File.new("Wize_Print_Template.tpl", "r") | |
rescue | 125 | 128 | rescue | |
puts "cannot open print file for reading" | 126 | 129 | puts "cannot open print file for reading" | |
exit | 127 | 130 | exit | |
end | 128 | 131 | end | |
tabfile = [] | 129 | 132 | tabfile = [] | |
stor = '%%HOME%%' | 130 | 133 | stor = '%%HOME%%' | |
rby = ENV['HOME'] | 131 | 134 | rby = ENV['HOME'] | |
printTemplate.readlines.each { |line| | 132 | 135 | printTemplate.readlines.each { |line| | |
line.gsub!(stor,rby) | 133 | 136 | line.gsub!(stor,rby) | |
tabfile.push(line) | 134 | 137 | tabfile.push(line) | |
} | 135 | 138 | } | |
printTemplate.close | 136 | 139 | printTemplate.close | |
begin | 137 | 140 | begin | |
printFile = File.new("Wize_Print.html", "w") | 138 | 141 | printFile = File.new("Wize_Print.html", "w") | |
rescue | 139 | 142 | rescue | |
puts "cannot open print file for writing" | 140 | 143 | puts "cannot open print file for writing" | |
exit | 141 | 144 | exit | |
end | 142 | 145 | end | |
tabfile.each { |line| | 143 | 146 | tabfile.each { |line| | |
printFile.write line | 144 | 147 | printFile.write line | |
} | 145 | 148 | } | |
printFile.close | 146 | 149 | printFile.close | |
147 | 150 | |||
Dir.chdir($zimConfigDir) | 148 | 151 | Dir.chdir($zimConfigDir) | |
system("rm -f style.conf") | 149 | 152 | system("rm -f style.conf") | |
system("ln -s #{$zimShareDir}/style.conf style.conf") | 150 | 153 | system("ln -s #{$zimShareDir}/style.conf style.conf") | |
151 | 154 | |||
# Now apply wize theme | 152 | 155 | # Now apply wize theme | |
applyConfig("wize") | 153 | 156 | applyConfig("wize") | |
createStyleFile("wize") | 154 | 157 | createStyleFile("wize") | |
end | 155 | 158 | end | |
156 | 159 | |||
# Create style-name.conf according to common-name/zim-variables.css | 157 | 160 | # Create style-name.conf according to common-name/zim-variables.css | |
def createStyleFile(name) | 158 | 161 | def createStyleFile(name) | |
lfTags = ["--main-header-color", | 159 | 162 | lfTags = ["--main-header-color", | |
"--main-menu-u-decoration", | 160 | 163 | "--main-menu-u-decoration", | |
"--main-menu-u-background-color", | 161 | 164 | "--main-menu-u-background-color", | |
"--main-strike-color", | 162 | 165 | "--main-strike-color", | |
"--main-tt-color", | 163 | 166 | "--main-tt-color", | |
"--main-pre-color", | 164 | 167 | "--main-pre-color", | |
"--main-link-color"] | 165 | 168 | "--main-link-color"] | |
166 | 169 | |||
# First we load the css file and parse it | 167 | 170 | # First we load the css file and parse it | |
Dir.chdir($zimHtmlTemplateDir) | 168 | 171 | Dir.chdir($zimHtmlTemplateDir) | |
begin | 169 | 172 | begin | |
nameCSS = File.new("common-#{name}/zim-variables.css", "r") | 170 | 173 | nameCSS = File.new("common-#{name}/zim-variables.css", "r") | |
rescue | 171 | 174 | rescue | |
puts "cannot open common-#{name}/zim-variables.css for reading" | 172 | 175 | puts "cannot open common-#{name}/zim-variables.css for reading" | |
exit | 173 | 176 | exit | |
end | 174 | 177 | end | |
sCSS = nameCSS.read | 175 | 178 | sCSS = nameCSS.read | |
regComment = /\/\*.*?\*\//m | 176 | 179 | regComment = /\/\*.*?\*\//m | |
sCSS.gsub!(regComment, '') | 177 | 180 | sCSS.gsub!(regComment, '') | |
sTags = {} | 178 | 181 | sTags = {} | |
lfTags.each { |tag| | 179 | 182 | lfTags.each { |tag| | |
var = Regexp.escape(tag) | 180 | 183 | var = Regexp.escape(tag) | |
reg = /#{var}\s*:\s*(\S+)\s*;/ | 181 | 184 | reg = /#{var}\s*:\s*(\S+)\s*;/ | |
sCSS =~ reg | 182 | 185 | sCSS =~ reg | |
sTags[tag] = $1 | 183 | 186 | sTags[tag] = $1 | |
# On traite le cas particulier du u-decoration | 184 | 187 | # On traite le cas particulier du u-decoration | |
if tag == "--main-menu-u-decoration" | 185 | 188 | if tag == "--main-menu-u-decoration" | |
sTags[tag] = sTags[tag] == 'underline' ? 'PANGO_UNDERLINE_SINGLE' : '' | 186 | 189 | sTags[tag] = sTags[tag] == 'underline' ? 'PANGO_UNDERLINE_SINGLE' : '' | |
end | 187 | 190 | end | |
} | 188 | 191 | } | |
189 | 192 | |||
# know read the style template | 190 | 193 | # know read the style template | |
Dir.chdir($zimShareDir) | 191 | 194 | Dir.chdir($zimShareDir) | |
begin | 192 | 195 | begin | |
styleTemplate = File.new("style-template.conf", "r") | 193 | 196 | styleTemplate = File.new("style-template.conf", "r") | |
rescue | 194 | 197 | rescue | |
puts "cannot open style-template.conf for reading" | 195 | 198 | puts "cannot open style-template.conf for reading" | |
exit | 196 | 199 | exit | |
end | 197 | 200 | end | |
styleTemplateString = styleTemplate.read | 198 | 201 | styleTemplateString = styleTemplate.read | |
lfTags.each { |tag| | 199 | 202 | lfTags.each { |tag| | |
styleTemplateString.gsub!("%%#{tag}%%", sTags[tag]) | 200 | 203 | styleTemplateString.gsub!("%%#{tag}%%", sTags[tag]) | |
} | 201 | 204 | } | |
# Now write the file | 202 | 205 | # Now write the file | |
begin | 203 | 206 | begin | |
styleOutput = File.new("style-#{name}.conf", "w") | 204 | 207 | styleOutput = File.new("style-#{name}.conf", "w") | |
rescue | 205 | 208 | rescue | |
puts "cannot open style-#{name}.conf for writing" | 206 | 209 | puts "cannot open style-#{name}.conf for writing" | |
exit | 207 | 210 | exit | |
end | 208 | 211 | end | |
styleOutput.write styleTemplateString | 209 | 212 | styleOutput.write styleTemplateString | |
styleOutput.close | 210 | 213 | styleOutput.close | |
# Now create the symbolic link | 211 | 214 | # Now create the symbolic link | |
system("rm style.conf") | 212 | 215 | system("rm style.conf") | |
system("ln -s style-#{name}.conf style.conf") | 213 | 216 | system("ln -s style-#{name}.conf style.conf") | |
end | 214 | 217 | end | |
215 | 218 | |||
# Parse arguments using the class and get results in a hash | 216 | 219 | # Parse arguments using the class and get results in a hash | |
options = ParseOptions.parse(ARGV) | 217 | 220 | options = ParseOptions.parse(ARGV) | |
if options.length == 0 | 218 | 221 | if options.length == 0 | |
puts "Utiliser -h pour l'aide" | 219 | 222 | puts "Utiliser -h pour l'aide" | |
exit | 220 | 223 | exit | |
end | 221 | 224 | end | |
222 | 225 | |||
# get existing configs | 223 | 226 | # get existing configs | |
configs = listConfigs | 224 | 227 | configs = listConfigs | |
225 | 228 | |||
# Installation | 226 | 229 | # Installation | |
if options[:install] | 227 | 230 | if options[:install] | |
installWize | 228 | 231 | installWize | |
exit | 229 | 232 | exit | |
end | 230 | 233 | end | |
231 | 234 | |||
# List | 232 | 235 | # List | |
if options[:list] | 233 | 236 | if options[:list] | |
configs.each { |c| | 234 | 237 | configs.each { |c| | |
puts c | 235 | 238 | puts c |