Skip to content

Instantly share code, notes, and snippets.

@sintaxi
Created January 23, 2009 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sintaxi/51140 to your computer and use it in GitHub Desktop.
Save sintaxi/51140 to your computer and use it in GitHub Desktop.
PhoneGap now has a Rakefile to make you life easier. It will allow
you to keep your css and javascript files separated and will scrape
your index.html file and import all the included stylesheets and
javascript files and put them in your header. This is Ideal for
working with dashcode (or for just keeping your directory structure
organized).
Dependencies
- Ruby (ships with leopard)
- Hpricot (install by running 'sudo gem install hpricot' in your terminal)
How it works
1. go to the root of your PhoneGap dir in your terminal 'cd path/to/your/phonegap'
2. see list of tasks 'rake -T'
3. run 'rake iphone:import' (or 'rake iphone' for short)
you will now see that your index.html file has all your javascript
and css in the header. And a backup of your index.html (before the
rake task) file was created. Run 'rake iphone:revert') to revert back.
* below is an example of what your index.html file should look before
you run the rake task.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>a typical dashcode html file</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="minimum-scale=1.0, width=device-width, maximum-scale=1.1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="YES">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="apple-touch-icon" href="Images/WebClipIcon.png">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="Parts/Transitions.css">
<script type="text/javascript" src="Parts/setup.js" charset="utf-8"></script>
<script type="text/javascript" src="main.js" charset="utf-8"></script>
<script type="text/javascript" src="Parts/Header.js" charset="utf-8"></script>
<script type="text/javascript" src="Parts/utilities.js" charset="utf-8"></script>
<script type="text/javascript" src="Parts/Transitions.js" charset="utf-8"></script>
<script type="text/javascript" src="Parts/StackLayout.js" charset="utf-8"></script>
<script type="text/javascript" src="Parts/Browser.js" charset="utf-8"></script>
<script type="text/javascript" src="Parts/Text.js" charset="utf-8"></script>
<script type="text/javascript" src="Parts/PushButton.js" charset="utf-8"></script>
<script type="text/javascript" src="Parts/ButtonHandler.js" charset="utf-8"></script>
<script type="text/javascript" src="gap.js" charset="utf-8"></script>
</head>
<body onload="load();">
<div id="browser">
<div id="header">
<div id="back_button" class="back_button_template dashcode-header-backbutton"></div>
</div>
<div id="stackLayout">
<div id="listLevel">
</div>
<div id="detailLevel">
</div>
</div>
</div>
</body>
</html>
require 'rubygems'
require "hpricot"
PHONEGAP_WEB_PATH = "iphone/www"
namespace :iphone do
task :default => [:import] do
end
desc 'reverts file back to original form'
task :revert do
back = open("#{PHONEGAP_WEB_PATH}/index.back.html").read
File.open("#{PHONEGAP_WEB_PATH}/index.html", "w") {|f| f.puts back}
end
desc 'backups index.html file'
task :backup do
puts "creating backup file (if needed)"
index = open("#{PHONEGAP_WEB_PATH}/index.html").read
File.open("#{PHONEGAP_WEB_PATH}/index.back.html", "w") {|f| f.write index} unless index.include?("RAKED::")
end
desc 'puts external javascript and css content into index page'
task :import => :backup do
#open backup file
doc = Hpricot(open("#{PHONEGAP_WEB_PATH}/index.back.html"))
#find and import stylesheets
(doc/"head/link[@rel='stylesheet']").remove.each do |elem|
path = file elem.attributes['href']
puts " importing stylesheet: #{path}"
css_content = open("#{PHONEGAP_WEB_PATH}/#{path}").read
elem.after("<style type=\"text/css\" media=\"screen\">\n/*RAKED::css file::#{path} */\n" + css_content + "\n</style>\n")
end
#find and import javascripts
doc.search("head/script").remove.each do |elem|
path = file elem.attributes['src']
puts " importing javascript: #{path}"
js_content = open("#{PHONEGAP_WEB_PATH}/#{path}").read
elem.after( "<script type=\"text/javascript\" charset=\"utf-8\">\n//RAKED::javascript file::#{path}\n" + js_content + "\n</script>\n\n")
end
#write and save as index.html
open("#{PHONEGAP_WEB_PATH}/index.html", "w") { |f| f.write doc}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment