Sha256: d02f212d389402511c3f59e702f34048b105b7382eaaece59288eafaf749983a

Contents?: true

Size: 1.97 KB

Versions: 11

Compression:

Stored size: 1.97 KB

Contents

module TurnipFormatter
  module Renderer
    module Html
      @script_codes = []
      @script_files = []
      @style_codes = []
      @style_files = []

      class << self
        attr_accessor :script_codes, :script_files, :style_codes, :style_files

        def reset!
          @script_codes = []
          @script_files = []
          @style_codes = []
          @style_files = []
        end

        def project_name
          TurnipFormatter.configuration.title
        end

        def add_javascript(script)
          case
          when local_file?(script)
            script_codes << File.read(script)
          when remote_url?(script)
            script_files << script
          end
        end

        def add_stylesheet(stylesheet)
          case
          when local_file?(stylesheet)
            style_codes << File.read(stylesheet)
          when remote_url?(stylesheet)
            style_files << stylesheet
          end
        end

        def render_javascript_codes
          script_codes.join("\n")
        end

        def render_javascript_links
          script_files.map do |file|
            "<script src=\"#{file}\"></script>"
          end.join("\n")
        end

        def render_stylesheet_codes
          style_codes.join("\n")
        end

        def render_stylesheet_links
          style_files.map do |file|
            "<link rel=\"stylesheet\" href=\"#{file}\">"
          end.join("\n")
        end

        def render_step_template_stylesheet_codes
          TurnipFormatter.step_templates.map do |template|
            template.class.css
          end.join("\n")
        end

        private

        def local_file?(path)
          File.exist? path
        end

        def remote_url?(path)
          uri = URI.parse(path)
          return true if %w(http https).include?(uri.scheme)
          return true if uri.scheme.nil? && path.start_with?('//')
          false
        rescue URI::InvalidURIError
          false
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
turnip_formatter-0.8.0 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.7.2 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.7.1 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.7.0 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.6.1 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.6.0 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.6.0.pre.beta.7 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.6.0.pre.beta.6 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.6.0.pre.beta.5 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.6.0.pre.beta.4 lib/turnip_formatter/renderer/html.rb
turnip_formatter-0.6.0.pre.beta.3 lib/turnip_formatter/renderer/html.rb