Commit ad9c10b6d1fe6b732f9cbe851dd26842972e7b5c

Authored by William Daniau
1 parent 881c5c4142
Exists in master

Affichage de l'aide en 80 colonnes.

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