zevarito-uglify_html 0.11

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,8 @@
1
+ version 0.11
2
+
3
+ * Process UL and OL lists
4
+ * Process Underlines, Line through, Bold and Italics
5
+ * Hpricot Extensions
6
+ ** Hpricot::Styles class
7
+ ** Hpricot::Elem change_tag, styles
8
+ ** Hpricot::Elem::Trav set_style
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Nicolas Sanguinetti, entp.com
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,60 @@
1
+ = UglifyHtml
2
+
3
+ Ugly small lib to convert HTML markup to ugly HTML markup.
4
+
5
+ Things such these happen...
6
+
7
+ <strong>some bold text</strong>
8
+
9
+ to
10
+
11
+ <span style="font-weight: bold"></span>
12
+
13
+ -
14
+
15
+ <em>some bold text</em>
16
+
17
+ to
18
+
19
+ <span style="font-style: italic"></span>
20
+
21
+ -
22
+
23
+ <ul>
24
+ <li>Item 1</li>
25
+ <li>Item 2
26
+ <ul>
27
+ <li>Item 1 List 2</li>
28
+ </ul>
29
+ </li>
30
+ </ul>
31
+
32
+ to
33
+
34
+ <ul>
35
+ <li>Item 1</li>
36
+ <li>Item 2
37
+ <ul>
38
+ <li>Item 1 List 2</li>
39
+ </ul>
40
+ </li>
41
+ </ul>
42
+
43
+
44
+ == Why?
45
+
46
+ * Put it inside a nicEdit[http://nicedit.com] Wysiwyg editor.
47
+
48
+ == With
49
+
50
+ * Hpricot
51
+ * Hpricot extensions Elem::Trav::add_style, Elem::Trav::has_style?, Elem::change_tag!
52
+
53
+ == Get it
54
+
55
+ gem install zevarito-uglify_html
56
+
57
+ == License
58
+
59
+ Authors:: Alvaro Gil (zevarito[http://github.com/zevarito])
60
+ License:: MIT (Check LICENSE for details)
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require "rake/testtask"
2
+
3
+ begin
4
+ require "hanna/rdoctask"
5
+ rescue LoadError
6
+ require "rake/rdoctask"
7
+ end
8
+
9
+ Rake::RDocTask.new do |rd|
10
+ rd.main = "README"
11
+ rd.title = "API Documentation for UglifyHtml"
12
+ rd.rdoc_files.include("README.rdoc", "LICENSE", "lib/**/*.rb")
13
+ rd.rdoc_dir = "doc"
14
+ end
15
+
16
+ begin
17
+ require "metric_fu"
18
+ rescue LoadError
19
+ end
20
+
21
+ begin
22
+ require "mg"
23
+ MG.new("uglify_html.gemspec")
24
+ end
25
+
26
+ desc "Default: run tests"
27
+ task :default => :test
28
+
29
+ Rake::TestTask.new do |t|
30
+ t.test_files = FileList["test/test_*.rb"]
31
+ end
@@ -0,0 +1,54 @@
1
+ require 'hpricot'
2
+
3
+ module ::Hpricot
4
+ module Elem::Trav
5
+ def has_style?(name)
6
+ end
7
+
8
+ def get_style(name)
9
+ end
10
+
11
+ def set_style(name, value)
12
+ styles[name.to_s] = value.fast_xs
13
+ end
14
+ end
15
+
16
+ class Styles
17
+ def initialize e
18
+ @element = e
19
+ end
20
+
21
+ def []= k, v
22
+ s = properties.map {|pty,val| "#{pty}:#{val}"}.join(";")
23
+ @element.set_attribute("style", "#{s.chomp(";")};#{k}:#{v}".sub(/^\;/, ""))
24
+ end
25
+
26
+ def properties
27
+ return {} if not @element.has_attribute?("style")
28
+ @element.get_attribute("style").split(";").inject({}) do |hash,v|
29
+ v = v.split(":")
30
+ hash.update v.first.strip => v.last.strip
31
+ end
32
+ end
33
+
34
+ def to_s
35
+ properties.to_s
36
+ end
37
+
38
+ def to_h
39
+ properties
40
+ end
41
+ end
42
+
43
+ class Elem
44
+ def change_tag!(new_tag, preserve_attr = true)
45
+ return if not etag
46
+ self.name = new_tag
47
+ attributes.each {|k,v| remove_attribute(k)} if not preserve_attr
48
+ end
49
+
50
+ def styles
51
+ Styles.new self
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,40 @@
1
+ require 'hpricot_ext'
2
+
3
+ class UglifyHtml
4
+ def initialize(html)
5
+ @doc = Hpricot html
6
+ end
7
+
8
+ def make_ugly
9
+ (@doc/"*").each do |e|
10
+ case e.name
11
+ when 'b', 'strong' then process_with_style(e, "font-weight", "bold")
12
+ when 'i', 'em' then process_with_style(e, "font-style", "italic")
13
+ when 'u', 'ins' then process_with_style(e, "text-decoration", "underline")
14
+ when 'del' then process_with_style(e, "text-decoration", "line-through")
15
+ when 'ul', 'ol' then process_list(e)
16
+ end
17
+ end
18
+
19
+ (@doc/"li ul | li ol").remove
20
+
21
+ @doc.to_html
22
+ end
23
+
24
+ private
25
+
26
+ def process_with_style(e, style, value)
27
+ if e.parent and e.parent.name == "span" and e.parent.children.size == 1
28
+ e.parent.set_style(style, value)
29
+ e.swap e.inner_html
30
+ else
31
+ e.change_tag! "span"
32
+ e.set_style(style, value)
33
+ end
34
+ end
35
+
36
+ def process_list(e)
37
+ return if not e.parent or not e.parent.name == "li"
38
+ e.parent.after(e.to_html)
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ require "contest"
4
+ Dir[File.expand_path(File.dirname(__FILE__) + "/../lib/**/*.rb")].each do |file|
5
+ require file
6
+ end
7
+
8
+ begin
9
+ require "redgreen"
10
+ rescue LoadError
11
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ class HpricotExtensionsTest < Test::Unit::TestCase
4
+ GET_ELEM = lambda {|content| Hpricot(content).children[0]}
5
+
6
+ def setup_elements
7
+ @p_without_styles = GET_ELEM.call("<p>some text inside</p>")
8
+ @p_with_styles = GET_ELEM.call("<p styles='font-weight:bold; color:#000; font-style: italic'>some text inside</p>")
9
+ @img_without_styles = GET_ELEM.call("<img src='some.jpg' />")
10
+ end
11
+
12
+ context "Hpricot::Styles extensions" do
13
+ setup do
14
+ setup_elements
15
+ end
16
+
17
+ test "get a hash of all styles from an element" do
18
+ assert_kind_of Hpricot::Styles, @p_with_styles.styles
19
+ end
20
+
21
+ test "add new style to an element trough Elem" do
22
+ @p_without_styles.set_style("color", "red")
23
+ assert_equal({"color" => "red"}, @p_without_styles.styles.to_h)
24
+ end
25
+
26
+ test "add new style to an element trough Styles" do
27
+ @p_without_styles.styles["color"] = "red"
28
+ assert_equal({"color" => "red"}, @p_without_styles.styles.to_h)
29
+ end
30
+ end
31
+
32
+ context "Hpricot::Elem extensions" do
33
+ setup do
34
+ setup_elements
35
+ end
36
+
37
+ test "change an element tag name preserving attributes" do
38
+ @p_with_styles.change_tag! "span"
39
+ assert_equal "<span styles=\"font-weight:bold; color:#000; font-style: italic\">some text inside</span>", @p_with_styles.to_s
40
+ end
41
+
42
+ test "change an element tag name not preserving attributes" do
43
+ @p_with_styles.change_tag! "span", false
44
+ assert_equal "<span>some text inside</span>", @p_with_styles.to_s
45
+ end
46
+
47
+ test "ignore self closed element tag name" do
48
+ @img_without_styles.change_tag! "hr"
49
+ assert_equal "<img src=\"some.jpg\" />", @img_without_styles.to_s
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ class UglifyHtmlTest < Test::Unit::TestCase
4
+ def assert_renders_uglify(uglify, html)
5
+ assert_equal uglify, UglifyHtml.new(html).make_ugly
6
+ end
7
+
8
+ context "convert common tags" do
9
+ test "it should convert a simple <strong> tag" do
10
+ html = "<p>some <strong>bold</strong> text inside a paragraph</p>"
11
+ uglify = "<p>some <span style=\"font-weight:bold\">bold</span> text inside a paragraph</p>"
12
+ assert_renders_uglify uglify, html
13
+ end
14
+
15
+ test "it should convert a <strong> nested on a <em>" do
16
+ html = "<p>some <em><strong>em bold</strong></em> text inside a paragraph</p>"
17
+ uglify = "<p>some <span style=\"font-style:italic;font-weight:bold\">em bold</span> text inside a paragraph</p>"
18
+ assert_renders_uglify uglify, html
19
+ end
20
+
21
+ test "it should convert a <ins> tag" do
22
+ html = "<p>some <ins>underline</ins> text inside a paragraph</p>"
23
+ uglify = "<p>some <span style=\"text-decoration:underline\">underline</span> text inside a paragraph</p>"
24
+ assert_renders_uglify uglify, html
25
+ end
26
+
27
+ test "it should convert a <del> tag" do
28
+ html = "<p>some <del>deleted</del> text inside a paragraph</p>"
29
+ uglify = "<p>some <span style=\"text-decoration:line-through\">deleted</span> text inside a paragraph</p>"
30
+ assert_renders_uglify uglify, html
31
+ end
32
+ end
33
+
34
+ context "convert lists" do
35
+ test "it should convert a simple ul nested list" do
36
+ html = "<ul><li>item 1</li><li>item 2<ul><li>nested 1 item 1</li></ul></li></ul>"
37
+ uglify = "<ul><li>item 1</li><li>item 2</li><ul><li>nested 1 item 1</li></ul></ul>"
38
+ assert_renders_uglify uglify, html
39
+ end
40
+
41
+ test "it should convert a simple ol nested list" do
42
+ html = "<ol><li>item 1</li><li>item 2<ol><li>nested 1 item 1</li></ol></li></ol>"
43
+ uglify = "<ol><li>item 1</li><li>item 2</li><ol><li>nested 1 item 1</li></ol></ol>"
44
+ assert_renders_uglify uglify, html
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,37 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "uglify_html"
3
+ s.version = "0.11"
4
+ s.date = "2009-08-10"
5
+
6
+ s.description = "Make ugly a Html document"
7
+ s.summary = "Make ugly a Html document to use for example on wysiwyg editors"
8
+ s.homepage = ""
9
+
10
+ s.authors = "Alvaro Gil"
11
+ s.email = "zevarito@gmail.com"
12
+
13
+ s.require_paths = ["lib"]
14
+ s.has_rdoc = true
15
+ s.rubygems_version = "1.3.1"
16
+
17
+ s.add_dependency "hpricot"
18
+
19
+ if s.respond_to?(:add_development_dependency)
20
+ s.add_development_dependency "sr-mg"
21
+ s.add_development_dependency "contest"
22
+ s.add_development_dependency "redgreen"
23
+ end
24
+
25
+ s.files = %w[
26
+ LICENSE
27
+ CHANGELOG
28
+ README.rdoc
29
+ Rakefile
30
+ uglify_html.gemspec
31
+ lib/hpricot_ext.rb
32
+ lib/uglify_html.rb
33
+ test/test_helper.rb
34
+ test/test_hpricot_ext.rb
35
+ test/test_uglify.rb
36
+ ]
37
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zevarito-uglify_html
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.11"
5
+ platform: ruby
6
+ authors:
7
+ - Alvaro Gil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: sr-mg
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: contest
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: redgreen
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: Make ugly a Html document
56
+ email: zevarito@gmail.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files: []
62
+
63
+ files:
64
+ - LICENSE
65
+ - CHANGELOG
66
+ - README.rdoc
67
+ - Rakefile
68
+ - uglify_html.gemspec
69
+ - lib/hpricot_ext.rb
70
+ - lib/uglify_html.rb
71
+ - test/test_helper.rb
72
+ - test/test_hpricot_ext.rb
73
+ - test/test_uglify.rb
74
+ has_rdoc: true
75
+ homepage: ""
76
+ licenses:
77
+ post_install_message:
78
+ rdoc_options: []
79
+
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ version:
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.3.5
98
+ signing_key:
99
+ specification_version: 2
100
+ summary: Make ugly a Html document to use for example on wysiwyg editors
101
+ test_files: []
102
+