rtf-templater 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ config/environment.rb
19
+ DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rtf-templater.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 eicca
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # RtfTemplater
2
+
3
+ This is a very simple gem designed to generate RTF documents from a given template.
4
+
5
+ This gem is written in haste, but it works in most cases (I tested it on mac office, and TextEdit).
6
+
7
+ It is heavily inspired by [Serenity](https://github.com/kremso/serenity).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'rtf-templater'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rtf-templater
22
+
23
+ ## Usage
24
+
25
+ Look at the example in the showcase directory.
26
+
27
+ In short this template:
28
+
29
+ ![template](http://github.com/eicca/rtf-templater/blob/master/showcase/template.png?raw=true)
30
+
31
+ Generates such document:
32
+
33
+ ![document](http://github.com/eicca/rtf-templater/blob/master/showcase/document.png?raw=true)
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature or bug-fix branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
42
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ module RtfTemplater
2
+ module Generator
3
+
4
+ def dec string
5
+ string.unpack('U*').map { |n| n < 128 ? n.chr : n < 256 ? "\\'#{n.to_s(16)}" : "\\u#{n}\\'3f" }.join
6
+ end
7
+
8
+ def render_rtf content
9
+ template = Template.new
10
+ template.process content, binding
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require "erb"
2
+
3
+ module RtfTemplater
4
+ class Template
5
+
6
+ def initialize
7
+ end
8
+
9
+ def process content, context
10
+ content = content.gsub /<[^>]*>/ do |erb|
11
+ erb = erb.gsub("\r", "\\r").gsub("\n", "\\n")
12
+ erb = erb.gsub(/\\\w*\s?/,'').gsub(/[{}]/,'')
13
+ end
14
+ content = content.gsub("<%=", "<%= dec ")
15
+ template = ERB.new content
16
+ template.result context
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module RtfTemplater
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require "rtf-templater/version"
2
+ require "rtf-templater/generator"
3
+ require "rtf-templater/template"
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/rtf-templater/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["eicca"]
6
+ gem.email = ["wtltl2@gmail.com"]
7
+ gem.description = %q{Embedded ruby for RTF documents. You provide .rtf template and data and rtf-templater generates the document.}
8
+ gem.summary = %q{Generate RTF documents based on template with erb}
9
+ gem.homepage = "https://github.com/eicca/rtf-templater"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "rtf-templater"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RtfTemplater::VERSION
17
+ end
Binary file
Binary file
@@ -0,0 +1 @@
1
+ {\rtf1\adeflang1025\ansi\ansicpg10000\uc1\adeff0\deff0\stshfdbch0\stshfloch0\stshfhich0\stshfbi0\deflang1033\deflangfe1033\themelang1033\themelangfe0\themelangcs0{\fonttbl{\f0\fbidi \fnil\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f1\fbidi \fnil\fcharset0\fprq2{\*\panose 020b0604020202020204}Arial;}
@@ -0,0 +1,34 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
+
3
+ require 'rtf-templater'
4
+
5
+ class Showcase
6
+ include RtfTemplater::Generator
7
+
8
+ Person = Struct.new(:name, :items)
9
+ Item = Struct.new(:name, :usage)
10
+
11
+ def generate_showcase
12
+ @title = 'Serenity inventory'
13
+
14
+ mals_items = [Item.new('Moses Brothers Self-Defense Engine Frontier Model B', 'Lock and load')]
15
+ mal = Person.new('Malcolm Reynolds', mals_items)
16
+
17
+ jaynes_items = [Item.new('Vera', 'Callahan full-bore auto-lock with a customized trigger, double cartridge and thorough gauge'),
18
+ Item.new('Lux', 'Ratatata'),
19
+ Item.new('Knife', 'Cut-throat')]
20
+ jayne = Person.new('Jayne Cobb', jaynes_items)
21
+
22
+ @crew = [mal, jayne]
23
+
24
+ File.open 'showcase.rtf' do |f|
25
+ content = f.read
26
+ f = File.new('showcase-out.rtf', 'w')
27
+ f << render_rtf(content)
28
+ f.close
29
+ end
30
+ end
31
+ end
32
+
33
+ showcase = Showcase.new
34
+ showcase.generate_showcase
@@ -0,0 +1 @@
1
+ {\rtf1\adeflang1025\ansi\ansicpg10000\uc1\adeff0\deff0\stshfdbch0\stshfloch0\stshfhich0\stshfbi0\deflang1033\deflangfe1033\themelang1033\themelangfe0\themelangcs0{\fonttbl{\f0\fbidi \fnil\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f1\fbidi \fnil\fcharset0\fprq2{\*\panose 020b0604020202020204}Arial;}
Binary file
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rtf-templater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - eicca
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-07 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Embedded ruby for RTF documents. You provide .rtf template and data and
15
+ rtf-templater generates the document.
16
+ email:
17
+ - wtltl2@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .DS_Store
23
+ - .gitignore
24
+ - Gemfile
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - lib/rtf-templater.rb
29
+ - lib/rtf-templater/generator.rb
30
+ - lib/rtf-templater/template.rb
31
+ - lib/rtf-templater/version.rb
32
+ - rtf-templater.gemspec
33
+ - showcase/.DS_Store
34
+ - showcase/document.png
35
+ - showcase/showcase-out.rtf
36
+ - showcase/showcase.rb
37
+ - showcase/showcase.rtf
38
+ - showcase/template.png
39
+ homepage: https://github.com/eicca/rtf-templater
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.21
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Generate RTF documents based on template with erb
63
+ test_files: []