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