fira 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,17 +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
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 CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in fira.gemspec
4
- gemspec
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fira.gemspec
4
+ gemspec
@@ -1,22 +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
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
22
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -20,15 +20,15 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- To use Fira, rename HTML files to .html.fi and rails will automatically handle them.
23
+ To use Fira, rename HTML files to .html.fi and rails will automatically handle them. It will even evaluate embedded ruby code using Erubis. (note: Erubis runs after Fira)
24
24
 
25
25
  ##Examples
26
26
 
27
27
  ###Id's
28
- <div #my_id &rt;
28
+ <div #my_id >
29
29
 
30
30
  ###Classes
31
- <div .multiple .classes &rt;
31
+ <div .multiple .classes >
32
32
 
33
33
  ## Contributing
34
34
 
data/Rakefile CHANGED
@@ -1 +1 @@
1
- require "bundler/gem_tasks"
1
+ require "bundler/gem_tasks"
@@ -1,23 +1,40 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ ID_PATTERN = /(<.*)#([a-z_A-Z\-]+)(.*>.*<\/.*>)/
4
+ CLASS_PATTERN = /\.([a-z_A-Z\-]+)/
5
+ TAG_PATTERN = /<.*>/
6
+ OPEN_TAG_PATTERN = /(<.*>)(.*<\/.*>)/
7
+
8
+ #begin parsing text
9
+ def parse_text(contents)
10
+
11
+ #find id's
12
+ result = parse_ids(contents)
13
+
14
+ #find classes
15
+ output = parse_classes(result)
16
+
17
+ end
18
+
3
19
  #scan for ids and replace with html id attributes
4
20
  def parse_ids(contents)
5
- id_pattern = /(#)([a-z_A-Z\-]+)/
6
- result = contents.gsub(id_pattern, 'id="\2"')
21
+
22
+ result = contents.gsub(ID_PATTERN, '\1 id="\2" \3')
7
23
  end
8
24
 
9
25
  #scan for classes and create html class attributes
10
- def parse_classes(contents, tag_pattern)
11
- class_pattern = /\.([a-z_A-Z\-]+)/
12
-
26
+ def parse_classes(contents)
27
+
13
28
  result = contents
14
29
 
15
30
  #scan for classes
16
- tags = contents.scan(tag_pattern)
31
+ tags = contents.scan(TAG_PATTERN)
17
32
  tags.each do |tag|
18
-
19
- classes = tag.scan(class_pattern)
20
-
33
+
34
+ open_tag = tag.scan(OPEN_TAG_PATTERN)
35
+
36
+ classes = open_tag[0][0].scan(CLASS_PATTERN)
37
+
21
38
  if ! classes.empty?
22
39
 
23
40
  #build a class attribute
@@ -32,12 +49,15 @@ def parse_classes(contents, tag_pattern)
32
49
  #remove the space before the first class
33
50
  att = att.sub(/class=' /, "class='")
34
51
 
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")
52
+ #remove the fira class attributes
53
+ new_open_tag = open_tag[0][0].gsub(CLASS_PATTERN, "")
40
54
 
55
+ #save the html class attributes back into the tag
56
+ new_open_tag = new_open_tag.sub(/>/,att + "\\0")
57
+
58
+ #replace the old opening tag with the new
59
+ new_tag = tag.gsub(OPEN_TAG_PATTERN, new_open_tag + '\2')
60
+
41
61
  #save the whole html tag back into the file
42
62
  result = result.sub(tag, new_tag)
43
63
  end
@@ -68,7 +88,7 @@ files.each do |fi|
68
88
  result = parse_classes(result, tag_pattern)
69
89
 
70
90
  #make the new file's name
71
- new_file = fi.sub(/\.fi$/, ".html")
91
+ new_file = fi.sub(/(\.fi$|\.html.fi$)/, ".html")
72
92
 
73
93
  File.open(new_file, "w") { |nf| nf.write(result)}
74
94
  }
@@ -2,10 +2,9 @@ require "fira/version"
2
2
 
3
3
  module Fira
4
4
 
5
- def self.render(template)
6
- text = template.source
5
+ def self.render(source)
7
6
  @engine = Fira::Engine.new
8
- output = @engine.parse_text(text)
7
+ output = @engine.parse_text(source)
9
8
  return output
10
9
  end
11
10
 
@@ -35,33 +35,36 @@ module Fira
35
35
 
36
36
  open_tag = tag.scan(OPEN_TAG_PATTERN)
37
37
 
38
- classes = open_tag[0][0].scan(CLASS_PATTERN)
38
+ if ! open_tag.empty?
39
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, "")
40
+ classes = open_tag[0][0].scan(CLASS_PATTERN)
56
41
 
57
- #save the html class attributes back into the tag
58
- new_open_tag = new_open_tag.sub(/>/,att + "\\0")
42
+ if ! classes.empty?
59
43
 
60
- #replace the old opening tag with the new
61
- new_tag = tag.gsub(OPEN_TAG_PATTERN, new_open_tag + '\2')
44
+ #build a class attribute
45
+ att = "class='"
62
46
 
63
- #save the whole html tag back into the file
64
- result = result.sub(tag, new_tag)
47
+ classes.each do |cl|
48
+ att += " #{cl[0]}"
49
+ end
50
+
51
+ att += "'"
52
+
53
+ #remove the space before the first class
54
+ att = att.sub(/class=' /, "class='")
55
+
56
+ #remove the fira class attributes
57
+ new_open_tag = open_tag[0][0].gsub(CLASS_PATTERN, "")
58
+
59
+ #save the html class attributes back into the tag
60
+ new_open_tag = new_open_tag.sub(/>/,att + "\\0")
61
+
62
+ #replace the old opening tag with the new
63
+ new_tag = tag.gsub(OPEN_TAG_PATTERN, new_open_tag + '\2')
64
+
65
+ #save the whole html tag back into the file
66
+ result = result.sub(tag, new_tag)
67
+ end
65
68
  end
66
69
  end
67
70
 
@@ -1,4 +1,5 @@
1
1
  require 'action_view/template'
2
+ require 'erubis'
2
3
 
3
4
  module Fira
4
5
 
@@ -17,67 +18,14 @@ module Fira
17
18
  end
18
19
 
19
20
  def call(template)
20
- output = ''
21
- add_preamble(output)
22
- txt = Fira::render(template)
23
- add_text(output, txt)
24
- add_postamble(output)
25
21
 
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/
22
+ results = Fira::render(template.source)
40
23
 
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
24
+ input = ActionView::Template::new(results, template.identifier, template.handler, {:format => template.formats[0]})
25
+ output = ActionView::Template::Handlers::ERB::call(input)
47
26
  end
48
27
 
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
28
  end
65
29
  handler_klass = Fira::FiraHandler
66
30
  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
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Fira
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fira
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-22 00:00:00.000000000 Z
12
+ date: 2013-01-23 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Adds improvements to HTML syntax for id and class attributes
15
15
  email: