drawght 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 776b1ea99f39fc1716543bd837504d10ca2648babb6e283c0bbf0dcc6db10b5f
4
+ data.tar.gz: 0aa2b64aaa564b8194266ac18d37627ae384ba3cf4e39d7ad84a3a5c342ef36c
5
+ SHA512:
6
+ metadata.gz: 73a0b217f9ba7ecca5aaad07a182bc9756714245bd12af06a75b288b781f62bbbb6cce10013dcbcce66cc4e7a3631f62c07ed7ea9a0a50edf4525eb29dd3fd45
7
+ data.tar.gz: 96895069175bf3326d10e4a40f6276120043b650d77211ed00dca259c00f4ddedeaa2d28622c35a70c12e15c02a42db5e4dd1dbda936ae1d2398b3b8c092cf0b
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright © 2021, Hallison Batista
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,211 @@
1
+ # Drawght
2
+
3
+ Drawght is a data handler for texts without logical statements. The goal is
4
+ to use a dataset (such as the subject of a text) to draft a document
5
+ template. It can be considered a mini template processor.
6
+
7
+ Data is accessed through `{}` braces, replaced by their respective values.
8
+
9
+ Considering the following data:
10
+
11
+ ```yaml
12
+ title: Drawght is a very useful sketch
13
+ author:
14
+ name: Hallison Batista
15
+ email: email@hallison.dev.br
16
+ networks:
17
+ - name: Github
18
+ url: //github.com/hallison
19
+ - name: Twitter
20
+ url: //twitter.com/hallison
21
+ creation-date: 2021-06-28
22
+ publishing date: 2021-07-01
23
+ references:
24
+ - name: Mustache
25
+ url: //mustache.github.io
26
+ - name: Handlebars
27
+ url: //handlebarsjs.com
28
+ tags:
29
+ - Template
30
+ - Draft
31
+ ```
32
+
33
+ Note that the `creation-date` and `publishing date` fields are normally
34
+ identified by the parser.
35
+
36
+ In a template written in Markdown:
37
+
38
+ ```markdown
39
+ # {title}
40
+
41
+ Drawght is a good tool for writing draft documents using datasets without
42
+ logical statements.
43
+
44
+ Written by {author.name} <{author.email}>, created in {creation-date},
45
+ published in {publishing date} and tagged by {tags#1}.
46
+
47
+ - [{author.networks:name}]({author.networks:url})
48
+
49
+ Follow the news on [{author.networks#1.name}]({author.networks#1.url}).
50
+
51
+ The syntax was inspired by:
52
+
53
+ - [{references:name}]({references:url})
54
+
55
+ Tags:
56
+
57
+ - {tags} (tagged by {author.name}).
58
+ ```
59
+
60
+ The Drawght processing returns the following result:
61
+
62
+ ```markdown
63
+ # Drawght is a very useful sketch
64
+
65
+ Drawght is a good tool for writing draft documents using datasets without
66
+ logical statements.
67
+
68
+ Written by Hallison Batista <email@hallison.dev.br>, created in 2021-06-28,
69
+ published in 2021-07-01 and tagged by Template.
70
+
71
+ - [Dev.to](//dev.to/hallison)
72
+ - [Github](//github.com/hallison)
73
+ - [Twitter](//twitter.com/hallison)
74
+
75
+ Follow the news on [Dev.to](//dev.to/hallison).
76
+
77
+ The syntax was inspired by:
78
+
79
+ - [Mustache](//mustache.github.io)
80
+ - [Handlebars](//handlebarsjs.com)
81
+
82
+ Tags:
83
+
84
+ - Template (tagged by Hallison Batista).
85
+ - Draf (tagged by Hallison Batista).
86
+ ```
87
+
88
+ In a template written in HTML:
89
+
90
+ ```html
91
+ <h1>{title}</h1>
92
+
93
+ <p>
94
+ Drawght is a good tool for writing draft documents using datasets without
95
+ logical statements.
96
+ </p>
97
+
98
+ <p>
99
+ Written by <a href="mailto:{author.email}">{author.name}</a>, created
100
+ published in {publishing date} and tagged by {tags#1}.
101
+ </p>
102
+
103
+ <ul>
104
+ <li><a href="{author.networks:url}">{author.networks:name}</a></li>
105
+ </ul>
106
+
107
+ <p>
108
+ Follow the news on <a href="{author.networks#1.url}">author.networks#1.name</a>.
109
+ </p>
110
+
111
+ <p>
112
+ The syntax was inspired by:
113
+ </p>
114
+
115
+ <ul>
116
+ <a href="{references:url}">{references:name}</a>
117
+ </ul>
118
+
119
+ <p>
120
+ Tags:
121
+ </p>
122
+
123
+ <ul>
124
+ <li>{tags} (tagged by {author.name}).</li>
125
+ </ul>
126
+ ```
127
+
128
+ The Drawght processing returns the following result:
129
+
130
+ ```html
131
+ <h1>Drawght is a very useful sketch</h1>
132
+
133
+ <p>
134
+ Drawght is a good tool for writing draft documents using datasets without
135
+ logical statements.
136
+ </p>
137
+
138
+ <p>
139
+ Written by <a href="mailto:email@hallison.dev.br">Hallison Batista</a>,
140
+ created published in 2021-07-01 and tagged by Template.
141
+ </p>
142
+
143
+ <ul>
144
+ <li><a href="//dev.to/hallison">Dev.to</a></li>
145
+ <li><a href="//github.com/hallison">Github</a></li>
146
+ <li><a href="//twitter.com/hallison">Twitter</a></li>
147
+ </ul>
148
+
149
+ <p>
150
+ Follow the news on <a href="//dev.to/hallison">Dev.to</a>.
151
+ </p>
152
+
153
+ <p>
154
+ The syntax was inspired by:
155
+ </p>
156
+
157
+ <ul>
158
+ <a href="//mustache.github.io">Mustache</a>
159
+ <a href="//handlebarsjs.com">Handlebars</a>
160
+ </ul>
161
+
162
+ <p>
163
+ Tags:
164
+ </p>
165
+
166
+ <ul>
167
+ <li>Template (tagged by Hallison Batista).</li>
168
+ <li>Draf (tagged by Hallison Batista).</li>
169
+ </ul>
170
+ ```
171
+
172
+ ## Syntax
173
+
174
+ Drawght has a simple syntax:
175
+
176
+ - `{key}`: converts `key` to its respective value. If the value is a list, then
177
+ the row will be replicated and converted with the respective values.
178
+
179
+ - `{object.key}`: converts `key` to its respective value inside `object`,
180
+ assuming the same behavior as `{key}`.
181
+
182
+ - `{list:key}`: selects `list`, replicates the line for each item in the list,
183
+ and converts `key` to its respective value contained in an object within
184
+ `list`. The `list` key can also be accessed by `object.list`, just as `key`
185
+ can also be accessed by `object.key` which will be converted following the
186
+ same process in case it is a list.
187
+
188
+ - `{list#n.key}`: selects item object `n` (from 1) from `list` and converts
189
+ `key` to its respective value. The whole process is similar to `object.key`.
190
+
191
+ ## License (MIT)
192
+
193
+ ### Copyright (c) 2021, Hallison Batista
194
+
195
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
196
+ this software and associated documentation files (the "Software"), to deal in
197
+ the Software without restriction, including without limitation the rights to
198
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
199
+ of the Software, and to permit persons to whom the Software is furnished to do
200
+ so, subject to the following conditions:
201
+
202
+ The above copyright notice and this permission notice shall be included in all
203
+ copies or substantial portions of the Software.
204
+
205
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
206
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
207
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
208
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
209
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
210
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
211
+ SOFTWARE.
data/drawght.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "drawght"
3
+ spec.summary = "Drawght parser implementation in Ruby."
4
+ spec.authors = ["Hallison Batista"]
5
+ spec.email = "email@hallison.dev.br"
6
+ spec.homepage = "https://github.com/drawght/drawght-ruby"
7
+ spec.version = %x(git describe --tags --abbrev=0)
8
+ spec.date = %x(git log --format='%as' --max-count=1)
9
+ spec.licenses = ["MIT"]
10
+ spec.platform = Gem::Platform::RUBY
11
+
12
+ spec.files = %x(git ls-files).split.reject do |out|
13
+ ignore = out =~ /([MR]ake|Gem)file/ || out =~ /^\./
14
+ ignore = ignore || out =~ /example\..*/
15
+ ignore = ignore || out =~ %r{^doc/api} || out =~ %r{^test/.*}
16
+ ignore
17
+ end
18
+
19
+ spec.test_files = spec.files.select do |path|
20
+ path =~ %r{^test/.*}
21
+ end
22
+
23
+ spec.description = <<-end.gsub /^[ ]{4}/m, ""
24
+ Drawght is a data handler for texts without logical statements. The goal is
25
+ to use a dataset (such as the subject of a text) to draft a document
26
+ template. It can be considered a mini template processor.
27
+ end
28
+
29
+ spec.require_paths = ["lib"]
30
+ end
data/lib/drawght.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Drawght
2
+ require_relative "drawght/parser"
3
+
4
+ def self.new(template)
5
+ Drawght::Parser.new template
6
+ end
7
+ end
@@ -0,0 +1,68 @@
1
+ class Drawght::Parser
2
+ PREFIX, ATTRIBUTE, QUERY, ITEM, SUFFIX, = "{", ".", ":", "#", "}"
3
+ KEY, LIST, INDEX = "(?<key>.*?)", "(?<list>.*?)", "(?<index>.*?)"
4
+ EOL = /\r?\n/
5
+
6
+ KEY_PATTERN = Regexp.new "#{PREFIX}#{KEY}#{SUFFIX}"
7
+ LIST_PATTERN = Regexp.new "#{PREFIX}#{LIST}#{QUERY}#{KEY}#{SUFFIX}"
8
+ ITEM_PATTERN = Regexp.new "#{LIST}#{ITEM}#{INDEX}([#{ATTRIBUTE}]#{KEY})?$"
9
+
10
+ def initialize(template)
11
+ @template = template
12
+ end
13
+
14
+ def parse(data)
15
+ self.parse_keys(self.parse_queries(@template, data), data)
16
+ end
17
+
18
+ def parse_queries(template, data)
19
+ template.split(EOL).map do |line|
20
+ result = line
21
+ line.scan LIST_PATTERN do |(list, key)|
22
+ value = value_from_key(list, data)
23
+ if value && (value.kind_of? Array) && key
24
+ partial = line.gsub("#{list}#{QUERY}", "")
25
+ parsed_lines = value.map { |item| parse_template(partial, item) }
26
+ result = result.gsub(line, parsed_lines.join("\n"))
27
+ end
28
+ end
29
+ result
30
+ end.join("\n")
31
+ end
32
+
33
+ def parse_keys(template, data)
34
+ template.split(EOL).map do |line|
35
+ parse_template(line, data)
36
+ end.join("\n");
37
+ end
38
+
39
+ def parse_template(template, data)
40
+ result = template
41
+ template.scan KEY_PATTERN do |(key)|
42
+ template_key = "#{PREFIX}#{key}#{SUFFIX}"
43
+ value = value_from_key(key, data) || template_key
44
+ if value.kind_of? Array
45
+ result = value.map { |item| template.gsub(template_key, item) }.join("\n")
46
+ else
47
+ result = result.gsub(template_key, value.to_s)
48
+ end
49
+ end
50
+ result
51
+ end
52
+
53
+ private
54
+
55
+ def value_from_key(nested_key, data)
56
+ item = nested_key.match ITEM_PATTERN
57
+ if item
58
+ list, index, key = item.captures
59
+ index = index && (index.to_i)
60
+ value = data.dig *list.split(ATTRIBUTE)
61
+ if value && (value.kind_of? Array) && index
62
+ key ? value[index - 1][key] : value[index - 1]
63
+ end
64
+ else
65
+ data.dig *nested_key.split(ATTRIBUTE)
66
+ end
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drawght
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hallison Batista
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ Drawght is a data handler for texts without logical statements. The goal is
15
+ to use a dataset (such as the subject of a text) to draft a document
16
+ template. It can be considered a mini template processor.
17
+ email: email@hallison.dev.br
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - LICENSE
23
+ - README.md
24
+ - drawght.gemspec
25
+ - lib/drawght.rb
26
+ - lib/drawght/parser.rb
27
+ homepage: https://github.com/drawght/drawght-ruby
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.1.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Drawght parser implementation in Ruby.
50
+ test_files: []