From 282dc75d22f1c7dfc12ff4c3e6f2ddf98817ec4f Mon Sep 17 00:00:00 2001 From: Scott Little Date: Thu, 18 Feb 2016 14:57:35 +0100 Subject: [PATCH 1/6] Add tests to validate the sub variables works --- test/_expected/index.html | 9 +++++++++ test/index.html | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/test/_expected/index.html b/test/_expected/index.html index fe649cd..453cd9f 100644 --- a/test/_expected/index.html +++ b/test/_expected/index.html @@ -21,5 +21,14 @@ AWESOME → AWESOME whatever-man → whatever-man +## Assignment with sub variables +sub_var1 → sub_var1 +sub_var2 → sub_var2 +hello → hello +HELLO → HELLO +hello-goodbye → hello-goodbye +HELLO-GOODBYE → HELLO-GOODBYE +complex-hello-goodbye → COMPLEX-HELLO-GOODBYE + ## Filters on non-string variables ABab diff --git a/test/index.html b/test/index.html index 5689ae1..2de2b35 100644 --- a/test/index.html +++ b/test/index.html @@ -1,6 +1,8 @@ --- layout: null test_array: [a, b, A, B] +sub_var1: hello +sub_var2: goodbye --- ## Simple assign yep → {% assign var1 = 'yep' %}{{ var1 }} @@ -25,5 +27,14 @@ AWESOME → {% assign var10 = var9 | upcase %}{{ var10 }} whatever-man → {% assign var11 = 'whatever man' || nil | replace:' ','-' %}{{ var11 }} +## Assignment with sub variables +sub_var1 → {% assign test_var1 = 'sub_var1' %}{{ test_var1 }} +sub_var2 → {% assign test_var2 = 'sub_var2' %}{{ test_var2 }} +hello → {% assign var12 = page.{{ test_var1 }} %}{{ var12 }} +HELLO → {% assign var13 = page.{{ test_var1 }} | upcase %}{{ var13 }} +hello-goodbye → {% assign var15 = page.{{ test_var1 }} | append:'-' | append:page.{{ test_var2 }} %}{{ var15 }} +HELLO-GOODBYE → {% assign var16 = page.{{ test_var1 }} | append:'-' | append:page.{{ test_var2 }} | upcase %}{{ var16 }} +complex-hello-goodbye → {% assign var17 = (page.{{ test_var1 }} != null ? 'complex-{{ page.sub_var1 }}-{{ page.sub_var2 }}' : 'lame' ) | upcase %}{{ var17 }} + ## Filters on non-string variables {% assign items = page.test_array | sort %}{{ items }} From 5b0a7745044e31fac19216a6ade802cae6cccc94 Mon Sep 17 00:00:00 2001 From: Scott Little Date: Thu, 18 Feb 2016 14:58:50 +0100 Subject: [PATCH 2/6] Add functionality to embed sub variables in the assign. For instance, this allows something like this {% assign var = site.data.{{ myDataFileName }} %} --- lib/octopress-assign-tag.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/octopress-assign-tag.rb b/lib/octopress-assign-tag.rb index 4327280..6c58c19 100644 --- a/lib/octopress-assign-tag.rb +++ b/lib/octopress-assign-tag.rb @@ -7,6 +7,8 @@ module Tags module Assign class Tag < Liquid::Tag SYNTAX = /([[:word:]]+)\s*(=|\+=|\|\|=)\s*(.*)\s*/o + VAR_MATCH = /([^{]*)({{(.*)}})(.*)/o + SUB_VARS = /([^{]*)({{\s([^}]*)\s}})/o def initialize(tag_name, markup, tokens) @markup = markup @@ -22,7 +24,25 @@ def render(context) operator = $2 value = $3 + if value =~ VAR_MATCH + firstPart = $1 + evaluatePart = $2 + lastPart = $4 + newValue = "" + + evaluatePart.scan (SUB_VARS) { + preValue = $1 + evaluatedVar = TagHelpers::Var.get_value($3, context) + newValue = newValue + preValue + evaluatedVar + print newValue + } + value = firstPart + newValue + lastPart + tempvalue = TagHelpers::Var.get_value(value, context) + context = TagHelpers::Var.set_var(var, '=', tempvalue, context) + return + end value = TagHelpers::Var.get_value(value, context) + return if value.nil? context = TagHelpers::Var.set_var(var, operator, value, context) From dc6f1d98ab207804332d981b4340dc994ba0a6ba Mon Sep 17 00:00:00 2001 From: Scott Little Date: Thu, 18 Feb 2016 15:22:01 +0100 Subject: [PATCH 3/6] Change variables to use underscore separators. --- lib/octopress-assign-tag.rb | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/octopress-assign-tag.rb b/lib/octopress-assign-tag.rb index 6c58c19..817bbf0 100644 --- a/lib/octopress-assign-tag.rb +++ b/lib/octopress-assign-tag.rb @@ -25,22 +25,20 @@ def render(context) value = $3 if value =~ VAR_MATCH - firstPart = $1 - evaluatePart = $2 - lastPart = $4 - newValue = "" + first_part = $1 + evaluate_part = $2 + last_part = $4 + new_value = "" - evaluatePart.scan (SUB_VARS) { - preValue = $1 - evaluatedVar = TagHelpers::Var.get_value($3, context) - newValue = newValue + preValue + evaluatedVar - print newValue + evaluate_part.scan (SUB_VARS) { + pre_value = $1 + evaluated_var = TagHelpers::Var.get_value($3, context) + new_value = new_value + pre_value + evaluated_var + print new_value } - value = firstPart + newValue + lastPart - tempvalue = TagHelpers::Var.get_value(value, context) - context = TagHelpers::Var.set_var(var, '=', tempvalue, context) - return + value = first_part + new_value + last_part end + value = TagHelpers::Var.get_value(value, context) return if value.nil? From 79f8d9e76040d82c2856e11c3150b2c0aca841ec Mon Sep 17 00:00:00 2001 From: Scott Little Date: Thu, 18 Feb 2016 15:22:14 +0100 Subject: [PATCH 4/6] Update tests and docs. --- README.md | 8 ++++++++ test/_expected/index.html | 3 ++- test/index.html | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4a12aa9..c9400ec 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,14 @@ Use fancy operators in assignment. {% assign title_text += ' →' if linkpost %} ``` +Use variables in assignment. + +``` +{% assign my_data = site.data.{{ data_file_name }} %} +{% assign name = site.name | append:page.{{ product_name }} | downcase %} +{% assign stupid_example = (page.{{ product_name }} == null ? 'no-product' : 'product-{{ page.product_code }} | downcase }}') %} +``` + ## Contributing 1. Fork it ( https://github.com/octopress/assign-tag/fork ) diff --git a/test/_expected/index.html b/test/_expected/index.html index 453cd9f..0a79c3a 100644 --- a/test/_expected/index.html +++ b/test/_expected/index.html @@ -28,7 +28,8 @@ HELLO → HELLO hello-goodbye → hello-goodbye HELLO-GOODBYE → HELLO-GOODBYE -complex-hello-goodbye → COMPLEX-HELLO-GOODBYE +COMPLEX-HELLO-GOODBYE → COMPLEX-HELLO-GOODBYE +complex-hello-GOODBYE → complex-hello-GOODBYE ## Filters on non-string variables ABab diff --git a/test/index.html b/test/index.html index 2de2b35..ae80010 100644 --- a/test/index.html +++ b/test/index.html @@ -34,7 +34,8 @@ HELLO → {% assign var13 = page.{{ test_var1 }} | upcase %}{{ var13 }} hello-goodbye → {% assign var15 = page.{{ test_var1 }} | append:'-' | append:page.{{ test_var2 }} %}{{ var15 }} HELLO-GOODBYE → {% assign var16 = page.{{ test_var1 }} | append:'-' | append:page.{{ test_var2 }} | upcase %}{{ var16 }} -complex-hello-goodbye → {% assign var17 = (page.{{ test_var1 }} != null ? 'complex-{{ page.sub_var1 }}-{{ page.sub_var2 }}' : 'lame' ) | upcase %}{{ var17 }} +COMPLEX-HELLO-GOODBYE → {% assign var17 = (page.{{ test_var1 }} == 'hello' ? 'complex-{{ page.sub_var1 }}-{{ page.sub_var2 }}' : 'lame' ) | upcase %}{{ var17 }} +complex-hello-GOODBYE → {% assign var17 = (page.{{ test_var1 }} == null ? 'lame' : 'complex-{{ page.sub_var1 }}-{{ page.sub_var2 | upcase }}') %}{{ var17 }} ## Filters on non-string variables {% assign items = page.test_array | sort %}{{ items }} From 4a94de1ab661b8b246600109516718da1db49828 Mon Sep 17 00:00:00 2001 From: Scott Little Date: Thu, 18 Feb 2016 15:36:50 +0100 Subject: [PATCH 5/6] Removed useless print statement --- lib/octopress-assign-tag.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/octopress-assign-tag.rb b/lib/octopress-assign-tag.rb index 817bbf0..9431f35 100644 --- a/lib/octopress-assign-tag.rb +++ b/lib/octopress-assign-tag.rb @@ -34,11 +34,10 @@ def render(context) pre_value = $1 evaluated_var = TagHelpers::Var.get_value($3, context) new_value = new_value + pre_value + evaluated_var - print new_value } value = first_part + new_value + last_part end - + value = TagHelpers::Var.get_value(value, context) return if value.nil? From 1a9f1da32a5f5e5a95fd2728d3354477af1c966e Mon Sep 17 00:00:00 2001 From: Scott Little Date: Thu, 18 Feb 2016 15:40:57 +0100 Subject: [PATCH 6/6] Change the version number as well. --- lib/octopress-assign-tag/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/octopress-assign-tag/version.rb b/lib/octopress-assign-tag/version.rb index 693760a..2f60b6a 100644 --- a/lib/octopress-assign-tag/version.rb +++ b/lib/octopress-assign-tag/version.rb @@ -1,7 +1,7 @@ module Octopress module Tags module Assign - VERSION = "1.0.3" + VERSION = "1.0.4" end end end