Class: Rulex::Rex::Reader
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(str) ⇒ Object
TODO.
-
#import_content(content) ⇒ Object
Reads some content.
-
#import_file(filepath) ⇒ Object
Reads a file, given a filepath.
-
#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
- #tex(str) ⇒ Object
- #tex_command(name, *params) ⇒ Object
- #tex_environment(name, *args, &block) ⇒ Object
Constructor Details
#initialize ⇒ Reader
Returns a new instance of Reader.
24 25 26 27 28 29 30 31 32 |
# File 'lib/rulex/rex/reader.rb', line 24 def initialize @content = [] @content_stack = [@content] @latex_reader = Rulex::Tex::Reader.new @delimiters = { raw: { open: "<##", close: "##>"}, tex: { open: "<(#", close: "#)>"} } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m_id, *args, &block) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/rulex/rex/reader.rb', line 171 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
68 69 70 |
# File 'lib/rulex/rex/reader.rb', line 68 def add_node_to_content node @content_stack.last << node end |
#append_nodes_to_content(arr) ⇒ Object
72 73 74 |
# File 'lib/rulex/rex/reader.rb', line 72 def append_nodes_to_content arr @content_stack.last.concat arr end |
#build_tex_command(name, params) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rulex/rex/reader.rb', line 127 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
155 156 157 |
# File 'lib/rulex/rex/reader.rb', line 155 def depth @content_stack.length - 1 end |
#export ⇒ Object
123 124 125 |
# File 'lib/rulex/rex/reader.rb', line 123 def export @content end |
#import(str) ⇒ Object
TODO
92 93 94 |
# File 'lib/rulex/rex/reader.rb', line 92 def import str import_file str end |
#import_content(content) ⇒ Object
Reads some content
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rulex/rex/reader.rb', line 109 def import_content content if content =~ /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m content = $' data = YAML.load($1).deep_symbolize_keys end content ||= "" data ||= {} if(delimiters = data[:delimiters]) @delimiters.merge! delimiters end read content end |
#import_file(filepath) ⇒ Object
Reads a file, given a filepath
100 101 102 103 |
# File 'lib/rulex/rex/reader.rb', line 100 def import_file filepath content = File.read(filepath) import_content content end |
#md(str) ⇒ Object
80 81 82 83 84 |
# File 'lib/rulex/rex/reader.rb', line 80 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
76 77 78 |
# File 'lib/rulex/rex/reader.rb', line 76 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.
39 40 41 42 43 44 45 46 |
# File 'lib/rulex/rex/reader.rb', line 39 def read *args, &block if args.length == 1 read_rex args.first elsif block instance_eval &block end self end |
#read_rex(str) ⇒ Object
48 49 50 |
# File 'lib/rulex/rex/reader.rb', line 48 def read_rex str instance_eval rex_to_ruby str end |
#rex_to_ruby(str) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rulex/rex/reader.rb', line 52 def rex_to_ruby str @delimiters.inject(str) do |str, d| key = d.first val = d[1] open_delimiter = Regexp.escape val[:open] close_delimiter = Regexp.escape val[:close] regexp = /#{open_delimiter}(((?!#{close_delimiter})[\s\S])+)#{close_delimiter}/ # There are a few characters ('\\', '\[' and '\]') that even %q[] "un"-escapes, # hence the second gsub str.gsub(regexp) { |m| "#{key} %q[" + $1.gsub("\\","\\\\\\\\") + "]"} end end |
#tex(str) ⇒ Object
86 87 88 |
# File 'lib/rulex/rex/reader.rb', line 86 def tex str append_nodes_to_content @latex_reader.read(str).to_a end |
#tex_command(name, *params) ⇒ Object
159 160 161 |
# File 'lib/rulex/rex/reader.rb', line 159 def tex_command(name, *params) add_node_to_content build_tex_command name, *params end |
#tex_environment(name, *args, &block) ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/rulex/rex/reader.rb', line 163 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 |