deplot 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/deplot +522 -0
  2. metadata +65 -0
@@ -0,0 +1,522 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'thor'
4
+ require 'fileutils'
5
+ require 'tilt'
6
+
7
+ # Deplot DSL
8
+
9
+ $assets_dir = Dir.pwd+"/assets"
10
+ $content_dir = Dir.pwd+"/content"
11
+ $root_dir = Dir.pwd
12
+ $template_arguments = {}
13
+ $variables = {}
14
+
15
+ $tasks = []
16
+ $render_type = ""
17
+ $current_source = ""
18
+ $layout = ""
19
+ $options = {}
20
+
21
+ # dsl methods for page specification
22
+ def assets_dir dir
23
+ $assets_dir = $root_dir + "/" + dir.gsub(/^\//, "").gsub(/\/$/, "")
24
+ end
25
+ def content_dir dir
26
+ $content_dir = $root_dir + "/" + dir.gsub(/^\//, "").gsub(/\/$/, "")
27
+ end
28
+ def variables vars
29
+ $variables.merge! vars
30
+ end
31
+ def layout file
32
+ $layout = file
33
+ end
34
+ def render file
35
+ $render_type = "render-single"
36
+ $current_source = file
37
+ end
38
+ def render_each base = "", paths = []
39
+ $render_type = "render-all"
40
+ unless base.is_a? Array
41
+ $current_source = [base, paths]
42
+ else
43
+ $current_source = ["./", base]
44
+ end
45
+ end
46
+ def render_all path
47
+ $render_type = "render-all"
48
+ $current_source = path
49
+ end
50
+
51
+ # base of route specification
52
+ def path path, &block
53
+ # reset
54
+ $render_type = ""
55
+ $current_source = ""
56
+ $layout = "layout.erb"
57
+ $options = {}
58
+ $variables = {}
59
+
60
+ # set
61
+ block.call
62
+
63
+ # remember
64
+ if $render_type == "render-all"
65
+ $render_type = (/\#/ =~ path) ? "render-all" : "render-all-one-file"
66
+ $tasks << [$render_type, path, $current_source, $layout, $options, $variables]
67
+ else
68
+ $tasks << [$render_type, path, $current_source, $layout, $options, $variables]
69
+ end
70
+ end
71
+
72
+ # set options
73
+ def reverse_order
74
+ $options[:reverse_order] = true
75
+ end
76
+ def extension ext
77
+ $options[:extension] = ext
78
+ end
79
+
80
+ # methods for partials
81
+ def partial name, options = {}
82
+ begin
83
+ source_tilt = Tilt.new($assets_dir+"/"+name)
84
+ rescue Exception => e
85
+ puts "- ERROR creating tilt instance (possibly missing source file for partial)."
86
+ return ""
87
+ end
88
+ if options.has_key? :layout
89
+ begin
90
+ layout_tilt = Tilt.new(options[:layout])
91
+ rescue Exception => e
92
+ puts "- ERROR creating tilt instance (possibly missing layout file for partial)."
93
+ return ""
94
+ end
95
+ return layout_tilt.render(Object.new, $template_arguments) do
96
+ source_tilt.render(Object.new, $template_arguments)
97
+ end
98
+ else
99
+ return source_tilt.render(Object.new, $template_arguments)
100
+ end
101
+ end
102
+
103
+ # publishing methods
104
+ def check_directories?
105
+ [$assets_dir, $content_dir].each do |dir|
106
+ unless File.exists?(dir) and File.directory?(dir)
107
+ return false
108
+ end
109
+ end
110
+ return true
111
+ end
112
+ def publish
113
+ puts "Publishing site..."
114
+ $tasks.each do |task|
115
+ action, path, source, $layout, options, variables = task
116
+ content = ""
117
+ $template_arguments = {:path => "", :source => "", :file_name => "", :depth => 0}
118
+ $template_arguments.merge! variables
119
+ options[:extension] = (options[:extension].nil?) ? "html" : options[:extension]
120
+ unless check_directories?
121
+ puts "- ERROR: content or assets directory does't exist. Exiting..."
122
+ return
123
+ end
124
+
125
+ if action == "render-single"
126
+
127
+ ## R E N D E R S I N G L E
128
+ # Destination
129
+ destination_dir = File.dirname path
130
+ destination_base = File.basename path
131
+ if /\/$/ =~ path
132
+ destination_dir = path.gsub /\/$/, ""
133
+ destination_base = "index.#{options[:extension]}"
134
+ end
135
+ $template_arguments.merge! :path => "#{destination_dir}/#{destination_base}"
136
+ $template_arguments.merge! :file_name => destination_base
137
+ $template_arguments.merge! :source => source
138
+ $template_arguments.merge! :depth => (destination_dir.count "/")
139
+
140
+ puts "Rendering path '#{destination_dir}/#{destination_base}'"
141
+ # make path
142
+ FileUtils.mkdir_p "."+destination_dir
143
+ unless File.directory? "."+destination_dir
144
+ puts " Path '"+path+"' could not be created. Skipping..."
145
+ next
146
+ end
147
+
148
+ # Read source
149
+ Dir.chdir $content_dir do
150
+ unless File.exist? source
151
+ puts " Source file could not be found. Skipping..."
152
+ next
153
+ end
154
+ content = File.read source
155
+ end
156
+ puts " Read source file."
157
+
158
+ # Create tilt instances
159
+ layout_tilt = nil
160
+ Dir.chdir $assets_dir do
161
+ begin
162
+ layout_tilt = Tilt.new($layout)
163
+ rescue Exception => e
164
+ puts "- ERROR creating tilt instance (possibly missing layout file)."
165
+ layout_tilt = nil
166
+ end
167
+ end
168
+ if layout_tilt.nil?
169
+ puts " Layout could not be found or rendered. Skipping..."
170
+ next
171
+ end
172
+ source_tilt = nil
173
+ Dir.chdir $content_dir do
174
+ begin
175
+ source_tilt = Tilt.new(source)
176
+ rescue Exception => e
177
+ puts "- ERROR creating tilt instance (possibly missing source file)."
178
+ source_tilt = nil
179
+ end
180
+ end
181
+ if source_tilt.nil?
182
+ puts " Source file could not be rendered. Skipping..."
183
+ next
184
+ end
185
+ puts " Created tilt instances."
186
+
187
+ # Render and write
188
+ Dir.chdir "."+destination_dir do
189
+ File.open destination_base, "w" do |file|
190
+ file.puts layout_tilt.render(Object.new, $template_arguments){
191
+ source_tilt.render(Object.new, $template_arguments)
192
+ }
193
+ end
194
+ end
195
+ puts " Wrote to file."
196
+ elsif action == "render-all"
197
+
198
+ ## R E N D E R A L L
199
+ # Destination
200
+ destination_dir = File.dirname path
201
+ destination_base = File.basename path
202
+ if /\/$/ =~ path
203
+ destination_dir = path.gsub /\/$/, ""
204
+ destination_base = "index.#{options[:extension]}"
205
+ end
206
+
207
+ puts "Rendering path/s '"+destination_dir+"/...'"
208
+ # make path
209
+ FileUtils.mkdir_p "."+destination_dir
210
+ unless File.directory? "."+destination_dir
211
+ next
212
+ end
213
+
214
+ # Read source
215
+ Dir.chdir $content_dir do
216
+ paths = []
217
+ if source.is_a? Array
218
+ source, paths = source
219
+ end
220
+ source.gsub!(/\/$/, "")
221
+ Dir.chdir source do
222
+ # Create layout tilt instance
223
+ layout_tilt = nil
224
+ Dir.chdir $assets_dir do
225
+ begin
226
+ layout_tilt = Tilt.new($layout)
227
+ rescue Exception => e
228
+ puts "- ERROR creating tilt instance (possibly missing layout file)."
229
+ layout_tilt = nil
230
+ end
231
+ end
232
+ if layout_tilt.nil?
233
+ puts " Layout could not be found or rendered. Skipping..."
234
+ next
235
+ end
236
+ (paths == [] ? Dir.glob("*") : paths).each do |content_file|
237
+ puts " Read source file #{source}/#{content_file}"
238
+ $template_arguments.merge! :source => "#{source}/#{content_file}"
239
+
240
+ # Create tilt instances
241
+ source_tilt = nil
242
+ begin
243
+ source_tilt = Tilt.new(content_file)
244
+ rescue Exception => e
245
+ puts "- ERROR creating tilt instance (possibly missing source file)."
246
+ source_tilt = nil
247
+ end
248
+ if source_tilt.nil?
249
+ puts " Source file could not be rendered. Skipping..."
250
+ next
251
+ end
252
+
253
+ # Render and write
254
+ Dir.chdir $root_dir+destination_dir do
255
+ file_name = File.basename(content_file).gsub(File.extname(content_file), ".#{options[:extension]}")
256
+ $template_arguments.merge! :path => "#{destination_dir}/#{file_name}"
257
+ $template_arguments.merge! :file_name => file_name
258
+ $template_arguments.merge! :depth => (destination_dir.count "/")
259
+ File.open destination_base.gsub("#", file_name), "w" do |file|
260
+ file.puts layout_tilt.render(Object.new, $template_arguments){
261
+ source_tilt.render(Object.new, $template_arguments)
262
+ }
263
+ end
264
+ puts " Wrote to file: " + file_name
265
+ end
266
+ end
267
+ end
268
+ end
269
+ elsif action == "render-all-one-file"
270
+
271
+ ## R E N D E R A L L O N E F I L E
272
+ # Destination
273
+ destination_dir = File.dirname path
274
+ destination_base = File.basename path
275
+ if /\/$/ =~ path
276
+ destination_dir = path.gsub /\/$/, ""
277
+ destination_base = "index.#{options[:extension]}"
278
+ end
279
+
280
+ $template_arguments.merge! :path => "#{destination_dir}/#{destination_base}"
281
+ $template_arguments.merge! :file_name => destination_base
282
+ $template_arguments.merge! :depth => (destination_dir.count "/")
283
+
284
+ puts "Rendering path '#{destination_dir}/#{destination_base}'"
285
+ # make path
286
+ FileUtils.mkdir_p "."+destination_dir
287
+ unless File.directory? "."+destination_dir
288
+ puts " Path '"+path+"' could not be created. Skipping..."
289
+ next
290
+ end
291
+
292
+ sources_rendered = []
293
+ # Read source/s
294
+ Dir.chdir $content_dir do
295
+ paths = []
296
+ if source.is_a? Array
297
+ source, paths = source
298
+ end
299
+ Dir.chdir source do
300
+ paths = (paths == [] ? Dir.glob("*") : paths)
301
+ if options.has_key? :reverse_order and options[:reverse_order] == true
302
+ paths.reverse!
303
+ end
304
+ paths.each do |content_file|
305
+ #content = File.read content_file
306
+ puts " Read source file #{source}/#{content_file}"
307
+ $template_arguments.merge! :source => "#{source}#{content_file}"
308
+
309
+ # Create tilt instance
310
+ source_tilt = nil
311
+ begin
312
+ source_tilt = Tilt.new(content_file)
313
+ rescue Exception => e
314
+ puts "- ERROR creating tilt instance (possibly missing source file)."
315
+ source_tilt = nil
316
+ end
317
+ if source_tilt.nil?
318
+ puts " Source file could not be rendered. Skipping..."
319
+ next
320
+ end
321
+ sources_rendered << {:content => source_tilt.render(Object.new, $template_arguments)}.merge($template_arguments)
322
+ end
323
+ end
324
+ end
325
+
326
+ # Create layout tilt instance
327
+ layout_tilt = nil
328
+ Dir.chdir $assets_dir do
329
+ begin
330
+ layout_tilt = Tilt.new($layout)
331
+ rescue Exception => e
332
+ puts "- ERROR creating tilt instance (possibly missing layout file)."
333
+ layout_tilt = nil
334
+ end
335
+ end
336
+ if layout_tilt.nil?
337
+ puts " Layout could not be found or rendered. Skipping..."
338
+ next
339
+ end
340
+ # Render and write
341
+ Dir.chdir "."+destination_dir do
342
+ File.open destination_base, "w" do |file|
343
+ file.puts layout_tilt.render(Object.new, {:items => sources_rendered}.merge($template_arguments))
344
+ end
345
+ end
346
+ puts " Wrote to file."
347
+ end # End of action switching
348
+ end
349
+ end
350
+
351
+ # Thor: Deplot CLI
352
+
353
+ class Deplot < Thor
354
+
355
+ desc "init", "Create a new deplot project in the current directory"
356
+ def init
357
+ # Content directory
358
+ puts " Making directory '"+$content_dir+"'..."
359
+ FileUtils.mkdir $content_dir
360
+ # Assets directory
361
+ puts " Making directory '"+$assets_dir+"'..."
362
+ FileUtils.mkdir $assets_dir
363
+ # Basic Layout
364
+ Dir.chdir $assets_dir do
365
+ File.open "layout.erb", "w" do |file|
366
+ file.write <<-EOF
367
+ <html>
368
+ <head>
369
+ <title>Deplot Project</title>
370
+ <link rel="stylesheet" href="style.css" />
371
+ </head>
372
+ <body>
373
+ <div id="wrapper">
374
+ <%= yield %>
375
+ </div>
376
+ </body>
377
+ </html>
378
+ EOF
379
+ end
380
+ File.open "style.less", "w" do |file|
381
+ file.write <<-EOF
382
+ body {
383
+ font-family: "Helvetica", sans-serif;
384
+ }
385
+ #wrapper {
386
+ width: 500px;
387
+ margin: 0px auto;
388
+ }
389
+ EOF
390
+ end
391
+ end
392
+ # Deplotfile
393
+ puts " Creating Deplotfile..."
394
+ File.open "Deplotfile", "w" do |file|
395
+ file.write <<-EOF
396
+ path '/' do
397
+ render "index.markdown"
398
+ end
399
+
400
+ publish
401
+ EOF
402
+ end
403
+ # Guardfile
404
+ puts " Creating Guardfile..."
405
+ File.open "Guardfile", "w" do |file|
406
+ file.write <<-EOF
407
+ # CoffeeScript
408
+ guard :coffeescript, :input => 'assets', :output => '.'
409
+
410
+ # CSS <- LESS, SASS
411
+ guard :sass, :input => 'assets', :output => '.'
412
+ guard :less, :output => '.' do
413
+ watch(/^assets\\/(.+\.less)$/)
414
+ end
415
+
416
+ # Invoke deplot
417
+ guard :shell do
418
+ watch(/(assets|content)\\/(.*)/) do |m|
419
+ `echo ""; echo "File '\#{m[2]}' changed."; deplot make`
420
+ end
421
+ end
422
+ EOF
423
+ end
424
+ # Example content
425
+ Dir.chdir $content_dir do
426
+ File.open "index.markdown", "w" do |file|
427
+ file.write <<-EOF
428
+ # New deplot project
429
+
430
+ This is the default page of your new project. Change the Deplotfile in the project
431
+ directory to add new routes (see examples below) or change the contents of this file
432
+ by editing `content/index.markdown`. Remember to call `deplot make` to build the
433
+ project or, better, run `guard` to recompile if anything changes.
434
+
435
+ ## Editing the Deplotfile
436
+
437
+ The basic syntax to describe a page of your site is to call `path` with the page location
438
+ as a string and with a block containing the specific instructions, like the `render`
439
+ method:
440
+
441
+ ```ruby
442
+ path '/' do
443
+ render "index.markdown"
444
+ end
445
+ ```
446
+
447
+ This is, by the way, also the code that is used to generate this introduction page.
448
+ To render more than one file, for example all files in a directory called `blog/`,
449
+ you can use `render_all`:
450
+
451
+ ```ruby
452
+ path '/blog/#'
453
+ render_all "blog/"
454
+ end
455
+ ```
456
+
457
+ The symbol `#` will then be replaced by the name of the source file (without its
458
+ extension and with `.html` at the end). Omitting the symbol will cause deplot to render
459
+ all source files into one single file. The code below will produce an index file
460
+ `blog/index.html` that uses another layout (`layout_index.erb`).
461
+
462
+ ```ruby
463
+ path '/blog/'
464
+ layout "layout_index.erb"
465
+ render_all "blog/"
466
+ end
467
+
468
+ ## Editing content
469
+
470
+ All content is by default in the directory `content/` and can be written in any language
471
+ that `tilt` understands. The directory in which deplot searches for the source files can
472
+ be changed with a call to `content_dir`, and the directory for all assets (layouts,
473
+ stylesheets) with `assets_dir`.
474
+
475
+ EOF
476
+ end
477
+ end
478
+ end
479
+
480
+ desc "new <project_name> (<git_repo>)", "Create a new deplot project, if a git repo is specified by cloning an existing skeleton"
481
+ def new(project_name, git_skeleton = "")
482
+ if File.exist? project_name
483
+ puts "Directory '"+project_name+"' already exists. Exiting..."
484
+ return
485
+ end
486
+ puts "Creating project folder..."
487
+ if git_skeleton == ""
488
+ FileUtils.mkdir project_name
489
+ $root_dir = Dir.pwd + "/" + project_name
490
+ $assets_dir = $root_dir + "/assets"
491
+ $content_dir = $root_dir + "/content"
492
+ Dir.chdir project_name do
493
+ init
494
+ end
495
+ puts "Finished creating '#{project_name}'- ready to build your site!"
496
+ else
497
+ `git clone #{git_skeleton} #{project_name}`
498
+ if File.exist? project_name
499
+ begin
500
+ `rm -rf #{project_name}/.git`
501
+ rescue
502
+ puts "Unable to delete git references."
503
+ end
504
+ puts "Finished creating '#{project_name}' - ready to build your site! (If 'git clone' didn't fail.)"
505
+ else
506
+ puts "Git clone failed (see output above)."
507
+ end
508
+ end
509
+ end
510
+
511
+ desc "make", "Run deplot and build your project"
512
+ def make()
513
+ puts "Making project..."
514
+ unless File.exist? "Deplotfile"
515
+ puts "Deplotfile not found. Exiting..."
516
+ return
517
+ end
518
+ $LOAD_PATH.unshift(Dir.pwd)
519
+ load "Deplotfile"
520
+ end
521
+ end
522
+ Deplot.start
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deplot
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Cyril Nusko
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-23 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A ruby static web site generator
22
+ email: gitcdn@gmail.com
23
+ executables:
24
+ - deplot
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - bin/deplot
31
+ homepage: http://www.github.com/cdn64/deplot
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.12
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Deplot
64
+ test_files: []
65
+