Sha256: e0d7777a8c9bd59c20c183128ab34a2633de85a651293f16a497a45d9be73dc0

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module HTML
  class Pipeline
    module RubyMarkup
      # To go through a markdown document. Mainly used to decide if we are in
      # code blocks to decide if we should escape underscored usernames.
      class MarkdownTraverser
        def initialize(markdown)
          @lines = markdown.dup.lines
          init_position
        end

        def each
          lines.each do |line|
            update_position(line.include?(CODEBLOCK_MARKER))
            yield(line)
          end
        end

        def in_codeblock?
          prev == true && current == false
        end

        private

        CODEBLOCK_MARKER = "```".freeze
        private_constant :CODEBLOCK_MARKER

        attr_reader :lines
        attr_accessor :prev, :current

        def init_position
          self.prev = false
          self.current = lines.first.include?(CODEBLOCK_MARKER)
        end

        def update_position(current)
          self.current = current
          self.prev = current ? !prev : prev
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
html-pipeline-ruby_markup-0.9.2 lib/html/pipeline/ruby_markup/markdown_traverser.rb
html-pipeline-ruby_markup-0.9.1 lib/html/pipeline/ruby_markup/markdown_traverser.rb
html-pipeline-ruby_markup-0.9.0 lib/html/pipeline/ruby_markup/markdown_traverser.rb