Blame view

wize.rb 5.35 KB
17b522613   William Daniau   Ajout d'un script...
1
2
3
4
5
6
7
8
  #!/usr/bin/env ruby
  
  require 'optparse'
  
  # Variables globales $version, $auteur et $date
  $version = "0.1"
  $auteur = "William Daniau"
  $date = "2019 10 11"
2623019d5   William Daniau   Amélioration de w...
9
10
11
12
  # Global directory definitions
  $zimShareDir = "#{ENV['HOME']}/.local/share/zim"
  $zimHtmlTemplateDir = "#{$zimShareDir}/templates/html"
  $zimConfigDir = "#{ENV['HOME']}/.config/zim"
17b522613   William Daniau   Ajout d'un script...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  # Parse arguments class
  class ParseOptions
    def self.parse(args)
      options = Hash.new
  
      opt_parser = OptionParser.new do |opts|
        opts.banner = "
  Utilisation : wize.rb [options]"
        opts.separator ""
        whatIDo = <<-AFP
  
        liste ou applique une configuartion de template wize pour zim
  
              AFP
  
        opts.separator whatIDo
2623019d5   William Daniau   Amélioration de w...
29
30
31
32
33
34
        opts.on("-i",
                "--install",
                "modifie le fichier Wize_Print.html en remplaçant",
                "%%HOME%% par la valeur de $HOME") do |l|
          options[:install] = l
        end
17b522613   William Daniau   Ajout d'un script...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        opts.on("-l",
                "--list",
                "Liste les configurations disponibles") do |l|
          options[:list] = l
        end
  
        opts.on("-a",
                "--apply CONF",
                "applique la config CONF") do |conf|
          options[:conf] = conf
        end
  
        opts.separator ""
        opts.separator "       Options standard:"
        opts.on_tail("-h", "--help", "Affiche ce message") do
          puts opts
          puts ""
          exit
        end
        opts.on_tail("-v", "--version", "Affiche la version") do
          puts "Version : " + $version
          puts "Auteur : " + $auteur
          puts "Date : " + $date
          puts ""
          exit
        end
      end
  
      opt_parser.parse!(args)
      options
    end
  end
2623019d5   William Daniau   Amélioration de w...
67
  # Get the config list
17b522613   William Daniau   Ajout d'un script...
68
  def listConfigs
2623019d5   William Daniau   Amélioration de w...
69
    Dir.chdir($zimHtmlTemplateDir)
17b522613   William Daniau   Ajout d'un script...
70
71
72
73
74
75
76
77
78
79
80
    a = `ls`.split(/
  /)
    names = []
    r = /^common-(.+)$/
    a.each { |f|
      if f =~ r
        names.push($1)
      end
    }
    names
  end
2623019d5   William Daniau   Amélioration de w...
81
  # Apply a config
17b522613   William Daniau   Ajout d'un script...
82
  def applyConfig(name)
2623019d5   William Daniau   Amélioration de w...
83
    Dir.chdir($zimHtmlTemplateDir)
17b522613   William Daniau   Ajout d'un script...
84
85
86
87
88
89
90
91
92
93
    list = ["Wize", "Wize_with_index", "Wize_with_index_sod"]
    list.each { |l|
      com = "rm -f #{l}/common-spec"
      system(com)
      com = "ln -s ../common-#{name} #{l}/common-spec"
      system(com)
    }
    system("rm -f print-common")
    system("ln -s common-#{name} print-common")
  end
2623019d5   William Daniau   Amélioration de w...
94
  # Is the name a valid config?
17b522613   William Daniau   Ajout d'un script...
95
96
97
  def configInList?(name, names)
    names.include? name
  end
