Class: Rulex::Rex::Reader
- Inherits:
-
Object
- Object
- Rulex::Rex::Reader
- Defined in:
- lib/rulex/rex/reader.rb
Instance Method Summary collapse
- #add_node_to_content(node) ⇒ Object
- #append_nodes_to_content(arr) ⇒ Object
- #build_tex_command(name, params) ⇒ Object
- #depth ⇒ Object
- #export ⇒ Object
- #import(filepath) ⇒ Object
-
#initialize ⇒ Reader
constructor
A new instance of Reader.
- #md(str) ⇒ Object
- #method_missing(m_id, *args, &block) ⇒ Object
- #raw(str) ⇒ Object
-
#read(*args, &block) ⇒ Object
Feeds instructions, either as a [String] or Rulex instructions (parsed and then interpreted with instance_eval) or as a block (it must be either or).
- #read_rex(str) ⇒ Object
-
#rex_to_ruby(str) ⇒ Object
There are a few characters (‘\’, ‘[’ and ‘]’) that even %q[] escapes.
- #tex(str) ⇒ Object
- #tex_command(name, *params) ⇒ Object
- #tex_environment(name, *args, &block) ⇒ Object
Constructor Details
#initialize ⇒ Reader
Returns a new instance of Reader.
7 8 9 10 11 |
# File 'lib/rulex/rex/reader.rb', line 7 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
110 111 112 113 114 115 116 117 118 |
# File 'lib/rulex/rex/reader.rb', line 110 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
36 37 38 |
# File 'lib/rulex/rex/reader.rb', line 36 def add_node_to_content node @content_stack.last << node end |
#append_nodes_to_content(arr) ⇒ Object
40 41 42 |
# File 'lib/rulex/rex/reader.rb', line 40 def append_nodes_to_content arr @content_stack.last.concat arr end |
#build_tex_command(name, params) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rulex/rex/reader.rb', line 66 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 |
#depth ⇒ Object
94 95 96 |
# File 'lib/rulex/rex/reader.rb', line 94 def depth @content_stack.length - 1 end |
#export ⇒ Object
62 63 64 |
# File 'lib/rulex/rex/reader.rb', line 62 def export @content end |
#import(filepath) ⇒ Object
58 59 60 |
# File 'lib/rulex/rex/reader.rb', line 58 def import filepath read File.open(filepath).read end |
#md(str) ⇒ Object
48 49 50 51 52 |
# File 'lib/rulex/rex/reader.rb', line 48 def md str latex_str = PandocRuby.new(str).to_latex arr = @latex_reader.read(latex_str).to_a append_nodes_to_content arr end |
#raw(str) ⇒ Object
44 45 46 |
# File 'lib/rulex/rex/reader.rb', line 44 def raw str add_node_to_content(type: :text, text: str) end |
#read(*args, &block) ⇒ Object
Feeds instructions, either as a [String] or Rulex instructions (parsed and then interpreted with instance_eval) or as a block (it must be either or). All the functions of Rulex::Rex::Reader are available.
18 19 20 21 22 23 24 25 |
# File 'lib/rulex/rex/reader.rb', line 18 def read *args, &block if args.length == 1 read_rex args.first elsif block instance_eval &block end self end |
#read_rex(str) ⇒ Object
27 28 29 |
# File 'lib/rulex/rex/reader.rb', line 27 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
32 33 34 |
# File 'lib/rulex/rex/reader.rb', line 32 def rex_to_ruby str str.gsub(/<##(((?!##>)[\s\S])+)##>/) { |m| "raw %q[" + $1.gsub("\\","\\\\\\\\") + "]"} end |
#tex(str) ⇒ Object
54 55 56 |
# File 'lib/rulex/rex/reader.rb', line 54 def tex str append_nodes_to_content @latex_reader.read(str).to_a end |
#tex_command(name, *params) ⇒ Object
98 99 100 |
# File 'lib/rulex/rex/reader.rb', line 98 def tex_command(name, *params) add_node_to_content build_tex_command name, *params end |
#tex_environment(name, *args, &block) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/rulex/rex/reader.rb', line 102 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 |