|
| 1 | +require 'liquid' |
| 2 | + |
| 3 | +module Jekyll |
| 4 | + class IncludeLinesTag < Liquid::Tag |
| 5 | + def initialize(tag_name, markup, tokens) |
| 6 | + super |
| 7 | + @markup = markup.to_s.strip |
| 8 | + end |
| 9 | + |
| 10 | + def render(context) |
| 11 | + site = context.registers[:site] |
| 12 | + source = site.source |
| 13 | + |
| 14 | + rendered_markup = Liquid::Template.parse(@markup).render(context) |
| 15 | + path, options = parse_markup(rendered_markup) |
| 16 | + raise ArgumentError, "include_lines: missing file path" if path.nil? || path.empty? |
| 17 | + |
| 18 | + from = integer_option(options, 'from') |
| 19 | + to = integer_option(options, 'to') |
| 20 | + start = integer_option(options, 'start') |
| 21 | + finish = integer_option(options, 'end') |
| 22 | + |
| 23 | + from ||= start |
| 24 | + to ||= finish |
| 25 | + |
| 26 | + format = true |
| 27 | + if options.key?('format') |
| 28 | + format = boolean_option(options, 'format') |
| 29 | + end |
| 30 | + indent = integer_option(options, 'indent') |
| 31 | + |
| 32 | + unless from && to |
| 33 | + raise ArgumentError, "include_lines: must specify from/to (1-indexed), e.g. {% include_lines path from:11 to:35 %}" |
| 34 | + end |
| 35 | + |
| 36 | + if from < 1 || to < 1 |
| 37 | + raise ArgumentError, "include_lines: from/to must be >= 1 (got from:#{from} to:#{to})" |
| 38 | + end |
| 39 | + |
| 40 | + if to < from |
| 41 | + raise ArgumentError, "include_lines: to must be >= from (got from:#{from} to:#{to})" |
| 42 | + end |
| 43 | + |
| 44 | + absolute_path = File.expand_path(path, source) |
| 45 | + unless absolute_path.start_with?(File.expand_path(source) + File::SEPARATOR) |
| 46 | + raise ArgumentError, "include_lines: path must be within the site source directory" |
| 47 | + end |
| 48 | + |
| 49 | + unless File.file?(absolute_path) |
| 50 | + raise ArgumentError, "include_lines: file not found: #{path}" |
| 51 | + end |
| 52 | + |
| 53 | + lines = File.read(absolute_path, encoding: 'UTF-8').split("\n", -1) |
| 54 | + max_line = lines.length |
| 55 | + |
| 56 | + if from > max_line |
| 57 | + raise ArgumentError, "include_lines: from (#{from}) is beyond end of file (#{max_line} lines): #{path}" |
| 58 | + end |
| 59 | + |
| 60 | + to = [to, max_line].min |
| 61 | + |
| 62 | + selected = lines[(from - 1)..(to - 1)] |
| 63 | + |
| 64 | + if format |
| 65 | + selected = dedent_lines(selected) |
| 66 | + end |
| 67 | + |
| 68 | + if indent && indent > 0 |
| 69 | + prefix = ' ' * indent |
| 70 | + selected = selected.map { |l| l.strip.empty? ? l : (prefix + l) } |
| 71 | + end |
| 72 | + |
| 73 | + selected.join("\n") |
| 74 | + rescue StandardError => e |
| 75 | + if defined?(Jekyll) && Jekyll.respond_to?(:logger) && Jekyll.logger |
| 76 | + Jekyll.logger.error("include_lines:", e.message) |
| 77 | + end |
| 78 | + raise |
| 79 | + end |
| 80 | + |
| 81 | + private |
| 82 | + |
| 83 | + def parse_markup(markup) |
| 84 | + tokens = markup.scan(/\"[^\"]+\"|\'[^\']+\'|\S+/) |
| 85 | + return [nil, {}] if tokens.empty? |
| 86 | + |
| 87 | + path_token = tokens.shift |
| 88 | + path = unquote(path_token) |
| 89 | + |
| 90 | + options = {} |
| 91 | + tokens.each do |t| |
| 92 | + key, value = t.split(':', 2) |
| 93 | + next if value.nil? |
| 94 | + options[key] = unquote(value) |
| 95 | + end |
| 96 | + |
| 97 | + [path, options] |
| 98 | + end |
| 99 | + |
| 100 | + def unquote(value) |
| 101 | + v = value.to_s |
| 102 | + if (v.start_with?('"') && v.end_with?('"')) || (v.start_with?("'") && v.end_with?("'")) |
| 103 | + v[1..-2] |
| 104 | + else |
| 105 | + v |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + def integer_option(options, key) |
| 110 | + return nil unless options.key?(key) |
| 111 | + Integer(options[key]) |
| 112 | + rescue ArgumentError |
| 113 | + raise ArgumentError, "include_lines: #{key} must be an integer (got #{options[key].inspect})" |
| 114 | + end |
| 115 | + |
| 116 | + def boolean_option(options, key) |
| 117 | + return nil unless options.key?(key) |
| 118 | + |
| 119 | + v = options[key].to_s.strip.downcase |
| 120 | + return true if %w[1 true yes y on].include?(v) |
| 121 | + return false if %w[0 false no n off].include?(v) |
| 122 | + |
| 123 | + raise ArgumentError, "include_lines: #{key} must be a boolean (got #{options[key].inspect})" |
| 124 | + end |
| 125 | + |
| 126 | + def dedent_lines(lines) |
| 127 | + non_empty = lines.reject { |l| l.strip.empty? } |
| 128 | + return lines if non_empty.empty? |
| 129 | + |
| 130 | + min_indent = non_empty.map { |l| l[/\A[ \t]*/].length }.min |
| 131 | + return lines if min_indent.nil? || min_indent.zero? |
| 132 | + |
| 133 | + lines.map do |l| |
| 134 | + l.strip.empty? ? l : l[min_indent..] |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | +end |
| 139 | + |
| 140 | +Liquid::Template.register_tag('include_lines', Jekyll::IncludeLinesTag) |
0 commit comments