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]*[^\w\n])include\s*\(\s*['"]([^"']+)["']\s*\)(?:\s*;)?(.*?\r?\n|$)}
- INCLUDE_TEXT_REGEXP =
%r{include_(?:text|css)\s*\(\s*['"]([^"']+)["']\s*\)}
- BACKGROUND_URL_REGEXP =
%r{url\s*\(\s*['"]?([^"'\)]+)["']?\s*\)}
- DATA_URI_REGEXP =
%r{/\*!\s*data:([^\s\*]*)\s*\*/['"]?([^"']+)['"]}
Class Method Summary
collapse
Class Method Details
.append_include(path, include_path) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/uki/include_js.rb', line 84
def self.append_include path, include_path
count = (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
|
76
77
78
79
80
81
82
|
# File 'lib/uki/include_js.rb', line 76
def self. path
result = []
File.read(path).scan(INCLUDE_REGEXP) do |match|
result << $2 unless $1.include? '//'
end
result
end
|
.include_js(path, included = {}, stack = [], &block) ⇒ Object
Preprocesses include() calls in js files
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/uki/include_js.rb', line 14
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_TEXT_REGEXP) do |match|
include_text File.join(base, $1), &block
end.gsub(DATA_URI_REGEXP) do |match|
uri = to_data_uri File.join(base, $2), $1
"'#{uri}'"
end.gsub(INCLUDE_REGEXP) do |match|
if $1.include? '//' 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
end
|
.include_text(path, &block) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/uki/include_js.rb', line 60
def self.include_text path, &block
code = if block_given?
yield path
else
File.read(path).gsub(BACKGROUND_URL_REGEXP) do |match|
url = $1;
unless url =~ %r{^(/|\w+://)}
filename = File.join(File.dirname(path), url);
url = self.to_data_uri(filename)
end
"url(#{url})";
end
end
(code || '').to_json
end
|
.to_data_uri(path, type = nil) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/uki/include_js.rb', line 46
def self.to_data_uri path, type = nil
if !type || type.empty?
ext = File.extname(path)
if ext == '.png'
type = 'image/png'
elsif ext == '.gif'
type = 'image/gif'
else
type = 'text/plain'
end
end
"data:#{type};base64," + Base64.encode64(File.read(path)).gsub("\n", '')
end
|