Class: Rulex::Rex::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/rulex/rex/reader.rb

Instance Method Summary collapse

Constructor Details

#initializeReader

Returns a new instance of Reader.



5
6
7
8
9
# File 'lib/rulex/rex/reader.rb', line 5

def initialize
  @content = []
  @content_stack = [@content]
  @latex_reader = Rulex::Tex::Reader.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m_id, *args, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/rulex/rex/reader.rb', line 92

def method_missing(m_id, *args, &block) 
  if block
    tex_environment(m_id, *args, &block)
  elsif /pure_([a-zA-Z]+)/.match(m_id)
    Rulex::Tex::Writer.to_str(build_tex_command($1,args))
  else
    tex_command(m_id, args)
  end
end

Instance Method Details

#add_node_to_content(node) ⇒ Object



28
29
30
# File 'lib/rulex/rex/reader.rb', line 28

def add_node_to_content node
  @content_stack.last << node
end

#build_tex_command(name, params) ⇒ Object



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
# File 'lib/rulex/rex/reader.rb', line 48

def build_tex_command(name, params)

  fail ArgumentError, "Command name must be a String or a Symbol, got #{name} of type #{name.class}" unless
      String === name or Symbol === name

  case params.length
  when 0
    {type: :command, name: name}
  when 1
    fail ArgumentError "Command arguments must all be String s or Symbol s, got #{params}" unless
      params.all?{|s| String === s or Symbol === s}
    {type: :command, name: name, arguments: params} 
  when 2
    first = params[0]
    second = params[1]
    if Array === params[0] && Array === params[1]
      {type: :command, name: name, arguments: second, options: first}
    elsif String === params[0] && String === params[1]
      {type: :command, name: name, arguments: [first, second]}
    else
      raise ArgumentError, "something is not quite right with the parameters"
    end
  else
    raise ArgumentError, "wrong number of params"
  end
end

#depthObject



76
77
78
# File 'lib/rulex/rex/reader.rb', line 76

def depth
  @content_stack.length - 1
end

#exportObject



44
45
46
# File 'lib/rulex/rex/reader.rb', line 44

def export
  @content
end

#import(filepath) ⇒ Object



40
41
42
# File 'lib/rulex/rex/reader.rb', line 40

def import filepath
  read File.open(filepath).read
end

#raw(str) ⇒ Object



32
33
34
# File 'lib/rulex/rex/reader.rb', line 32

def raw str
  add_node_to_content(type: :text, text: str)
end

#read(*args, &block) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rulex/rex/reader.rb', line 11

def read *args, &block
  if args.length == 1
    read_rex args.first
  elsif block
    instance_eval &block
  end
end

#read_rex(str) ⇒ Object



19
20
21
# File 'lib/rulex/rex/reader.rb', line 19

def read_rex str
  instance_eval rex_to_ruby str
end

#rex_to_ruby(str) ⇒ Object

There are a few characters (‘\’, ‘[’ and ‘]’) that even %q[] escapes



24
25
26
# File 'lib/rulex/rex/reader.rb', line 24

def rex_to_ruby str
  str.gsub(/<##(((?!##>)[\s\S])+)##>/) { |m| "raw %q[" + $1.gsub("\\","\\\\\\\\") + "]"}
end

#tex(str) ⇒ Object



36
37
38
# File 'lib/rulex/rex/reader.rb', line 36

def tex str
  add_node_to_content(type: :tex, children: @latex_reader.read(str))
end

#tex_command(name, *params) ⇒ Object



80
81
82
# File 'lib/rulex/rex/reader.rb', line 80

def tex_command(name, *params)
  add_node_to_content build_tex_command name, *params
end

#tex_environment(name, *args, &block) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/rulex/rex/reader.rb', line 84

def tex_environment(name, *args, &block)
  new_node = {type: :environment, name: name, arguments: args}
  @content_stack.push []
  read &block
  new_node.merge!(children: @content_stack.pop)
  add_node_to_content new_node
end