MiniMarkup 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,14 @@
1
+ === 1.0.1 / 2008-12-19
2
+
3
+ * 6 major enhancements
4
+
5
+ * Improved Paragraph finding
6
+ * Added line breaks inside paragraphs
7
+ * Added more tests & removed fail safes to let library error
8
+ * Broke up elements to smaller parts for better testability
9
+ * Added support for & and ... to Html encoding
10
+ * Added formatting fixes for Flash Rendering issues
11
+
1
12
  === 1.0.0 / 2008-12-17
2
13
 
3
14
  * 1 major enhancement
data/README.txt CHANGED
@@ -1,23 +1,33 @@
1
1
  = MiniMarkup
2
2
 
3
3
  * http://rubyforge.org/projects/uwruby
4
- * http://uwruby.rubyforge.org/mini_markup/
4
+ * http://uwruby.rubyforge.org/
5
5
 
6
6
  == DESCRIPTION:
7
7
 
8
- This is a simplified version of how Textile works
8
+ This is a very simplified version of how Textile works and also
9
+ adds some fixes if you use the same HTML in a flash feed
10
+ or widget.
9
11
 
10
12
  == FEATURES/PROBLEMS:
11
13
 
12
- Currently includes replacement <b> <i> and <p>
14
+ Currently includes replacement of <b> <i> <p> <br /> inside a <p> & and ...
15
+ as well as some fixes for spacing formatting issues with the same HTML
16
+ provided to a Flash TextArea element.
13
17
 
14
18
  == SYNOPSIS:
15
19
 
16
- Coming soon
20
+ The general idea is to provide some of the more simple benefits that
21
+ Textile provides for mobile users which they can type easily. And also
22
+ give some sensible solutions to common output methods like HTML and Flash.
23
+ While providing valid XHTML and looking at common user patterns and asking
24
+ less of them to get what they really want. Most of this is based off of
25
+ my observations designing, coding and doing customer support for Glue.
26
+ http://gluenow.com/
17
27
 
18
28
  == REQUIREMENTS:
19
29
 
20
- * None
30
+ * Awesomeness! Thats it!
21
31
 
22
32
  == INSTALL:
23
33
 
@@ -1,22 +1,119 @@
1
+ class MiniMarkupNeedInputText < StandardError; end
2
+ class MiniMarkupUnknownError < StandardError; end
3
+ class MiniMarkupMissingInstructions < StandardError; end
4
+
1
5
  class MiniMarkup
