commaparty 0.0.1pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03a8339495655f770639b4379f0efd00ed002bc4
4
+ data.tar.gz: 3e7f1c68a4075921dfc9499db7be3d74d4689456
5
+ SHA512:
6
+ metadata.gz: 63980a3c5b5cf2b9849443cdb99a9f1937f8bbc6e8d24cdd9b060c7ca11e4f3b7cb6ba9bc14befe22eec5988489f40ac0cf18a3e3819e8521c211684f3745ce4
7
+ data.tar.gz: fa24ef4475c3e4e55cb46b6405a1ccd826bb5d568abdd12858c7156a2e8b6f4766a68ab7e30dba1bf1a8a2860f6d86d2c85c54aad4c4e8eb54b37c0fb3f90575
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in commaparty.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Connor Mendenhall
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,66 @@
1
+ CommaParty
2
+ ======
3
+ CommaParty is a Ruby implementation of Clojure's
4
+ [Hiccup](https://github.com/weavejester/hiccup/) HTML generation library.
5
+ It uses arrays to represent elements, and hashes to represent an element's
6
+ attributes. Unlike in Clojure, you have to use a lot of commas everywhere.
7
+
8
+ Install
9
+ -------
10
+ Add the following dependency to your `Gemfile`:
11
+
12
+ ```ruby
13
+ gem 'commaparty'
14
+ ```
15
+
16
+ then
17
+
18
+ ```ruby
19
+ require 'commaparty'
20
+ ```
21
+
22
+ Syntax
23
+ ------
24
+
25
+ Here is a basic example of commaparty syntax:
26
+
27
+ ```ruby
28
+ user=> (use 'hiccup.core)
29
+ nil
30
+ user=> (html [:span {:class "foo"} "bar"])
31
+ "<span class=\"foo\">bar</span>"
32
+ ```
33
+
34
+ The first element of the vector is used as the element name. The second
35
+ attribute can optionally be a map, in which case it is used to supply
36
+ the element's attributes. Every other element is considered part of the
37
+ tag's body.
38
+
39
+ Hiccup is intelligent enough to render different HTML elements in
40
+ different ways, in order to accommodate browser quirks:
41
+
42
+ ```clojure
43
+ user=> (html [:script])
44
+ "<script></script>"
45
+ user=> (html [:p])
46
+ "<p />"
47
+ ```
48
+
49
+ And provides a CSS-like shortcut for denoting `id` and `class`
50
+ attributes:
51
+
52
+ ```clojure
53
+ user=> (html [:div#foo.bar.baz "bang"])
54
+ "<div id=\"foo\" class=\"bar baz\">bang</div>"
55
+ ```
56
+
57
+ If the body of the element is a seq, its contents will be expanded out
58
+ into the element body. This makes working with forms like `map` and
59
+ `for` more convenient:
60
+
61
+ ```clojure
62
+ user=> (html [:ul
63
+ (for [x (range 1 4)]
64
+ [:li x])])
65
+ "<ul><li>1</li><li>2</li><li>3</li></ul>"
66
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'commaparty/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "commaparty"
8
+ spec.version = CommaParty::VERSION
9
+ spec.authors = ["Connor Mendenhall"]
10
+ spec.email = ["ecmendenhall@gmail.com"]
11
+ spec.description = %q{A ruby implementation of Clojure's Hiccup markup generation library.}
12
+ spec.summary = %q{Hiccup, with commas.}
13
+ spec.homepage = "https://github.com/ecmendenhall/commaparty"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "nokogiri"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,36 @@
1
+ require 'commaparty/parse_tag'
2
+
3
+ module CommaParty
4
+ class DestructureElement
5
+
6
+ def initialize(element)
7
+ @element = element
8
+ end
9
+
10
+ def call
11
+ normalize_element(@element)
12
+ end
13
+
14
+ private
15
+
16
+ def normalize_element(element)
17
+ tag, tag_attributes = CommaParty::ParseTag.new(element.shift).call
18
+ tag = safe_tagname(tag)
19
+ attributes = attribute(element) || {}
20
+ [tag,
21
+ attributes.merge(tag_attributes),
22
+ element]
23
+ end
24
+
25
+ def safe_tagname(tag)
26
+ "#{tag.to_s}_".to_sym
27
+ end
28
+
29
+ def attribute(element)
30
+ if element.first && element.first.is_a?(Hash)
31
+ element.shift
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ require 'nokogiri'
2
+ require 'commaparty/markup'
3
+
4
+ module CommaParty
5
+ class HTML
6
+
7
+ def self.build(markup)
8
+ new(markup).call
9
+ end
10
+
11
+ def initialize(markup)
12
+ @markup = CommaParty::Markup.new(markup)
13
+ end
14
+
15
+ def call
16
+ build(@markup.call)
17
+ end
18
+
19
+ private
20
+
21
+ def build(markup)
22
+ Nokogiri::HTML::Builder.new do |doc|
23
+ doc.html {|html| html << markup }
24
+ end.to_html
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ require 'nokogiri'
2
+ require 'commaparty/destructure_element'
3
+
4
+ module CommaParty
5
+ class Markup
6
+
7
+ def initialize(*hiccup)
8
+ @hiccup = hiccup
9
+ end
10
+
11
+ def call
12
+ build(*@hiccup)
13
+ end
14
+
15
+ private
16
+
17
+ def build(*elements)
18
+ doc = Nokogiri::XML::Builder.new do |doc|
19
+ doc.root do |root|
20
+ elements.each do |element|
21
+ create_child(root, element)
22
+ end
23
+ end
24
+ end.doc.root.children.to_html
25
+ end
26
+
27
+ def create_child(node, element)
28
+ tag, attributes, children = CommaParty::DestructureElement.new(element).call
29
+ if children.empty? || children.first.is_a?(String)
30
+ node.send(tag, attributes) {|n| n << children.first }
31
+ else
32
+ node.send(tag, attributes) {|n| n << build(*children) }
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,36 @@
1
+ module CommaParty
2
+ class ParseTag
3
+
4
+ def initialize(tag)
5
+ @tag = tag
6
+ end
7
+
8
+ def call
9
+ [tag_name, attributes]
10
+ end
11
+
12
+ private
13
+
14
+ def attributes
15
+ attributes = {}
16
+ attributes = attributes.merge(class: classes.join(' ')) if classes
17
+ attributes = attributes.merge(id: id) if id
18
+ return attributes
19
+ end
20
+
21
+ def classes
22
+ class_names = @tag.to_s.scan(/\.([^\.,#,\b]*)/)
23
+ class_names if !class_names.empty?
24
+ end
25
+
26
+ def id
27
+ ids = @tag.to_s.match(/\#([^\.,#,\b]*)/)
28
+ ids.captures.first if ids
29
+ end
30
+
31
+ def tag_name
32
+ @tag.to_s.match(/([^\.,#,\b]+)/).captures.first.to_sym
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module CommaParty
2
+ VERSION = "0.0.1pre"
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'nokogiri'
2
+ require 'commaparty/markup'
3
+
4
+ module CommaParty
5
+ class XML
6
+
7
+ def self.build(markup)
8
+ new(markup).call
9
+ end
10
+
11
+ def initialize(markup)
12
+ @markup = CommaParty::Markup.new(markup).call
13
+ end
14
+
15
+ def call
16
+ build(@markup)
17
+ end
18
+
19
+ private
20
+
21
+ def build(markup)
22
+ Nokogiri::XML::Builder.new do |doc|
23
+ doc << markup
24
+ end.to_xml
25
+ end
26
+ end
27
+
28
+ end
data/lib/commaparty.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "commaparty/html"
2
+ require "commaparty/markup"
3
+ require "commaparty/version"
4
+ require "commaparty/xml"
5
+
6
+ module CommaParty
7
+
8
+ def self.html(hiccup)
9
+ CommaParty::HTML.new(hiccup).call
10
+ end
11
+
12
+ def self.xml(hiccup)
13
+ CommaParty::XML.new(hiccup).call
14
+ end
15
+
16
+ def self.markup(hiccup)
17
+ CommaParty::Markup.new(hiccup).call
18
+ end
19
+
20
+ end
21
+
@@ -0,0 +1,42 @@
1
+ require 'rspec'
2
+ require 'commaparty/destructure_element'
3
+
4
+ describe CommaParty::DestructureElement do
5
+
6
+ describe 'Normalizing elements' do
7
+ it 'destructures an element with tag only' do
8
+ tag, attributes, values = described_class.new([:lol]).call
9
+ expect(tag).to eq(:lol_)
10
+ expect(attributes).to eq({})
11
+ expect(values).to eq([])
12
+ end
13
+
14
+ it 'destructures an element with tag, attributes, and values' do
15
+ tag, attributes, values = described_class.new([:lol, {id: "the-id"}, "lol"]).call
16
+ expect(tag).to eq(:lol_)
17
+ expect(attributes).to eq({id: "the-id"})
18
+ expect(values).to eq(["lol"])
19
+ end
20
+
21
+ it 'destructures an element with tag and attributes only' do
22
+ tag, attributes, values = described_class.new([:lol, {id: "the-id"}]).call
23
+ expect(tag).to eq(:lol_)
24
+ expect(attributes).to eq({id: "the-id"})
25
+ expect(values).to eq([])
26
+ end
27
+
28
+ it 'destructures an element with tag and values only' do
29
+ tag, attributes, values = described_class.new([:lol, 'wut']).call
30
+ expect(tag).to eq(:lol_)
31
+ expect(attributes).to eq({})
32
+ expect(values).to eq(['wut'])
33
+ end
34
+
35
+ it 'destructures an element with sibling children' do
36
+ tag, attributes, values = described_class.new([:lol, {}, [:div], [:ul], [:other]]).call
37
+ expect(tag).to eq(:lol_)
38
+ expect(attributes).to eq({})
39
+ expect(values).to eq([[:div], [:ul], [:other]])
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,14 @@
1
+ require 'rspec'
2
+ require 'commaparty/html'
3
+ require 'commaparty/markup'
4
+
5
+ describe CommaParty::HTML do
6
+
7
+ let(:markup) { [:parent, [:child1], [:child2]] }
8
+
9
+ it 'creates HTML from markup' do
10
+ html = described_class.new(markup).call
11
+ expect(html).to match(/<!DOCTYPE html/)
12
+ end
13
+
14
+ end
@@ -0,0 +1,44 @@
1
+ require 'rspec'
2
+ require 'commaparty/markup'
3
+
4
+ describe CommaParty::Markup do
5
+
6
+ describe 'Generating markup' do
7
+
8
+ it 'generates a single tag' do
9
+ html = described_class.new([:tag]).call
10
+ expect(html).to eq("<tag></tag>")
11
+ end
12
+
13
+ it 'generates a single tag with an attribute' do
14
+ html = described_class.new([:tag, {attribute: 'something'}]).call
15
+ expect(html).to eq("<tag attribute=\"something\"></tag>")
16
+ end
17
+
18
+ it 'generates a single tag with a value' do
19
+ html = described_class.new([:tag, 'value']).call
20
+ expect(html).to eq("<tag>value</tag>")
21
+ end
22
+
23
+ it 'generates two sibling tags' do
24
+ html = described_class.new([:tag1], [:tag2]).call
25
+ expect(html).to eq("<tag1></tag1><tag2></tag2>")
26
+ end
27
+
28
+ it 'generates nested tags' do
29
+ html = described_class.new([:theparent, [:tag1], [:tag2]]).call
30
+ expect(html).to eq("<theparent><tag1></tag1><tag2></tag2></theparent>")
31
+ end
32
+
33
+ it 'handles tags with the same names as ruby methods' do
34
+ html = described_class.new([:parent]).call
35
+ expect(html).to eq("<parent></parent>")
36
+ end
37
+
38
+ it 'handles tags with shortcut syntax' do
39
+ html = described_class.new([:'tag.one.two.three#id']).call
40
+ expect(html).to eq("<tag class=\"one two three\" id=\"id\"></tag>")
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,45 @@
1
+ require 'rspec'
2
+ require 'commaparty/parse_tag'
3
+
4
+ describe CommaParty::ParseTag do
5
+
6
+ describe 'Tag shortcuts' do
7
+
8
+ it 'parses tags without shortcut syntax' do
9
+ tag, attributes = described_class.new(:boring).call
10
+ expect(tag).to eq(:boring)
11
+ expect(attributes).to eq({})
12
+ end
13
+
14
+ it 'parses tags with a class shortcut' do
15
+ tag, attributes = described_class.new(:'exciting.class').call
16
+ expect(tag).to eq(:exciting)
17
+ expect(attributes).to eq({class: 'class'})
18
+ end
19
+
20
+ it 'parses tags with an id shortcut' do
21
+ tag, attributes = described_class.new(:'exciting#id').call
22
+ expect(tag).to eq(:exciting)
23
+ expect(attributes).to eq({id: 'id'})
24
+ end
25
+
26
+ it 'parses tags with a class shortcut and id shortcut' do
27
+ tag, attributes = described_class.new(:'exciting.class#id').call
28
+ expect(tag).to eq(:exciting)
29
+ expect(attributes).to eq({class: 'class', id: 'id'})
30
+ end
31
+
32
+ it 'handles different orders' do
33
+ tag, attributes = described_class.new(:'exciting#id.class').call
34
+ expect(tag).to eq(:exciting)
35
+ expect(attributes).to eq({class: 'class', id: 'id'})
36
+ end
37
+
38
+ it 'handles multiple classes' do
39
+ tag, attributes = described_class.new(:'exciting.one.two').call
40
+ expect(tag).to eq(:exciting)
41
+ expect(attributes).to eq({class: 'one two'})
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,14 @@
1
+ require 'rspec'
2
+ require 'commaparty/xml'
3
+ require 'commaparty/markup'
4
+
5
+ describe CommaParty::XML do
6
+
7
+ let(:markup) { [:parent, [:child1], [:child2]] }
8
+
9
+ it 'creates XML from markup' do
10
+ xml = described_class.new(markup).call
11
+ expect(xml).to match(/<?xml/)
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commaparty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1pre
5
+ platform: ruby
6
+ authors:
7
+ - Connor Mendenhall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A ruby implementation of Clojure's Hiccup markup generation library.
70
+ email:
71
+ - ecmendenhall@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - commaparty.gemspec
82
+ - lib/commaparty.rb
83
+ - lib/commaparty/destructure_element.rb
84
+ - lib/commaparty/html.rb
85
+ - lib/commaparty/markup.rb
86
+ - lib/commaparty/parse_tag.rb
87
+ - lib/commaparty/version.rb
88
+ - lib/commaparty/xml.rb
89
+ - spec/commaparty/destructure_element_spec.rb
90
+ - spec/commaparty/html_spec.rb
91
+ - spec/commaparty/markup_spec.rb
92
+ - spec/commaparty/parse_tag_spec.rb
93
+ - spec/commaparty/xml_spec.rb
94
+ homepage: https://github.com/ecmendenhall/commaparty
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>'
110
+ - !ruby/object:Gem::Version
111
+ version: 1.3.1
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Hiccup, with commas.
118
+ test_files:
119
+ - spec/commaparty/destructure_element_spec.rb
120
+ - spec/commaparty/html_spec.rb
121
+ - spec/commaparty/markup_spec.rb
122
+ - spec/commaparty/parse_tag_spec.rb
123
+ - spec/commaparty/xml_spec.rb