This is an automated email from the ASF dual-hosted git repository. heneveld pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/brooklyn-docs.git
commit 287806417f8776eec5aeedb8369568b7096ebf75 Author: Alex Heneveld <alex.henev...@cloudsoftcorp.com> AuthorDate: Thu Jul 22 23:54:31 2021 +0100 tidy the read/readj tag rename readj to read_jekyll, add read_literal, and make default read be auto --- README.md | 8 ++++++ _plugins/read.rb | 74 ++++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index e562c6f..87b9aca 100644 --- a/README.md +++ b/README.md @@ -285,6 +285,14 @@ We use some custom Jekyll plugins, in the `_plugins` dir: * include markdown files inside other files (see, for example, the `*.include.md` files which contain text which is used in multiple other files) + * `read_jekyll` will read a file and jekyll-process it (eg for nested `{% ... readj ` calls; + it's just like `include_relative` but with two differences: when `readj` is used + nested `read_jekyll` calls are relative to the included file where it is contained, + rather than relative to the root md file which made the first `read_jekyll` call + (so in general it's more intuitive than `include_relative`), + and `include_relative` processes front matter whereas `read_jekyll` mangles it + * `read_literal` is like `read_jekyll` but without the jekyll/tag processing + * `read` will autodetect based on filename -- jekyll for `*.md` and literal for all others * generate the site structure / menu objects * parse JSON which we can loop over in our markdown docs (to build up models; previously used for the TOC in the guide, but now replaced with site_structure) diff --git a/_plugins/read.rb b/_plugins/read.rb index 23cf0f2..af910bb 100644 --- a/_plugins/read.rb +++ b/_plugins/read.rb @@ -21,7 +21,7 @@ # (like include, but in the dir where it is invoked) # there is also readj which reads a file and applies jekyll processing to it -# handy if we want to include a toc.json file which itself calls {% readj child/toc.json %} +# handy if we want to include a toc.json file which itself calls {% read_jekyll child/toc.json %} # (note however variables do not seem to be exported when use readj (TODO), # although they are exported if you have _includes/file.md and use the standard include file) @@ -35,47 +35,73 @@ module JekyllRead def initialize(tag_name, text, tokens) super @text = text + @mode = :auto end - def render(context) - filename = @text.strip - filename = context[filename] || filename + def load_file(context, filename, context_page) # Pathname API ignores first arg below if second is absolute - file = Pathname.new(File.dirname(context['page']['path'])) + filename + file = Pathname.new(File.dirname(context_page['path'])) + filename file = file.cleanpath + # is there a better way to trim a leading / ? file = file.relative_path_from(Pathname.new("/")) unless file.relative? raise "No such file #{file} in read call (from #{context['page']['path']})" unless file.exist? + file + end + + def render_jekyll(context, filename) + file = load_file(context, filename, context['page'] || context.registers[:page]) + + # support vars and paths relative to a file being readj'd + jekyllSite = context.registers[:site] + targetPage = Jekyll::Page.new(jekyllSite, jekyllSite.source, File.dirname(file), File.basename(file)) + targetPage.render(jekyllSite.layouts, jekyllSite.site_payload) + targetPage.output + end + + def render_literal(context, filename) + file = load_file(context, filename, context['page']) file = File.open(file, "rb") return file.read end + + def render(context) + filename = @text.strip + context[filename] || filename + + mode = @mode + if (mode == :auto) + mode = filename.end_with?(".md") ? :jekyll : :literal + end + if (mode == :jekyll) + render_jekyll(context, filename) + elsif (mode == :literal) + render_literal(context, filename) + else + raise "Unknown mode #{mode}" + end + + end end - class ReadjTag < Liquid::Tag + class ReadJekyllTag < ReadTag def initialize(tag_name, text, tokens) super - @text = text + @mode = :jekyll end - def render(context) - filename = @text.strip - filename = context[filename] || filename - # Pathname API ignores first arg below if second is absolute - page = context['page'] || context.registers[:page] - file = Pathname.new(File.dirname(page['path'])) + filename - file = file.cleanpath - # is there a better way to trim a leading / ? - file = file.relative_path_from(Pathname.new("/")) unless file.relative? - raise "No such file #{file} in readj call (from #{context['page']['path']})" unless file.exist? - - # with readj we support vars and paths relative to a file being readj'd - jekyllSite = context.registers[:site] - targetPage = Jekyll::Page.new(jekyllSite, jekyllSite.source, File.dirname(file), File.basename(file)) - targetPage.render(jekyllSite.layouts, jekyllSite.site_payload) - return targetPage.output + end + class ReadLiteralTag < ReadTag + def initialize(tag_name, text, tokens) + super + @mode = :literal end end end Liquid::Template.register_tag('read', JekyllRead::ReadTag) -Liquid::Template.register_tag('readj', JekyllRead::ReadjTag) +Liquid::Template.register_tag('read_jekyll', JekyllRead::ReadJekyllTag) +Liquid::Template.register_tag('read_literal', JekyllRead::ReadLiteralTag) + +# for compatibility with old markdown +Liquid::Template.register_tag('readj', JekyllRead::ReadJekyllTag)