Sha256: 898abc18d8cc7c94d624433c4b28b9b445ee4f71e7d29210bfed0428e3ca82f1

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require_relative "element"
require_relative "markdown_traverser"

module HTML
  class Pipeline
    module RubyMarkup
      class Transformer
        def self.run(content)
          new(content).run
        end

        def initialize(content)
          @content = content
        end

        def run
          transform_ruby_markups!
        end

        private

        # Matching [漢字(かんじ)] or [漢字(かんじ)](url)
        RubyTagPattern = %r(
          (?<!!)
          \[
            (?<word>[^\[\(]+(?=\())(?<!\s)
            \((?<reading>[^\)]+(?=\)))\)
          \]
          (\((?<uri>[^\)]+(?=\)))\))*
        )x
        RubyMarkupInsideCodePattern = /`.*#{RubyTagPattern}.*`/

        attr_reader :content

        def traverser
          @traverser ||= content.empty? ? [] : MarkdownTraverser.new(content)
        end

        def transform_ruby_markups!
          traverser.each do |line|
            next if traverser.in_codeblock?
            next if line.match?(RubyMarkupInsideCodePattern)

            matches = line.scan(RubyTagPattern)

            if !matches.empty?
              matches.each do |word, reading, uri|
                ruby_element = Element.new(word, reading, uri)
                line.sub!(ruby_element.original, ruby_element.to_html)
              end
            end
          end.join
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
html-pipeline-ruby_markup-0.9.2 lib/html/pipeline/ruby_markup/transformer.rb