thot 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a6bb1b17f33806eecd83ba7697b0e66a311772fdf0b368cfc307bddf6c74ee3
4
- data.tar.gz: 12cba548403a8cf9ab2edee54a7a8f4567bd612367b798a3138468c185462b59
3
+ metadata.gz: 57ada5bc80f13298187297034ec1b9c754cdf0cd2313bd5a2674f8bc8ad2f165
4
+ data.tar.gz: '0961751bbb5709516c88958ee50fee4e62c0802a9ce7b0d58c72bf1a2d544b1a'
5
5
  SHA512:
6
- metadata.gz: 73592234300f96931e105eab69a0dfa71b6817e23e12736485c1529b8105af86fe8765a585b80b2ca950a206661e78f8d625433de8c98b9d054b97cb94ac43e4
7
- data.tar.gz: 22236a2db50ad40e55761e24068788a8b17f6356bff00050b6b40263f708c3015979a35e6e4e9c3c4b54341f9a7d4df2088c2b8836dc04822c3051dbc7e5cd39
6
+ metadata.gz: bb39588c702fbc6ba92ffb46cc16dff45401f18bd8e82c68e10639f242b0ff571b3119b0ba3be83b83f212dffd6b354cfa1519a13603bba0a32e50e3da1d379f
7
+ data.tar.gz: a6533192e004e53ea6a9f456a60f11b72fa4e57ff7c9840a19b7e75f7846fa9a8477560f0655f5d2c1ad5d9f3898e4c05c5a52e23124a3f4b9e808a519117782
data/README.md CHANGED
@@ -18,9 +18,150 @@ Or install it yourself as:
18
18
 
19
19
  $ gem install thot
20
20
 
21
+
22
+ ## Principe
23
+
24
+ Thot is a simple templating tool, with :
25
+ - a template including token, like : %%TOKEN_NAME%% => Token MUST be in uppercase
26
+ - a hash of data (symbols as keys) corresponding, like : <pre>{token_name: 'value'}</pre>
27
+ It could generate an output.
28
+
29
+ ### Usecase
30
+
31
+ - with data : <pre>{name: 'Romain'}</pre>
32
+ - and template content : "Hello %%NAME%% !"
33
+
34
+ Thot simply generate :
35
+ 'Hello Romain !'
36
+
37
+ ### Advanced usecase
38
+
39
+ - with data : <pre>{firstname: 'romain', name: 'georges', nickname: 'zaidyur'}</pre>
40
+ - and template content : "Hello %%FIRSTNAME.capitalize%% %%NAME.upcase%% your nickname is : %%NICKNAME.reverse.capitalize%% !"
41
+
42
+ Thot generate :
43
+ "Hello Romain GEORGES your nickname is : Ruydiaz !"
44
+
45
+
46
+ Thot actually supports String to String piped filters :
47
+ - filters must be stacked seperated by '.'
48
+ - filters must be in lowercase
49
+ - filters must be String instance methods returning a String (Modifier)
50
+
51
+ Note : Your could monkey patch String or use Refinment for implementing our own filters.
52
+
53
+
21
54
  ## Usage
22
55
 
