Class: DWT

Inherits:
Object
  • Object
show all
Defined in:
lib/dwt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filesObject

Returns the value of attribute files.



2
3
4
# File 'lib/dwt.rb', line 2

def files
  @files
end

#templateObject

Returns the value of attribute template.



2
3
4
# File 'lib/dwt.rb', line 2

def template
  @template
end

Instance Method Details

#applyObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dwt.rb', line 4

def apply
  files = [@files] if @files.is_a? String

  tpl_end_editable_str = "<!-- TemplateEndEditable -->"
  tpl = File.open(@template).read
  tpl_copy = []

  begin
    tpl_src = @template.index('Templates/').nil? ? '/Templates/' + @template.gsub(/^.*\/([^\/]*)/,'\1') : @template.gsub(/^.*Templates\/(.*)$/, '/Templates/\1')
    tpl_end = tpl.index("<!-- TemplateBeginEditable")
    tpl_start = tpl.index(tpl_end_editable_str)
    tpl_start = tpl_start + tpl_end_editable_str.length if tpl_start
    copy = (tpl_end ? tpl[0,tpl_end] : tpl)
    copy.gsub!(/<html([^>]*)>/, '<html\1><!-- InstanceBegin template="'+tpl_src+'" codeOutsideHTMLIsLocked="false" -->')
    copy.gsub!(/<\/html>/,'<!-- InstanceEnd --></html>')
    html_index = copy.index('<html')
    tpl_copy << (html_index ? copy[html_index, copy.length - html_index] : copy)
    tpl = tpl_start ? tpl[tpl_start,tpl.length] : ''
  end while tpl.length > 0

  tar_end_editable_str = "<!-- InstanceEndEditable -->"
  filename = files[0]
  tar = File.open(filename).read
  tar_copy = []

  html_index = tar.index('<html')
  tar_copy << tar[0, html_index]

  begin
    tar_start = tar.index("<!-- InstanceBeginEditable")
    tar_end = tar.index(tar_end_editable_str)
    tar_end = tar_end + tar_end_editable_str.length if tar_end
    tar_copy << tar[tar_start,tar_end-tar_start] if tar_start
    tar = tar_end ? tar[tar_end,tar.length-tar_end] : ''
  end while tar.length > 0

  new = ''
  (0..(tar_copy.length)).to_a.each do |i|
    next if tar_copy[i].nil?
    new = new += tar_copy[i]
    copy = tpl_copy[i]
    copy = apply_link_update(copy,'link','href',filename)
    copy = apply_link_update(copy,'script','src',filename)
    copy = apply_link_update(copy,'form','action',filename)
    copy = apply_link_update(copy,'a','href',filename)
    copy = apply_link_update(copy,'img','src',filename)
    new = new += copy
  end

  return new
end