lib/dyndoc-html-servers.rb in dyndoc-ruby-1.0.1 vs lib/dyndoc-html-servers.rb in dyndoc-ruby-1.0.2

- old
+ new

@@ -1,10 +1,187 @@ require 'dyndoc/init/home' require 'pathname' require 'yaml' + module Dyndoc + module Browser + @@browser_cfg_file= File.join(Dyndoc.home,"etc","browser.yml") + + @@cfg=nil + + def Browser.cfg + unless @@cfg + @@cfg=(File.exist? @@browser_cfg_file) ? ::YAML::load_file(@@browser_cfg_file) : {} + end + @@cfg + end + + def Browser.name + mode=(Browser.cfg["browser"] || :safari).to_sym + case mode + when :chrome + "Google Chrome" + when :canary + "Google Chrome Canary" + when :firefox + "Firefox" + when :firefox_nightly + "FirefoxNightly" + when :firefox_developer + "FirefoxDeveloperEdition" + when :safari + "Safari" + end + end + + @@browser_reload_osa= File.join(Dyndoc.home,"etc","browser_reload.osa") + def Browser.set_browser_reload + activate=Browser.cfg["activate"] || false + mode=(Browser.cfg["browser"] || :safari).to_sym + code=case mode + when :chrome + %Q{ + tell application "Google Chrome" + #{activate ? 'activate' : ''} + "chrome" + set winref to a reference to (first window whose title does not start with "Developer Tools - ") + set winref's index to 1 + reload active tab of winref + end tell + } + + when :canary + %Q{ + tell application "Google Chrome Canary" + #{activate ? 'activate' : ''} + "chrome canary" + set winref to a reference to (first window whose title does not start with "Developer Tools - ") + set winref's index to 1 + reload active tab of winref + end tell + } + + when :firefox + %Q{ + set a to path to frontmost application as text + tell application "Firefox" + activate + tell application "System Events" to keystroke "r" using command down + end tell + #{activate ? '' : 'delay 0.2\nactivate application a'} + } + + when :firefox_nightly + %Q{ + set a to path to frontmost application as text + tell application "FirefoxNightly" + activate + tell application "System Events" to keystroke "r" using command down + end tell + #{activate ? '' : 'delay 0.2\nactivate application a'} + } + + when :firefox_developer + %Q{ + set a to path to frontmost application as text + tell application "FirefoxDeveloperEdition" + activate + tell application "System Events" to keystroke "r" using command down + end tell + #{activate ? '' : 'delay 0.2\nactivate application a'} + } + + when :safari + %Q{ + tell application "Safari" + #{activate ? 'activate' : ''} + tell its first document + set its URL to (get its URL) + end tell + end tell + } + end + File.open(@@browser_reload_osa,"w") do |f| + f << code.strip + end + end + + def Browser.reload + if RUBY_PLATFORM =~ /darwin/ + Browser.set_browser_reload unless File.exists? @@browser_reload_osa + `osascript #{@@browser_reload_osa}` + end + end + + def Browser.load(url) + mode=(Browser.cfg["browser"] || :safari).to_sym + cmd_to_open='tell application "'+Browser.name+'" to set URL of current tab of front window to "'+url+'"' + `osascript -e '#{cmd_to_open}'` + end + end +end +=begin +Commands = { + darwin: { + firefox: MacFirefoxCmd, + firefoxNightly: MacFirefoxNightlyCmd, + firefoxDeveloperEdition: MacFirefoxDeveloperEditionCmd, + chrome: MacChromeCmd, + chromeCanary: MacChromeCanaryCmd, + safari: MacSafariCmd + }, + linux: { + firefox: [ + 'search', + '--sync', + '--onlyvisible', + '--class', + 'firefox', + 'key', + 'F5', + 'windowactivate' + ], + chrome: [ + 'search', + '--sync', + '--onlyvisible', + '--class', + 'chrome', + 'key', + 'F5', + 'windowactivate' + ] + } +} + +RunMacCmd = (cmd) -> + new BufferedProcess({ + command: 'osascript' + args: ['-e', BrowserCmd] + stderr: (data) -> + OpenPanel(type: 'alert', message: data.toString()) + }) + +RunLinuxCmd = (BrowserArgs) -> + new BufferedProcess({ + command: 'xdotool' + args: BrowserArgs + stderr: (data) -> + OpenPanel(type: 'alert', message: data.toString()) + }) + +RunCmd = (browser) -> + if OS.platform() == 'darwin' + RunMacCmd(Commands['darwin'][browser]) + else if OS.platform() == 'linux' and browser isnt 'safari' + RunLinuxCmd(Commands['linux'][browser]) + else + OpenPanel(type: 'alert', message: 'Unsupported platform') +=end + +module Dyndoc module HtmlServers @@cfg=nil def HtmlServers.cfg @@ -86,19 +263,21 @@ if RUBY_PLATFORM =~ /darwin/ 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) - cmd_to_open='tell application "Safari" to set URL of current tab of front window to "'+url+'"' - `osascript -e '#{cmd_to_open}'` + # cmd_to_open='tell application "Safari" to set URL of current tab of front window to "'+url+'"' + # `osascript -e '#{cmd_to_open}'` + Dyndoc::Browser.load(url) else -%x{osascript<<ENDREFRESH -tell app "Safari" to activate -tell application "System Events" - keystroke "r" using {command down} -end tell -ENDREFRESH -} + Dyndoc::Browser.reload +# %x{osascript<<ENDREFRESH +# tell app "Safari" to activate +# tell application "System Events" +# keystroke "r" using {command down} +# end tell +# ENDREFRESH +# } end end end else if RUBY_PLATFORM =~ /darwin/