markdown-expander 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dec4cfc4d81d3e1fb1bd1c9d819fb1787725184e
4
- data.tar.gz: 9fb3a69fc05ccc7f5fe004c9d23382f1e33aaa50
3
+ metadata.gz: f5cc0aa3fd21ded1886f9c0cd66093d376f734de
4
+ data.tar.gz: 2202dadac71019f67a5739be0183fa3e0a019788
5
5
  SHA512:
6
- metadata.gz: 286118a7ac49628c4c0915b34b6d8525e0f070d356934bd143c9dd491a84d866a86722cca97295b3a4ca371ccd9c8c52e6e0feeaf4844d849746699906e540ac
7
- data.tar.gz: a3fb4536c86024b03051eb7c8c9c4a0f42d3be1628d572220849fe236c399a4e3a4402322da52f144d8f361eaffb1f18e2c8d90c04d40d10a11f63ac72e211a8
6
+ metadata.gz: d18c4084c7dd37e24e77935d32d0428f6a8f09988447119a9f848f9e271bbb8f7cd560d7325ff8568514b1696b0690fe0646eab30e9ef402015ba5e327518b0c
7
+ data.tar.gz: 8deb31e8fd6843b9d6ebf99c90a39e63dff7c7c0becba7a697bb1e7d132ccfec05b95919b09b1975bd9a14fb75ffdd639d645dbeb536a24a1a8108a83f5939aa
data/README.md CHANGED
@@ -58,3 +58,22 @@ MarkdownExpander::Expander.new(template).render(
58
58
  )
59
59
  #=> "# First!\n# Second!\n"
60
60
  ```
61
+
62
+ ## Logic
63
+
64
+ ```ruby
65
+ require "markdown-expander"
66
+
67
+ template = <<-TEMPLATE
68
+ {{animal in animals}}
69
+ {{if animal.name == "cats"}}
70
+ # {{animal.name}} are the best!!!!!
71
+ {{end}}
72
+ {{end}}
73
+ TEMPLATE
74
+
75
+ MarkdownExpander::Expander.new(template).render(
76
+ animals: [ {name: "dogs"}, {name: "cats"} ]
77
+ )
78
+ #=> "# cats are the best!!!!!\n"
79
+ ```
data/README.md.docu CHANGED
@@ -32,6 +32,7 @@ Or install it yourself as:
32
32
  ## Expressions
33
33
 
34
34
  ```ruby
35
+ :example:
35
36
  require "markdown-expander"
36
37
 
