Sha256: 21d8d69cc52d5668e2c648997231e04dd653644dfb59fc29fd85ea7d8ede26ea

Contents?: true

Size: 1.83 KB

Versions: 11

Compression:

Stored size: 1.83 KB

Contents

# -*- coding: utf-8; frozen_string_literal: true -*-
#
#--
# Copyright (C) 2009-2019 Thomas Leitner <t_leitner@gmx.at>
#
# This file is part of kramdown which is licensed under the MIT.
#++
#

module Kramdown
  module Parser
    class Kramdown

      EMPHASIS_START = /(?:\*\*?|__?)/

      # Parse the emphasis at the current location.
      def parse_emphasis
        start_line_number = @src.current_line_number
        saved_pos = @src.save_pos

        result = @src.scan(EMPHASIS_START)
        element = (result.length == 2 ? :strong : :em)
        type = result[0..0]

        if (type == '_' && @src.pre_match =~ /[[:alpha:]]-?[[:alpha:]]*\z/) || @src.check(/\s/) ||
            @tree.type == element || @stack.any? {|el, _| el.type == element }
          add_text(result)
          return
        end

        sub_parse = lambda do |delim, elem|
          el = Element.new(elem, nil, nil, location: start_line_number)
          stop_re = /#{Regexp.escape(delim)}/
          found = parse_spans(el, stop_re) do
            (@src.pre_match[-1, 1] !~ /\s/) &&
              (elem != :em || !@src.match?(/#{Regexp.escape(delim * 2)}(?!#{Regexp.escape(delim)})/)) &&
              (type != '_' || !@src.match?(/#{Regexp.escape(delim)}[[:alnum:]]/)) && !el.children.empty?
          end
          [found, el, stop_re]
        end

        found, el, stop_re = sub_parse.call(result, element)
        if !found && element == :strong && @tree.type != :em
          @src.revert_pos(saved_pos)
          @src.pos += 1
          found, el, stop_re = sub_parse.call(type, :em)
        end
        if found
          @src.scan(stop_re)
          @tree.children << el
        else
          @src.revert_pos(saved_pos)
          @src.pos += result.length
          add_text(result)
        end
      end
      define_parser(:emphasis, EMPHASIS_START, '\*|_')

    end
  end
end

Version data entries

11 entries across 11 versions & 4 rubygems

Version Path
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/kramdown-2.4.0/lib/kramdown/parser/kramdown/emphasis.rb
kramdown-2.4.0 lib/kramdown/parser/kramdown/emphasis.rb
kramdown-2.3.2 lib/kramdown/parser/kramdown/emphasis.rb
kramdown-2.3.1 lib/kramdown/parser/kramdown/emphasis.rb
daqing_kramdown-2.3.2 lib/kramdown/parser/kramdown/emphasis.rb
daqing_kramdown-2.3.1 lib/kramdown/parser/kramdown/emphasis.rb
zine_brewer-1.5.0 vendor/bundle/ruby/2.7.0/gems/kramdown-2.3.0/lib/kramdown/parser/kramdown/emphasis.rb
zine_brewer-1.3.0 vendor/bundle/ruby/2.7.0/gems/kramdown-2.3.0/lib/kramdown/parser/kramdown/emphasis.rb
kramdown-2.3.0 lib/kramdown/parser/kramdown/emphasis.rb
kramdown-2.2.1 lib/kramdown/parser/kramdown/emphasis.rb
kramdown-2.2.0 lib/kramdown/parser/kramdown/emphasis.rb