flurin-html_mockup 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/html_mockup/cli.rb +54 -2
  2. metadata +2 -2
@@ -94,10 +94,10 @@ module HtmlMockup
94
94
 
95
95
  desc "convert [directory]","Inject all partials, into all HTML files within directory"
96
96
  method_options :partial_path => :optional, # Defaults to [directory]/../partials
97
- :filter => :optional # What files should be converted defaults to *.html
97
+ :filter => :optional # What files should be converted defaults to **/*.html
98
98
  def convert(path=".")
99
99
  path,partial_path = template_paths(path,options["partial_path"])
100
- filter = options["filter"] || "*.html"
100
+ filter = options["filter"] || "**/*.html"
101
101
  puts "Converting #{filter} in #{path}"
102
102
  puts " Taking partials from #{partial_path} (#{HtmlMockup::Template.partial_files(partial_path).size} found)"
103
103
 
@@ -112,6 +112,43 @@ module HtmlMockup
112
112
 
113
113
  end
114
114
 
115
+ desc "extract [source_path] [target_path]", "Extract a fully relative html mockup into target_path. It will expand all absolute href's, src's and action's into relative links if they are absolute"
116
+ method_options :partial_path => :optional, # Defaults to [directory]/../partials
117
+ :filter => :optional # What files should be converted defaults to **/*.html
118
+ def extract(source_path=".",target_path="../out")
119
+ require 'hpricot'
120
+ source_path,target_path = Pathname.new(source_path),Pathname.new(target_path)
121
+ source_path,partial_path = template_paths(source_path,options["partial_path"])
122
+ filter = options["filter"] || "**/*.html"
123
+ raise "Target #{target_path} already exists, please choose a new directory to extract into" if target_path.exist?
124
+
125
+ mkdir_p(target_path)
126
+ target_path = target_path.realpath
127
+
128
+ # Copy source to target first, we'll overwrite the templates later on.
129
+ cp_r(source_path.children,target_path)
130
+
131
+ Dir.chdir(source_path) do
132
+ Dir.glob(filter).each do |file_name|
133
+ source = HtmlMockup::Template.open(file_name, :partial_path => partial_path).render
134
+ cur_dir = Pathname.new(file_name).dirname
135
+ up_to_root = File.join([".."] * (file_name.split("/").size - 1))
136
+ doc = Hpricot(source)
137
+ %w{src href action}.each do |attribute|
138
+ (doc/"*[@#{attribute}]").each do |tag|
139
+ next unless tag[attribute] =~ /\A\//
140
+ if true_file = resolve_path(cur_dir + up_to_root + tag[attribute].sub(/\A\//,""))
141
+ tag[attribute] = true_file.relative_path_from(cur_dir)
142
+ else
143
+ puts "Could not resolve link #{tag[attribute]} in #{file_name}"
144
+ end
145
+ end
146
+ end
147
+
148
+ File.open(target_path + file_name,"w"){|f| f.write(doc.to_original_html) }
149
+ end
150
+ end
151
+ end
115
152
 
116
153
  protected
117
154
 
@@ -130,5 +167,20 @@ module HtmlMockup
130
167
  end
131
168
  validator.valid
132
169
  end
170
+
171
+ def resolve_path(path)
172
+ path = Pathname.new(path) unless path.kind_of?(Pathname)
173
+ # Append index.html/index.htm/index.rhtml if it's a diretory
174
+ if path.directory?
175
+ search_files = %w{.html .htm}.map!{|p| path + "index#{p}" }
176
+ # If it ends with a slash or does not contain a . and it's not a directory
177
+ # try to add .html/.htm/.rhtml to see if that exists.
178
+ elsif (path =~ /\/$/) || (path =~ /^[^.]+$/)
179
+ search_files = [path.to_s + ".html", path.to_s + ".htm"].map!{|p| Pathname.new(p) }
180
+ else
181
+ search_files = [path]
182
+ end
183
+ search_files.find{|p| p.exist? }
184
+ end
133
185
  end
134
186
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flurin-html_mockup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flurin Egger
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-20 00:00:00 -07:00
12
+ date: 2009-04-27 00:00:00 -07:00
13
13
  default_executable: mockup
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency