fira 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 fira.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Cameron Sutter
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.
@@ -0,0 +1,39 @@
1
+ # Fira
2
+
3
+ ##Description
4
+
5
+ Fira adds ease-of-use functionality to HTML files. Instead of typing id="foo" or class="foo bar", you can simply type #foo or .foo .bar in HTML tags (in the normal place where you put id or class attributes)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'fira'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fira
20
+
21
+ ## Usage
22
+
23
+ To use Fira, rename HTML files to .html.fi and rails will automatically handle them.
24
+
25
+ ##Examples
26
+
27
+ ###Id's
28
+ <div #my_id&rt;
29
+
30
+ ###Classes
31
+ <div .multiple .classes&rt;
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #scan for ids and replace with html id attributes
4
+ def parse_ids(contents)
5
+ id_pattern = /(#)([a-z_A-Z\-]+)/
6
+ result = contents.gsub(id_pattern, 'id="\2"')
7
+ end
8
+
9
+ #scan for classes and create html class attributes
10
+ def parse_classes(contents, tag_pattern)
11
+ class_pattern = /\.([a-z_A-Z\-]+)/
12
+
13
+ result = contents
14
+
15
+ #scan for classes
16
+ tags = contents.scan(tag_pattern)
17
+ tags.each do |tag|
18
+
19
+ classes = tag.scan(class_pattern)
20
+
21
+ if ! classes.empty?
22
+
23
+ #build a class attribute
24
+ att = "class='"
25
+
26
+ classes.each do |cl|
27
+ att += " #{cl[0]}"
28
+ end
29
+
30
+ att += "'"
31
+
32
+ #remove the space before the first class
33
+ att = att.sub(/class=' /, "class='")
34
+
35
+ #remove the eml class attributes
36
+ new_tag = tag.gsub(class_pattern, "")
37
+
38
+ #save the html class attributes back into the tag
39
+ new_tag = new_tag.sub(/>/,att + "\\0")
40
+
41
+ #save the whole html tag back into the file
42
+ result = result.sub(tag, new_tag)
43
+ end
44
+ end
45
+
46
+ return result
47
+ end
48
+
49
+
50
+ #####################
51
+ # START HERE
52
+ #####################
53
+
54
+ files = []
55
+ ARGV.each do |a|
56
+ files.push a
57
+ end
58
+
59
+ files.each do |fi|
60
+ File.open(fi, 'r+'){
61
+ |f|
62
+ contents = f.read
63
+
64
+ tag_pattern = /<.*>/
65
+
66
+ result = parse_ids(contents)
67
+
68
+ result = parse_classes(result, tag_pattern)
69
+
70
+ #make the new file's name
71
+ new_file = fi.sub(/\.fi$/, ".html")
72
+
73
+ File.open(new_file, "w") { |nf| nf.write(result)}
74
+ }
75
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fira/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "fira"
8
+ gem.version = Fira::VERSION
9
+ gem.authors = ["Cameron Sutter"]
10
+ gem.email = ["cameronsutter0@gmail.com"]
11
+ gem.description = %q{Adds improvements to HTML syntax for id and class attributes}
12
+ gem.summary = %q{Smarter HTML}
13
+ gem.homepage = "https://github.com/cameronsutter/fira"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,15 @@
1
+ require "fira/version"
2
+
3
+ module Fira
4
+
5
+ def self.render(template)
6
+ text = template.source
7
+ @engine = Fira::Engine.new
8
+ output = @engine.parse_text(text)
9
+ return output
10
+ end
11
+
12
+ end
13
+
14
+ require 'fira/engine'
15
+ require 'fira/helpers'
@@ -0,0 +1,72 @@
1
+ module Fira
2
+
3
+ class Engine
4
+
5
+ ID_PATTERN = /(<.*)#([a-z_A-Z\-]+)(.*>.*<\/.*>)/
6
+ CLASS_PATTERN = /\.([a-z_A-Z\-]+)/
7
+ TAG_PATTERN = /<.*>/
8
+ OPEN_TAG_PATTERN = /(<.*>)(.*<\/.*>)/
9
+
10
+ #begin parsing text
11
+ def parse_text(contents)
12
+
13
+ #find id's
14
+ result = parse_ids(contents)
15
+
16
+ #find classes
17
+ output = parse_classes(result)
18
+
19
+ end
20
+
21
+ #scan for ids and replace with html id attributes
22
+ def parse_ids(contents)
23
+
24
+ result = contents.gsub(ID_PATTERN, '\1 id="\2" \3')
25
+ end
26
+
27
+ #scan for classes and create html class attributes
28
+ def parse_classes(contents)
29
+
30
+ result = contents
31
+
32
+ #scan for classes
33
+ tags = contents.scan(TAG_PATTERN)
34
+ tags.each do |tag|
35
+
36
+ open_tag = tag.scan(OPEN_TAG_PATTERN)
37
+
38
+ classes = open_tag[0][0].scan(CLASS_PATTERN)
39
+
40
+ if ! classes.empty?
41
+
42
+ #build a class attribute
43
+ att = "class='"
44
+
45
+ classes.each do |cl|
46
+ att += " #{cl[0]}"
47
+ end
48
+
49
+ att += "'"
50
+
51
+ #remove the space before the first class
52
+ att = att.sub(/class=' /, "class='")
53
+
54
+ #remove the fira class attributes
55
+ new_open_tag = open_tag[0][0].gsub(CLASS_PATTERN, "")
56
+
57
+ #save the html class attributes back into the tag
58
+ new_open_tag = new_open_tag.sub(/>/,att + "\\0")
59
+
60
+ #replace the old opening tag with the new
61
+ new_tag = tag.gsub(OPEN_TAG_PATTERN, new_open_tag + '\2')
62
+
63
+ #save the whole html tag back into the file
64
+ result = result.sub(tag, new_tag)
65
+ end
66
+ end
67
+
68
+ return result
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,83 @@
1
+ require 'action_view/template'
2
+
3
+ module Fira
4
+
5
+ class FiraHandler
6
+
7
+ def initialize
8
+
9
+ end
10
+
11
+ def render(contents, local_assigns = {})
12
+ return Fira::render(contents)
13
+ end
14
+
15
+ def self.call(template)
16
+ new.call(template)
17
+ end
18
+
19
+ def call(template)
20
+ output = ''
21
+ add_preamble(output)
22
+ txt = Fira::render(template)
23
+ add_text(output, txt)
24
+ add_postamble(output)
25
+
26
+ return output
27
+ end
28
+
29
+ #The next several methods are taken from the Erubis Template Handler erb.erb in action_view
30
+ def add_preamble(src)
31
+ src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
32
+ end
33
+
34
+ def add_text(src, text)
35
+ return if text.empty?
36
+ src << "@output_buffer.safe_concat('" << escape_text(text) << "');"
37
+ end
38
+
39
+ BLOCK_EXPR = /\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/
40
+
41
+ def add_expr_literal(src, code)
42
+ if code =~ BLOCK_EXPR
43
+ src << '@output_buffer.append= ' << code
44
+ else
45
+ src << '@output_buffer.append= (' << code << ');'
46
+ end
47
+ end
48
+
49
+ def add_expr_escaped(src, code)
50
+ if code =~ BLOCK_EXPR
51
+ src << "@output_buffer.safe_append= " << code
52
+ else
53
+ src << "@output_buffer.safe_concat((" << code << ").to_s);"
54
+ end
55
+ end
56
+
57
+ def add_postamble(src)
58
+ src << '@output_buffer.to_s'
59
+ end
60
+
61
+ def escape_text(text)
62
+ text.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\'
63
+ end
64
+ end
65
+ handler_klass = Fira::FiraHandler
66
+ ActionView::Template::register_default_template_handler :fi, handler_klass
67
+ end
68
+ # module ActionView
69
+ # module TemplateHandlers
70
+ # class FiraHandler < TemplateHandler
71
+
72
+ # def initialize(template)
73
+ # @template = template
74
+ # end
75
+
76
+ # def render(contents, local_assigns = {})
77
+ # return Fira::render(contents)
78
+ # end
79
+ # end
80
+ # end
81
+ # handler_klass = TemplateHandlers::FiraHandler
82
+ # Template.register_default_template_handler :fi, handler_klass
83
+ # end
@@ -0,0 +1,3 @@
1
+ module Fira
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fira
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cameron Sutter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Adds improvements to HTML syntax for id and class attributes
15
+ email:
16
+ - cameronsutter0@gmail.com
17
+ executables:
18
+ - fira.rb
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/fira.rb
28
+ - fira.gemspec
29
+ - lib/fira.rb
30
+ - lib/fira/engine.rb
31
+ - lib/fira/helpers.rb
32
+ - lib/fira/version.rb
33
+ homepage: https://github.com/cameronsutter/fira
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Smarter HTML
57
+ test_files: []