tag_formatter 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/HISTORY.md +13 -0
- data/README.md +57 -0
- data/lib/tag_formatter/formatter.rb +81 -0
- data/lib/tag_formatter/version.rb +30 -0
- data/lib/tag_formatter.rb +22 -0
- data/test/test_tag_formatter.rb +45 -0
- metadata +52 -0
data/HISTORY.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
HISTORY
|
2
|
+
=======
|
3
|
+
|
4
|
+
0.0.1.pre-1 - January 21, 2012
|
5
|
+
------------------------------
|
6
|
+
* Initial prerelease commit
|
7
|
+
|
8
|
+
0.0.1 - May 01, 2012
|
9
|
+
--------------------
|
10
|
+
* Full release commit
|
11
|
+
* Removed pre identifier
|
12
|
+
* Updated readme with new instructions
|
13
|
+
* Updating gemspec with proper repo url and description
|
data/README.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
Tag Formatter
|
2
|
+
=============
|
3
|
+
**Tag Formatter** (for a lack of a better name) is—of course—a basic templating system that takes in text input with "tags", and replaces those tags with text as defined by the programmer.
|
4
|
+
|
5
|
+
Installation
|
6
|
+
------------
|
7
|
+
|
8
|
+
### Installation via `gem`
|
9
|
+
gem install tag_formatter
|
10
|
+
|
11
|
+
### Installation via GIT
|
12
|
+
git clone git://github.com/mseymour/tag_formatter.git
|
13
|
+
cd tag_formatter/
|
14
|
+
gem build tag_formatter.gemspec
|
15
|
+
gem install tag_formatter-0.0.1.pre1.gem
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
Valid option symbols for configuring an instance of `TagFormatter`:
|
20
|
+
|
21
|
+
* `tags` (`Hash`)
|
22
|
+
* `tag_start` (`String`)
|
23
|
+
* `tag_end` (`String`)
|
24
|
+
* `inline_comment_delimiter` (`String`)
|
25
|
+
* `block_comment_start` (`String`)
|
26
|
+
* `block_comment_end` (`String`)
|
27
|
+
|
28
|
+
Formatting a block of text using the default options:
|
29
|
+
|
30
|
+
text = <<-HEREDOC
|
31
|
+
Hello, I am {firstname} {lastname}.
|
32
|
+
I like {something}. #Something can be anything.
|
33
|
+
/*I do not know about you,
|
34
|
+
but block comments are pretty rad.*/
|
35
|
+
HEREDOC
|
36
|
+
|
37
|
+
tf = TagFormatter.new text, tags: {firstname: "Mark", lastname: "Seymour", something: "listening to music"}
|
38
|
+
|
39
|
+
puts tf.parse! #=> Hello, I am Mark Seymour.\nI like listening to music.
|
40
|
+
|
41
|
+
Formatting a block of text using user-supplied options:
|
42
|
+
|
43
|
+
tf = TagFormatter.new "I like to %verb% it, %verb% it!", tags: {verb: "move"}, tag_start: '%', tag_end: '%'
|
44
|
+
puts tf.parse! #=> I like to move it, move it!
|
45
|
+
|
46
|
+
Contribute
|
47
|
+
----------
|
48
|
+
Any and all issues should be reported via the Issues tab in Github.
|
49
|
+
|
50
|
+
Fixes, additions, patches, etc. are greatly welcome.
|
51
|
+
|
52
|
+
Author information
|
53
|
+
------------------
|
54
|
+
* Mark Seymour ('Azure')
|
55
|
+
* Email: mark.seymour.ns@gmail.com
|
56
|
+
* WWW: http://lain.rustedlogic.net/
|
57
|
+
* IRC: #shakesoda @ irc.freenode.net
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# @author Mark Seymour
|
3
|
+
|
4
|
+
module TagFormatter
|
5
|
+
class Formatter
|
6
|
+
@@_known_params = [:tags, :tag_start, :tag_end, :inline_comment_delimiter, :block_comment_start, :block_comment_end]
|
7
|
+
attr_accessor :input
|
8
|
+
attr_accessor *@@_known_params
|
9
|
+
|
10
|
+
# Creates a new instance of TagFormatter::Formatter.
|
11
|
+
#
|
12
|
+
# @param [String, File] input The input; may be either a String or a File.
|
13
|
+
# @param [Hash] params The options for parsing the String or File.
|
14
|
+
# @option params [Hash] :tags ({}) Key+value pairs representing the tags and values associated with the tags.
|
15
|
+
# @option params [String] :tag_start ({) The character(s) denoting the beginning of a tag.
|
16
|
+
# @option params [String] :tag_end (}) The character(s) denoting the end of a tag.
|
17
|
+
# @option params [String] :inline_comment_delimiter (#) The character(s) denoting an inline comment
|
18
|
+
# @option params [String] :block_comment_start (/*) The character(s) denoting the beginning of a comment block.
|
19
|
+
# @option params [String] :block_comment_end (*/) The character(s) denoting the end of a comment block.
|
20
|
+
# @raise [TypeError] #input must be a String or File.
|
21
|
+
def initialize input, params={}
|
22
|
+
params = {
|
23
|
+
tags: {},
|
24
|
+
tag_start: '{',
|
25
|
+
tag_end: '}',
|
26
|
+
inline_comment_delimiter: '#',
|
27
|
+
block_comment_start: '/*',
|
28
|
+
block_comment_end: '*/'
|
29
|
+
}.merge(params)
|
30
|
+
|
31
|
+
# Assigning input to @input if string
|
32
|
+
@input = if input.is_a? String
|
33
|
+
input
|
34
|
+
# Reading input and assigning the resultant string to @input
|
35
|
+
elsif input.is_a? File
|
36
|
+
input.read.to_s
|
37
|
+
# Raise a TypeError.
|
38
|
+
else
|
39
|
+
raise TypeError, "input was an `#{input.class.name}', expected a `String' or `File'."
|
40
|
+
end
|
41
|
+
# Assign the param values to the (known) attributes.
|
42
|
+
@@_known_params.each do |attr|
|
43
|
+
self.send(attr.to_s + "=", params[attr])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Parses the supplied input and returned a decommented, tagified, cleaned string.
|
48
|
+
#
|
49
|
+
# @return A string with the parsed input.
|
50
|
+
def parse!
|
51
|
+
tagify(decommentify(@input)).strip
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
# Decommentifies the supplied input.
|
57
|
+
#
|
58
|
+
# @param [String] input The string to decommentify.
|
59
|
+
# @return A string with the decommented input.
|
60
|
+
def decommentify input
|
61
|
+
output = input.dup
|
62
|
+
# Remove multiline comments:
|
63
|
+
output.gsub!(/(#{Regexp.quote @block_comment_start}.+?#{Regexp.quote @block_comment_end})/m, "")
|
64
|
+
# Remove inline comments:
|
65
|
+
output.gsub!(/(#{Regexp.quote @inline_comment_delimiter}.+$)/,"")
|
66
|
+
return output.lines.map(&:strip).join($/)
|
67
|
+
end
|
68
|
+
|
69
|
+
# Tagifies the supplied input.
|
70
|
+
#
|
71
|
+
# @param [String] input The string to tagify.
|
72
|
+
# @raise [StandardError] @tags must not be empty.
|
73
|
+
# @return A string with the tags replaced with their values.
|
74
|
+
def tagify input
|
75
|
+
output = input.dup
|
76
|
+
raise StandardError, "@tags is empty!" if @tags.empty? #improve on this
|
77
|
+
@tags.each {|key,value| output.gsub!(tag_start.to_s+key.to_s+tag_end.to_s, value.to_s)}
|
78
|
+
return output
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module TagFormatter
|
2
|
+
class Version
|
3
|
+
|
4
|
+
# @return [Integer]
|
5
|
+
def self.major
|
6
|
+
0
|
7
|
+
end
|
8
|
+
|
9
|
+
# @return [Integer]
|
10
|
+
def self.minor
|
11
|
+
0
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [Integer]
|
15
|
+
def self.patch
|
16
|
+
1
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [String, NilClass]
|
20
|
+
def self.pre
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [String]
|
25
|
+
def self.to_s
|
26
|
+
[major, minor, patch, pre].compact.join('.')
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'tag_formatter/formatter.rb'
|
2
|
+
|
3
|
+
module TagFormatter
|
4
|
+
class << self
|
5
|
+
# Alias for TagFormatter::Formatter.new
|
6
|
+
#
|
7
|
+
# @return [TagFormatter::Formatter]
|
8
|
+
def new(input, params={})
|
9
|
+
TagFormatter::Formatter.new(input, params)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Delegate to TagFormatter::Formatter
|
13
|
+
def method_missing(method, *args, &block)
|
14
|
+
return super unless new.respond_to?(method)
|
15
|
+
new.send(method, *args, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def respond_to?(method, include_private=false)
|
19
|
+
new.respond_to?(method, include_private) || super(method, include_private)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'tag_formatter'
|
3
|
+
|
4
|
+
class TagFormatterTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_default_parser
|
7
|
+
input = "\n\n{test} #inline comment\n{test}/*multiline\nblockcomment*/{test2}{test3}\n{test4}+{unknowntest}"
|
8
|
+
expected_output = "Hello, World!\nHello, World!Test 2...Test 3\nTest 4+{unknowntest}"
|
9
|
+
my_tags = {
|
10
|
+
test: "Hello, World!",
|
11
|
+
test2: "Test 2...",
|
12
|
+
test3: "Test 3",
|
13
|
+
test4: "Test 4",
|
14
|
+
unusedtest5: "Unused test 5"
|
15
|
+
}
|
16
|
+
options = {
|
17
|
+
tags: my_tags
|
18
|
+
}
|
19
|
+
assert_equal expected_output,
|
20
|
+
TagFormatter.new(input, options).parse!
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_parser_with_options
|
24
|
+
input = "\n\n%test% \/\/inline comment\n%test%(=multiline\nblockcomment=)%test2%%test3%\n%test4%+%unknowntest%"
|
25
|
+
expected_output = "Hello, World!\nHello, World!Test 2...Test 3\nTest 4+%unknowntest%"
|
26
|
+
my_tags = {
|
27
|
+
test: "Hello, World!",
|
28
|
+
test2: "Test 2...",
|
29
|
+
test3: "Test 3",
|
30
|
+
test4: "Test 4",
|
31
|
+
unusedtest5: "Unused test 5"
|
32
|
+
}
|
33
|
+
options = {
|
34
|
+
tags: my_tags,
|
35
|
+
tag_start: '%',
|
36
|
+
tag_end: '%',
|
37
|
+
inline_comment_delimiter: '//',
|
38
|
+
block_comment_start: '(=',
|
39
|
+
block_comment_end: '=)'
|
40
|
+
}
|
41
|
+
assert_equal expected_output,
|
42
|
+
TagFormatter.new(input, options).parse!
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tag_formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mark Seymour
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple file/string templating system.
|
15
|
+
email:
|
16
|
+
- mark.seymour.ns@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README.md
|
22
|
+
- HISTORY.md
|
23
|
+
- lib/tag_formatter/formatter.rb
|
24
|
+
- lib/tag_formatter/version.rb
|
25
|
+
- lib/tag_formatter.rb
|
26
|
+
- test/test_tag_formatter.rb
|
27
|
+
homepage: https://github.com/mseymour/tag_formatter
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
post_install_message: Happy tagging!
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 1.9.2
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.16
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: Tag Formatter
|
52
|
+
test_files: []
|