2
- VERSION = '1.0.0'
3
- B = {:seek => /(\*\b)([a-zA-Z0-9 -]+)(\b\*)/, :tag => "b"}
4
- I = {:seek => /(_(?=\S))([a-zA-Z0-9 -&]+)((?=\S)_)/, :tag => "i"}
5
- P = {:seek => /()((.+(\n|\z)){1,})/, :tag => "p"}
6
+
7
+ VERSION = '1.0.1'
8
+
9
+ ###
10
+ # REPLACEMENTS
11
+
12
+ # Default HTML Replacements
13
+ B = { :seek => /\*\b([a-zA-Z0-9 -&]+)\b\*/, :tag => "b"}
14
+ I = { :seek => /_(?=\S)([a-zA-Z0-9 -&]+)(?=\S)_/, :tag => "i"}
15
+ P = { :seek => /(^(?:.+(\n|\z))+)/, :tag => "p"}
16
+
17
+ R = { :seek => /(<p>.+<\/p>|<p>.+\n((.+<\/p>)|(.+)(\n))+)/i,
18
+ :loc => /([ ]?\n)(?!\z)/, :tag => "br"}
19
+
20
+ C = { :ampr =>[ /&(?!(\w{2,9};|[#]\d{2,4};))/, "&amp;"],
21
+ :elip =>[ /(\.{3})(?=\w|\z|\n|\s|[<])/, "&hellip;"]
22
+ }
23
+
24
+ # Flash HTML Formatting Fixes
25
+ F_P={ :seek => /<p>/i, :tag => "br"}
26
+
27
+ ###
28
+ # DEFAULT GROUPS
29
+ #
30
+ # There must be a better way to do this
31
+ # These get eval applied later on to call these functions
32
+
33
+ # Add block style replacements at start of array
34
+ TO_HTML_DEFAULTS = [
35
+ "add_p_tags",
36
+ "add_br_tags",
37
+ "add_b_tags",
38
+ "add_i_tags",
39
+ "add_symbols"
40
+ ]
41
+
42
+ FLASH_HTML_FIXES = [
43
+ "flash_p_fix"
44
+ ]
6
45
 
7
46
  attr_accessor :txt
8
47
 
9
48
  def initialize (t="")
10
- t = t.to_s if !t.is_a?(String)
11
- @txt = t
49
+ @txt = t.to_s
50
+ end
51
+
52
+ def add_inline m=nil
53
+ check_data([@txt,m])
54
+ @txt = @txt = @txt.gsub(m[:seek]) { "<#{m[:tag]}>#{$1}</#{m[:tag]}>" }
55
+ end
56
+
57
+ def add_block m=nil
58
+ check_data([@txt,m])
59
+ @txt = @txt.gsub(m[:seek]) { "<#{m[:tag]}>#{$1.chomp}</#{m[:tag]}>#{$2}" }
60
+ end
61
+
62
+ def add_p_breaks m=nil
63
+ check_data([@txt,m])
64
+ @txt = @txt.gsub(m[:seek]) { $1.gsub(m[:loc]){ " <#{m[:tag]} />" } }
12
65
  end
13
66
 
14
- def raw_to_tags (m=nil)
15
- @txt.gsub(m[:seek]) { "<#{m[:tag]}>#{$2.chomp}</#{m[:tag]}>" }
67
+ def encode_chars m=nil
68
+ check_data([@txt,m])
69
+ m.each_pair { |seek, char| @txt = @txt.gsub(char[0]) { char[1] } }
70
+ @txt
16
71
  end
17
72
 
18
- def add_b_tags; raw_to_tags B; end;
19
- def add_i_tags; raw_to_tags I; end;
20
- def add_p_tags; raw_to_tags P; end;
73
+ def add_flash_br m=nil
74
+ check_data([@txt,m])
75
+ @txt = @txt.gsub(m[:seek]) { "#{$~}<#{m[:tag]} />" }
76
+ end
77
+
78
+ # Friendly easy to read one off methods
79
+ def add_b_tags; add_inline B; end
80
+ def add_i_tags; add_inline I; end
81
+ def add_p_tags; add_block P; end
82
+ def add_br_tags; add_p_breaks R; end
83
+ def add_symbols; encode_chars C; end
84
+ def flash_p_fix; add_flash_br F_P; end
85
+
86
+ # Run all markup methods
87
+ def to_HTML type=:default
88
+ case type
89
+ when :default
90
+ apply_updates [ TO_HTML_DEFAULTS ]
91
+ when :flash
92
+ apply_updates [ TO_HTML_DEFAULTS , FLASH_HTML_FIXES ]
93
+ end
94
+ return @txt
95
+ end
96
+
97
+ def apply_updates ary
98
+ ary.each{ |group| group.each { |func_name| eval func_name } }
99
+ end
100
+
101
+ # Error Checking
102
+ def check_data(c)
103
+ raise_error(:content) if c[0].empty? # Text Content
104
+ raise_error(:instructions) unless c[1] # RegEx Intructions
105
+ end
106
+
107
+ # Error Handlin' / California Rasin'
108
+ def raise_error type=:unknown
109
+ case type
110
+ when :instructions
111
+ raise MiniMarkupMissingInstructions, "Instructions are required for this markup replacement."
112
+ when :content
113
+ raise MiniMarkupNeedInputText, "No content was provided or it was lost in process."
114
+ when :unknown
115
+ raise MiniMarkupUnknownError , "An unknown Error occurred."
116
+ end
117
+ end
21
118
 
22
119
  end
@@ -1,50 +1,11 @@
1
- #!/usr/bin/env ruby -w
2
-
3
1
  require 'test/unit'
4
2
  require 'mini_markup.rb'
5
3
 
6
- ##
7
- # Student Name: Jordan Dobson
8
- # Homework Week: 8
9
- #
10
-
11
4
  class TestMiniMarkup < Test::Unit::TestCase
12
5
 
13
6
  def setup
14
-
15
- @inst = MiniMarkup.new
16
-
7
+ @inst = MiniMarkup.new
17
8
  @no_inline_change = "* This text_should be unchanged."
18
-
19
- @b_simple_before = "*bold text* *b*"
20
- @b_simple_after = "<b>bold text</b> <b>b</b>"
21
-
22
- @b_complex_before = "* this shouldn't b*e bold* \n ** but this should be *bold text*"
23
- @b_complex_after = "* this shouldn't b<b>e bold</b> \n ** but this should be <b>bold text</b>"
24
-
25
- @i_simple_before = "_italics_ _i_"
26
- @i_simple_after = "<i>italics</i> <i>i</i>"
27
-
28
- @i_complex_before = "http://_squadblog_.com/news_view/ th_is i_s _awesome_ and really _ _should work_\n _here_ "
29
- @i_complex_after = "http://<i>squadblog</i>.com/news_view/ th<i>is i</i>s <i>awesome</i> and really _ <i>should work</i>\n <i>here</i> "
30
-
31
- @p_simple_before = "We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool"
32
- @p_simple_after = "<p>We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool</p>"
33
-
34
- @p_medium_before = "We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool\n\nHere's some topics to help you get started!"
35
- @p_medium_after = "<p>We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool</p>\n<p>Here's some topics to help you get started!</p>"
36
-
37
- @p_complex_before = "We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool\n\nHere's some topics to help you get started!\n\n\n\n*Glue Help Blog Topics*"
38
- @p_complex_after = "<p>We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool</p>\n<p>Here's some topics to help you get started!</p>\n\n\n<p>*Glue Help Blog Topics*</p>"
39
-
40
- @p_with_breaks_before = "We release about *50 new accounts weekly* on Glue,\n our simple web & mobile content publishing tool\n\nHere's some topics to help you get started!\n\n*Glue Help Blog Topics*"
41
- @p_with_breaks_after = "<p>We release about *50 new accounts weekly* on Glue,\n our simple web & mobile content publishing tool</p>\n<p>Here's some topics to help you get started!</p>\n<p>*Glue Help Blog Topics*</p>"
42
-
43
- @all_with_breaks_before = "We release about *50 new accounts weekly* on Glue,\n our simple _web & mobile_ _content publish_ing tool\n\nHere's some topics to help you get started!\n\n*Glue Help Blog Topics*"
44
-
45
-
46
- @all_with_breaks_after = "<p>We release about <b>50 new accounts weekly</b> on Glue,\n our simple <i>web & mobile</i> <i>content publish</i>ing tool</p>\n\n<p>Here's some topics to help you get started!</p>\n\n<p><b>Glue Help Blog Topics</b></p>"
47
-
48
9
  end
49
10
 
50
11
  ###
@@ -54,7 +15,7 @@ class TestMiniMarkup < Test::Unit::TestCase
54
15
  def test_is_mini_markup
55
16
  assert_kind_of MiniMarkup, @inst
56
17
  end
57
-
18
+
58
19
  ###
59
20
  # DataType
60
21
  #
@@ -67,19 +28,28 @@ class TestMiniMarkup < Test::Unit::TestCase
67
28
 
68
29
  ###
69
30
  # Empty
70
- #
31
+ #
71
32
  # Ensure if nothing sent it returns nil
72
33
  def test_for_nil_if_no_params
73
34
  assert_equal "", @inst.txt
74
35
  end
75
36
 
76
- # Ensure if tags added on nil still returns
77
- def test_return_if_empty
78
- assert_equal "", @inst.add_b_tags
79
- assert_equal "", @inst.add_i_tags
80
- assert_equal "", @inst.add_p_tags
37
+ # Ensure if worker functions Error if missing Content
38
+ def test_error_without_content
39
+ assert_raise MiniMarkupNeedInputText do
40
+ @inst.txt = ""
41
+ @inst.add_p_tags
42
+ end
43
+ end
44
+
45
+ # Ensure if worker functions Error if missing Contstant
46
+ def test_error_without_instructions
47
+ assert_raise MiniMarkupMissingInstructions do
48
+ @inst.txt = @no_inline_change
49
+ @inst.add_block
50
+ end
81
51
  end
82
-
52
+
83
53
  ###
84
54
  # Initialize
85
55
  #
@@ -93,25 +63,28 @@ class TestMiniMarkup < Test::Unit::TestCase
93
63
 
94
64
  ###
95
65
  # Bold
96
- #
66
+ #
97
67
  # Check that no bold matches are returned
98
68
  def test_no_bold_matches
99
- actual = MiniMarkup.new(@no_inline_change).add_b_tags
69
+ txt = @no_inline_change
100
70
  expected = @no_inline_change
71
+ actual = MiniMarkup.new(txt).add_b_tags
101
72
  assert_equal expected, actual
102
73
  end
103
74
 
104
75
  # Check for simple bold matches
105
76
  def test_find_bold_simple_matches
106
- actual = MiniMarkup.new(@b_simple_before).add_b_tags
107
- expected = @b_simple_after
77
+ txt = "*bold text* *b*"
78
+ expected = "<b>bold text</b> <b>b</b>"
79
+ actual = MiniMarkup.new(txt).add_b_tags
108
80
  assert_equal expected, actual
109
81
  end
110
82
 
111
83
  # Check for complex bold matches
112
84
  def test_find_bold_complex_matches
113
- actual = MiniMarkup.new(@b_complex_before).add_b_tags
114
- expected = @b_complex_after
85
+ txt = "* this shouldn't b*e bold* \n ** but this should be *bold text*"
86
+ expected = "* this shouldn't b<b>e bold</b> \n ** but this should be <b>bold text</b>"
87
+ actual = MiniMarkup.new(txt).add_b_tags
115
88
  assert_equal expected, actual
116
89
  end
117
90
 
@@ -120,53 +93,108 @@ class TestMiniMarkup < Test::Unit::TestCase
120
93
  #
121
94
  # Check that no italic matches are returned
122
95
  def test_no_italic_matches
123
- actual = MiniMarkup.new(@no_inline_change).add_i_tags
124
- expected = @no_inline_change
96
+ txt = @no_inline_change
97
+ expected = txt
98
+ actual = MiniMarkup.new(txt).add_i_tags
125
99
  assert_equal expected, actual
126
100
  end
127
101
 
128
102
  # Check for simple italic matches
129
103
  def test_find_italic_simple_matches
130
- actual = MiniMarkup.new(@i_simple_before).add_i_tags
131
- expected = @i_simple_after
104
+ txt = "_italics_ _i_"
105
+ expected = "<i>italics</i> <i>i</i>"
106
+ actual = MiniMarkup.new(txt).add_i_tags
132
107
  assert_equal expected, actual
133
108
  end
134
109
 
135
110
  # Check for complex italic matches
136
111
  def test_find_italic_complex_matches
137
- actual = MiniMarkup.new(@i_complex_before).add_i_tags
138
- expected = @i_complex_after
112
+ txt = "http://_squadblog_.com/news_view/ th_is i_s _awesome_ and really _ _should work_\n _here_ "
113
+ expected = "http://<i>squadblog</i>.com/news_view/ th<i>is i</i>s <i>awesome</i> and really _ <i>should work</i>\n <i>here</i> "
114
+ actual = MiniMarkup.new(txt).add_i_tags
139
115
  assert_equal expected, actual
140
116
  end
141
117
 
142
118
  ###
143
119
  # Paragraph
144
- #
120
+ #
145
121
  # Check that simple paragraph matches are returned
146
122
  def test_find_paragraph_simple
147
- actual = MiniMarkup.new(@p_simple_before).add_p_tags
148
- expected = @p_simple_after
123
+ txt = "We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool"
124
+ expected = "<p>We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool</p>"
125
+ actual = MiniMarkup.new(txt).add_p_tags
149
126
  assert_equal expected, actual
150
127
  end
151
128
 
152
129
  # Check that well formed paragraph matches are returned
153
130
  def test_find_paragraph_medium
154
- actual = MiniMarkup.new(@p_medium_before).add_p_tags
155
- expected = @p_medium_after
131
+ txt = "We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool\n\nHere's some topics to help you get started!"
132
+ expected = "<p>We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool</p>\n\n<p>Here's some topics to help you get started!</p>"
133
+ actual = MiniMarkup.new(txt).add_p_tags
156
134
  assert_equal expected, actual
157
135
  end
158
136
 
159
137
  # Check that poorly formed paragraph matches are returned
160
138
  def test_find_paragraph_complex
161
- actual = MiniMarkup.new(@p_complex_before).add_p_tags
162
- expected = @p_complex_after
139
+ txt = "We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool\n\nHere's some topics to help you get started!\n\n\n\n*Glue Help Blog Topics*"
140
+ expected = "<p>We release about *50 new accounts weekly* on Glue, our simple web & mobile content publishing tool</p>\n\n<p>Here's some topics to help you get started!</p>\n\n\n\n<p>*Glue Help Blog Topics*</p>"
141
+ actual = MiniMarkup.new(txt).add_p_tags
163
142
  assert_equal expected, actual
164
143
  end
165
-
144
+
166
145
  # Check that line breaks are preserved inside paragraphs
167
- def test_find_paragraph_keep breaks
168
- actual = MiniMarkup.new(@p_with_breaks_before).add_p_tags
169
- expected = @p_with_breaks_after
146
+ def test_find_paragraph_keep_linebreaks
147
+ txt = "\n\nHello\n\nWe release about *50 new accounts weekly* on Glue,\n our simple web & mobile content \npublishing tool\n\nHere's some topics to help you get started!\n\n*Glue Help Blog Topics*\n\n\n"
148
+ expected = "\n\n<p>Hello</p>\n\n<p>We release about *50 new accounts weekly* on Glue,\n our simple web & mobile content \npublishing tool</p>\n\n<p>Here's some topics to help you get started!</p>\n\n<p>*Glue Help Blog Topics*</p>\n\n\n"
149
+ actual = MiniMarkup.new(txt).add_p_tags
150
+ assert_equal expected, actual
151
+ end
152
+
153
+ # Add Line breaks inside Paragraph
154
+ def test_add_break_tags_to_paragraph
155
+ txt = "<p>Hello</p>\n\n<p>We release about *50 new accounts weekly* on Glue,\nour simple web & mobile content \npublishing tool</p>\n\n<p>Here's some topics to help you get started!</p>\n\n<p>*Glue Help Blog Topics*</p>"
156
+ expected = "<p>Hello</p>\n\n<p>We release about *50 new accounts weekly* on Glue, <br />our simple web & mobile content <br />publishing tool</p>\n\n<p>Here's some topics to help you get started!</p>\n\n<p>*Glue Help Blog Topics*</p>"
157
+ actual = MiniMarkup.new(txt).add_br_tags
158
+ assert_equal expected, actual
159
+ end
160
+
161
+ # Add Line breaks inside short and sloppy Paragraph
162
+ def test_add_breaks_in_sloppy_paragraph
163
+ txt = "\n<p>L1\nL2\nL3</p>\n<hr />\n\n<p>L4\nL5</p>\n"
164
+ expected = "\n<p>L1 <br />L2 <br />L3</p>\n<hr />\n\n<p>L4 <br />L5</p>\n"
165
+ actual = MiniMarkup.new(txt).add_br_tags
166
+ assert_equal expected, actual
167
+ end
168
+
169
+ #Test Character encodings
170
+ def test_character_encoding
171
+ txt = "...This sh&ould happen\n\nand then wyou will know.......... that it will happen & it's fine...&hellip; why just do this&#8903;\n\nright..."
172
+ expected = "&hellip;This sh&amp;ould happen\n\nand then wyou will know.......&hellip; that it will happen &amp; it's fine...&hellip; why just do this&#8903;\n\nright&hellip;"
173
+ actual = MiniMarkup.new(txt).add_symbols
174
+ assert_equal expected, actual
175
+ end
176
+
177
+ # Add all the tags together
178
+ def test_add_all_tags
179
+ txt="Hello\n\nWe release about *50 new accounts weekly* on Glue... \nour simple _web & mobile_ content \npublishing tool\n\nHere's some topics to help you get started...\n\n*Glue Help Blog Topics*"
180
+ expected = "<p>Hello</p>\n\n<p>We release about <b>50 new accounts weekly</b> on Glue&hellip; <br />our simple <i>web &amp; mobile</i> content <br />publishing tool</p>\n\n<p>Here's some topics to help you get started&hellip;</p>\n\n<p><b>Glue Help Blog Topics</b></p>"
181
+ actual = MiniMarkup.new(txt).to_HTML
182
+ assert_equal expected, actual
183
+ end
184
+
185
+ # test formatted HTML with only adding Flash fixes
186
+ def test_add_all_flash_fixes
187
+ txt="<p>Hello</p>\n\n<p>We release about <b>50 new accounts weekly</b> on Glue&hellip; <br />our simple <i>web &amp; mobile</i> content <br />publishing tool</p>\n\n<p>Here's some topics to help you get started&hellip;</p>\n\n<p><b>Glue Help Blog Topics</b></p>"
188
+ expected = "<p><br />Hello</p>\n\n<p><br />We release about <b>50 new accounts weekly</b> on Glue&hellip; <br />our simple <i>web &amp; mobile</i> content <br />publishing tool</p>\n\n<p><br />Here's some topics to help you get started&hellip;</p>\n\n<p><br /><b>Glue Help Blog Topics</b></p>"
189
+ actual = MiniMarkup.new(txt).flash_p_fix
190
+ assert_equal expected, actual
191
+ end
192
+
193
+ # Test entirely raw text with default HTML replacements & flash fixes
194
+ def test_add_all_flash_fixes
195
+ txt="Hello\n\nWe release about *50 new accounts weekly* on Glue... \nour simple _web & mobile_ content \npublishing tool\n\nHere's some topics to help you get started...\n\n*Glue Help Blog Topics*"
196
+ expected = "<p><br />Hello</p>\n\n<p><br />We release about <b>50 new accounts weekly</b> on Glue&hellip; <br />our simple <i>web &amp; mobile</i> content <br />publishing tool</p>\n\n<p><br />Here's some topics to help you get started&hellip;</p>\n\n<p><br /><b>Glue Help Blog Topics</b></p>"
197
+ actual = MiniMarkup.new(txt).to_HTML(:flash)
170
198
  assert_equal expected, actual
171
199
  end
172
200
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: MiniMarkup
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Dobson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-17 00:00:00 -08:00
12
+ date: 2008-12-20 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,7 +22,7 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 1.8.2
24
24
  version:
25
- description: This is a simplified version of how Textile works
25
+ description: This is a very simplified version of how Textile works and also adds some fixes if you use the same HTML in a flash feed or widget.
26
26
  email:
27
27
  - jordandobson@gmail.com
28
28
  executables: []
@@ -66,6 +66,6 @@ rubyforge_project: uwruby
66
66
  rubygems_version: 1.3.1
67
67
  signing_key:
68
68
  specification_version: 2
69
- summary: This is a simplified version of how Textile works
69
+ summary: This is a very simplified version of how Textile works and also adds some fixes if you use the same HTML in a flash feed or widget.
70
70
  test_files:
71
71
  - test/test_mini_markup.rb