twofold 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1c4cd42bb2d0a4784a901001526f0e8b1be277a246216ddfc01a90b5b90a4f5e
4
+ data.tar.gz: 0c46e895372b3b787a395a93c3d450bd76b9f9a1ba60ec662a0ad4f4748a0c75
5
+ SHA512:
6
+ metadata.gz: 003cfddf5433e6ecc2524222c37f02aab54bbef54a49233ee5737c9a73ac033401828b3392f21fe860d874503708525c0015d11bd86ee6f36ae8843ac1557a5b
7
+ data.tar.gz: 67f645616f34aa5573812ef1fe782ea7c5f2cccef15fb866f7abef286be1ec20fc5bb753e74bbb28ae7ea022dbdc1fd0bfad948e3de254dbf0bf8be83cc23e0e
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,8 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
7
+
8
+ script: bundle exec rake spec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at andreas.reischuck@hicknhack-software.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
4
+ gem "rake", "~> 12.0"
5
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Andreas Reischuck
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,120 @@
1
+ # Twofold Ruby
2
+
3
+ **Generate correctly indented source code with the power of Ruby**
4
+
5
+ Welcome to the Ruby implementation of Twofold.
6
+
7
+ ## Motivation
8
+
9
+ Most template engines like Slim or Haml are focused towards HTML.
10
+ ERB allows you to generate any text, but control structures are cumbersome.
11
+
12
+ We want to be able to read the target language and also the control language.
13
+
14
+ With the power of Ruby it's all very easy.
15
+
16
+ ## Features
17
+
18
+ * indentation stacking
19
+ * control every generated line and every whitespace
20
+ * some unit tests
21
+
22
+ ## Syntax
23
+
24
+ Unlike most template languages every line is Ruby unless it starts with on of:
25
+
26
+ * `|` - followed by output text, breaks the output line
27
+ * `\\` - followed by output text without breaking the output line
28
+ * `=` - followed by ruby code
29
+
30
+ Output text can contain `#{interpolated}` ruby code.
31
+
32
+ Whitespaces BEFORE these symbols are ignored.
33
+ But whitespaces AFTER the symbols are used as output indentations.
34
+ These indentations stack.
35
+
36
+ Let's look at an example:
37
+
38
+ ## Example
39
+
40
+ ```twofold
41
+ def self.greet_method(name, greet)
42
+ |def greet_#{name}(name)
43
+ | puts("#{greet} \#{name}")
44
+ |end
45
+ return # be careful what you return.
46
+ end
47
+ |class Greeters
48
+ methods.each do |m|
49
+ = greet_method(m[:name], m[:greet])
50
+ end
51
+ |end
52
+ ```
53
+
54
+ If we provide the following locals:
55
+ ```ruby
56
+ locals = {
57
+ methods: [
58
+ { name: "hello", greet: "Hello World of " },
59
+ { name: "tag", greet: "Guten Tag " },
60
+ ]
61
+ }
62
+ ```
63
+
64
+ Twofold will produce:
65
+ ```ruby
66
+ class Greeters
67
+ def greet_world(name)
68
+ puts("Hello World of #{name}")
69
+ end
70
+ def greet_tag(name)
71
+ puts("Guten Tag #{name}")
72
+ end
73
+ end
74
+ ```
75
+
76
+ Notice how all the methods and their content are indented into the class, even though there is no indentation in the template?
77
+ This is because we call `greet_method` with the two space indentation.
78
+
79
+ ## Installation
80
+
81
+ Add this line to your application's Gemfile:
82
+
83
+ ```ruby
84
+ gem 'twofold-temple'
85
+ ```
86
+
87
+ ## Usage
88
+
89
+ You simply create a Tilt template (`Twofold::Template`)
90
+
91
+ ```ruby
92
+ require 'twofold'
93
+
94
+ Twofold::Template.new('template.twofold').render(scope)
95
+ ```
96
+
97
+ ## Development
98
+
99
+ After checking out the repo, run `bundle install` to install dependencies.
100
+ Then, run `rake spec` to run the tests.
101
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ To install this gem onto your local machine, run `bundle exec rake install`.
104
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
105
+ which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
106
+
107
+ ## Contributing
108
+
109
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hicknhack-software/twofold.temple.
110
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
111
+ the [code of conduct](https://github.com/hicknhack-software/twofold-temple/blob/master/CODE_OF_CONDUCT.md).
112
+
113
+ ## License
114
+
115
+ Twofold is released under the terms of the [MIT License](https://opensource.org/licenses/MIT).
116
+
117
+ ## Code of Conduct
118
+
119
+ Everyone interacting in the twofold project's codebases, issue trackers, chat rooms and mailing lists
120
+ is expected to follow the [code of conduct](https://github.com/[USERNAME]/Twofold-Temple/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "twofold"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,11 @@
1
+ require 'temple'
2
+ require 'twofold/version'
3
+
4
+ module Twofold
5
+ autoload :Engine, 'twofold/engine'
6
+ autoload :Parser, 'twofold/parser'
7
+ autoload :Template, 'twofold/template'
8
+ autoload :Filter, 'twofold/filter'
9
+ autoload :Interpolation, 'twofold/interpolation'
10
+ autoload :Indentation, 'twofold/indentation'
11
+ end
@@ -0,0 +1,14 @@
1
+ module Twofold
2
+ class Engine < Temple::Engine
3
+ use Twofold::Parser
4
+
5
+ use Twofold::Interpolation
6
+ use Twofold::Indentation
7
+
8
+ filter :ControlFlow
9
+ filter :MultiFlattener
10
+ filter :StaticMerger
11
+
12
+ generator :ArrayBuffer, buffer: "@_buf" # we use buffer attribute so custom methods can use it too
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ module Twofold
2
+
3
+ # Base class for Filters used in Twofold
4
+ #
5
+ # Add all recursive S-Expressions!
6
+ #
7
+ # @api private
8
+ class Filter < Temple::Filter
9
+
10
+ # Pass-through handler
11
+ def on_twofold_indented(indent, inner)
12
+ [:twofold, :indented, indent, compile(inner)]
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,71 @@
1
+ module Twofold
2
+ class Indentation < Filter
3
+ define_options indent: "@_indent", # stack of indentation strings
4
+ indented: "@_indented" # is current line already indented?
5
+
6
+ def initialize(opts = {})
7
+ super
8
+ @indent = options[:indent]
9
+ @indented = options[:indented]
10
+ end
11
+
12
+ def on_twofold_indented(indent, inner)
13
+ block = [:multi]
14
+ block << [:code, "(#{@indent} ||= []) << '#{indent}'"]
15
+ block << compile(inner)
16
+ block << [:code, "#{@indent}.pop"]
17
+ block
18
+ end
19
+
20
+ def on_twofold_newline
21
+ [:multi,
22
+ [:code, "#{@indented} = false"],
23
+ [:static, "\n"]
24
+ ]
25
+ end
26
+
27
+ def on_static(string)
28
+ if string.empty?
29
+ [:static, string]
30
+ else
31
+ [:multi,
32
+ ensure_indent,
33
+ [:static, string]
34
+ ]
35
+ end
36
+ end
37
+
38
+ def on_dynamic(ruby)
39
+ [:multi,
40
+ [:capture, 'tmp', [:dynamic, ruby]],
41
+ [:if,
42
+ 'not tmp.nil? and not tmp.empty?',
43
+ [:block,
44
+ 'tmp.split(/(?<=\n)/).each do |line|',
45
+ [:multi,
46
+ ensure_indent,
47
+ [:dynamic, "line"],
48
+ [:if,
49
+ "line[-1] == #{'"\n"'}",
50
+ [:code, "#{@indented} = false"]
51
+ ]
52
+ ]
53
+ ]
54
+ ]
55
+ ]
56
+ end
57
+
58
+ private
59
+
60
+ def ensure_indent
61
+ [:if,
62
+ "not #{@indented}",
63
+ [:multi,
64
+ [:code, "#{@indented} = true"],
65
+ [:dynamic, "(#{@indent} ||= [])*''"]
66
+ ]
67
+ ]
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,29 @@
1
+ module Twofold
2
+ class Interpolation < Filter
3
+ # Works like Temple::Filters::StringSplitter but does not require ripper
4
+ def on_twofold_interpolate(input)
5
+ # Interpolate ruby code in text (#{variable}).
6
+ # Split the text into multiple dynamic and static parts.
7
+ block = [:multi]
8
+ string = input
9
+ begin
10
+ case string
11
+ when /\A\\#\{/
12
+ # Escaped interpolation
13
+ block << [:static, '#{']
14
+ string = $'
15
+ when /\A#\{((?>[^{}]|(\{(?>[^{}]|\g<1>)*\}))*)\}/
16
+ # Interpolation
17
+ string, code = $', $1
18
+ escape = code !~ /\A\{.*\}\Z/
19
+ block << [:dynamic, escape ? code : code[1..-2]]
20
+ when /\A([#\\]?[^#\\]*([#\\][^\\#\{][^#\\]*)*)/
21
+ # Static text
22
+ block << [:static, $&]
23
+ string = $'
24
+ end
25
+ end until string.empty?
26
+ block
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,59 @@
1
+ module Twofold
2
+ class Parser < Temple::Parser
3
+
4
+ def call(input)
5
+ result = [:multi]
6
+
7
+ @lines = input.split(/\r?\n/)
8
+ @line_no = 0
9
+ @stack = [result]
10
+
11
+ parse_line while next_line
12
+
13
+ result
14
+ end
15
+
16
+ def next_line
17
+ if @lines.empty?
18
+ @line = nil
19
+ else
20
+ @line = @lines.shift
21
+ @line_no += 1
22
+ end
23
+ end
24
+
25
+ def parse_line
26
+ line = @line.lstrip
27
+
28
+ case line[0]
29
+ when '\\'
30
+ indented line[1..] do |text|
31
+ @stack.last << [:twofold, :interpolate, text]
32
+ end
33
+ when '|'
34
+ indented line[1..] do |text|
35
+ @stack.last << [:twofold, :interpolate, text]
36
+ end
37
+ @stack.last << [:twofold, :newline]
38
+ when '='
39
+ indented line[1..] do |code|
40
+ @stack.last << [:code, code]
41
+ end
42
+ when nil
43
+ # empty line
44
+ else
45
+ @stack.last << [:code, line]
46
+ end
47
+ @stack.last << [:newline]
48
+ end
49
+
50
+ def indented(str)
51
+ indent = str[/\A\s*/]
52
+ @stack.push([:multi])
53
+ yield str.lstrip
54
+ inner = @stack.pop
55
+ @stack.last << [:twofold, :indented, indent, inner]
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module Twofold
2
+ Template = Temple::Templates::Tilt(Engine)
3
+ end
@@ -0,0 +1,3 @@
1
+ module Twofold
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/twofold/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "twofold"
5
+ spec.version = Twofold::VERSION
6
+ spec.authors = ["Andreas Reischuck"]
7
+ spec.email = ["andreas.reischuck@hicknhack-software.com"]
8
+
9
+ spec.summary = %q{Twofold template engine to generate any indented source code.}
10
+ #spec.description = %q{Work in progress}
11
+ spec.homepage = "https://github.com/hicknhack-software/twofold-ruby"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/hicknhack-software/twofold-ruby"
17
+ spec.metadata["changelog_uri"] = "https://github.com/hicknhack-software/twofold-ruby"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_runtime_dependency('temple')
29
+ spec.add_runtime_dependency('tilt')
30
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twofold
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andreas Reischuck
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: temple
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tilt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - andreas.reischuck@hicknhack-software.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - lib/twofold.rb
58
+ - lib/twofold/engine.rb
59
+ - lib/twofold/filter.rb
60
+ - lib/twofold/indentation.rb
61
+ - lib/twofold/interpolation.rb
62
+ - lib/twofold/parser.rb
63
+ - lib/twofold/template.rb
64
+ - lib/twofold/version.rb
65
+ - twofold.gemspec
66
+ homepage: https://github.com/hicknhack-software/twofold-ruby
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/hicknhack-software/twofold-ruby
71
+ source_code_uri: https://github.com/hicknhack-software/twofold-ruby
72
+ changelog_uri: https://github.com/hicknhack-software/twofold-ruby
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.3.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.1.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Twofold template engine to generate any indented source code.
92
+ test_files: []