module JadePug

This module contains common thing related Jade and Pug.

Public Instance Methods

compile(source, options = {}) click to toggle source

Compiles the template.

@param source [String, read] @param options [Hash] @return [String]

# File lib/jade-pug/base.rb, line 16
def compile(source, options = {})
  compiler.compile(source, options)
end
compiler(wanted_version = version) click to toggle source

Returns engine compiler for given version. Compilers are cached.

@param wanted_version [String, :system] @return [JadePug::Compiler]

# File lib/jade-pug/base.rb, line 26
def compiler(wanted_version = version)
  (@compilers ||= {})["#{ name }-#{ wanted_version }"] ||= begin
    case wanted_version
      when :system then self::SystemCompiler.new
      else              self::ShippedCompiler.new(wanted_version)
    end
  end
end
config() click to toggle source

Returns config object for engine. Executed only once per engine (return value is memoized).

@return [JadePug::Config]

# File lib/jade-pug/base.rb, line 116
def config
  self::Config.new
end
did_switch_version(version_from, version_to) click to toggle source

Executed after compiler version switched. Outputs some useful information about version being used.

@param version_from [String, :system] @param version_to [String, :system] @return [nil]

# File lib/jade-pug/base.rb, line 68
def did_switch_version(version_from, version_to)
  if version_from != version_to
    if Symbol === version_to
      echo "Using #{ version_to } #{ name }."
    else
      echo "Using #{ name } #{ version_to }. NOTE: Advanced features like includes, extends and blocks will not work."
    end
  end
  nil
end
echo(*messages) click to toggle source

Prints messages. By default messages are sent to the STDOUT by using {Kernel#puts}.

@param *messages [Array<Object>] @return [nil]

# File lib/jade-pug/base.rb, line 127
def echo(*messages)
  puts(*messages) unless silence?
end
runtime_versions() click to toggle source

Returns the list of all available engine runtime versions shipped with the gem.

@return [Array<String>]

# File lib/jade-pug/base.rb, line 95
def runtime_versions
  sort_versions(Dir[File.expand_path("../../../vendor/#{ name.downcase }-*.js", __FILE__)].map do |path|
    match = File.basename(path).match(/\A#{name.downcase}-runtime-(?<v>.+)\.js\z/)
    match[:v] if match
  end.compact)
end
silence=(silence) click to toggle source

Turns the effect of {#echo} on or off.

@param silence [true, false] @return [true, false]

# File lib/jade-pug/base.rb, line 136
def silence=(silence)
  @silence = !!silence
end
silence?() click to toggle source

Returns true if {#echo} should print messages. Otherwise returns false.

@return [true, false]

# File lib/jade-pug/base.rb, line 145
def silence?
  !!@silence
end
system_version() click to toggle source

Returns version for system-wide installed engine compiler.

@return [String]

# File lib/jade-pug/base.rb, line 107
def system_version
  compiler(:system).version
end
use(wanted_version) { || ... } click to toggle source

Switches compiler version.

  • If you want to switch compiler to one of that shipped with gem simple pass version as a string.

  • If you want to switch compiler to system pass :system as a version.

Pass block to temporarily switch the version. Without block the version is switched permanently.

@param wanted_version [String, :system] @return [String, :system] Returns the version if no block has been given. @return Passes through the returned value from the block if it has been given.

# File lib/jade-pug/base.rb, line 46
def use(wanted_version)
  previous_version = version
  @version         = wanted_version
  did_switch_version(previous_version, wanted_version)

  return @version unless block_given?

  begin
    yield
  ensure
    @version = previous_version
    did_switch_version(wanted_version, previous_version)
  end
end
versions() click to toggle source

Returns the list of all available engine compiler versions shipped with the gem.

@return [Array<String>]

# File lib/jade-pug/base.rb, line 83
def versions
  sort_versions(Dir[File.expand_path("../../../vendor/#{ name.downcase }-*.js", __FILE__)].map do |path|
    match = File.basename(path).match(/\A#{name.downcase}-(?!runtime-)(?<v>.+)\.min\.js\z/)
    match[:v] if match
  end.compact)
end

Private Instance Methods

sort_versions(versions) click to toggle source

Sorts versions in ascending order. @see {stackoverflow.com/a/33290373/2369428}

@param versions [Array<String>] @return [Array<String>]

# File lib/jade-pug/base.rb, line 175
def sort_versions(versions)
  versions.sort_by { |v| Gem::Version.new(v) }
end
version() click to toggle source

Returns version of currently used engine compiler. If no version has been set returns :system.

Only for internal usage.

To get the actual version of engine compiler refer to {Compiler#version}.

Jade.compiler.version => "1.11.0"

@return [String, :system]

# File lib/jade-pug/base.rb, line 160
def version
  if instance_variable_defined?(:@version)
    @version
  else
    :system
  end
end