Class: Rulex::Tex::Writer

Inherits:
Object show all
Defined in:
lib/rulex/tex/writer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



5
6
7
# File 'lib/rulex/tex/writer.rb', line 5

def initialize
  @content = ''
end

Class Method Details

.to_str(item) ⇒ Object

Raises:

  • (TypeError)


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
46
# File 'lib/rulex/tex/writer.rb', line 9

def self.to_str item
  raise TypeError, "item must be a Hash, got #{item}" unless Hash === item
  raise ArgumentError, ":type must be defined" unless item[:type]
  case item[:type]
  when :command
    raise TypeError, "command name must be a Symbol or a String" unless
                  Symbol === item[:name] or String === item[:name]

    str = "\\#{item[:name]}"
    if opts = item[:options]
      str += '[' + opts.join(',') + ']'
    end
    if args = item[:arguments] and not args.empty?
      str += '{' + args.join('}{') + '}'
    end
    str += "\n"
  when :text
    res = item[:text]
  when :environment
    str = "\\begin{#{item[:name]}}"
    if args = item[:arguments] and not args.empty?
      str += '{' + args.join('}{') + '}'
    end
    str += "\n"
    if children = item[:children]
      str += item[:children].inject(""){|acc, c| acc += Rulex::Tex::Writer.to_str c} 
    end
    res = str += "\\end{#{item[:name]}}\n"
  else
    if item[:children]
      raise TypeError, "Children must form an array" unless Array === item[:children]
      str = item[:children].inject(""){|acc, c| acc += Rulex::Tex::Writer.to_str c} if item[:children] 
    else 
      ""
    end

  end
end

Instance Method Details

#import(arr) ⇒ Object



48
49
50
# File 'lib/rulex/tex/writer.rb', line 48

def import arr
  arr.each {|i| @content += Rulex::Tex::Writer.to_str i} if arr
end

#to_sObject Also known as: export



52
53
54
# File 'lib/rulex/tex/writer.rb', line 52

def to_s
  @content
end