general 1.5.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +166 -20
  3. data/Rakefile +11 -2
  4. data/exp/expected/applied1.txt +1 -0
  5. data/exp/expected/applied2.txt +3 -0
  6. data/exp/expected/applied3.txt +9 -0
  7. data/exp/expected/default1.txt +1 -0
  8. data/exp/expected/default2.txt +3 -0
  9. data/exp/expected/default3.txt +9 -0
  10. data/exp/out/sample1.txt +1 -0
  11. data/exp/out/sample2.txt +3 -0
  12. data/exp/out/sample3.txt +9 -0
  13. data/exp/templates/sample0.general +7 -0
  14. data/exp/templates/sample1.general +1 -0
  15. data/exp/templates/sample2.general +3 -0
  16. data/exp/templates/sample3.general +4 -0
  17. data/exp/templates/sampleE0.general +7 -0
  18. data/exp/templates/sampleE1.general +3 -0
  19. data/exp/templates/sampleE2.general +3 -0
  20. data/exp/templates/sampleE3.general +7 -0
  21. data/lib/general.rb +3 -3
  22. data/lib/gerror.rb +33 -0
  23. data/lib/goperations.rb +95 -27
  24. data/lib/gpartials/garrayplaceholder.rb +87 -0
  25. data/lib/gpartials/gfullplaceholder.rb +63 -0
  26. data/lib/gpartials/gpartial.rb +11 -1
  27. data/lib/gpartials/gplaceholder.rb +1 -52
  28. data/lib/gpartials/gspecial.rb +81 -0
  29. data/lib/gpartials/gtext.rb +5 -33
  30. data/lib/gprepartials/gextend.rb +42 -0
  31. data/lib/gprepartials/ginclude.rb +47 -0
  32. data/lib/gprepartials/gprepartial.rb +45 -0
  33. data/lib/gprepartials/gpretext.rb +33 -0
  34. data/lib/gprepartials/gyield.rb +33 -0
  35. data/lib/{templates → gtemplates}/gbasetemplate.rb +46 -3
  36. data/lib/gtemplates/gio.rb +143 -0
  37. data/lib/gtemplates/gmeta.rb +106 -0
  38. data/lib/{templates → gtemplates}/gtemplate.rb +10 -30
  39. data/lib/{templates → gtemplates}/gtimeformat.rb +6 -12
  40. data/spec/gdothash_spec.rb +6 -10
  41. data/spec/goperations_spec.rb +112 -82
  42. data/spec/gpartial_garrayplaceholder_spec.rb +165 -0
  43. data/spec/gpartial_gfullplaceholder_spec.rb +82 -0
  44. data/spec/gpartial_gplaceholder_spec.rb +237 -0
  45. data/spec/gpartial_gspecial_spec.rb +88 -0
  46. data/spec/gpartial_gtext_spec.rb +87 -0
  47. data/spec/gtamplate_gtemplate_spec.rb +266 -0
  48. data/spec/gtemplate_gio_spec.rb +147 -0
  49. data/spec/gtemplate_gtimeformat_spec.rb +77 -0
  50. data/spec/spec_require.rb +7 -4
  51. metadata +41 -13
  52. data/exp/future.general +0 -11
  53. data/exp/out.txt +0 -1
  54. data/exp/sample.general +0 -1
  55. data/lib/templates/gio.rb +0 -68
  56. data/spec/garrayplaceholder_spec.rb +0 -163
  57. data/spec/gplaceholder_spec.rb +0 -300
  58. data/spec/gtemplates_spec.rb +0 -480
  59. data/spec/gtext_spec.rb +0 -150
