Class: Germinate::Librarian

Inherits:
Object
  • Object
show all
Includes:
SharedStyleAttributes
Defined in:
lib/germinate/librarian.rb

Overview

The Librarian is responsible for organizing all the chunks of content derived from reading a source file and making them available for later re-assembly and formatting.

Defined Under Namespace

Classes: VariableStore

Constant Summary

Constants included from SharedStyleAttributes

SharedStyleAttributes::TEXT_TRANSFORMS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SharedStyleAttributes

#copy_attributes!, #copy_shared_style_attributes_from, #disable_all_transforms!, #shared_style_attributes

Constructor Details

#initializeLibrarian

Returns a new instance of Librarian.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/germinate/librarian.rb', line 57

def initialize
  @lines              = []
  @text_lines         = []
  @code_lines         = []
  @front_matter_lines = []
  @sections           = OrderedHash.new do |hash, key| 
    hash[key] = Germinate::TextHunk.new([], shared_style_attributes)
  end
  @samples            = OrderedHash.new do |hash, key| 
    hash[key] = Germinate::CodeHunk.new([], shared_style_attributes)
  end
  @processes = {'_transform' => Germinate::TransformProcess.new}
  @publishers = OrderedHash.new
end

Instance Attribute Details

#code_linesObject (readonly)

Returns the value of attribute code_lines.



47
48
49
# File 'lib/germinate/librarian.rb', line 47

def code_lines
  @code_lines
end

#front_matter_linesObject (readonly)

Returns the value of attribute front_matter_lines.



48
49
50
# File 'lib/germinate/librarian.rb', line 48

def front_matter_lines
  @front_matter_lines
end

#linesObject (readonly)

Returns the value of attribute lines.



45
46
47
# File 'lib/germinate/librarian.rb', line 45

def lines
  @lines
end

#text_linesObject (readonly)

Returns the value of attribute text_lines.



46
47
48
# File 'lib/germinate/librarian.rb', line 46

def text_lines
  @text_lines
end

Instance Method Details

#[](selector, origin = "<Unknown>", template = {}) ⇒ Object

TODO Too big, refactor.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/germinate/librarian.rb', line 175

def [](selector, origin="<Unknown>", template={})
  log.debug "Selecting #{selector}, from #{origin}"
  selector = case selector
             when Germinate::Selector then selector
             else Germinate::Selector.new(selector, "SECTION0", origin)
             end
  sample = 
    case selector.selector_type
    when :code then 
      sample(selector.key)
    when :special then 
      case selector.key
      when "SOURCE" 
        source_hunk = 
          if selector.whole?
            Germinate::FileHunk.new(lines, self)
          else
            Germinate::CodeHunk.new(lines, self)
          end
        source_hunk.disable_all_transforms!
        source_hunk
      when "CODE"   then Germinate::CodeHunk.new(code_lines, self)
      when "TEXT"   then Germinate::TextHunk.new(text_lines, self)
      else raise "Unknown special section '$#{selector.key}'"
      end
    else
      raise Exception, 
            "Unknown selector type #{selector.selector_type.inspect}"
    end

  sample.copy_shared_style_attributes_from(template)
  sample.origin.source_path ||= source_path
  sample.origin.selector    ||= selector

  sample = if selector.excerpt_output?
             excerpt(execute_pipeline(sample, selector.pipeline), selector)
           else
             execute_pipeline(excerpt(sample, selector), selector.pipeline)
           end
  sample
end

#add_code!(sample, line) ⇒ Object



87
88
89
90
91
# File 'lib/germinate/librarian.rb', line 87

def add_code!(sample, line)
  add_line!(line)
  @code_lines << line
  @samples[sample] << line
end

#add_control!(line) ⇒ Object



77
78
79
# File 'lib/germinate/librarian.rb', line 77

def add_control!(line)
  add_line!(line)
end

#add_front_matter!(line) ⇒ Object



72
73
74
75
# File 'lib/germinate/librarian.rb', line 72

def add_front_matter!(line)
  add_line!(line)
  @front_matter_lines << line
end

#add_insertion!(section, selector, attributes) ⇒ Object



93
94
95
96
97
# File 'lib/germinate/librarian.rb', line 93

