Skip to content

Commit

Permalink
feat: implement ruby jekyll plugins for git-hash and root-include
Browse files Browse the repository at this point in the history
  • Loading branch information
tsypuk committed Aug 6, 2023
1 parent ba80fc1 commit 1e62a1a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/_plugins/git-hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Jekyll
class GitHashGenerator < Jekyll::Generator
priority :high
safe true

def generate(site)
hash = %x( git rev-parse --short HEAD ).strip
full_hash = %x( git rev-parse HEAD ).strip
site.data['hash'] = hash
site.data['full_hash'] = full_hash
date = %x( git show -s --format=%ci)
site.data['date'] = date
end
end
end

49 changes: 49 additions & 0 deletions docs/_plugins/root-include.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class RootInclude < Liquid::Tag
def initialize(_tag_name, markup, _parse_context)
super
@markup = markup.strip
end

def render(context)
@dir = @markup
if (@markup.include?(" "))
@dir = @markup.split[0]
@lines = @markup.split[1]
split_strings = @lines.split(':')
start_string = split_strings[0].to_i
end_string = split_strings[1].to_i

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, start_string, end_string)
else
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
File.read(final_path, **file_read_opts)
end

end

def read_lines_from_file(file_path, file_read_opts, n, m)
lines = []
line_number = 1

File.foreach(file_path, **file_read_opts) do |line|
if line_number >= n && line_number <= m
lines << line
elsif line_number > m
break
end
line_number += 1
end

lines
end
end

Liquid::Template.register_tag('root_include', RootInclude)

0 comments on commit 1e62a1a

Please sign in to comment.