jekyll: remove include_remote liquid tag and use frontmatter for remote include

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2022-07-25 14:50:41 +02:00
parent 511c0a0f70
commit f2f55d1e8e
6 changed files with 36 additions and 64 deletions

View File

@@ -16,6 +16,7 @@ end
gem 'rouge', '3.27.0'
gem 'archive-zip', '0.12.0'
gem 'front_matter_parser', '1.0.1'
gem 'html-proofer', '3.19.4'
gem 'mdl', '0.11.0'
gem 'octopress-hooks', '2.6.2'

View File

@@ -17,6 +17,7 @@ GEM
eventmachine (1.2.7)
ffi (1.15.5)
forwardable-extended (2.6.0)
front_matter_parser (1.0.1)
html-proofer (3.19.4)
addressable (~> 2.3)
mercenary (~> 0.3)
@@ -26,7 +27,7 @@ GEM
typhoeus (~> 1.3)
yell (~> 2.0)
http_parser.rb (0.8.0)
i18n (1.10.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
io-like (0.3.1)
jekyll (4.2.2)
@@ -71,16 +72,16 @@ GEM
mercenary (0.4.0)
mini_portile2 (2.8.0)
mixlib-cli (2.1.8)
mixlib-config (3.0.9)
mixlib-config (3.0.27)
tomlrb
mixlib-shellout (3.2.7)
chef-utils
nokogiri (1.13.6)
nokogiri (1.13.8)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.13.6-aarch64-linux)
nokogiri (1.13.8-aarch64-linux)
racc (~> 1.4)
nokogiri (1.13.6-x86_64-linux)
nokogiri (1.13.8-x86_64-linux)
racc (~> 1.4)
octopress-hooks (2.6.2)
jekyll (>= 2.0)
@@ -101,7 +102,7 @@ GEM
ffi (~> 1.9)
terminal-table (2.0.0)
unicode-display_width (~> 1.1, >= 1.1.1)
tomlrb (2.0.1)
tomlrb (2.0.3)
typhoeus (1.4.0)
ethon (>= 0.9.0)
unicode-display_width (1.8.0)
@@ -114,6 +115,7 @@ PLATFORMS
DEPENDENCIES
archive-zip (= 0.12.0)
front_matter_parser (= 1.0.1)
html-proofer (= 3.19.4)
jekyll (= 4.2.2)
jekyll-redirect-from

View File

@@ -179,6 +179,6 @@ fetch-remote:
default_branch: "master"
ref: "master"
paths:
- dest: "_includes/dockerfile/reference.md"
- dest: "engine/reference/builder.md"
src:
- "frontend/dockerfile/docs/reference.md"

View File

@@ -1,4 +1,5 @@
require 'archive/zip'
require 'front_matter_parser'
require 'jekyll'
require 'json'
require 'octopress-hooks'
@@ -33,6 +34,16 @@ module Jekyll
end
end
def self.resolve_line_numbers(first, last)
if first.nil? && last.nil?
first = 0
last = -1
elsif last.nil?
last = first
end
[first.to_i, last.to_i]
end
def pre_read(site)
beginning_time = Time.now
puts "Starting plugin fetch_remote.rb..."
@@ -77,7 +88,18 @@ module Jekyll
file_clean = ent.path.delete_prefix(ztmpdir).split("/").drop(2).join("/")
destent = FileUtils::Entry_.new(d, ent.rel, false)
puts " #{file_clean} => #{destent.path}"
ent.copy destent.path
if File.file?(destent.path)
fmp = FrontMatterParser::Parser.parse_file(destent.path)
if fmp['fetch_remote'].nil?
raise "Local file #{destent.path} already exists"
end
line_start, line_end = FetchRemote.resolve_line_numbers(fmp['fetch_remote'].kind_of?(Hash) ? fmp['fetch_remote']['line_start'] : nil, fmp['fetch_remote'].kind_of?(Hash) ? fmp['fetch_remote']['line_end'] : nil)
lines = File.readlines(ent.path)[line_start..line_end]
File.open(destent.path, "a") { |fow| fow.puts lines.join }
else
ent.copy destent.path
end
next unless File.file?(ent.path) && File.extname(ent.path) == ".md"
# set edit and issue url and remote info for markdown files in site config defaults

View File

@@ -1,54 +0,0 @@
module Jekyll
class IncludeRemoteTag < Liquid::Tag
def initialize(tag_name, params, tokens)
@page, @line_start, @line_end = params.split
@line_start, @line_end = resolve_line_numbers(@line_start, @line_end)
super
end
def render(context)
site = context.registers[:site]
page = context.registers[:page]
beginning_time = Time.now
Jekyll.logger.info "Starting plugin include_remote.rb..."
if context[@page.strip]
@page = context[@page.strip]
end
inc = File.join("_includes", @page)
Jekyll.logger.info " Inject #{inc} to #{page['path']}"
lines = File.readlines(inc)[@line_start..@line_end]
site.config['defaults'].each do |default|
if default['scope']['path'] == inc
page['edit_url'] = default['values']['edit_url']
page['issue_url'] = default['values']['issue_url']
Jekyll.logger.info " edit_url: #{page['edit_url']}"
Jekyll.logger.info " issue_url: #{page['issue_url']}"
break
end
end
end_time = Time.now
Jekyll.logger.info "done in #{(end_time - beginning_time)} seconds"
lines.join
end
def resolve_line_numbers(first, last)
if first.nil? && last.nil?
first = 0
last = -1
elsif last.nil?
last = first
end
[first.to_i, last.to_i]
end
end
end
Liquid::Template.register_tag('include_remote', Jekyll::IncludeRemoteTag)

View File

@@ -5,6 +5,7 @@ keywords: build, dockerfile, reference
toc_max: 3
redirect_from:
- /reference/builder/
fetch_remote:
line_start: 2
line_end: -1
---
{% include_remote dockerfile/reference.md 2 -1 -%}