12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/nanoc/helpers/capturing.rb', line 12
def content_for(*args, &block)
if block_given? case args.size
when 1
name = args[0]
params = {}
when 2
name = args[0]
params = args[1]
else
raise ArgumentError, 'expected 1 or 2 argument (the name ' \
"of the capture, and optionally params) but got #{args.size} instead"
end
name = args[0]
existing_behavior = params.fetch(:existing, :error)
content_string = capture(&block)
snapshot_contents = @item.reps[:default].unwrap.snapshot_contents
capture_name = "__capture_#{name}".to_sym
old_content_string =
case existing_behavior
when :overwrite
''
when :append
c = snapshot_contents[capture_name]
c ? c.string : ''
when :error
if snapshot_contents[capture_name] && snapshot_contents[capture_name].string != content_string
raise "a capture named #{name.inspect} for #{@item.identifier} already exists"
else
''
end
else
raise ArgumentError, 'expected :existing_behavior param to #content_for to be one of ' \
":overwrite, :append, or :error, but #{existing_behavior.inspect} was given"
end
snapshot_contents[capture_name] = Nanoc::Int::TextualContent.new(old_content_string + content_string)
else if args.size != 2
raise ArgumentError, 'expected 2 arguments (the item ' \
"and the name of the capture) but got #{args.size} instead"
end
item = args[0]
name = args[1]
rep = item.reps[:default].unwrap
if @item.nil? || item != @item.unwrap
dependency_tracker = @config._context.dependency_tracker
dependency_tracker.bounce(item.unwrap, compiled_content: true)
unless rep.compiled?
Fiber.yield(Nanoc::Int::Errors::UnmetDependency.new(rep))
return content_for(*args, &block)
end
end
content = rep.snapshot_contents["__capture_#{name}".to_sym]
content ? content.string : nil
end
end
|