Skip to content

Commit

Permalink
Initial refactoring from teapot.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 6, 2017
0 parents commit 0a7b1e1
Show file tree
Hide file tree
Showing 16 changed files with 814 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
@@ -0,0 +1,23 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
*.bundle
*.so
*.o
*.a
mkmf.log
.rspec_status
4 changes: 4 additions & 0 deletions .rspec
@@ -0,0 +1,4 @@
--format documentation
--backtrace
--warnings
--require spec_helper
19 changes: 19 additions & 0 deletions .travis.yml
@@ -0,0 +1,19 @@
language: ruby
sudo: false
dist: trusty
cache: bundler
rvm:
- 2.1
- 2.2
- 2.3
- 2.4
- ruby-head
- jruby-head
matrix:
allow_failures:
- rvm: "ruby-head"
- rvm: "jruby-head"
addons:
apt:
packages:
- graphviz
13 changes: 13 additions & 0 deletions Gemfile
@@ -0,0 +1,13 @@
source 'https://rubygems.org'

gemspec

group :development do
gem 'guard'
gem 'guard-rspec'
end

group :test do
gem 'simplecov'
gem 'coveralls', require: false
end
9 changes: 9 additions & 0 deletions Guardfile
@@ -0,0 +1,9 @@

directories %w(lib spec)
clearing :on

guard :rspec, cmd: "bundle exec rspec" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch("spec/spec_helper.rb") { "spec" }
end
121 changes: 121 additions & 0 deletions README.md
@@ -0,0 +1,121 @@
# Build::Text

`Build::Text` provides substitutions and merging for textual files, typically code.

[![Build Status](https://secure.travis-ci.org/ioquatix/build-text.svg)](http://travis-ci.org/ioquatix/build-text)
[![Code Climate](https://codeclimate.com/github/ioquatix/build-text.svg)](https://codeclimate.com/github/ioquatix/build-text)
[![Coverage Status](https://coveralls.io/repos/ioquatix/build-text/badge.svg)](https://coveralls.io/r/ioquatix/build-text)

## Installation

Add this line to your application's Gemfile:

gem 'build-text'

And then execute:

$ bundle

Or install it yourself as:

$ gem install build-text

## Usage

### Substitutions

Provides a very simple template language for doing text substitutions:

```ruby
require 'build/text'

substitutions = Build::Text::Substitutions.new
substitutions['ANIMAL'] = 'cat'
substitutions['SOUND'] = 'meow'

puts substitutions.apply("The $ANIMAL says $SOUND.")
# Prints "The cat says meow."
```

It can also do indented expressions.

```ruby
require 'build/text'

substitutions = Build::Text::Substitutions.new
substitutions['MODULE'] = ["module Meow\n", "end\n"]

puts substitutions.apply("<MODULE>\ndef function\nend\n</MODULE>")
# Prints:
# module Meow
# def function
# end
# end
```

### Merging

A best effort merging strategy to combine two files. It uses LCS to match up sections, and then appends segments together.

Given the following input file:

```c++
class Bob {
int alice();
}
```
and the following template:
```c++
class Bob {
int bob();
}
```

it can be merged

```ruby
merged = Build::Text::Merge.combine(input.lines, template.lines)
```

and the merged result will be:

```c++
class Bob {
int alice();
int bob();
}
```
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
## License
Released under the MIT license.
Copyright, 2017, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
8 changes: 8 additions & 0 deletions Rakefile
@@ -0,0 +1,8 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec) do |task|
task.rspec_opts = ["--require", "simplecov"] if ENV['COVERAGE']
end

task :default => :spec
21 changes: 21 additions & 0 deletions build-text.gemspec
@@ -0,0 +1,21 @@
# coding: utf-8
require_relative 'lib/build/text/version'

Gem::Specification.new do |spec|
spec.name = "build-text"
spec.version = Build::Text::VERSION
spec.authors = ["Samuel Williams"]
spec.email = ["samuel.williams@oriontransfer.co.nz"]
spec.summary = %q{Text substitutions and merging.}
spec.homepage = ""
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rspec", "~> 3.6"
spec.add_development_dependency "rake"
end
24 changes: 24 additions & 0 deletions lib/build/text.rb
@@ -0,0 +1,24 @@
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'text/version'

require_relative 'text/merge'
require_relative 'text/substitutions'
67 changes: 67 additions & 0 deletions lib/build/text/indentation.rb
@@ -0,0 +1,67 @@
# Copyright, 2013, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

module Build
module Text
class Indentation
TAB = "\t".freeze

def initialize(prefix, level, indent)
@prefix = prefix
@level = level
@indent = indent
end

def freeze
indentation

@prefix.freeze
@level.freeze
@indent.freeze

super
end

def indentation
@indentation ||= @prefix + (@indent * @level)
end

def + other
indentation + other
end

def << text
text.gsub(/^/){|m| m + indentation}
end

def by(depth)
Indentation.new(@prefix, @level + depth, @indent)
end

def with_prefix(prefix)
Indentation.new(prefix, @level, @indent)
end

def self.none
self.new('', 0, TAB)
end
end
end
end

0 comments on commit 0a7b1e1

Please sign in to comment.