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

Internal parsing line structure

Methods

parse  

Public Instance methods

[Source]

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

            offset = 0
            hash_str_len = 0 # (rane) FIXME: this is not used. what is it for?
            pl = texy.line_patterns
            keys = (0..pl.size - 1).to_a
            arr_matches = []
            arr_pos = Array.new(pl.size, -1)

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

                keys.each_with_index do |key, index|
                    next unless key

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


                        if match_data = pl[key][:pattern].match(text[(offset + delta)..-1])
                            next if match_data[0].empty?

                            arr_pos[key] = match_data.begin(0) + offset + delta
                            arr_matches[key] = match_data.to_a
                        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

                break if min_key == -1

                px = pl[min_key]
                offset = arr_pos[min_key]
                replacement = px[:handler].call(self, arr_matches[min_key])
                len = arr_matches[min_key][0].length

                text[offset, len] = replacement

                delta = replacement.length - len

                keys.each do |key|
                    next unless key

                    if arr_pos[key] < offset + len
                        arr_pos[key] = -1
                    else
                        arr_pos[key] += delta
                    end
                end

                arr_pos[min_key] = -2
            end

            text = Html.html_chars(text, false, true)

            texy.modules.each do |mod|
                text = mod.line_post_process(text)
            end

            element.set_content(text, true)

            if element.content_type == DomElement::CONTENT_NONE
                s = text.gsub(/[#{HASH}]+/, '').strip
                unless s.empty?
                    element.content_type = DomElement::CONTENT_TEXTUAL
                end
            end
        end

[Validate]