Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9109#discussion_r42388292
  
    --- Diff: docs/_plugins/include_example.rb ---
    @@ -0,0 +1,70 @@
    +require 'octopress-code-highlighter'
    +require 'liquid'
    +
    +module Octopress
    +  module IncludeExample
    +    class Tag < Liquid::Tag
    +      
    +      # Pattern for {% include_example /path/to/a/file.lan %}
    +      FileOnly = /^(\S+)$/
    +      # Pattern for {% include_example /path/to/a/file.lan title %}
    +      FileTitle = /(\S+)\s+(\S.*?)$/i
    +
    +      def initialize(tag_name, markup, tokens)
    +        @markup = markup
    +        super
    +      end
    +
    +      def render(context)
    +        site = context.registers[:site]
    +        config_dir = (site.config['code_dir'] || 
'../examples/src/main').sub(/^\//,'')
    +        @code_dir = File.join(site.source, config_dir)
    +
    +        begin
    +          options = get_options
    +          code = File.open(@file).read.encode("UTF-8")
    +          code = select_lines(code, options)
    +
    +          CodeHighlighter.highlight(code, options)
    +        rescue => e
    +          puts "not work"
    +        end
    +      end
    +
    +      def get_options
    +        defaults = {}
    +        clean_markup = CodeHighlighter.clean_markup(@markup).strip
    +
    +        if clean_markup =~ FileOnly
    +          @file = File.join(@code_dir, $1)
    +        elsif clean_markup =~ FileTitle
    +          @file = File.join(@code_dir, $1)
    +          defaults[:title] = $2
    +        end
    +
    +        options = CodeHighlighter.parse_markup(@markup, defaults)
    +        options[:lang] ||= File.extname(@file).delete('.')
    +        options[:link_text] ||= "Example code"
    +        options
    +      end
    +
    +      # Select lines according to labels in code. Currently we use "begin 
code" and "end code" as
    +      # labels.
    +      def select_lines(code, options)
    +        lines = code.each_line.to_a
    +        start = lines.each_with_index.select { |l, i| l.include? "begin 
code" }.last.last
    --- End diff --
    
    We don't check the comment symbol like "//" or "#". Just do a string match 
on the content. So both of the following work. I use `$` just to make sure the 
text doesn't appear in normal comments.
    
    ~~~scala
    // $example on$
    ~~~
    
    ~~~python
    # $example off$
    ~~~


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to