@@ -0,0 +1,87 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ require_relative "gpartial"
18
+ require_relative "../goperations"
19
+
20
+ # General is a templating system in ruby
21
+ #
22
+ # Author: Anshul Kharbanda
23
+ # Created: 3 - 4 - 2016
24
+ module General
25
+ # Represents an array placeholder partial in a GTemplate
26
+ #
27
+ # Author: Anshul Kharbanda
28
+ # Created: 7 - 1 - 2016
29
+ class GArrayPlaceholder < GPartial
30
+ # Regular expression that matches array placeholders
31
+ REGEX = /\A@\[#{NAME}\s*(#{OPERATION}\s*#{ARGUMENTS}?)?\]( +|\n+)?(?<text>.*?)( +|\n+)?@\[(?<delimeter>.+)?\]/m
32
+
33
+ # Default delimeter
34
+ DEFAULT_DELIMETER = " "
35
+
36
+ # Initializes the GArrayPlaceholder with the given match
37
+ #
38
+ # Parameter: match - the match data from the string being parsed
39
+ # Parameter: defaults - the hash of default data from the GTemplate
40
+ def initialize match, defaults={}
41
+ super
42
+ @template = General::GTemplate.new match[:text]
43
+ @delimeter = match[:delimeter] || DEFAULT_DELIMETER
44
+ @operation = match[:operation]
45
+ if match[:arguments]
46
+ @arguments = match[:arguments].gsub(ARGUMENT).collect { |arg|
47
+ ARGUMENT.match(arg)[:text]
48
+ }
49
+ else
50
+ @arguments = []
51
+ end
52
+ end
53
+
54
+ # Returns the value of the array placeholder in the given data
55
+ # formatted by the given GTemplate and joined by the given delimeter
56
+ #
57
+ # Parameter: data - the data being applied
58
+ #
59
+ # Return: the value of the array placeholder in the given data
60
+ # formatted by the given GTemplate and joined by the given delimeter
61
+ def apply(data)
62
+ array = (@operation ? General::GOperations.send(@operation, data[@name], *@arguments) : data[@name])
63
+ return @template.apply_all(array).join(@delimeter)
64
+ end
65
+
66
+ # Throws TypeError
67
+ #
68
+ # Parameter: first - true if the placeholder is the first of it's kind
69
+ def regex(first=true); raise TypeError.new("Array Templates cannot be matched."); end
70
+
71
+ # Returns the string representation of the array placeholder
72
+ #
73
+ # Parameter: first - true if the placeholder is the first of it's kind
74
+ #
75
+ # Return: the string representation of the array placeholder
76
+ def string(first=true)
77
+ str = "@[#{@name}"
78
+ if @operation
79
+ str += " -> #{@operation}"
80
+ unless @arguments.empty?
81
+ str += @arguments.collect{|s| " \"#{s}\""}.join
82
+ end
83
+ end
84
+ return str + "] #{@template.to_s} @[#{@delimeter.inspect[1...-1]}]"
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,63 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ require_relative "gpartial"
18
+ require_relative "../goperations"
19
+
20
+ # General is a templating system in ruby
21
+ #
22
+ # Author: Anshul Kharbanda
23
+ # Created: 3 - 4 - 2016
24
+ module General
25
+ # Inserts the full data value passed into the template
26
+ #
27
+ # Author: Anshul Kharbanda
28
+ # Created: 1 - 19 - 2016
29
+ class GFullPlaceholder < GPartial
30
+ # Matches GFullPlaceholders
31
+ REGEX = /@#/
32
+
33
+ # String representation of GFullPlaceholder
34
+ STRING = "@#"
35
+
36
+ # Initializes the GFullPlaceholder with the given match
37
+ #
38
+ # Parameter: match - the match data from the string being parsed
39
+ # Parameter: defaults - the hash of default data from the GTemplate
40
+ def initialize(match, defaults={}); super({name: :__full}, defaults); end
41
+
42
+ # Returns a string representation of the given data
43
+ #
44
+ # Parameter: data - the data being applied
45
+ #
46
+ # Returns: a string representation of the given data
47
+ def apply(data); data.to_s; end
48
+
49
+ # Returns a string representation of the GFullPlaceholder
50
+ #
51
+ # Parameter: first - true if this is the first in a given template
52
+ #
53
+ # Returns: a string representation of the GFullPlaceholder
54
+ def string(first=false); STRING; end
55
+
56
+ # Raises TypeError
57
+ #
58
+ # Parameter: first - true if this is the first in a given template
59
+ #
60
+ # Raises: TypeError
61
+ def regex(first=false); raise TypeError.new("GFullPlaceholder cannot be matched"); end
62
+ end
63
+ end
@@ -27,6 +27,15 @@ module General
27
27
  class GPartial
28
28
  protected
29
29
 
30
+ # Regular expression that matches a single placeholder
31
+ ARGUMENT = /(?<text>\w+)|((?<qtat>'|")(?<text>.*)\k<qtat>)/
32
+
33
+ # Regular expression that matches placeholder arguments
34
+ ARGUMENTS = /(?<arguments>(#{ARGUMENT}\s*)*)/
35
+
36
+ # Regular expression that matches placeholder operations
37
+ OPERATION = /(->\s*(?<operation>\w+))/
38
+
30
39
  # Regular expression that matches placeholder names
31
40
  NAME = /(?<name>[a-zA-Z]\w*(\.[a-zA-Z]\w*)*)/
32
41
 
@@ -38,6 +47,7 @@ module General
38
47
  # Initializes the GPartial with the given object
39
48
  #
40
49
  # Parameter: obj - the object containing information for the partial
41
- def initialize(obj); @name = obj[:name].to_sym; end
50
+ # Parameter: dft - the hash of default data from the GTemplate
51
+ def initialize(obj,dft={}); @name = obj[:name].to_sym; end
42
52
  end
43
53
  end
@@ -29,15 +29,6 @@ module General
29
29
  class GPlaceholder < GPartial
30
30
  private
31
31
 
32
- # Regular expression that matches a single placeholder
33
- ARGUMENT = /(?<text>\w+)|((?<qtat>'|")(?<text>.*)\k<qtat>)/
34
-
35
- # Regular expression that matches placeholder arguments
36
- ARGUMENTS = /(?<arguments>(#{ARGUMENT}\s*)*)/
37
-
38
- # Regular expression that matches placeholder operations
39
- OPERATION = /(->\s*(?<operation>\w+))/
40
-
41
32
  # Regular expression that matches placeholder defaults
42
33
  DEFAULT = /(\:\s*(?<default>[^(\-\>)]+))/
43
34
 
@@ -51,7 +42,7 @@ module General
51
42
  # Parameter: match - the match data from the string being parsed
52
43
  # Parameter: defaults - the hash of default data from the GTemplate
53
44
  def initialize match, defaults
54
- super match
45
+ super match, defaults
55
46
  @operation = match[:operation]
56
47
  if match[:arguments]
57
48
  @arguments = match[:arguments].gsub(ARGUMENT).collect { |arg|
@@ -111,46 +102,4 @@ module General
111
102
  return str + ")"
112
103
  end
113
104
  end
114
-
115
- # Represents an array placeholder partial in a GTemplate
116
- #
117
- # Author: Anshul Kharbanda
118
- # Created: 7 - 1 - 2016
119
- class GArrayPlaceholder < GPartial
120
- # Regular expression that matches array placeholders
121
- REGEX = /\A@\[#{NAME}\]( +|\n+)?(?<text>.*?)( +|\n+)?@\[(?<delimeter>.+)?\]/m
122
-
123
- # Default delimeter
124
- DEFAULT_DELIMETER = " "
125
-
126
- # Initializes the GPlaceholder with the given match
127
- #
128
- # Parameter: match - the match data from the string being parsed
129
- def initialize match
130
- super
131
- @delimeter = match[:delimeter] || DEFAULT_DELIMETER
132
- @template = General::GTemplate.new match[:text]
133
- end
134
-
135
- # Returns the value of the array placeholder in the given data
136
- # formatted by the given GTemplate and joined by the given delimeter
137
- #
138
- # Parameter: data - the data being applied
139
- #
140
- # Return: the value of the array placeholder in the given data
141
- # formatted by the given GTemplate and joined by the given delimeter
142
- def apply(data); @template.apply_all(data[@name]).join(@delimeter); end
143
-
144
- # Throws TypeError
145
- #
146
- # Parameter: first - true if the placeholder is the first of it's kind
147
- def regex(first=true); raise TypeError.new("Array Templates cannot be matched."); end
148
-
149
- # Returns the string representation of the array placeholder
150
- #
151
- # Parameter: first - true if the placeholder is the first of it's kind
152
- #
153
- # Return: the string representation of the array placeholder
154
- def string(first=true); "@[#{@name}] #{@template.to_s} @[#{@delimeter.inspect[1...-1]}]"; end
155
- end
156
105
  end
@@ -0,0 +1,81 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ require_relative "gtext"
18
+
19
+ # General is a templating system in ruby
20
+ #
21
+ # Author: Anshul Kharbanda
22
+ # Created: 3 - 4 - 2016
23
+ module General
24
+ # Represents a special character in a GTemplate
25
+ #
26
+ # Author: Anshul Kharbanda
27
+ # Created: 7 - 29 - 2016
28
+ class GSpecial < GText
29
+ # Regular expression that matches special partials
30
+ REGEX = /\A@(?<key>\w+)\;/
31
+
32
+ # Special character information
33
+ SPECIALS = {
34
+ at: "@", pd: "#",
35
+ lt: "<", gt: ">",
36
+ op: "(", cp: ")",
37
+ ob: "[", cb: "]",
38
+ oc: "{", cc: "}",
39
+ ms: "-", ps: "+",
40
+ st: "*", pc: "%",
41
+ bs: "\\", fs: "/",
42
+ dl: "$"
43
+ }
44
+
45
+ # Special regex information
46
+ REGEXES = {
47
+ at: /@/, pd: /#/,
48
+ lt: /\</, gt: /\>/,
49
+ op: /\(/, cp: /\)/,
50
+ ob: /\[/, cb: /\]/,
51
+ oc: /\{/, cc: /\}/,
52
+ ms: /\-/, ps: /\+/,
53
+ st: /\*/, pc: /\%/,
54
+ bs: /\\/, fs: /\//,
55
+ dl: /\$/
56
+ }
57
+
58
+ # Initializes the GSpecial with the given match
59
+ #
60
+ # Parameter: match - the match object of the GSpecial
61
+ # Parameter: defaults - the hash of default data from the GTemplate
62
+ def initialize(match, defaults={})
63
+ super SPECIALS[match[:key].to_sym], {}
64
+ @key = match[:key].to_sym
65
+ end
66
+
67
+ # Returns the string representation of the GSpecial
68
+ #
69
+ # Parameter: first - true if this partial is the first of it's kind in a GTemplate
70
+ #
71
+ # Returns: the string representation of the GSpecial
72
+ def string(first=true); "@#{@key.to_s};"; end
73
+
74
+ # Returns the GSpecial as a regex
75
+ #
76
+ # Parameter: first - true if this partial is the first of it's kind in a GTemplate
77
+ #
78
+ # Returns: the GSpecial as a regex
79
+ def regex(first=true); REGEXES[@key]; end
80
+ end
81
+ end
@@ -30,13 +30,14 @@ module General
30
30
  REGEX = /\A[^@]+?(?=(@|\\@|\z))/m
31
31
 
32
32
  # The partial name of a text
33
- PTNAME = :gpartialstring
33
+ PTNAME = :__text
34
34
 
35
35
  # Initializes the GText with the given match
36
36
  #
37
- # Parameter: match - the match object of the GText
38
- def initialize(match)
39
- super name: PTNAME
37
+ # Parameter: match - the match object of the GText
38
+ # Parameter: defaults - the hash of default data from the GTemplate
39
+ def initialize match, defaults={}
40
+ super({name: PTNAME}, defaults)
40
41
  @text = match.to_s
41
42
  end
42
43
 
@@ -61,33 +62,4 @@ module General
61
62
  # Returns: the text as a regex
62
63
  def regex(first=true); @text.inspect[1...-1].gsub(/[\.\+\-\*]/) { |s| "\\#{s}" }; end
63
64
  end
64
-
65
- # Represents a special character in a GTemplate
66
- #
67
- # Author: Anshul Kharbanda
68
- # Created: 7 - 29 - 2016
69
- class GSpecial < GText
70
- # Regular expression that matches special partials
71
- REGEX = /\A@(?<key>\w+)\;/
72
-
73
- # Special character information
74
- SPECIALS = {
75
- at: "@", pd: "#",
76
- lt: "<", gt: ">",
77
- op: "(", cp: ")",
78
- ob: "[", cb: "]",
79
- oc: "{", cc: "}",
80
- ms: "-", ps: "+",
81
- st: "*", pc: "%",
82
- bs: "\\", fs: "/",
83
- dl: "$",
84
- }
85
-
86
- # Initializes the GSpecial with the given match
87
- #
88
- # Parameter: match - the match object of the GSpecial
89
- def initialize(match)
90
- super SPECIALS[match[:key].to_sym]
91
- end
92
- end
93
65
  end
@@ -0,0 +1,42 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ # Imports
18
+ require_relative "gprepartial"
19
+ require_relative "../gtemplates/gio"
20
+
21
+ # General is a templating system in ruby
22
+ #
23
+ # Author: Anshul Kharbanda
24
+ # Created: 3 - 4 - 2016
25
+ module General
26
+ # Includes another file in the template
27
+ #
28
+ # Author: Anshul Kharbanda
29
+ # Created: 1 - 30 - 2017
30
+ class GExtend < GPrePartial
31
+ # Regular expression matches GExtend
32
+ REGEX = /\A@@extend\s+(?<filename>\w+)\r?\n/
33
+
34
+ # Read filename
35
+ attr :filename
36
+
37
+ # Creates a new GExtend
38
+ #
39
+ # Parameters: match - the match result returned from the parser
40
+ def initialize(match); super; @filename = match[:filename]; end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ # Imports
18
+ require_relative "gprepartial"
19
+ require_relative "../gtemplates/gio"
20
+
21
+ # General is a templating system in ruby
22
+ #
23
+ # Author: Anshul Kharbanda
24
+ # Created: 3 - 4 - 2016
25
+ module General
26
+ # Includes another file in the template
27
+ #
28
+ # Author: Anshul Kharbanda
29
+ # Created: 1 - 30 - 2017
30
+ class GInclude < GPrePartial
31
+ # Regular expression matches GInclude
32
+ REGEX = /\A@@include\s+(?<filename>\w+)\r?\n/
33
+
34
+ # Creates a new GInclude
35
+ #
36
+ # Parameters: match - the match result returned from the parser
37
+ def initialize(match)
38
+ super
39
+ @filename = match[:filename]
40
+ end
41
+
42
+ # Applies the GInclude
43
+ #
44
+ # Return: the value returned from the executed GInclude
45
+ def apply; IO.read(@filename+General::GIO::EXTENSION) + "\r\n"; end
46
+ end
47
+ end
@@ -0,0 +1,45 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ # Imports
18
+ require_relative "../gtemplates/gio"
19
+
20
+ # General is a templating system in ruby
21
+ #
22
+ # Author: Anshul Kharbanda
23
+ # Created: 3 - 4 - 2016
24
+ module General
25
+ # Includes another file in the template
26
+ #
27
+ # Author: Anshul Kharbanda
28
+ # Created: 1 - 30 - 2017
29
+ class GPrePartial
30
+ # Creates a new GPrePartial
31
+ #
32
+ # Parameters: match - the match result returned from the parser
33
+ def initialize(match); @text = match.to_s; end
34
+
35
+ # Applies the GPrePartial
36
+ #
37
+ # Return: the value returned from the executed GPrePartial
38
+ def apply; @text; end
39
+
40
+ # Returns the string representation of the GPrePartial
41
+ #
42
+ # Return: the string representation of the GPrePartial
43
+ def to_s; @text; end
44
+ end
45
+ end
@@ -0,0 +1,33 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ # Imports
18
+ require_relative "gprepartial"
19
+
20
+ # General is a templating system in ruby
21
+ #
22
+ # Author: Anshul Kharbanda
23
+ # Created: 3 - 4 - 2016
24
+ module General
25
+ # Matches plain General Text
26
+ #
27
+ # Author: Anshul Kharbanda
28
+ # Created: 1 - 30 - 2017
29
+ class GPretext < GPrePartial
30
+ # Regular expression matches GPretext
31
+ REGEX = /\A(?<text>.+?)(?=@@|\z)/m
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ # Imports
18
+ require_relative "gprepartial"
19
+
20
+ # General is a templating system in ruby
21
+ #
22
+ # Author: Anshul Kharbanda
23
+ # Created: 3 - 4 - 2016
24
+ module General
25
+ # Matches plain General Text
26
+ #
27
+ # Author: Anshul Kharbanda
28
+ # Created: 1 - 30 - 2017
29
+ class GYield < GPrePartial
30
+ # Regular expression matches GYield
31
+ REGEX = /\A@@yield\r?\n/
32
+ end
33
+ end
@@ -14,6 +14,8 @@
14
14
  # You should have received a copy of the GNU General Public License
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
+ require_relative "../gdothash"
18
+
17
19
  # General is a templating system in ruby
18
20
  #
19
21
  # Author: Anshul Kharbanda
@@ -28,9 +30,31 @@ module General
28
30
  class GBaseTemplate
29
31
  # Initializes the GBaseTemplate with the given string
30
32
  #
31
- # Parameter: string - the string to initialize the GBaseTemplate with
32
- def initialize string
33
+ # Parameter: string - the string to initialize the GBaseTemplate with
34
+ # Parameter: partials - the partials used to parse the GBaseTemplate string
35
+ def initialize string, partials
36
+ # Variables
33
37
  @partials = []
38
+ @defaults = General::GDotHash.new
39
+
40
+ # Partial breakdown algorithm
41
+ m = nil
42
+ loop do
43
+ # Match the front of the string to a partial
44
+ partial = partials.find { |partial| m = partial::REGEX.match(string) }
45
+
46
+ # Raise error if no matching partial can be found
47
+ raise GError.new("Unmatched partial at #{(string.length <= 5 ? string : string[0..5] + "...").inspect}") if partial.nil?
48
+
49
+ # Add the partial to the array
50
+ @partials << partial.new(m, @defaults)
51
+
52
+ # Trim the front of the string
53
+ string = string[m.end(0)..-1]
54
+
55
+ # End when string is empty
56
+ return if string.length == 0
57
+ end
34
58
  end
35
59
 
36
60
  # Applies the given data to the template and returns the generated string
@@ -38,7 +62,14 @@ module General
38
62
  # Parameter: data - the data to be applied (as a hash. merges with defaults)
39
63
  #
40
64
  # Return: string of the template with the given data applied
41
- def apply(data={}); @partials.collect { |partial| partial.apply(data) }.join; end
65
+ def apply data={}
66
+ # Apply generalized data if can be generalized. Else apply regular data
67
+ if data.respond_to? :generalized
68
+ return apply(data.generalized)
69
+ else
70
+ return @partials.collect { |part| part.apply(data) }.join
71
+ end
72
+ end
42
73
 
43
74
  # Applies each data structure in the array independently to the template
44
75
  # and returns an array of the generated strings
@@ -51,5 +82,17 @@ module General
51
82
  def apply_all array
52
83
  array.collect { |data| apply(data) }
53
84
  end
85
+
86
+ # Returns a string representation of the template
87
+ #
88
+ # Return: a string representation of the template
89
+ def to_s
90
+ first = Hash.new(true); str = ""
91
+ @partials.each do |part|
92
+ str += part.string(first[part.name])
93
+ first[part.name] &&= false
94
+ end
95
+ return str
96
+ end
54
97
  end
55
98
  end