Skip to content

Commit

Permalink
feat: ruby plugin root-include-snippet now able to read content based…
Browse files Browse the repository at this point in the history
… on func name
  • Loading branch information
tsypuk committed Aug 11, 2023
1 parent f67e53e commit 130a6fc
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions docs/_plugins/root-include-snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,45 @@ def initialize(_tag_name, markup, _parse_context)
end

def render(context)
expanded_path = Liquid::Template.parse(@markup).render(context)
@func = ''
@dir = @markup
if (@markup.include?(" "))
@dir = @markup.split[0]
@func = @markup.split[1]
end

expanded_path = Liquid::Template.parse(@dir).render(context)
root_path = File.expand_path(context.registers[:site].config['source'])
final_path = File.join(root_path, expanded_path)
file_read_opts = context.registers[:site].file_read_opts

read_lines_from_file(final_path, file_read_opts)
read_lines_from_file(final_path, file_read_opts, @func)
end

def read_lines_from_file(file_path, file_read_opts)
def read_lines_from_file(file_path, file_read_opts, func_name)
lines = []
in_section = false
in_func = false
func = 'def test_' + func_name

# print(func)

File.foreach(file_path, **file_read_opts) do |line|
# print(file_path)
if line.include?('# given')
in_section = true
elsif line.include?('# then')
break
if line.include?(func)
in_func = true
end

if in_func
if line.include?('# given')
in_section = true
elsif line.include?('# then')
break
end
# line.sub!(/^\t{2}/, "")
line.sub!(/^ {8}/, "")
lines << line if in_section and in_func
end
# line.sub!(/^\t{2}/, "")
line.sub!(/^ {8}/, "")
lines << line if in_section
end
# print(lines)
# lines << '# then'
Expand All @@ -35,4 +52,4 @@ def read_lines_from_file(file_path, file_read_opts)
end
end

Liquid::Template.register_tag('root_include_snippet', RootIncludeSnippet)
Liquid::Template.register_tag('root_include_snippet', RootIncludeSnippet)

0 comments on commit 130a6fc

Please sign in to comment.