Class: Bayeux
- Inherits:
-
Object
- Object
- Bayeux
- Defined in:
- lib/bayeux/bayeux.rb
Instance Attribute Summary collapse
-
#block_forest ⇒ Object
readonly
Accessors.
Class Method Summary collapse
Instance Method Summary collapse
-
#content_string(first, last) ⇒ Object
Helper functions.
-
#parse(content) ⇒ Object
Start parsing.
- #to_json(*a) ⇒ Object
-
#to_s ⇒ Object
Generators: Convinience functions taking an AST (Abstract Syntax Tree), and returning a document of the specified type.
Instance Attribute Details
#block_forest ⇒ Object (readonly)
Accessors
338 339 340 |
# File 'lib/bayeux/bayeux.rb', line 338 def block_forest @block_forest end |
Class Method Details
.json_create(json_hash) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/bayeux/bayeux.rb', line 322 def self.json_create(json_hash) begin require 'json' syntax_tree = new(json_hash["block_forest"]) return syntax_tree rescue LoadError warn "The JSON gem couldn't be loaded, and so the JSON representation could not be generated" end end |
Instance Method Details
#content_string(first, last) ⇒ Object
Helper functions
344 345 346 347 348 349 350 |
# File 'lib/bayeux/bayeux.rb', line 344 def content_string(first, last) unless first.nil? or last.nil? then return @content[first, (last - first)] else return "" end end |
#parse(content) ⇒ Object
Start parsing
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 47 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/bayeux/bayeux.rb', line 11 def parse(content) # Set-up the logger @parse_log = Logger.new('bayex_parser') @parse_log.outputters = Outputter.stdout # Save the content for later @content = content # Final string to return to the caller pandoc_str = String.new # Forest containing the paragraph level block trees @block_forest = Array.new # Index of the paragraph blocks @block_index = 0 # Indicies into the content, denoting the current limits of # parsing from_index = 0 to_index = content.length ## Procedure for adding a node to the tree new_node = ->(node_type = :none, node_contents = ""){ @block_index += 100 @parse_log.info {"creating node #{@block_index}"} return Tree::TreeNode.new(@block_index, ParaBlock.new(node_type, node_contents)) } # Chunk the content into paragraph blocks while from_index < to_index do # Look for the next space, and the next next '[' character # which should tell us what type of paragraph we are dealing with space_index = content.index(' ', from_index) left_sb_index = content.index(']', from_index) block_start = content.index('[', from_index) if block_start.nil? then # It's a paragraph unless space_index.nil? then block = ParaBlock.new(:paragraph, "") block_start = from_index else # No more blocks break end elsif (left_sb_index < space_index) and (left_sb_index > block_start) then # This should be a block start marker orig_type_length = (left_sb_index - 1) - block_start block = ParaBlock.from_s(content[block_start + 1, orig_type_length]) elsif space_index > block_start then # This should be an annotated block orig_type_length = space_index - (block_start + 1) block = ParaBlock.from_s(content[block_start + 1, orig_type_length]) elsif space_index < block_start then # It's a paragraph unless space_index.nil? then block = ParaBlock.new(:paragraph, "") block_start = from_index else @parse_log.error {"Cannot find a valid block (syntax error in the start of block characters)"} return nil end end # Now we know the type of block, and where it starts, work out # where the end of the block is case block.type when :h1,:h2,:h3,:h4,:h5,:h6,:bib # Look for a terminating ']' block_end = left_sb_index # Add the node, and paragraph sub_tree, to the block forest block.content = content_string(block_start + 4, block_end) para_node = Tree::TreeNode.new(@block_index, block) para_subtree(para_node) @block_forest << para_node when :paragraph # Look for a single new line, containing only whitespace block_end = content.index(/^\s/, block_start) # Add a node to the block forest block.content = content_string(block_start, block_end) para_node = Tree::TreeNode.new(@block_index, block) para_subtree(para_node) @block_forest << para_node when :medskip # Self-terminating block_end = block_start + 9 # Add a node to the block forest para_node = Tree::TreeNode.new(@block_index, block) @block_forest << para_node when :command, :output, :file # Look for a terminating '[end]' block_end = content.index('[end]', block_start) content_index = block_start + (block.orig_type_length + 2) # Add a node to the block forest, and do no further processing block.content = content_string(content_index, block_end) para_node = Tree::TreeNode.new(@block_index, block) @block_forest << para_node # Skip the 'end' block_end += 5 when :code # Look for a terminating '[end]' block_end = content.index('[end]', block_start) content_index = block_start + (block.orig_type_length + 2) # Look for arguments to the block unless content[from_index + 1] == ']' then # Find the end of the arguments arg_end = content.index(/]\s/, from_index) arguments = content[from_index + 6, (arg_end - (from_index + 6))] # Create the parent node for this content code_start = content.index(/^/, arg_end) block.content = content_string(code_start, block_end) para_node = Tree::TreeNode.new(@block_index, block) # Split the item items = arguments.split('|') # Create a node for the description title unless items[0].nil? then para_node << new_node.call(:code_language, items[0].strip) else para_node << new_node.call(:code_language, "") end # Create the description text unless items[1].nil? then para_node << new_node.call(:code_start_number, items[1].strip) else para_node << new_node.call(:code_start_number, "0") end else # Create the parent node for this content block.content = content_string(content_index, block_end) para_node = Tree::TreeNode.new(@block_index, block) para_node << new_node.call(:code_language, "") para_node << new_node.call(:code_start_number, "0") end # Add a node to the block forest, and do no further processing @block_forest << para_node # Skip the 'end' block_end += 5 when :block_quote, :figure, :note, :quote # Look for a terminating '[end]' block_end = content.index('[end]', block_start) content_index = block_start + (block.orig_type_length + 2) # Add a node to the block forest block.content = content_string(content_index, block_end) para_node = Tree::TreeNode.new(@block_index, block) para_subtree(para_node) @block_forest << para_node # Skip the 'end' block_end += 5 when :dl # Look for a terminating '[end]' block_end = content.index('[end]', block_start) ## Description List blocks are really two blocks, so should be ## parsed as such. First we look for (and parse) the description ## header, then we look for (and parse) the description body. We ## keep doing this until we run out of things to do. # Create the master node for the list dl_node = new_node.call(:dl) # Get the list contents content_index = block_start + (block.orig_type_length + 2) list_content = content_string(content_index, block_end) # First break the list into components list_items = list_content.split(/^\n/) # Process each list item list_items.each{|list_item| # Break the list item into a description header and # description body break_at = list_item.index(/:$/) item_index = list_item.index('[item') item_end = list_item.rindex(']') description_header = list_item[item_index + 5, break_at - 5] description_body = list_item[break_at + 1, (item_end - break_at - 1)] # Parse the description header header_node = new_node.call(:dl_header, description_header) para_subtree(header_node) dl_node << header_node # Parse the description body body_node = new_node.call(:dl_text, description_body) para_subtree(body_node) dl_node << body_node } # Add the node to the block forest @block_forest << dl_node # Skip the 'end' block_end += 5 when :ol, :ul # Lists are special because they can be nested. Hence we # have to be a bit careful about the end marker. block_end = content.index(/\[end\]((\n\n)|($\z))/, block_start) if block_end.nil? then @parse_log.error {"Error: Invalid end to a list"} @parse_log.error {"Backtrace: #{to_s}"} return nil end # Add a node to the block forest block.content = content_string(block_start + 4, block_end) para_node = Tree::TreeNode.new(@block_index, block) para_subtree(para_node) @block_forest << para_node # Skip the 'end' block_end += 5 else @parse_log.error {"Error: Unknown block type '#{block.type}'"} @parse_log.error {"Backtrace: #{to_s}"} return nil end @block_index += 100 # Skip spaces unless block_end.nil? then from_index = block_end + 1 until not (content[from_index] =~ /\n/) do from_index += 1 end else return nil end end end |
#to_json(*a) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/bayeux/bayeux.rb', line 306 def to_json(*a) begin require 'json' json_hash = { "block_forest" => @block_forest, JSON.create_id => self.class.name } return json_hash.to_json rescue LoadError warn "The JSON gem couldn't be loaded, and so the JSON representation could not be generated" end end |
#to_s ⇒ Object
Generators: Convinience functions taking an AST (Abstract Syntax Tree),
and returning a document of the specified type
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/bayeux/bayeux.rb', line 288 def to_s return_str = String.new @block_forest.each{|tree| return_str << "Paragraph Type: #{tree.content.type}\n" return_str << "\n" tree.each{|node| return_str << " Node Name: #{node.name}\n" return_str << " Node Type: #{node.content.type}\n" return_str << " Node Depth: #{node.node_depth}\n" node_content = node.content.content.inspect.line_wrap(70,2).indent(17) return_str << " Node Contents: |#{node_content[17, node_content.length]}|\n\n" } } return return_str end |