Class: Khaleesi::CLI::Construction

Inherits:
Khaleesi::CLI show all
Defined in:
lib/khaleesi/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Khaleesi::CLI

class_from_arg, #create_file_p, normalize_syntax

Constructor Details

#initialize(opts = {}) ⇒ Construction

Returns a new instance of Construction.



203
204
205
# File 'lib/khaleesi/cli.rb', line 203

def initialize(opts={})
  @directory_name = opts[:directory_name]
end

Instance Attribute Details

#directory_nameObject (readonly)

Returns the value of attribute directory_name.



201
202
203
# File 'lib/khaleesi/cli.rb', line 201

def directory_name
  @directory_name
end

Class Method Details

.descObject



177
178
179
# File 'lib/khaleesi/cli.rb', line 177

def self.desc
  'create a site directory with whole structure at present working directory(pwd)'
end

.doc {|'usage: khaleesi construction <directory_name>'| ... } ⇒ Object

Yields:



181
182
183
184
185
186
187
# File 'lib/khaleesi/cli.rb', line 181

def self.doc
  return enum_for(:doc) unless block_given?

  yield 'usage: khaleesi construction <directory_name>'
  yield ''
  yield '<directory_name>  specify a directory name of site'
end

.parse(argv) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/khaleesi/cli.rb', line 189

def self.parse(argv)
  opts = {:directory_name => nil}

  until argv.empty?
    opts[:directory_name] = argv.shift
  end

  puts 'unspecific directory name' unless opts[:directory_name]

  new(opts)
end

Instance Method Details

#runObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/khaleesi/cli.rb', line 207