def add_insertion!(section, selector, attributes)
  insertion = Germinate::Insertion.new(selector, self, attributes)
  @sections[section] << insertion
  @text_lines << insertion
end

#add_process!(process_name, command) ⇒ Object



105
106
107
108
# File 'lib/germinate/librarian.rb', line 105

def add_process!(process_name, command)
  @processes[process_name] = 
    Germinate::ShellProcess.new(process_name, command, variables)
end

#add_publisher!(name, identifier, options) ⇒ Object



110
111
112
# File 'lib/germinate/librarian.rb', line 110

def add_publisher!(name, identifier, options)
  @publishers[name] = Germinate::Publisher.make(name, identifier, self, options)
end

#add_text!(section, line) ⇒ Object



81
82
83
84
85
# File 'lib/germinate/librarian.rb', line 81

def add_text!(section, line)
  add_line!(line)
  @text_lines << line
  @sections[section] << line
end

#comment_prefix_known?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/germinate/librarian.rb', line 122

def comment_prefix_known?
  !comment_prefix.nil?
end

#has_sample?(sample_name) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/germinate/librarian.rb', line 170

def has_sample?(sample_name)
  @samples.key?(sample_name)
end

#has_section?(section_name) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/germinate/librarian.rb', line 135

def has_section?(section_name)
  @sections.key?(section_name)
end

#make_pipeline(process_names_or_string) ⇒ Object

Given a list of process names or a ‘|’-delimited string, return a Pipeline object representing a super-process of all the named processes chained together.



228
229
230
231
232
233
234
235
236
237
# File 'lib/germinate/librarian.rb', line 228

def make_pipeline(process_names_or_string)
  names = 
    if process_names_or_string.kind_of?(String)
      process_names_or_string.split("|")
    else
      process_names_or_string
    end
  processes = names.map{|n| process(n)}
  Germinate::Pipeline.new(processes)
end

#process(process_name) ⇒ Object

Fetch a process by name



149
150
151
152
153
# File 'lib/germinate/librarian.rb', line 149

def process(process_name)
  @processes.fetch(process_name)
rescue IndexError => error
  raise error.exception("Unknown process #{process_name.inspect}")
end

#process_namesObject



155
156
157
# File 'lib/germinate/librarian.rb', line 155

def process_names
  @processes.keys
end

#publisher(publisher_name) ⇒ Object

fetch a publisher by name



164
165
166
167
168
# File 'lib/germinate/librarian.rb', line 164

def publisher(publisher_name)
  @publishers.fetch(publisher_name)
rescue IndexError => error
  raise error.exception("Unknown publisher #{publisher_name.inspect}")
end

#publisher_namesObject



159
160
161
# File 'lib/germinate/librarian.rb', line 159

def publisher_names
  @publishers.keys
end

#sample(sample_name) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/germinate/librarian.rb', line 139

def sample(sample_name)
  unless has_sample?(sample_name)
    raise IndexError,
          "No code sample named '#{sample_name}'.  "\
          "Known samples: #{@samples.keys.join(', ')}"
  end
  Array(@samples[sample_name])
end

#sample_namesObject



221
222
223
# File 'lib/germinate/librarian.rb', line 221

def sample_names
  @samples.keys
end

#section(section_name) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/germinate/librarian.rb', line 126

def section(section_name)
  unless has_section?(section_name)
    raise IndexError, 
          "No text section named '#{section_name}'.  "\
          "Known sections: #{@sections.keys.join(', ')}"
  end
  Array(@sections[section_name])
end

#section_namesObject



217
218
219
# File 'lib/germinate/librarian.rb', line 217

def section_names
  @sections.keys
end

#set_code_attributes!(sample, attributes) ⇒ Object



99
100
101
102
103
# File 'lib/germinate/librarian.rb', line 99

def set_code_attributes!(sample, attributes)
  attributes.each_pair do |key, value| 
    @samples[sample].send(key, value) unless value.nil?
  end
end

#set_variable!(line, line_number, name, value) ⇒ Object



118
119
120
# File 'lib/germinate/librarian.rb', line 118

def set_variable!(line, line_number, name, value)
  variables.store(name,Germinate::Variable.new(name, value, line, source_path, line_number))
end

#store_changes!Object



114
115
116
# File 'lib/germinate/librarian.rb', line 114

def store_changes!
  source_file.write!(lines)
end