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

Constructor Details

#initialize(options, sources, outputs) ⇒ Site

Returns a new instance of Site.



624
625
626
627
628
# File 'lib/hx.rb', line 624

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.



528
529
530
# File 'lib/hx.rb', line 528

def options
  @options
end

#sourcesObject (readonly)

Returns the value of attribute sources.



529
530
531
# File 'lib/hx.rb', line 529

def sources
  @sources
end

Class Method Details

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



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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/hx.rb', line 540

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)

  if raw_config.has_key? 'require'
    for library in raw_config['require']
      Hx.local_require(options, 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



534
535
536
537
538
# File 'lib/hx.rb', line 534

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



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

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

#edit_entry(path, prototype = nil) ⇒ Object



630
631
632
633
# File 'lib/hx.rb', line 630

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

#get_entry(path) ⇒ Object



640
641
642
# File 'lib/hx.rb', line 640

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