Class: Hx::Site

Inherits:
Object
  • Object
show all
Includes:
Filter
Defined in:
lib/hx.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filter

#each_entry

Methods included from View

#each_entry

Constructor Details

#initialize(options, sources, outputs) ⇒ Site

Returns a new instance of Site.



589
590
591
592
593
# File 'lib/hx.rb', line 589

def initialize(options, sources, outputs)
  @options = options
  @sources = sources
  @output = Overlay.new(*outputs)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



491
492
493
# File 'lib/hx.rb', line 491

def options
  @options
end

#sourcesObject (readonly)

Returns the value of attribute sources.



492
493
494
# File 'lib/hx.rb', line 492

def sources
  @sources
end

Class Method Details

.load(io, config_file, option_overrides = {}) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/hx.rb', line 503

def load(io, config_file, option_overrides={})
  raw_config = YAML.load(io)
  options = {}
  options[:base_dir] = File.dirname(config_file)
  for key, value in raw_config.fetch('options', {})
    options[key.intern] = value
  end
  options[:config_file] = config_file
  options.update(option_overrides)

  Hx.setup_require_path(options)

  if raw_config.has_key? 'require'
    for library in raw_config['require']
      require library
    end
  end

  raw_sources_by_name = raw_config.fetch('sources', {})
  raw_outputs = raw_config.fetch('output', [])
  for name, raw_source in raw_sources_by_name
    raw_sources_by_name[name] = Hx.expand_chain(raw_source)
  end
  raw_outputs = raw_outputs.map! do |raw_output|
    Hx.expand_chain(raw_output)
  end

  # build input dependency graph
  source_dependencies = Hash.new { |h,k| h[k] = Set.new }
  source_count_by_dependency = Hash.new(0)
  for name, raw_source in raw_sources_by_name
    for input_name in Hx.get_input_names(raw_source)
      source_dependencies[name].add input_name
      source_count_by_dependency[input_name] += 1
    end
  end
  for raw_output in raw_outputs
    for input_name in Hx.get_input_names(raw_output)
      source_count_by_dependency[input_name] += 1
    end
  end

  source_names = raw_sources_by_name.keys

  # calculate depth for each input in the graph
  source_depths = Hash.new(0)
  to_process = Set.new(source_names)
  until to_process.empty?
    need_updating = Set.new
    for name in to_process
      depth = source_depths[name] + 1
      if depth > source_names.length
        raise "cycle in source graph involving #{name}"
      end
      for input_name in source_dependencies[name]
        if depth > source_depths[input_name]
          source_depths[input_name] = depth
          need_updating.add input_name
        end
      end
    end
    to_process = need_updating
  end

  # depth-first topological sort
  depth_first_names = source_names.sort_by { |n| -source_depths[n] }

  sources = {}
  for name in depth_first_names
    raw_source = raw_sources_by_name[name]
    source = Hx.build_source(options, NULL_INPUT, sources, raw_source)
    if source_count_by_dependency[name] > 1
      source = Cache.new(source, options)
    end
    sources[name] = source
  end

  outputs = []
  for raw_output in raw_outputs
    outputs << Hx.build_source(options, NULL_INPUT, sources, raw_output)
  end

  new(options, sources, outputs)
end

.load_file(config_file, option_overrides = {}) ⇒ Object



497
498
499
500
501
# File 'lib/hx.rb', line 497

def load_file(config_file, option_overrides={})
  File.open(config_file, 'r') do |stream|
    load(stream, config_file, option_overrides)
  end
end

Instance Method Details

#each_entry_path(selector) ⇒ Object



600
601
602
603
# File 'lib/hx.rb', line 600

def each_entry_path(selector)
  @output.each_entry_path(selector) { |path| yield path }
  self
end

#edit_entry(path, prototype = nil) ⇒ Object



595
596
597
598
# File 'lib/hx.rb', line 595

def edit_entry(path, prototype=nil)
  @output.edit_entry(path, prototype) { |text| yield text }
  self
end

#get_entry(path) ⇒ Object



605
606
607
# File 'lib/hx.rb', line 605

def get_entry(path)
  @output.get_entry(path)
end