Sha256: 82fb9cd0cdef4e557c86f9aeef95290e7a794f4a9cb8a3f82828dd01d15a65ec

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 KB

Contents

# -*- encoding : utf-8 -*-
module LoyalCore
  class TextUtil
    class << self
      def markdown(text)
        # options = [:hard_wrap, :filter_html, :autolink, :no_intraemphasis, :fenced_code, :gh_blockcode]

        options = [:hard_wrap, :autolink, :no_intraemphasis, :fenced_code, :gh_blockcode]
        self.syntax_highlighter(::RedcarpetCompat.new(text, *options).to_html).html_safe
      end

      def syntax_highlighter(html)
        doc = ::Nokogiri::HTML(html)

        doc.search("//pre").each do |pre|
          lang = pre.attr('lang')

          if lang.blank?
            _lang_class = pre.attr('class').to_s.split(' ').select {|_itm| _itm.include?('lang-') }.first

            if _lang_class
              lang = _lang_class.gsub('lang-', '')
            end
          end

          # debugger
          if (pre_code=pre.css('code')).any?
            lang = pre_code.attr('class').to_s
          end

          if lang.blank?
            lang = :text
          end

          text = pre.text.rstrip

          begin
            pre.replace ::CodeRay.scan(text, lang).div.to_s
          rescue Exception => error
            Rails.logger.debug "#{__FILE__} syntax_highlighter error: \ntext => #{text} \nlang => #{lang}\n origin error:#{error}"
          end
        end

        doc.css('body').try(:inner_html).to_s
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
loyal_core-0.0.10 lib/loyal_core/utils/text_util.rb
loyal_core-0.0.8 lib/loyal_core/utils/text_util.rb
loyal_core-0.0.7 lib/loyal_core/utils/text_util.rb
loyal_core-0.0.6 lib/loyal_core/utils/text_util.rb
loyal_core-0.0.5 lib/loyal_core/utils/text_util.rb
loyal_core-0.0.4 lib/loyal_core/utils/text_util.rb
loyal_core-0.0.3 lib/loyal_core/utils/text_util.rb
loyal_core-0.0.2 lib/loyal_core/utils/text_util.rb
loyal_core-0.0.1 lib/loyal_core/utils/text_util.rb