Module: Kitabu::Markup

Defined in:
lib/kitabu/base.rb

Class Method Summary collapse

Class Method Details

.content_for(options) ⇒ Object



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(/&lt;/, '<').gsub(/&gt;/, '>').gsub(/&amp;/, '&')
    end
  end
  
  # no code? set to default
  code ||= options[:code]
  
  # normalize indentation
  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
  
  # remove all line stubs
  code.gsub! %r(^[\t ]*__$), ""
  
  # return
  code
end

.syntax(code, syntax = 'plain_text') ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/kitabu/base.rb', line 47

def self.syntax(code, syntax='plain_text')
  # get chosen theme
  theme = Kitabu::Base.config['theme']
  theme = Kitabu::Base.default_theme unless Kitabu::Base.theme?(theme)
  
  # get syntax
  syntax = Kitabu::Base.default_syntax unless Kitabu::Base.syntax?(syntax)
  
  Uv.parse(code, "xhtml", syntax, false, theme)
end