Module: Hx

Defined in:
lib/hx/path.rb,
lib/hx.rb,
lib/hx/cli.rb,
lib/hx/listing.rb,
lib/hx/backend/hobix.rb,
lib/hx/listing/limit.rb,
lib/hx/backend/rawfiles.rb,
lib/hx/listing/paginate.rb,
lib/hx/rack/application.rb,
lib/hx/listing/flatindex.rb,
lib/hx/listing/datearchive.rb,
lib/hx/output/liquidtemplate.rb,
lib/hx/listing/recursiveindex.rb

Overview

hx/path - path selectors

Copyright © 2009-2010 MenTaLguY <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: Backend, CLI, CircumfixPath, Filter, Listing, Output, Path, Rack Classes: AddPath, Cache, EditingNotSupportedError, IncludeFragments, LazyContent, NoSuchEntryError, NullInput, Overlay, PathSubset, Site, Sort, StripPath

Constant Summary collapse

VERSION =
(Pathname.new(__FILE__).parent.parent + 'VERSION').read.strip
NULL_INPUT =
NullInput.new
Chain =
Object.new

Class Method Summary collapse

Class Method Details

.build_source(options, default_input, sources, raw_source) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/hx.rb', line 458

def self.build_source(options, default_input, sources, raw_source)
  input_names = get_input_names(raw_source)
  input_sources = input_names.map do |input_name|
    begin
      sources.fetch(input_name)
    rescue IndexError
      raise NameError, "No source named #{input_name} in scope"
    end
  end
  case input_sources.length
  when 0
    source = default_input
  when 1
    source = input_sources.first
  else
    source = Overlay.new(*input_sources)
  end

  if raw_source.has_key? 'fragments'
    source = IncludeFragments.new(source,
                                  :fragments => raw_source['fragments'])
  end

  if raw_source.has_key? 'filter'
    if raw_source.has_key? 'options'
      filter_options = options.dup
      for key, value in raw_source['options']
        filter_options[key.intern] = value
      end
    else
      filter_options = options
    end
    filter = raw_source['filter']
    begin
      factory = Hx.resolve_constant(filter)
    rescue NameError
      library = filter.gsub(/::/, '/').downcase
      Hx.local_require(options, library)
      factory = Hx.resolve_constant(filter)
    end
    source = factory.new(source, filter_options)
  end

  if raw_source.has_key? 'only' or raw_source.has_key? 'except'
    source = PathSubset.new(source, :only => raw_source['only'],
                                    :except => raw_source['except'])
  end

  if raw_source.has_key? 'strip_prefix' or
     raw_source.has_key? 'strip_suffix'
    source = StripPath.new(source, :prefix => raw_source['strip_prefix'],
                                   :suffix => raw_source['strip_suffix'])
  end

  if raw_source.has_key? 'add_prefix' or raw_source.has_key? 'add_suffix'
    source = AddPath.new(source, :prefix => raw_source['add_prefix'],
                                 :suffix => raw_source['add_suffix'])
  end

  if raw_source.has_key? 'sort_by' or raw_source.has_key? 'reverse'
    source = Sort.new(source, :sort_by => raw_source['sort_by'],
                              :reverse => raw_source['reverse'])
  end

  source
end

.cache_scopeObject



222
223
224
225
226
227
228
229
230
# File 'lib/hx.rb', line 222

def self.cache_scope
  saved_records = Thread.current[:hx_cache_records]
  Thread.current[:hx_cache_records] = {}
  begin
    yield
  ensure
    Thread.current[:hx_cache_records] = saved_records
  end
end

.expand_chain(raw_input) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/hx.rb', line 429

def self.expand_chain(raw_input)
  case raw_input
  when Array # rewrite array to Hx::Chain
    return NULL_INPUT if raw_input.empty?

    filter_defs = raw_input.dup
    first_filter = filter_defs[0] = filter_defs[0].dup

    raw_input = {
      'filter' => 'Hx::Chain',
      'options' => {'chain' => filter_defs}
    }

    if first_filter.has_key? 'input' # use input of first filter for chain
      raw_input['input'] = first_filter['input']
      first_filter.delete('input')
    end
  end
  raw_input
end

.get_default_author(options) ⇒ Object



399
400
401
# File 'lib/hx.rb', line 399

def self.get_default_author(options)
  options.fetch(:default_author, "nobody")
end

.get_input_names(raw_source) ⇒ Object



450
451
452
453
454
455
456
# File 'lib/hx.rb', line 450

def self.get_input_names(raw_source)
  if raw_source.has_key? 'input'
    return Array(raw_source['input'])
  else
    []
  end
end

.get_pathname(options, key) ⇒ Object



389
390
391
392
393
394
395
396
397
# File 'lib/hx.rb', line 389

def self.get_pathname(options, key)
  dir = Pathname.new(options[key] || ".")
  if dir.relative?
    base_dir = Pathname.new(options[:base_dir])
    (base_dir + dir).cleanpath(true)
  else
    dir
  end
end

.local_require(options, library) ⇒ Object



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/hx.rb', line 403

def self.local_require(options, library)
  saved_require_path = $:.dup
  begin
    $:.delete(".")
    lib_dir = Hx.get_pathname(options, :lib_dir)
    if lib_dir.relative?
      $:.push "./#{lib_dir}"
    else
      $:.push lib_dir.to_s
    end
    require library
  rescue LoadError
    raise
  ensure
    $:[0..-1] = saved_require_path
  end
end

.make_default_title(options, path) ⇒ Object



383
384
385
386
387
# File 'lib/hx.rb', line 383

def self.make_default_title(options, path)
  name = path.split('/').last
  words = name.split(/[_\s-]/)
  words.map { |w| w.capitalize }.join(' ')
end

.refresh_file(pathname, content, update_time, executable = false) ⇒ Object



645
646
647
648
649
650
651
652
# File 'lib/hx.rb', line 645

def self.refresh_file(pathname, content, update_time, executable=false)
  begin
    return false if update_time and update_time < pathname.mtime
  rescue Errno::ENOENT
  end
  write_file(pathname, content, executable)
  true
end

.resolve_constant(qualified_name, root = Object) ⇒ Object



421
422
423
424
425
426
427
# File 'lib/hx.rb', line 421

def self.resolve_constant(qualified_name, root=Object)
  begin
    qualified_name.split('::').inject(root) { |c, n| c.const_get(n) }
  rescue NameError
    raise NameError, "Unable to resolve #{qualified_name}"
  end
end

.write_file(pathname, content, executable = false) ⇒ Object



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/hx.rb', line 654

def self.write_file(pathname, content, executable=false)
  parent = pathname.parent
  parent.mkpath()
  tempfile = Tempfile.new('.hx-out', parent.to_s)
  begin
    File.open(tempfile.path, "wb") { |stream| stream << content.to_s }
    base_mode = 0666
    base_mode |= 0111 if executable
    File.chmod(base_mode & ~File.umask, tempfile.path)
    File.rename(tempfile.path, pathname.to_s)
  ensure
    tempfile.unlink
  end
  nil
end