Module: JadePug

Extended by:
Memoist
Included in:
Jade, Pug
Defined in:
lib/pug-ruby.rb,
lib/jade-pug/base.rb,
lib/jade-pug/config.rb,
lib/jade-pug/compiler.rb,
lib/jade-pug/system-compiler.rb,
lib/jade-pug/shipped-compiler.rb,
lib/jade-pug/errors/compiler-error.rb,
lib/jade-pug/errors/compilation-error.rb

Overview

:nodoc:

Defined Under Namespace

Classes: CompilationError, Compiler, CompilerError, Config, ShippedCompiler, SystemCompiler

Instance Method Summary collapse

Instance Method Details

#compile(source, options = {}) ⇒ String

Compiles the template.

Parameters:

  • source (String, #read)
  • options (Hash) (defaults to: {})

Returns:

  • (String)


16
17
18
# File 'lib/jade-pug/base.rb', line 16

def compile(source, options = {})
  compiler.compile(source, options)
end

#compiler(wanted_version = version) ⇒ JadePug::Compiler

Returns engine compiler for given version. Compilers are cached.

Parameters:

  • wanted_version (String, :system) (defaults to: version)

Returns:



26
27
28
29
30
31
32
33
# 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

#configJadePug::Config

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

Returns:



116
117
118
# File 'lib/jade-pug/base.rb', line 116

def config
  self::Config.new
end

#did_switch_version(version_from, version_to) ⇒ nil

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

Parameters:

  • version_from (String, :system)
  • version_to (String, :system)

Returns:

  • (nil)


68
69
70
71
72
73
74
75
76
77
# 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) ⇒ nil

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

Parameters:

  • *messages (Array<Object>)

Returns:

  • (nil)


127
128
129
# File 'lib/jade-pug/base.rb', line 127

def echo(*messages)
  puts(*messages) unless silence?
end

#runtime_versionsArray<String>

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

Returns:

  • (Array<String>)


95
96
97
98
99
100
# 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) ⇒ true, false

Turns the effect of #echo on or off.

Parameters:

  • silence (true, false)

Returns:

  • (true, false)


136
137
138
# File 'lib/jade-pug/base.rb', line 136

def silence=(silence)
  @silence = !!silence
end

#silence?true, false

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

Returns:

  • (true, false)


145
146
147
# File 'lib/jade-pug/base.rb', line 145

def silence?
  !!@silence
end

#sort_versions(versions) ⇒ Array<String> (private)

Sorts versions in ascending order.

Parameters:

  • versions (Array<String>)

Returns:

  • (Array<String>)

See Also:



175
176
177
# File 'lib/jade-pug/base.rb', line 175

def sort_versions(versions)
  versions.sort_by { |v| Gem::Version.new(v) }
end

#system_versionString

Returns version for system-wide installed engine compiler.

Returns:

  • (String)


107
108
109
# File 'lib/jade-pug/base.rb', line 107

def system_version
  compiler(:system).version
end

#use(wanted_version) ⇒ String, :system

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.

Parameters:

  • wanted_version (String, :system)

Returns:

  • (String, :system)

    Returns the version if no block has been given.

  • Passes through the returned value from the block if it has been given.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

#versionString, :system (private)

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 JadePug::Compiler#version.

Jade.compiler.version => "1.11.0"

Returns:

  • (String, :system)


160
161
162
163
164
165
166
# File 'lib/jade-pug/base.rb', line 160

def version
  if instance_variable_defined?(:@version)
    @version
  else
    :system
  end
end

#versionsArray<String>

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

Returns:

  • (Array<String>)


83
84
85
86
87
88
# 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