Module: Uki

Defined in:
lib/uki.rb,
lib/uki/server.rb,
lib/uki/builder.rb,
lib/uki/include_js.rb

Defined Under Namespace

Classes: Builder, Project, Server

Constant Summary collapse

INCLUDE_REGEXP =
%r{((?:^|\n)[^\n]\W|^|\n)\s*include\s*\(\s*['"]([^"']+)["']\s*\)(?:\s*;)?(.*?\r?\n|$)}
INCLUDE_CSS_REGEXP =
%r{include_css\s*\(\s*['"]([^"']+)["']\s*\)}

Class Method Summary collapse

Class Method Details

.append_include(path, include_path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/uki/include_js.rb', line 54

def self.append_include path, include_path
  count = extract_includes(path).size
  code = File.read(path)
  line_break = code.match(/\r\n/) ? "\r\n" : "\n"
  code = code.gsub(INCLUDE_REGEXP) do |match|
    next if $1.include? '//' 
    if (count -= 1) > 0
      match
    else
      ($3.length ? '' : line_break) + match + "include('#{include_path}');" + line_break
    end
  end
  File.open(path, 'w') { |f| f.write(code) }
end

.extract_includes(path) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/uki/include_js.rb', line 46

def self.extract_includes path
  result = []
  File.read(path).scan(INCLUDE_REGEXP) do |match|
    result << $2 unless $1.include? '//' 
  end
  result
end

.include_css(path, &block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/uki/include_js.rb', line 37

def self.include_css path, &block
  code = if block_given? 
    yield path 
  else 
    File.read(path) 
  end
  (code || '').to_json
end

.include_js(path, included = {}, stack = [], &block) ⇒ Object

Preprocesses include() calls in js files

Raises:

  • (Exception)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/uki/include_js.rb', line 10

def self.include_js path, included = {}, stack = [], &block
  raise Exception.new("File #{path} not found.\nStack: #{stack.join(' -> ')}") unless File.exists?(path)
  path = File.expand_path path
  base = File.dirname(path)
  code = if block_given? 
    yield path 
  else 
    File.read(path) 
  end
  
  included[path] = true
  code.gsub(INCLUDE_REGEXP) do |match|
    if $1.include? '//' # include commented out 
      match
    else
      include_path = File.expand_path File.join(base, $2)
      unless included[include_path]
        $1 + include_js(include_path, included, stack + [include_path], &block) + $3
      else
        $1 + $3
      end
    end
  end.gsub(INCLUDE_CSS_REGEXP) do |match|
    include_css File.join(base, $1), &block
  end
end