Skip to content

Sub variable expansion #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
17 changes: 17 additions & 0 deletions lib/octopress-assign-tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +24,22 @@ def render(context)
operator = $2
value = $3

if value =~ VAR_MATCH
first_part = $1
evaluate_part = $2
last_part = $4
new_value = ""

evaluate_part.scan (SUB_VARS) {
pre_value = $1
evaluated_var = TagHelpers::Var.get_value($3, context)
new_value = new_value + pre_value + evaluated_var
}
value = first_part + new_value + last_part
end

value = TagHelpers::Var.get_value(value, context)

return if value.nil?

context = TagHelpers::Var.set_var(var, operator, value, context)
Expand Down
2 changes: 1 addition & 1 deletion lib/octopress-assign-tag/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Octopress
module Tags
module Assign
VERSION = "1.0.3"
VERSION = "1.0.4"
end
end
end
10 changes: 10 additions & 0 deletions test/_expected/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,15 @@
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
complex-hello-GOODBYE → complex-hello-GOODBYE

## Filters on non-string variables
ABab
12 changes: 12 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -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 }}
Expand All @@ -25,5 +27,15 @@
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 }} == '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 }}