23
- TODO: Write usage instructions here
56
+ Thot is already a library for you usage and a CLI.
57
+
58
+ ### Ruby Library usage
59
+
60
+ you could use Thot in your Ruby code :
61
+
62
+ #### Strict mode and accessor input
63
+
64
+ Note : Considering 'template.txt' with : 'Hello %%NAME !!'
65
+ Note : in strict mode if the Tokens in template file don't match exactly the given token list, Thot raise an exception.
66
+
67
+ ```ruby
68
+ require 'thot'
69
+ include Thot
70
+ template = Template::new list_token: [:name] , template_file: './template.txt'
71
+ template.name = 'Romain'
72
+ puts template.output
73
+ ````
74
+
75
+ return
76
+
77
+ Hello Romain !!
78
+
79
+
80
+ #### Strict mode false with accesor input and template_content
81
+
82
+ ```ruby
83
+ require 'thot'
84
+ include Thot
85
+ template = Template::new list_token: [:name, :surname] , template_content: 'Hello %%NAME !!'
86
+ template.name = 'Romain'
87
+ puts template.output
88
+ ````
89
+
90
+ return
91
+
92
+ Hello Romain !!
93
+
94
+ #### Strict mode false with map input and template_content
95
+
96
+ ```ruby
97
+ require 'thot'
98
+ include Thot
99
+ template = Template::new list_token: [:name, :surname] , template_content: 'Hello %%NAME !!'
100
+ template.map {name: 'Romain', surname: 'Georges' }
101
+ puts template.output
102
+ ````
103
+
104
+ return
105
+
106
+ Hello Romain !!
107
+
108
+
109
+
110
+ ### CLI usage
111
+
112
+ Thot come with a CLI for templating :
113
+ - reading from STDIN or list files arguments
114
+ - getting values from variables file by argument [MANDATORY] --env-var-file FILENAME
115
+ - display output on STDOUT
116
+ - verbose mode on STDERR if -v options.
117
+
118
+ Note : CLI work only strict mode false, you could have unused keys in datas.
119
+
120
+ #### Pre-requisites
121
+
122
+ * a file 'template.txt' with : "Hello %%NAME%% !!"
123
+ * a variables file with lines, like :
124
+ ```
125
+ key=value
126
+ key = value
127
+ key = value
128
+ # comments and other lines are ignored
129
+ ```
130
+
131
+ sample, env.test:
132
+
133
+ ```
134
+ name=Romain
135
+ ```
136
+
137
+ In the same path
138
+
139
+ #### STDIN from echo
140
+
141
+ ```
142
+ $ echo "Hello %%NAME%% !!" |thot -e env.test
143
+ ```
144
+
145
+ #### STDIN from input
146
+
147
+ ```
148
+ $ thot -e env.test < template.txt
149
+ ```
150
+
151
+ #### Files list
152
+
153
+ ```
154
+ $ thot -e env.test template1.txt template2.txt
155
+ ```
156
+
157
+ #### Typical usage
158
+
159
+ ```
160
+ $ thot -e env.test < template.txt > output.txt
161
+ ```
162
+
163
+ ###
164
+
24
165
 
25
166
  ## Development
26
167
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/exe/thot ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+
4
+ require 'optparse'
5
+ require 'thot'
6
+ require 'thot/cli'
7
+
8
+ options = {}
9
+ OptionParser.new do |opts|
10
+ opts.banner = "Usage: thot [options]"
11
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
12
+ options[:verbose] = v
13
+ end
14
+ opts.on("-eFILENAME", "--env-var-file=FILENAME", "[MANDATORY] Environment variables file (key=value pairs by line)") do |file|
15
+ options[:env_var_file] = file
16
+ end
17
+ end.parse!
18
+
19
+ list_input = ARGV.dup
20
+ ARGV.clear
21
+
22
+
23
+ include Thot
24
+
25
+ cli = CLI::new options: options, list_templates_file: list_input
26
+ cli.generate
27
+
28
+
29
+
30
+
31
+
32
+
33
+
data/lib/thot/cli.rb ADDED
@@ -0,0 +1,63 @@
1
+ module Thot
2
+ class CLI
3
+
4
+ def initialize(options: , list_templates_file: nil )
5
+ @list_templates_file = list_templates_file
6
+ @options = options
7
+ getting_data
8
+ getting_content
9
+ end
10
+
11
+
12
+ def generate
13
+ template = Template::new(list_token: @data.keys, template_content: @content, strict: false)
14
+ template.map @data
15
+ STDERR.puts "Generating output" if @options[:verbose]
16
+ puts template.output
17
+ end
18
+
19
+ private
20
+
21
+ def getting_data
22
+ if @options[:env_var_file] then
23
+ STDERR.puts "Environment file given : #{@options[:env_var_file]}" if @options[:verbose]
24
+ @data = read_evt_file(@options[:env_var_file])
25
+ else
26
+ raise "Environment variables file argument missing, (--env-var-file) "
27
+ end
28
+ end
29
+
30
+ def getting_content
31
+ @content = ""
32
+ if @list_templates_file.empty?
33
+ STDERR.puts "Reading content from STDIN" if @options[:verbose]
34
+ @content = ARGF.readlines.join
35
+ else
36
+ STDERR.puts "Reading content from file(s) : #{@list_templates_file}" if @options[:verbose]
37
+ @list_templates_file.each do |item|
38
+ if File::exist? item
39
+ @content.concat(File::readlines(item).join)
40
+ else
41
+ raise "file not found #{item}"
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ def read_evt_file(file)
48
+ res = {}
49
+ if File::exist? file
50
+ content = File::readlines(file)
51
+ else
52
+ raise "Environment variables file not found #{file}"
53
+ end
54
+ content.each do |line|
55
+ next if line =~ /#/
56
+ key,value = line.split('=')
57
+ res[key.strip.to_sym] = value.strip if value
58
+ end
59
+ return res
60
+ end
61
+
62
+ end
63
+ end
data/lib/thot.rb CHANGED
@@ -17,16 +17,24 @@ module Thot
17
17
  attr_reader :content
18
18
 
19
19
  # constructor : generate the pseudo accessor for template Class from token list
20
- def initialize(template_file: , list_token: , strict: true)
20
+ def initialize(template_file: nil, list_token: , strict: true, template_content: nil)
21
21
 
22
22
  @result = ""
23
- @template_file = template_file
24
- raise NoTemplateFile::new('No template file found') unless File::exist?(@template_file)
25
- begin
26
- @content = IO::readlines(@template_file).join.chomp
27
- rescue
28
- raise NoTemplateFile::new('No template file found')
23
+ if template_file
24
+ @template_file = template_file
25
+ raise NoTemplateFile::new('No template file found') unless File::exist?(@template_file)
26
+ begin
27
+ @content = IO::readlines(@template_file).join.chomp
28
+ rescue
29
+ raise NoTemplateFile::new('Template file read error')
30
+ end
31
+ elsif template_content
32
+ @content = template_content
33
+ else
34
+ raise NoTemplateFile::new('No template file found or template content')
29
35
  end
36
+
37
+
30
38
  token_from_template = @content.scan(/%%(\w+)%%/).flatten.uniq.map{ |item| item.downcase.to_sym}
31
39
  begin
32
40
  @list_token = list_token
@@ -65,7 +73,6 @@ module Thot
65
73
  raise ArgumentError::new("#{item} : Not a String") unless val.class == String
66
74
  _data[item.to_s.downcase] = val
67
75
  }
68
- raise InvalidTokenList::new("Token list malformation") unless _data.keys.sort == @list_token.map{|_token| _token.to_s }.sort
69
76
  @hash_token = _data
70
77
  end
71
78
 
@@ -81,7 +88,7 @@ module Thot
81
88
  @result = @content
82
89
  @list_token.each{|_token|
83
90
  self.filtering @content.scan(/%%(#{_token.to_s.upcase}[\.\w+]+)%%/).flatten
84
- @result.gsub!(/%%#{_token.to_s.upcase}%%/,@hash_token[_token.to_s])
91
+ @result.gsub!(/%%#{_token.to_s.upcase}%%/,@hash_token[_token.to_s]) if @hash_token.include? _token.to_s
85
92
  }
86
93
  return @result
87
94
  end
data/samples/.env.prod ADDED
@@ -0,0 +1,3 @@
1
+ # test
2
+ name=romain
3
+ surname = georges
@@ -0,0 +1 @@
1
+ Hello %%NAME.capitalize%% %%SURNAME.upcase%% !!
@@ -0,0 +1 @@
1
+ Hello %%NAME.capitalize%% %%SURNAME.upcase%% !!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain GEORGES
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-16 00:00:00.000000000 Z
11
+ date: 2022-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -125,7 +125,8 @@ dependencies:
125
125
  description: the simpliest way to template in Ruby and command
126
126
  email:
127
127
  - gems@ultragreen.net
128
- executables: []
128
+ executables:
129
+ - thot
129
130
  extensions: []
130
131
  extra_rdoc_files: []
131
132
  files:
@@ -139,8 +140,13 @@ files:
139
140
  - VERSION
140
141
  - bin/console
141
142
  - bin/setup
143
+ - exe/thot
142
144
  - lib/thot.rb
145
+ - lib/thot/cli.rb
143
146
  - lib/thot/version.rb
147
+ - samples/.env.prod
148
+ - samples/template.txt
149
+ - samples/template2.txt
144
150
  - thot.gemspec
145
151
  - ultragreen_roodi_coding_convention.yml
146
152
  homepage: https://github.com/Ultragreen/thot
@@ -148,7 +154,7 @@ licenses:
148
154
  - MIT
149
155
  metadata:
150
156
  homepage_uri: https://github.com/Ultragreen/thot
151
- post_install_message:
157
+ post_install_message:
152
158
  rdoc_options: []
153
159
  require_paths:
154
160
  - lib
@@ -163,8 +169,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
169
  - !ruby/object:Gem::Version
164
170
  version: '0'
165
171
  requirements: []
166
- rubygems_version: 3.2.3
167
- signing_key:
172
+ rubygems_version: 3.3.5
173
+ signing_key:
168
174
  specification_version: 4
169
175
  summary: THe Operative Templating
170
176
  test_files: []