From ced8b1389e20f5d928b5479d8c1ae28313030276 Mon Sep 17 00:00:00 2001 From: Xusen Yin Date: Tue, 3 Nov 2015 15:20:15 +0800 Subject: [PATCH] add label support --- docs/_plugins/include_example.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/docs/_plugins/include_example.rb b/docs/_plugins/include_example.rb index 0f4184c7462be..6f084b5b60a0b 100644 --- a/docs/_plugins/include_example.rb +++ b/docs/_plugins/include_example.rb @@ -20,10 +20,22 @@ module Jekyll class IncludeExampleTag < Liquid::Tag + + FileOnly = /^(\S+\.\S+)$/ + FileTag = /^(\S+\.\S+)\s+(\S+)$/ def initialize(tag_name, markup, tokens) - @markup = markup super + clean_markup = markup.strip + if clean_markup =~ FileOnly + @markup = $1 + @label = '' + elsif clean_markup =~ FileTag + @markup = $1 + @label = " #{$2}" + else + raise "Invalid syntax. Use {% include_example path/to/file [label] %}." + end end def render(context) @@ -31,9 +43,8 @@ def render(context) config_dir = (site.config['code_dir'] || '../examples/src/main').sub(/^\//,'') @code_dir = File.join(site.source, config_dir) - clean_markup = @markup.strip - @file = File.join(@code_dir, clean_markup) - @lang = clean_markup.split('.').last + @file = File.join(@code_dir, @markup) + @lang = @markup.split('.').last code = File.open(@file).read.encode("UTF-8") code = select_lines(code) @@ -61,13 +72,13 @@ def select_lines(code) # Select the array of start labels from code. startIndices = lines .each_with_index - .select { |l, i| l.include? "$example on$" } + .select { |l, i| l.include? "$example on#{@label}$" } .map { |l, i| i } # Select the array of end labels from code. endIndices = lines .each_with_index - .select { |l, i| l.include? "$example off$" } + .select { |l, i| l.include? "$example off#{@label}$" } .map { |l, i| i } raise "Start indices amount is not equal to end indices amount, please check the code." \