Skip to content

Commit

Permalink
Nest ingested data recursively (#45)
Browse files Browse the repository at this point in the history
* Adds a nested duplicate structure inside ingested data object

* Due to an apparent limitation in Liquid, in order to access
top-tier variables during parse operations, we need to grant
those variables a parent scope to reference. This patch
leaves existing variables intact but adds a container scope
called 'data', which contains an exact replica of all the
data ingested from an external file.

* Also adds a regexreplace filter to Liquid, which does what
you'd expect.

* Bump version
  • Loading branch information
briandominick authored Jun 11, 2018
1 parent 931bbd2 commit 17edd7b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/liquidoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,18 @@ def parse_regex data_file, pattern

# Parse given data using given template, generating given output
def liquify datasrc, template_file, output, variables=nil
data = get_data(datasrc)
input = get_data(datasrc)
nested = { "data" => get_data(datasrc)}
input.merge!nested
validate_file_input(template_file, "template")
if variables
vars = { "vars" => variables }
data.merge!vars
input.merge!vars
end
begin
template = File.read(template_file) # reads the template file
template = Liquid::Template.parse(template) # compiles template
rendered = template.render(data) # renders the output
rendered = template.render(input) # renders the output
rescue Exception => ex
message = "Problem rendering Liquid template. #{template_file}\n" \
"#{ex.class} thrown. #{ex.message}"
Expand Down Expand Up @@ -1010,6 +1012,10 @@ def slugify input
s
end

def regexreplace input, regex, replacement=''
input.to_s.gsub(Regexp.new(regex), replacement.to_s)
end

end

# register custom Liquid filters
Expand Down
2 changes: 1 addition & 1 deletion lib/liquidoc/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Liquidoc
VERSION = "0.8.0"
VERSION = "0.8.1"
end

0 comments on commit 17edd7b

Please sign in to comment.