Module: Ueditor::Utils

Defined in:
lib/ueditor/utils.rb

Class Method Summary collapse

Class Method Details

.applay_options(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ueditor/utils.rb', line 28

def applay_options(options)
  str = []
  
  options.each do |key, value|
    item = case value
      when String then
        value.split(//).first == '^' ? value.slice(1..-1) : "'#{value}'"
      when Hash then 
        "{ #{applay_options(value)} }"
      when Array then 
        arr = value.collect { |v| "'#{v}'" }
        "[ #{arr.join(',')} ]"
      else value
    end
    
    str << %Q{"#{key}": #{item}}
  end
  
  str.sort.join(',')
end

.escape_single_quotes(str) ⇒ Object



5
6
7
# File 'lib/ueditor/utils.rb', line 5

def escape_single_quotes(str)
  str.gsub('\\','\0\0').gsub('</','<\/').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
end

.js_replace(dom_id, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ueditor/utils.rb', line 16

def js_replace(dom_id, options = {})
  js_options = applay_options(options)
  editor_id = "#{Ueditor.config.dom_prefix}#{dom_id}"
  js = ""
  if js_options.blank?
    js << "var #{editor_id} = new baidu.editor.ui.Editor();"
  else
    js << "var #{editor_id}= new baidu.editor.ui.Editor({ #{js_options} });"
  end
  js << "#{editor_id}.render('#{dom_id}');"
end

.parameterize_filename(filename) ⇒ Object



9
10
11
12
13
14
# File 'lib/ueditor/utils.rb', line 9

def parameterize_filename(filename)
  extension = File.extname(filename)
  basename = filename.gsub(/#{extension}$/, "")
  
  [basename.parameterize('_'), extension].join.downcase
end