monospace_tag_text_formatter 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog ADDED
@@ -0,0 +1,2 @@
1
+ 0.0.3 (February 18, 2012)
2
+ Initial release.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Jacek Mikrut
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ MonospaceTagTextFormatter
2
+ =========================
3
+
4
+ MonospaceTagTextFormatter is a Ruby Gem that is an **extension to [MonospaceTextFormatter](http://github.com/jacekmikrut/monospace_text_formatter)** Ruby Gem.
5
+
6
+ Features
7
+ --------
8
+
9
+ * It **treats HTML-like tags as zero-length**, according to an assumption that these tags are going to be eventually removed or replaced before the text is displayed (for instance, they may be converted into color ANSI escape sequences by [AnsiChameleon](http://github.com/jacekmikrut/ansi_chameleon) Ruby Gem).
10
+
11
+ Thanks to that, for example, the following text will not be truncated:
12
+
13
+ ```ruby
14
+ MonospaceTagTextFormatter::Line.new("<tag>Some text.</tag>", :width => 10).to_s
15
+ => "<tag>Some text.</tag>"
16
+ ```
17
+
18
+ * It **treats "&amp;lt;" and "&amp;gt;" character entities as having the length equal to 1 character**, according to an assumption that these entities were used to escape HTML-like tags and are going to be replaced with "&lt;" and "&gt;" signs.
19
+
20
+ For instance, the following text will not be truncated:
21
+
22
+ ```ruby
23
+ MonospaceTagTextFormatter::Line.new("&lt;div&gt;Some text.&lt;/div&gt;", :width => 21).to_s
24
+ => "&lt;div&gt;Some text.&lt;/div&gt;"
25
+ ```
26
+
27
+ * It **ensures that HTML-like tags are properly closed when a text is truncated**.
28
+
29
+ An example:
30
+
31
+ ```ruby
32
+ MonospaceTagTextFormatter::Line.new("<tag>This is some text.</tag>", :width => 8).to_s
33
+ => "<tag>This</tag> ..."
34
+ ```
35
+
36
+ * When a text is wrapped, it **closes opened tags at the end of each line, and reopens them at the beginning of the next line**, according to an assumption that these tags correspond to some text formatting. Thanks to that feature, formatting of each line is preserved.
37
+
38
+ ```ruby
39
+ MonospaceTagTextFormatter::Box.new("<tag>This is a bit longer text.</tag>", :width => 8).lines
40
+ => ["<tag>This is</tag> ",
41
+ "<tag>a bit</tag> ",
42
+ "<tag>longer</tag> ",
43
+ "<tag>text.</tag> "]
44
+ ```
45
+
46
+ Installation
47
+ ------------
48
+
49
+ As a Ruby Gem, MonospaceTagTextFormatter can be installed either by running
50
+
51
+ ```bash
52
+ gem install monospace_tag_text_formatter
53
+ ```
54
+
55
+ or adding
56
+
57
+ ```ruby
58
+ gem "monospace_tag_text_formatter"
59
+ ```
60
+
61
+ to the Gemfile and then invoking `bundle install`.
62
+
63
+ License
64
+ -------
65
+
66
+ License is included in the LICENSE file.
@@ -0,0 +1,12 @@
1
+ require "monospace_tag_text_formatter/version"
2
+
3
+ require "monospace_text_formatter"
4
+
5
+ require "monospace_tag_text_formatter/atomic_chunk"
6
+ require "monospace_tag_text_formatter/atomic_chunk_factory"
7
+ require "monospace_tag_text_formatter/chunk"
8
+ require "monospace_tag_text_formatter/line"
9
+ require "monospace_tag_text_formatter/box"
10
+
11
+ module MonospaceTagTextFormatter
12
+ end
@@ -0,0 +1,28 @@
1
+ module MonospaceTagTextFormatter
2
+ class AtomicChunk < MonospaceTextFormatter::AtomicChunk
3
+
4
+ def self.closing_tag(name)
5
+ new("</#{name}>")
6
+ end
7
+
8
+ def display_length
9
+ @display_length ||= %W(&lt; &gt;).include?(display_string) ? 1 : super
10
+ end
11
+
12
+ def display_string
13
+ @display_string ||= @string.gsub(/^<\/?[^\/>][^>]*>$/, '')
14
+ end
15
+
16
+ def opening_tag?
17
+ !!@string.match(/^<[^\/>][^>]*>$/)
18
+ end
19
+
20
+ def closing_tag?
21
+ !!@string.match(/^<\/[^>]+>$/)
22
+ end
23
+
24
+ def tag_name
25
+ m = @string.match(/^<\/?(?<name>\w+)[^>]*>$/) and m[:name].to_sym
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module MonospaceTagTextFormatter
2
+ class AtomicChunkFactory < MonospaceTextFormatter::AtomicChunkFactory
3
+
4
+ REGEXP = /^(?:\n|[ \t]+|&lt;|&gt;|<\/?\w+[^>]+>|(?:[^ \t\n<&]|&(?!lt;)(?!gt;))+)/
5
+
6
+ def new(string)
7
+ AtomicChunk.new(string)
8
+ end
9
+
10
+ protected
11
+
12
+ def regexp
13
+ REGEXP
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module MonospaceTagTextFormatter
2
+ class Box < MonospaceTextFormatter::Box
3
+
4
+ private
5
+
6
+ def string_to_chunk(string)
7
+ Chunk.new(string)
8
+ end
9
+
10
+ def new_line(string_or_chunk, attrs={})
11
+ Line.new(string_or_chunk, attrs)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ module MonospaceTagTextFormatter
2
+ class Chunk < MonospaceTextFormatter::Chunk
3
+
4
+ def slice!(max_display_length=nil, smartly_split_too_long_word=true)
5
+ sliced_chunk = super
6
+
7
+ sliced_chunk.unclosed_tags_data.reverse.each do |data|
8
+ sliced_chunk.push_atomic_chunk(AtomicChunk.closing_tag(data[:name]))
9
+ unshift_atomic_chunk(data[:chunk])
10
+ end
11
+
12
+ sliced_chunk
13
+ end
14
+
15
+ protected
16
+
17
+ def atomic_chunk_factory
18
+ @atomic_chunk_factory ||= AtomicChunkFactory.new
19
+ end
20
+
21
+ def unclosed_tags_data
22
+ opened_tags_data = []
23
+
24
+ atomic_chunks.each do |atomic_chunk|
25
+
26
+ if atomic_chunk.opening_tag?
27
+ opened_tags_data.push(:name => atomic_chunk.tag_name, :chunk => atomic_chunk)
28
+
29
+ elsif atomic_chunk.closing_tag? && opened_tags_data.last && atomic_chunk.tag_name == opened_tags_data.last[:name]
30
+ opened_tags_data.pop
31
+
32
+ end
33
+ end
34
+
35
+ opened_tags_data
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ module MonospaceTagTextFormatter
2
+ class Line < MonospaceTextFormatter::Line
3
+
4
+ private
5
+
6
+ def string_to_chunk(string)
7
+ Chunk.new(string)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module MonospaceTagTextFormatter
2
+ VERSION = "0.0.3"
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monospace_tag_text_formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jacek Mikrut
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: monospace_text_formatter
16
+ requirement: &73461590 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - =
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *73461590
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &73461330 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *73461330
36
+ description: Extension to monospace_text_formatter Gem. Treats HTML-like tags as zero-length
37
+ and ensures they are properly closed during text truncation/splitting and reopened
38
+ after line wrapping.
39
+ email: jacekmikrut.software@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - lib/monospace_tag_text_formatter.rb
45
+ - lib/monospace_tag_text_formatter/atomic_chunk_factory.rb
46
+ - lib/monospace_tag_text_formatter/box.rb
47
+ - lib/monospace_tag_text_formatter/version.rb
48
+ - lib/monospace_tag_text_formatter/atomic_chunk.rb
49
+ - lib/monospace_tag_text_formatter/line.rb
50
+ - lib/monospace_tag_text_formatter/chunk.rb
51
+ - README.md
52
+ - LICENSE
53
+ - Changelog
54
+ homepage: http://github.com/jacekmikrut/monospace_tag_text_formatter
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.12
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Extension to monospace_text_formatter Gem that handles HTML-like tags.
78
+ test_files: []