silly_templates 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/silly +14 -0
- data/lib/silly.rb +105 -0
- metadata +48 -0
data/bin/silly
ADDED
data/lib/silly.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
# Generates silly and simplistic erlang module templates
|
2
|
+
# from a very small subset of the moustache templating language.
|
3
|
+
# In fact, the only thing that is supported, are the moustaches:
|
4
|
+
#
|
5
|
+
# {{ parameter_name }}
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# Write your template files and save them as .tmpl in a folder
|
10
|
+
# of your choice.
|
11
|
+
#
|
12
|
+
# Run:
|
13
|
+
#
|
14
|
+
# Silly.new.transform
|
15
|
+
# :template_dir => "PATH_TO_TEMPLATES",
|
16
|
+
# :output_dir => "src"
|
17
|
+
#
|
18
|
+
# This will transform your tempaltes into Erlang files.
|
19
|
+
# Now why use this rather than, say, erlydtl or the erlang
|
20
|
+
# implementation of moustache?
|
21
|
+
# The only reason I can think of is if you want to use dialyzer.
|
22
|
+
# The template generators output beam files without debug_info,
|
23
|
+
# and since the source is not existant, you cannot do proper dialyzation!
|
24
|
+
#
|
25
|
+
# Happy templating!
|
26
|
+
|
27
|
+
class Silly
|
28
|
+
def transform options
|
29
|
+
templates = Dir.entries(options[:template_dir]).select {|r| r =~ /\.tmpl$/}
|
30
|
+
templates.each do |t|
|
31
|
+
t =~ /(.*)\.tmpl$/
|
32
|
+
name = $1
|
33
|
+
parsed = parse(read("#{options[:template_dir]}/#{t}"))
|
34
|
+
file =<<EOF
|
35
|
+
%% Warning! This is an automatically generated file.
|
36
|
+
%% Do not edit it, please! Instead edit #{t}.
|
37
|
+
|
38
|
+
-module(#{name}_silly).
|
39
|
+
-export([render/1]).
|
40
|
+
#{gen_types parsed}
|
41
|
+
#{gen_function parsed}
|
42
|
+
EOF
|
43
|
+
path = "#{options[:output_dir]}/#{name}_silly.erl"
|
44
|
+
File.open(path, "w") do |f|
|
45
|
+
f.write file
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def read file
|
51
|
+
File.read(file)
|
52
|
+
end
|
53
|
+
|
54
|
+
def parse text
|
55
|
+
texts = []
|
56
|
+
params = []
|
57
|
+
while count = text =~ /(\{\{\s*([a-zA-Z0-9_-]*)\s*\}\})/ do
|
58
|
+
texts << text[0...count]
|
59
|
+
text = text[(count + $1.size)..-1]
|
60
|
+
params << $2.to_sym
|
61
|
+
end
|
62
|
+
texts << text
|
63
|
+
|
64
|
+
{
|
65
|
+
:params => params,
|
66
|
+
:texts => texts
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
def gen_types parsed
|
71
|
+
individual_types = (parsed[:params].map {|t| "-type #{t}() :: {#{t}, binary()}."}).join("\n")
|
72
|
+
input_type = "-type silly_input() :: #{(parsed[:params].map {|t| "#{t}()"}).join(" | ")}."
|
73
|
+
"#{individual_types}\n#{input_type}\n"
|
74
|
+
end
|
75
|
+
|
76
|
+
def escape text
|
77
|
+
text.gsub(/\n/, "\\n").gsub(/"/, "\\\"")
|
78
|
+
end
|
79
|
+
|
80
|
+
def gen_function parsed
|
81
|
+
params =<<EOF
|
82
|
+
-spec get_param(atom(), [silly_input()]) -> binary().
|
83
|
+
get_param(Param, Inputs) ->
|
84
|
+
case proplists:get_value(Param, Inputs, undefined) of
|
85
|
+
undefined -> throw(list_to_binary("SillyTemplates requires parameter " ++ atom_to_list(Param)));
|
86
|
+
P -> P
|
87
|
+
end.
|
88
|
+
EOF
|
89
|
+
|
90
|
+
output = []
|
91
|
+
parsed[:texts].zip(parsed[:params]).each do |text, param|
|
92
|
+
out = " <<\"#{escape(text)}\">>"
|
93
|
+
out += ",\n get_param(#{param.to_s}, Inputs)," if param
|
94
|
+
output << out
|
95
|
+
end
|
96
|
+
<<EOF
|
97
|
+
#{params}
|
98
|
+
-spec render([silly_input()]) -> binary().
|
99
|
+
render(Inputs) ->
|
100
|
+
list_to_binary([
|
101
|
+
#{output.join("\n")}
|
102
|
+
]).
|
103
|
+
EOF
|
104
|
+
end
|
105
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: silly_templates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sebastian Probst Eide
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-31 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Converts a subset of the moustache templating language into Erlang template
|
15
|
+
modules. See https://github.com/Aircloak/silly-templates
|
16
|
+
email: sebastian@aircloak.com
|
17
|
+
executables:
|
18
|
+
- silly
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/silly.rb
|
23
|
+
- bin/silly
|
24
|
+
homepage: https://github.com/Aircloak/silly-templates
|
25
|
+
licenses: []
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.10
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: Generates Erlang template modules.
|
48
|
+
test_files: []
|