37
38
  MarkdownExpander::Expander.new(
@@ -40,6 +41,7 @@ MarkdownExpander::Expander.new(
40
41
  thing: {title: "Hello"}
41
42
  )
42
43
  #=> "# Title: Hello"
44
+ :end:
43
45
  ```
44
46
 
45
47
  ## Loops
@@ -60,3 +62,24 @@ MarkdownExpander::Expander.new(template).render(
60
62
  #=> "# First!\n# Second!\n"
61
63
  :end:
62
64
  ```
65
+
66
+ ## Logic
67
+
68
+ ```ruby
69
+ :example:
70
+ require "markdown-expander"
71
+
72
+ template = <<-TEMPLATE
73
+ {{animal in animals}}
74
+ {{if animal.name == "cats"}}
75
+ # {{animal.name}} are the best!!!!!
76
+ {{end}}
77
+ {{end}}
78
+ TEMPLATE
79
+
80
+ MarkdownExpander::Expander.new(template).render(
81
+ animals: [ {name: "dogs"}, {name: "cats"} ]
82
+ )
83
+ #=> "# cats are the best!!!!!\n"
84
+ :end:
85
+ ```
@@ -11,7 +11,7 @@ module MarkdownExpander
11
11
  @template = template
12
12
  end
13
13
 
14
- def render options
14
+ def render scope
15
15
  root = Node.new(nil, nil)
16
16
  node = root
17
17
 
@@ -20,12 +20,12 @@ module MarkdownExpander
20
20
  new_node = Node.new(node, LoopStart.new($1, $2))
21
21
  node.children << new_node
22
22
  node = new_node
23
- elsif line =~ END_MATCH
24
- node = node.parent
25
23
  elsif line =~ IF_START_MATCH
26
24
  new_node = Node.new(node, IfStart.new($1, $2, $3))
27
25
  node.children << new_node
28
26
  node = new_node
27
+ elsif line =~ END_MATCH
28
+ node = node.parent
29
29
  else
30
30
  loop do
31
31
  if line =~ EXPRESSION_MATCH
@@ -42,39 +42,38 @@ module MarkdownExpander
42
42
  end
43
43
  end
44
44
 
45
- evaluate_nodes(root, options)
45
+ evaluate_nodes(root, scope)
46
+ end
47
+
48
+ def drill_down_to_value scope, expression
49
+ expression_parts = expression.split(".").map(&:to_sym)
50
+ expression_parts.each do |part|
51
+ scope = scope[part]
52
+ end
53
+ scope
46
54
  end
47
55
 
48
56
  def evaluate_nodes root, scope
49
57
  lines = []
50
58
  root.children.each_with_index do |child, index|
51
- if child.value.class == Expression
52
- lines << child.value.evaluate(scope)
53
- elsif child.value.class == LoopStart
54
- name = child.value.name.to_sym
55
- parts = child.value.looper.split(".")
56
- parts.each do |part|
57
- scope = scope[part.to_sym]
58
- end
59
+ if child.element.class == Expression
60
+ lines << drill_down_to_value(scope, child.element.expression)
61
+ elsif child.element.class == LoopStart
62
+ name = child.element.name.to_sym
63
+ scope = drill_down_to_value(scope, child.element.expression)
59
64
  scope.each do |item|
60
65
  lines << evaluate_nodes(child, {name => item})
61
66
  end
62
- elsif child.value.class == IfStart
63
- parts = child.value.expression.split(".")
64
- value = scope
65
- parts.each do |part|
66
- value = value[part.to_sym]
67
- end
67
+ elsif child.element.class == IfStart
68
+ value = drill_down_to_value(scope, child.element.expression)
68
69
  expression_satisfied =
69
- (child.value.operator == "==" && value.to_s == child.value.value) ||
70
- (child.value.operator == "!=" && value.to_s != child.value.value)
70
+ (child.element.operator == "==" && value.to_s == child.element.value) ||
71
+ (child.element.operator == "!=" && value.to_s != child.element.value)
71
72
  if expression_satisfied
72
- scope.each do |item|
73
- lines << evaluate_nodes(child, scope)
74
- end
73
+ lines << evaluate_nodes(child, scope)
75
74
  end
76
75
  else
77
- lines << child.value
76
+ lines << child.element
78
77
  end
79
78
  end
80
79
  lines.join("")
@@ -83,19 +82,19 @@ module MarkdownExpander
83
82
  class Node
84
83
  attr_accessor :parent
85
84
  attr_accessor :children
86
- attr_accessor :value
87
- def initialize parent, value
85
+ attr_accessor :element
86
+ def initialize parent, element
88
87
  @parent = parent
89
- @value = value
88
+ @element = element
90
89
  @children = []
91
90
  end
92
91
  end
93
92
 
94
93
  class LoopStart
95
- attr_accessor :name, :looper
96
- def initialize name, looper
94
+ attr_accessor :name, :expression
95
+ def initialize name, expression
97
96
  @name = name
98
- @looper = looper
97
+ @expression = expression
99
98
  end
100
99
  end
101
100
 
@@ -111,16 +110,9 @@ module MarkdownExpander
111
110
  end
112
111
 
113
112
  class Expression
114
- def initialize value
115
- @value = value
116
- end
117
- def evaluate scope
118
- parts = @value.split(".")
119
- current_scope = scope
120
- parts.each do |part|
121
- current_scope = current_scope[part.to_sym]
122
- end
123
- current_scope
113
+ attr_accessor :expression
114
+ def initialize expression
115
+ @expression = expression
124
116
  end
125
117
  end
126
118
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MarkdownExpander
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown-expander
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Vos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-18 00:00:00.000000000 Z
11
+ date: 2016-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler