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, View Classes: AddPath, Cache, EditingNotSupportedError, 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



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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
# File 'lib/hx.rb', line 426

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? '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
      require 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



234
235
236
237
238
239
240
241
242
# File 'lib/hx.rb', line 234

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



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/hx.rb', line 397

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



375
376
377
# File 'lib/hx.rb', line 375

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

.get_entry(view, path) ⇒ Object

Raises:



52
53
54
55
56
57
# File 'lib/hx.rb', line 52

def self.get_entry(view, path)
  view.each_entry(Path.literal(path)) do |entry_path, entry|
    return entry
  end
  raise NoSuchEntryError, path
end

.get_input_names(raw_source) ⇒ Object



418
419
420
421
422
423
424
# File 'lib/hx.rb', line 418

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



365
366
367
368
369
370
371
372
373
# File 'lib/hx.rb', line 365

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

.last_update_time(*entries) ⇒ Object



635
636
637
# File 'lib/hx.rb', line 635

def self.last_update_time(*entries)
  entries.map { |e| e['updated'] || e['created'] }.compact.max
end

.make_default_title(options, path) ⇒ Object



359
360
361
362
363
# File 'lib/hx.rb', line 359

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



610
611
612
613
614
615
616
617
# File 'lib/hx.rb', line 610

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



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

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

.setup_require_path(options) ⇒ Object



379
380
381
382
383
384
385
386
387
# File 'lib/hx.rb', line 379

def self.setup_require_path(options)
  $:.delete(".")
  lib_dir = Hx.get_pathname(options, :lib_dir)
  if lib_dir.relative?
    $:.push "./#{lib_dir}"
  else
    $:.push lib_dir.to_s
  end
end

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



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/hx.rb', line 619

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