Module: Dyndoc::HtmlServers

Defined in:
lib/dyndoc-html-servers.rb

Constant Summary collapse

@@unwatch =

Deal with unwatched files!

[]
@@unwatch_yml =
File.join(Dyndoc.home,"etc","unwatch.yml")
@@cfg =
nil

Class Method Summary collapse

Class Method Details

.cfgObject



219
220
221
222
223
224
225
226
227
# File 'lib/dyndoc-html-servers.rb', line 219

def HtmlServers.cfg
  unless @@cfg
    dyndoc_home = Dyndoc.home
    cfg_yml = File.join(dyndoc_home,"etc","dyn-html.yml")
    @@cfg=(File.exist? cfg_yml) ? ::YAML::load_file(cfg_yml) : {}
    @@cfg["dyndoc_home"]=dyndoc_home
  end
  @@cfg
end

.dyn_html_filewatcher(cfg = {}) ⇒ Object

cfg



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/dyndoc-html-servers.rb', line 248

def HtmlServers.dyn_html_filewatcher(cfg={}) #cfg
  require 'dyndoc-convert'
  require 'dyndoc-edit'
  require 'filewatcher'
  require 'dyndoc-linter'

  $VERBOSE = nil
  options={first: true}
  ## To put inside yaml config file!
  root ||= cfg["root"] || HtmlServers.cfg["root"] || File.join(ENV["HOME"],"RCqls","RodaServer")
  dyn_root = cfg["dyn_root"] || HtmlServers.cfg["dyn_root"] || File.join(root ,"edit")
  public_root = cfg["public_root"] || HtmlServers.cfg["public_root"] || File.join(root ,"public")
  pages_root = File.join(public_root ,"pages")
  current_email = cfg["email"] || HtmlServers.cfg["email"] || "[email protected]" #default email user can be overriden by -u option
  host=(cfg["html-srv-host"] || HtmlServers.cfg["html-srv-host"] || "http://localhost").to_s
  port=(cfg["html-srv-port"] || HtmlServers.cfg["html-srv-port"] || "9294").to_s
  base_url= host+":"+port

  opts = {
    dyn_root: dyn_root,
    html_root: pages_root,
    user: nil #current_email
  }

  puts "watching "+ dyn_root
  old_html_file=""
  ::Filewatcher.new(dyn_root).watch() do |filename, event|
    ##p [:filename,filename,event]
    if [:changed,:updated,:new].include? event and File.extname(filename) == ".dyn"
      ##p [:filename_event,event,filename]
      if (lint_error=Dyndoc::Linter.check_file(filename)).empty?
        ## do not process preload, postload, lib and layout files
        unless filename =~ /(?:_pre|_post|_lib|_layout)\.dyn$/
          ## find dyn_file (relative path from root)
          dyn_file="/"+Pathname(filename).relative_path_from(Pathname(dyn_root)).to_s
          unless HtmlServers.unwatched?(dyn_file[1..-1]).empty?
            if RUBY_PLATFORM =~ /darwin/
              `osascript -e 'display notification "Warning: #{dyn_file} unwatched by #{HtmlServers.unwatched?(dyn_file[1..-1])}!" with title "dyn-ctl unwatch"'`
            end
          else
            opts_doc=Dyndoc::FileWatcher.get_dyn_html_info(filename,dyn_file,opts[:user])
            opts.merge! opts_doc
            ##p [:html_files,html_files]

            html_file=opts[:html_files][opts[:current_doc_tag]] # No more default # || html_files[""]
            ##p [:opts,opts,:current_doc_tag,opts[:current_doc_tag]]
            Dyndoc.cli_convert_from_file(dyn_file[1..-1],html_file, opts)
            ## fix html_file for _rmd, _adoc and _ttm
            if html_file =~ /^(.*)_(rmd|adoc|ttm)\.html$/
              html_file = $1+".html"
            end
            puts dyn_file[1..-1]+" processed => "+html_file+" created!"
            options[:first] = html_file != old_html_file
            if html_file != old_html_file
              old_html_file = html_file
              url=File.join(base_url,html_file)
              Dyndoc::Browser.load(url)
            else
              Dyndoc::Browser.reload
            end
          end
        end
      else
        if RUBY_PLATFORM =~ /darwin/
          ##p lint_error
          cmd_to_display='display notification "' +lint_error.map{|e| e[0].to_s+") "+e[1].to_s}.join('" & "')+ '" with title "Lint Error:'+filename+'"'
          p [:cmd_display,cmd_to_display]
          `osascript -e '#{cmd_to_display}'`
        else
          puts "Lint Error: "+filename+" with "+lint_error.inspect
        end
      end
    end
  end

end

.dyn_http_server(host = nil, port = nil) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/dyndoc-html-servers.rb', line 229

def HtmlServers.dyn_http_server(host=nil,port=nil)
  require 'thin'
  dyn_html_srv_ru=File.expand_path("../../share/html-srv/dyn-html-srv.ru",__FILE__)
  arg=["-R",dyn_html_srv_ru]
  if HtmlServers.cfg["html-srv-port"]
    arg += ["-p",HtmlServers.cfg["html-srv-port"].to_s]
  else
    arg += ["-p",port || "9294"]
  end
  if host
    arg += ["-a",host]
  elsif HtmlServers.cfg["html-srv-host"]
    arg += ["-a",HtmlServers.cfg["html-srv-host"].to_s]
  end
  arg << "start"
  ##p [:arg,arg]
  Thin::Runner.new(arg).run!
end

.unwatch_add(path) ⇒ Object



199
200
201
202
203
# File 'lib/dyndoc-html-servers.rb', line 199

def HtmlServers.unwatch_add(path)
  return unless path
  HtmlServers.unwatch_cfg.unshift path
  HtmlServers.unwatch_save
end

.unwatch_cfgObject



181
182
183
184
185
186
# File 'lib/dyndoc-html-servers.rb', line 181

def HtmlServers.unwatch_cfg
  if @@unwatch.empty?
    @@unwatch=(File.exist? @@unwatch_yml) ? ::YAML::load_file(@@unwatch_yml) : []
  end
  @@unwatch
end

.unwatch_lsObject



188
189
190
191
# File 'lib/dyndoc-html-servers.rb', line 188

def HtmlServers.unwatch_ls
  res=HtmlServers.unwatch_cfg.empty? ? "Empty folders list" : (HtmlServers.unwatch_cfg.each_with_index.map {|e,i| (i+1).to_s+") "+e}.join("\n") + "\n")
  puts res
end

.unwatch_rm(path) ⇒ Object



205
206
207
208
209
210
# File 'lib/dyndoc-html-servers.rb', line 205

def HtmlServers.unwatch_rm(path)
  return unless path
  HtmlServers.unwatch_cfg
  @@unwatch -= [path]
  HtmlServers.unwatch_save
end

.unwatch_saveObject



193
194
195
196
197
# File 'lib/dyndoc-html-servers.rb', line 193

def HtmlServers.unwatch_save
  File.open(@@unwatch_yml,"w") do |f|
    f << @@unwatch.to_yaml
  end
end

.unwatched?(file) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
215
# File 'lib/dyndoc-html-servers.rb', line 212

def HtmlServers.unwatched?(file)
  return "" if HtmlServers.unwatch_cfg.empty?
  return HtmlServers.unwatch_cfg.map{|e| file[0...e.length] == e ? e : ""}[0] || ""
end