fira 0.3.6 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/fira.rb +38 -49
  2. data/lib/fira/engine.rb +39 -50
  3. data/lib/fira/version.rb +1 -1
  4. metadata +2 -2
@@ -1,67 +1,56 @@
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 = /<[^\/]?.*>/
3
+ ID_REGEX = /([^'"]*)#([a-z_A-Z\-]+)(.*)/
4
+ CLASS_REGEX = / \.([a-z_A-Z\-]+)/
6
5
 
7
- #begin parsing text
8
- def parse_text(contents)
9
-
10
- #find id's
11
- result = parse_ids(contents)
12
-
13
- #find classes
14
- output = parse_classes(result)
15
-
16
- end
6
+ def parse_text(text)
7
+ output = ""
8
+ tokenizer = HTML::Tokenizer.new(text)
9
+ while token = tokenizer.next
10
+ if token[0] == "<" and token[token.length - 1] == ">" and token[1] != "/"
11
+ #if it's an opening tag, analyze it
17
12
 
18
- #scan for ids and replace with html id attributes
19
- def parse_ids(contents)
20
-
21
- result = contents.gsub(ID_PATTERN, '\1 id="\2"')
22
- end
13
+ #find and replace fira ID attributes
14
+ result = token.sub(ID_REGEX, '\1 id="\2" \3')
23
15
 
24
- #scan for classes and create html class attributes
25
- def parse_classes(contents)
16
+ #find fira class attributes
17
+ classes = result.scan(CLASS_REGEX)
26
18
 
27
- result = contents
28
-
29
- #scan for classes
30
- tags = contents.scan(TAG_PATTERN)
31
- tags.each do |tag|
32
-
33
- classes = tag.scan(CLASS_PATTERN)
19
+ if ! classes.empty?
20
+
21
+ #build an HTML class attribute
22
+ att = 'class="'
23
+
24
+ classes.each do |cl|
25
+ att += " #{cl[0]}"
26
+ end
27
+
28
+ att += '"'
29
+
30
+ #remove the space before the first class
31
+ att = att.sub(/class=" /, ' class="')
34
32
 
33
+ #remove the first fira class attribute
34
+ new_tag = result.sub(CLASS_REGEX, att)
35
+
36
+ #remove the rest of the fira class attributes
37
+ final_result = new_tag.gsub(CLASS_REGEX, "")
35
38
 
36
- if ! classes.empty?
37
-
38
- #build a class attribute
39
- att = "class='"
40
-
41
- classes.each do |cl|
42
- att += " #{cl[0]}"
39
+ output += final_result
40
+ else
41
+ output += result
43
42
  end
44
-
45
- att += "'"
46
-
47
- #remove the space before the first class
48
- att = att.sub(/class=' /, "class='")
49
-
50
- #remove the fira class attributes
51
- new_tag = tag.gsub(CLASS_PATTERN, "")
52
43
 
53
- #save the html class attributes back into the tag
54
- new_tag = new_tag.sub(/>/,att + "\\0")
55
-
56
- #save the whole html tag back into the file
57
- result = result.sub(tag, new_tag)
44
+ else
45
+ output += token
58
46
  end
59
47
  end
60
-
61
- return result
48
+
49
+ return output
62
50
  end
63
51
 
64
52
 
53
+
65
54
  #####################
66
55
  # START HERE
67
56
  #####################
@@ -2,67 +2,56 @@ module Fira
2
2
 
3
3
  class Engine
4
4
 
5
- ID_PATTERN = /(<[^\/]?.*)#([a-z_A-Z\-]+)(.*>.*<\/.*>)/
6
- CLASS_PATTERN = /\.([a-z_A-Z\-]+)/
7
- TAG_PATTERN = /<\/?\w+\s+[^>]*>/
8
5
 
9
- #begin parsing text
10
- def parse_text(contents)
11
-
12
- #find id's
13
- result = parse_ids(contents)
14
-
15
- #find classes
16
- output = parse_classes(result)
17
-
18
- end
19
-
20
- #scan for ids and replace with html id attributes
21
- def parse_ids(contents)
22
-
23
- result = contents.gsub(ID_PATTERN, '\1 id="\2" \3')
24
- end
25
-
26
- #scan for classes and create html class attributes
27
- def parse_classes(contents)
6
+ ID_REGEX = /([^'"]*)#([a-z_A-Z\-]+)(.*)/
7
+ CLASS_REGEX = / \.([a-z_A-Z\-]+)/
28
8
 
29
- result = contents
30
-
31
- #scan for classes
32
- tags = contents.scan(TAG_PATTERN)
33
- tags.each do |tag|
9
+ def parse_text(text)
10
+ output = ""
11
+ tokenizer = HTML::Tokenizer.new(text)
12
+ while token = tokenizer.next
13
+ if token[0] == "<" and token[token.length - 1] == ">" and token[1] != "/"
14
+ #if it's an opening tag, analyze it
34
15
 
35
- classes = tag.scan(CLASS_PATTERN)
16
+ #find and replace fira ID attributes
17
+ result = token.sub(ID_REGEX, '\1 id="\2" \3')
36
18
 
19
+ #find fira class attributes
20
+ classes = result.scan(CLASS_REGEX)
37
21
 
38
- if ! classes.empty?
39
-
40
- #build a class attribute
41
- att = "class='"
42
-
43
- classes.each do |cl|
44
- att += " #{cl[0]}"
45
- end
22
+ if ! classes.empty?
46
23
 
47
- att += "'"
24
+ #build an HTML class attribute
25
+ att = 'class="'
48
26
 
49
- #remove the space before the first class
50
- att = att.sub(/class=' /, "class='")
27
+ classes.each do |cl|
28
+ att += " #{cl[0]}"
29
+ end
30
+
31
+ att += '"'
32
+
33
+ #remove the space before the first class
34
+ att = att.sub(/class=" /, ' class="')
35
+
36
+ #remove the first fira class attribute
37
+ new_tag = result.sub(CLASS_REGEX, att)
38
+
39
+ #remove the rest of the fira class attributes
40
+ final_result = new_tag.gsub(CLASS_REGEX, "")
41
+
42
+ output += final_result
43
+ else
44
+ output += result
45
+ end
51
46
 
52
- #remove the fira class attributes
53
- #first one
54
- new_tag = tag.sub(CLASS_PATTERN, att)
55
-
56
- #the rest of the fira class attributes
57
- new_tag = new_tag.gsub(CLASS_PATTERN, "")
58
-
59
- #save the whole html tag back into the file
60
- result = result.sub(tag, new_tag)
47
+ else
48
+ output += token
61
49
  end
62
50
  end
63
-
64
- return result
51
+
52
+ return output
65
53
  end
66
54
 
55
+
67
56
  end
68
57
  end
@@ -1,3 +1,3 @@
1
1
  module Fira
2
- VERSION = "0.3.6"
2
+ VERSION = "0.4.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.3.6
4
+ version: 0.4.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-03-25 00:00:00.000000000 Z
12
+ date: 2013-03-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport