filestojs 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +29 -0
- data/lib/FilesToJS.rb +39 -0
- metadata +50 -0
data/README.rdoc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
= jscat
|
2
|
+
|
3
|
+
FilesToJs will take a directory of text files and formats them as string values in a global javascript object.
|
4
|
+
|
5
|
+
FilesToJs will take a directory of text files and formats them as string values in a global javascript object. An advantage of this is the ability to use the same directory of templates for both back end and front end rendering, and have all your templates in a single compressed transfer.
|
6
|
+
|
7
|
+
Mustache Template example:
|
8
|
+
|
9
|
+
template_joiner = TemplatesToJs.new({
|
10
|
+
:template_dir => 'templates', #Defaults to "templates"
|
11
|
+
:template_format => 'mustache', #Defaults to "*"
|
12
|
+
:js_object_name => 'templates', #Defaults to "files"
|
13
|
+
:output => 'output/mytemplates.js' #Path to write .js file, Defaults to false
|
14
|
+
})
|
15
|
+
template_joiner.write_js
|
16
|
+
puts template_joiner.js
|
17
|
+
|
18
|
+
|
19
|
+
Returns (javascript)
|
20
|
+
|
21
|
+
files = {
|
22
|
+
'file 1': 'file contents',
|
23
|
+
'file 2': 'file 2 contents',
|
24
|
+
}
|
25
|
+
|
26
|
+
== Copyright
|
27
|
+
|
28
|
+
Copyright (c) 2011 Daniel Sim
|
29
|
+
|
data/lib/FilesToJS.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class FilesToJs
|
2
|
+
attr_reader :js
|
3
|
+
def initialize(params = {})
|
4
|
+
@file_dir = params[:file_dir] || "templates"
|
5
|
+
@file_format = params[:file_format] || false
|
6
|
+
@js_object_name = params[:js_object_name] || "files"
|
7
|
+
@output = params[:output] || false
|
8
|
+
end
|
9
|
+
def list_files
|
10
|
+
if @file_format then
|
11
|
+
@files = Dir.glob("#{@file_dir}/*.#{@file_format}")
|
12
|
+
else
|
13
|
+
@files = Dir.glob("#{@file_dir}/*.*")
|
14
|
+
end
|
15
|
+
return @files
|
16
|
+
end
|
17
|
+
def render_js
|
18
|
+
@js = "#{@js_object_name} = {};"
|
19
|
+
list_files.each { |file_path|
|
20
|
+
file_filename = file_path.split('/').last.split('.').first
|
21
|
+
file_string = ""
|
22
|
+
File.open("#{file_path}",'r') do |f1|
|
23
|
+
while line = f1.gets
|
24
|
+
file_string = file_string + line.gsub('\'','"').strip
|
25
|
+
end
|
26
|
+
end
|
27
|
+
@js = @js + "#{@js_object_name}['#{file_filename}']='#{file_string}';"
|
28
|
+
}
|
29
|
+
return @js
|
30
|
+
end
|
31
|
+
def write_js
|
32
|
+
render_js unless @js
|
33
|
+
if @output then
|
34
|
+
File.open(@output,'w') { |f|
|
35
|
+
f.write(@js)
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filestojs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Sim
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-28 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Takes a directory of text files and formats them as string values in
|
15
|
+
a global javascript object.
|
16
|
+
email: dan@explodingbox.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README.rdoc
|
22
|
+
- lib/FilesToJS.rb
|
23
|
+
homepage: https://github.com/danielsim/FilesToJS
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.10
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: FilesToJs will take a directory of text files and formats them as string
|
47
|
+
values in a global javascript object. An advantage of this is the ability to use
|
48
|
+
the same directory of templates for both back end and front end rendering, and have
|
49
|
+
all your templates in a single compressed transfer.
|
50
|
+
test_files: []
|