Class Texy::BlockParser
In: lib/texy/parser.rb
Parent: Parser

Internal parsing block structure

Methods

Public Instance methods

[Source]

# File lib/texy/parser.rb, line 61
        def move_backward(lines_count = 1)
            @offset -= 1

            while @offset > 0
                if @text[@offset - 1] == ?\n
                    lines_count -= 1

                    break if lines_count < 1
                end

                @offset -= 1
            end

            @offset = [@offset, 0].max
        end

[Source]

# File lib/texy/parser.rb, line 77
        def parse(text)
            # Initialization
            texy = element.texy

            @text = text
            @offset = 0

            pb = texy.block_patterns
            keys = (0..pb.size - 1).to_a
            arr_matches = []
            arr_pos = Array.new(pb.size, -1)

            # Parsing
            while true
                min_key = -1
                min_pos = text.length

                break if @offset >= min_pos

                keys.each_with_index do |key, index|
                    next unless key
                    next unless arr_pos[key]

                    if arr_pos[key] < @offset
                        delta = arr_pos[key] == -2 ? 1 :0

                        # (rane) Take substring of input string to emulate php's preg_match, which has
                        # "offset" parameter. (FIXME: this solution is not completely correct - see
                        # http://www.php.net/manual/en/function.preg-match.php - but perhaps it will work)
                        if match_data = pb[key][:pattern].match(text[(@offset + delta)..-1])
                            arr_matches[key] = match_data.to_a
                            arr_pos[key] = match_data.begin(0) + @offset + delta # (rane) add offset to obtain absolute position
                        else
                            keys[index] = nil
                            next
                        end
                    end

                    if arr_pos[key] == @offset
                        min_key = key
                        break
                    end

                    if arr_pos[key] < min_pos
                        min_pos = arr_pos[key]
                        min_key = key
                    end
                end

                next_offset = (min_key == -1) ? text.length : arr_pos[min_key]

                if next_offset > @offset
                    string = text[@offset, next_offset - @offset]
                    @offset = next_offset

                    texy.generic_block_module.process_block(self, string)
                    next
                end

                matches = arr_matches[min_key]

                @offset = arr_pos[min_key] + matches[0].length + 1 # 1 = \n

                ok = pb[min_key][:handler].call(self, matches)

                if ok == false || @offset <= arr_pos[min_key] # module rejects text
                    @offset = arr_pos[min_key] # turn offset back
                    arr_pos[min_key] = -2
                    next
                end

                arr_pos[min_key] = -1
            end
        end

Match current line against RE.

If succesfull, increments current position and returns matched data.

[Source]

# File lib/texy/parser.rb, line 52
        def receive_next(pattern)
            if match_data = pattern.match(@text[@offset..-1])
                @offset += match_data[0].length + 1 # 1 = "\n"
                match_data.to_a
            else
                nil
            end
        end

[Validate]