Class: DashcodeConverter::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/dashcode-converter/project.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_bundle, output_folder) ⇒ Project

Returns a new instance of Project.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dashcode-converter/project.rb', line 10

def initialize(project_bundle, output_folder)
  @project_bundle= File.expand_path(project_bundle)
  @name= File.basename(@project_bundle, ".*")
  @namespace= @name
  @output_folder= File.expand_path(File.join(output_folder, "#{@name}.#{BUNDLE_EXTENSION}"))
  @parts_spec_path= File.join(@project_bundle, "project", "safari", "Parts", "setup.js")
  @datasources_spec_path= File.join(@project_bundle, "project", "Parts", "datasources.js")
  @css_path= File.join(@project_bundle, "project", "safari", "main.css")
  @markup_path= File.join(@project_bundle, "project", "index.html")
  @images_folder= File.join(@output_folder, "images")
  FileUtils.mkdir_p(@output_folder)
end

Instance Attribute Details

#namespaceObject

Returns the value of attribute namespace.



8
9
10
# File 'lib/dashcode-converter/project.rb', line 8

def namespace
  @namespace
end

Instance Method Details

#controllerObject



52
53
54
55
# File 'lib/dashcode-converter/project.rb', line 52

def controller
  return @controller if @controller
  @controller= Controller.new(@name, @namespace)
end

#convertObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dashcode-converter/project.rb', line 120

def convert
  fixup_html
  
  FileUtils.mkdir_p(@output_folder)
  FileUtils.mkdir_p(@images_folder)
  
  Dir.chdir(@output_folder) do
    File.open("#{@name.capitalize}Controller.js", "w") { |controller_file|
      controller_file << controller.declaration
    }
    File.open("#{@name}.js", "w") { |nib_file|
      nib_file << nib.declaration
    }
    File.open("#{@name}.css", "w") { |css_file|
      css_file << css
    }
    File.open("#{@name}.html", "w") { |html_file|
      html_file << doc.css("body > *:first-child")[0].serialize
    }
  end
end

#cssObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dashcode-converter/project.rb', line 66

def css
  text= File.read(@css_path)
  dirname= File.dirname(@css_path)
  text.gsub!(CSS_IMAGE_URL_REGEX) { |match|
    image_file= File.join(dirname, $1)

    if (!File.exists?(image_file))
      match
    else
      new_image_file= File.join(@images_folder, File.basename(image_file))
      FileUtils.mkdir_p(File.dirname(new_image_file))
      FileUtils.cp image_file, new_image_file
      "url(\"#{relative_path(new_image_file)}\")"
    end
  }
  text
end

#docObject



45
46
47
48
49
50
# File 'lib/dashcode-converter/project.rb', line 45

def doc
  return @doc if @doc
  
  html= File.read(@markup_path)
  @doc= Nokogiri::HTML(html)
end

#fixup_htmlObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dashcode-converter/project.rb', line 84

def fixup_html
  base= doc.css("base")
  base= base ? base.attribute("href") : nil
  
  content= doc.css("body > *:first-child")[0]
  content.traverse { |node|
    next unless node.attributes
    node.attributes.each { |attr, value|
      if 0==attr.index("apple-")
        node.remove_attribute(attr)
      end
    }
  }
  
  dirname= File.dirname(@markup_path)
  dirname= File.join(dirname, base) if base
  
  content.css("[src]").each { |node|
    image_file= File.join(dirname, node.attribute('src'))
    new_image_file= File.join(@images_folder, File.basename(image_file))
    FileUtils.mkdir_p(File.dirname(new_image_file))
    FileUtils.cp image_file, new_image_file
    node["src"]= relative_path(new_image_file)
  }
  
  nib.views.each { |view|
    # Use a copy of the view's items, because the iterator isn't stable if
    # items are removed while iterating.
    items= view.items.clone
    items.each { |item|
      html= doc.css(item.name)[0]
      item.fixup_html(html)
    }
  }
end

#nibObject



57
58
59
60
61
62
63
64
# File 'lib/dashcode-converter/project.rb', line 57

def nib
  return @nib if @nib
  
  @nib= Nib::Nib.new(@name, controller)
  @nib.add_view_from_path(@parts_spec_path)
  @nib.add_datasources_from_path(@datasources_spec_path)
  @nib
end

#path_relative_to_folder(path, folder) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dashcode-converter/project.rb', line 23

def path_relative_to_folder(path, folder)
  path= File.expand_path(path)
  outputFolder= File.expand_path(folder).to_s
  
  # Remove leading slash and split into parts
  file_parts= path.slice(1..-1).split('/');
  output_parts= outputFolder.slice(1..-1).split('/');

  common_prefix_length= 0

  file_parts.each_index { |i|
    common_prefix_length= i
    break if file_parts[i]!=output_parts[i]
  }

  return '../'*(output_parts.length-common_prefix_length) + file_parts[common_prefix_length..-1].join('/')
end

#relative_path(path) ⇒ Object



41
42
43
# File 'lib/dashcode-converter/project.rb', line 41

def relative_path(path)
  path_relative_to_folder(path, @output_folder)
end