3
4
5
6
7
8
9
10
11
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
|
# File 'lib/kitabu/base.rb', line 3
def self.content_for(options)
source_file = File.join(KITABU_ROOT, 'code', options[:source_file].to_s)
code = options[:code]
if options[:source_file] && File.exists?(source_file)
file = File.new(source_file)
if options[:from_line] && options[:to_line]
from_line = options[:from_line].to_i - 1
to_line = options[:to_line].to_i
offset = to_line - from_line
code = file.readlines.slice(from_line, offset).join
elsif block_name = options[:block_name]
re = %r(# ?begin: ?#{block_name} ?(?:[^\r\n]+)?\r?\n(.*?)\r?\n([^\r\n]+)?# ?end: #{block_name})sim
file.read.gsub(re) { |block| code = $1 }
else
code = file.read
code = code.gsub(/</, '<').gsub(/>/, '>').gsub(/&/, '&')
end
end
code ||= options[:code]
line = StringIO.new(code).readlines[0]
if line =~ /^(\t+)/
char = "\t"
size = $1.length
elsif line =~ /^( +)/
char = " "
size = $1.length
end
code.gsub! %r(^#{char}{#{size}}), "" if size.to_i > 0
code.gsub! %r(^[\t ]*__$), ""
code
end
|