2623019d5   William Daniau   Amélioration de w...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
  # Re-create Wize_Print.html from template
  # and initialize link into zim config directory
  def installWize
    Dir.chdir($zimHtmlTemplateDir)
    begin
      printTemplate = File.new("Wize_Print_Template.html", "r")
    rescue
      puts "cannot open print file for reading"
      exit
    end
    tabfile = []
    stor = '%%HOME%%'
    rby = ENV['HOME']
    printTemplate.readlines.each { |line|
      line.gsub!(stor,rby)
      tabfile.push(line)
    }
    printTemplate.close
    begin
      printFile = File.new("Wize_Print.html", "w")
    rescue
      puts "cannot open print file for writing"
      exit
    end
    tabfile.each { |line|
      printFile.write line
    }
    printFile.close
  
    Dir.chdir($zimConfigDir)
    system("rm -f style.conf")
    system("ln -s #{$zimShareDir}/style.conf style.conf")
  end
  
  # Create style-name.conf according to common-name/zim-variables.css
  def createStyleFile(name)
    lfTags = ["--main-header-color",
              "--main-menu-u-decoration",
              "--main-menu-u-background-color",
              "--main-strike-color",
              "--main-tt-color",
              "--main-pre-color",
              "--main-link-color"]
  
    # First we load the css file and parse it
    Dir.chdir($zimHtmlTemplateDir)
    begin
      nameCSS = File.new("common-#{name}/zim-variables.css", "r")
    rescue
      puts "cannot open common-#{name}/zim-variables.css for reading"
      exit
    end
    sCSS = nameCSS.read
    regComment = /\/\*.*?\*\//m
    sCSS.gsub!(regComment, '')
    sTags = {}
    lfTags.each { |tag|
      var = Regexp.escape(tag)
      reg = /#{var}\s*:\s*(\S+)\s*;/
      sCSS =~ reg
      sTags[tag] = $1
      # On traite le cas particulier du u-decoration
      if tag == "--main-menu-u-decoration"
        sTags[tag] = sTags[tag] == 'underline' ? 'PANGO_UNDERLINE_SINGLE' : ''
      end
    }
  
    # know read the style template
    Dir.chdir($zimShareDir)
    begin
      styleTemplate = File.new("style-template.conf", "r")
    rescue
      puts "cannot open style-template.conf for reading"
      exit
    end
    styleTemplateString = styleTemplate.read
    lfTags.each { |tag|
      styleTemplateString.gsub!("%%#{tag}%%", sTags[tag])
    }
    # Now write the file
    begin
      styleOutput = File.new("style-#{name}.conf", "w")
    rescue
      puts "cannot open style-#{name}.conf for writing"
      exit
    end
    styleOutput.write styleTemplateString
    styleOutput.close
    # Now create the symbolic link
    system("rm style.conf")
    system("ln -s style-#{name}.conf style.conf")
  end
17b522613   William Daniau   Ajout d'un script...
190
191
192
193
194
195
  # Parse arguments using the class and get results in a hash
  options = ParseOptions.parse(ARGV)
  if options.length == 0
    puts "Utiliser -h pour l'aide"
    exit
  end
17b522613   William Daniau   Ajout d'un script...
196
197
  # get existing configs
  configs = listConfigs
2623019d5   William Daniau   Amélioration de w...
198
199
200
201
202
  # Installation
  if options[:install]
    installPrintFile
    exit
  end
17b522613   William Daniau   Ajout d'un script...
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  # List
  if options[:list]
    configs.each { |c|
      puts c
    }
    exit
  end
  
  # Apply
  if options[:conf]
    conf = options[:conf]
    # is it a valid config?
    if !configInList?(conf, configs)
      puts "#{conf} n'est pas une configuration valide"
      exit
    end
    # we have a valid config
    applyConfig(conf)
2623019d5   William Daniau   Amélioration de w...
221
222
223
224
225
    createStyleFile(conf)
    #
    puts "All done!"
    puts "Les modifications de templates html sont appliquée directement,"
    puts "mais il faut redémarrer zim pour que les modifications y aapparaissent"
17b522613   William Daniau   Ajout d'un script...
226
  end