def run
  return unless @directory_name

  root_dir = "#{Dir.pwd}/#{@directory_name}"

  gen_site = "#{root_dir}/gen_site"
  execute_script = create_file_p(root_dir, @directory_name, '')
  open(execute_script, 'w') do |f|
    f.puts '#!/bin/bash'
    f.puts ''
    f.puts "src_dir=#{root_dir}"
    f.puts "dest_dir=#{gen_site}"
    f.puts 'line_numbers="false"'
    f.puts 'css_class="highlight"'
    f.puts 'time_pattern="%Y-%m-%d %H:%M"'
    f.puts 'date_pattern="%F"'
    f.puts 'highlighter=""'
    f.puts '# highlighter="pygments"'
    f.puts 'toc_selection="h1,h2,h3"'
    f.puts '# toc_selection="h1,h2,h3[unique]"'
    f.puts ''

    f.puts 'if [[ "$1" == "generate" ]]; then'
    f.puts '  diff=$([ "$2" == \'diff\' ] && echo "true" || echo "false")'
    f.puts '  khaleesi generate --src-dir "$src_dir" --dest-dir "$dest_dir" --line-numbers $line_numbers \\'
    f.puts '    --css-class $css_class --time-pattern "$time_pattern" --date-pattern "$date_pattern" \\'
    f.puts '    --diff-plus "$diff" --highlighter "$highlighter" --toc-selection "$toc_selection"'
    f.puts ''
    f.puts 'elif [[ "$1" == "build" ]]; then'
    f.puts '  temperary_dest_dir=~/tmp_site'
    f.puts '  mkdir $temperary_dest_dir'
    f.puts ''
    f.puts '  cd $src_dir'
    f.puts ''
    f.puts '  git checkout master'
    f.puts ''
    f.puts '  khaleesi build --src-dir "$src_dir" --dest-dir "$dest_dir" --line-numbers $line_numbers \\'
    f.puts '    --css-class $css_class --time-pattern "$time_pattern" --date-pattern "$date_pattern" \\'
    f.puts '    --highlighter "$highlighter" --toc-selection "$toc_selection"'
    f.puts ''
    f.puts '  git checkout gh-pages'
    f.puts ''
    f.puts '  rsync -acv $temperary_dest_dir/ .'
    f.puts ''
    f.puts '  rm -fr $temperary_dest_dir'
    f.puts ''
    f.puts 'elif [[ "$1" == "serve" ]]; then'
    f.puts '  ruby -run -e httpd $dest_dir -p 9090'
    f.puts ''
    f.puts 'fi'
  end
  File.chmod(0755, execute_script)
  FileUtils.mkdir_p(gen_site)

  css_dir = "#{root_dir}/_raw/css"
  css_file = create_file_p(css_dir, 'site', 'css')
  open(css_file, 'w') do |f|
    f.puts '* {margin:0; padding:0;}'
    f.puts 'body {margin:40px auto; width:940px; line-height:1.8em;}'

    f.puts 'a {color:#149ad4; text-decoration:none;}'
    f.puts 'a:hover {text-decoration:underline;}'

    f.puts '.header { font-size: 18px; padding-bottom: 10px; margin-bottom: 10px; border-bottom: 1px solid #ddd; box-shadow: 0 1px 0 rgba(255,255,255,0.5);}'
    f.puts '.content { font-size: 20px; padding-bottom: 10px; }'

    f.puts '.content .primary .post_list {'
    f.puts '  list-style-type: none;'
    f.puts '}'

    f.puts '.content .primary .post_list li {'
    f.puts '  border: 1px solid #ddd;'
    f.puts '  margin-bottom: 10px;'
    f.puts '  padding: 10px;'
    f.puts '}'

    f.puts '.content .primary .post_list li p {'
    f.puts '  color: #5c5855;'
    f.puts '  font-size: 16px;'
    f.puts '}'

    f.puts '.content .primary .post_list li span {'
    f.puts '  color: #60E;'
    f.puts '  font-size: 12px;'
    f.puts '}'

    f.puts '.content .post_title {'
    f.puts '  margin: 10px 0;'
    f.puts '  font-size: 32px;'
    f.puts '  text-align: center;'
    f.puts '}'

    f.puts '.footer {'
    f.puts '  width: 100%;'
    f.puts '  margin: 0 auto;'
    f.puts '  border-top: 2px solid #d5d5d5;'
    f.puts '  font-size: 13px;'
    f.puts '  color: #666;'
    f.puts '  padding-top: 10px;'
    f.puts '  padding-bottom: 60px;'
    f.puts '  line-height: 1.8em;'
    f.puts '}'

    f.puts '.footer .left {'
    f.puts '  float: left;'
    f.puts '}'

    f.puts '.footer .right {'
    f.puts '  float: right;'
    f.puts '}'

    f.puts '.footer .right div {'
    f.puts '  text-align: right;'
    f.puts '}'
  end

  FileUtils.mkdir_p("#{root_dir}/_raw/images")


  decorators_dir = "#{root_dir}/_decorators"
  decorator_file = create_file_p(decorators_dir, 'basic', 'html')
  open(decorator_file, 'w') do |f|
    f.puts '<!DOCTYPE html>'
    f.puts '<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">'
    f.puts '    <head>'
    f.puts '        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
    f.puts '        <link rel="stylesheet" href="/css/site.css" type="text/css" media="screen">'
    f.puts '        <title>${variable:title}</title>'
    f.puts '    </head>'
    f.puts '    <body>'
    f.puts '        <div class="header">'
    f.puts '            A khaleesi demonstration site'
    f.puts '        </div>'
    f.puts '        <div class="content">'
    f.puts '            ${decorator:content}'
    f.puts '        </div>'
    f.puts '        <div class="footer">'
    f.puts '          <div class="left">'
    f.puts '            <div>Licensed under the <a href="http://choosealicense.com/licenses/mit/">MIT License</a></div>'
    f.puts '          </div>'
    f.puts '          <div class="right">'
    f.puts '            <div>A pure-ruby static site generator</div>'
    f.puts '            <div>Find <a href="https://github.com/vince-styling/khaleesi">khaleesi</a> in github</div>'
    f.puts '          </div>'
    f.puts '        </div>'
    f.puts '    </body>'
    f.puts '</html>'
  end

  decorator_file = create_file_p(decorators_dir, 'post', 'html')
  open(decorator_file, 'w') do |f|
    f.puts 'decorator: basic'
    f.puts '‡‡‡‡‡‡‡‡‡‡‡‡‡‡'
    f.puts '<h1 class="post_title">${variable:title}</h1>'
    f.puts '<div class="post-thumb">'
    f.puts '    ${decorator:content}'
    f.puts '</div>'
  end


  pages_dir = "#{root_dir}/_pages"
  index_file = create_file_p(pages_dir, 'index', 'html')
  open(index_file, 'w') do |f|
    f.puts 'title: Khaleesi Index'
    f.puts 'decorator: basic'
    f.puts 'slug: index.html'
    f.puts '‡‡‡‡‡‡‡‡‡‡‡‡‡‡'
    f.puts '<div class="primary">'
    f.puts '    <ul class="post_list">'
    f.puts '        #foreach ($post : $posts)'
    f.puts '            <li title="${post:title}">'
    f.puts '                <a href="${post:link}">${post:title}</a>'
    f.puts '                <span>(${post:createtime})</span>'
    f.puts '                <p>${post:description}</p>'
    f.puts '            </li>'
    f.puts '        #end'
    f.puts '    </ul>'
    f.puts '</div>'
  end

  pages_dir << '/posts'
  post_file = create_file_p("#{pages_dir}/2014", 'khaleesi-introduction', 'md')
  open(post_file, 'w') do |f|
    f.puts 'title: khaleesi\'s introduction'
    f.puts 'decorator: post'
    f.puts 'description: Khaleesi is a static site generator that write by ruby.'
    f.puts '‡‡‡‡‡‡‡‡‡‡‡‡‡‡'
    f.puts ''
    f.puts 'Khaleesi is a static site generator that write by ruby, supported markdown parser, multiple decorators inheritance, simple page programming, page including, page dataset configurable etc.'
    f.puts ''
    f.puts 'please check [this](http://khaleesi.vincestyling.com/) for more details.'
  end

  post_file = create_file_p("#{pages_dir}/2013", 'netroid-introduction', 'md')
  open(post_file, 'w') do |f|
    f.puts 'title: netroid\'s introduction'
    f.puts 'decorator: post'
    f.puts 'description: Netroid is a Http Framework for Android that based on Volley.'
    f.puts '‡‡‡‡‡‡‡‡‡‡‡‡‡‡'
    f.puts ''
    f.puts 'Netroid library for Android'
    f.puts '---------------------------'
    f.puts ''
    f.puts 'Netroid is a http library for Android that based on Volley, That purpose is make your android development easier than before, provide fast, handly, useful way to do async http operation by background thread.'
    f.puts ''
    f.puts 'please check [this](https://github.com/vince-styling/Netroid) for more details.'
  end

  puts "A sample site of Khaleesi was built in #{root_dir}."
end