Class: Octopress::RenderCode::Tag
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Octopress::RenderCode::Tag
- Defined in:
- lib/octopress-render-code.rb
Constant Summary collapse
- FileOnly =
/^(\S+)$/- FileTitle =
/(\S+)\s+(\S.*?)$/i- TitleFile =
/(\S.*?)\s+(\S+)$/i
Instance Method Summary collapse
- #failure(message) ⇒ Object
- #get_options ⇒ Object
- #get_path(file) ⇒ Object
- #highlight(code, options) ⇒ Object
-
#initialize(tag_name, markup, tokens) ⇒ Tag
constructor
A new instance of Tag.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ Tag
Returns a new instance of Tag.
14 15 16 17 |
# File 'lib/octopress-render-code.rb', line 14 def initialize(tag_name, markup, tokens) @markup = markup super end |
Instance Method Details
#failure(message) ⇒ Object
75 76 77 |
# File 'lib/octopress-render-code.rb', line 75 def failure() CodeHighlighter.highlight_failed(, "{% include_code path/to/file [title] [lang:language] [start:#] [end:#] [range:#-#] [mark:#,#-#] [linenos:false] %}", @markup, '') end |
#get_options ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/octopress-render-code.rb', line 46 def # wrap with {% raw %} tags to prevent addititional processing defaults = { } clean_markup = CodeHighlighter.clean_markup(@markup).strip if clean_markup =~ FileOnly @file = get_path($1) elsif clean_markup =~ FileTitle if @file = get_path($1) defaults[:title] = $2 # Allow for deprecated title first syntax # elsif clean_markup =~ TitleFile defaults[:title] = $1 @file = get_path($2) puts "\nRenderCode Warning:".red puts " Passing title before path has been deprecated and will be removed in RenderCode 2.0".red puts " Update #{@page_path} with {% render_code #{$2} #{$1} ... %}.".yellow end end = CodeHighlighter.parse_markup(@markup, defaults) [:lang] ||= File.extname(@file).delete('.') [:link_text] ||= "Raw code" end |
#get_path(file) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/octopress-render-code.rb', line 36 def get_path(file) path = File.join(@code_dir, file) if File.symlink?(path) raise "Code directory '#{@code_path}' cannot be a symlink" end path if File.exists?(path) end |
#highlight(code, options) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/octopress-render-code.rb', line 80 def highlight(code, ) [:aliases] = @aliases || {} code = CodeHighlighter.highlight(code, ) code = "<notextile>#{code}</notextile>" if File.extname(@page_path).match(/textile/) code end |
#render(context) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/octopress-render-code.rb', line 19 def render(context) @page_path = context.environments.first['page']['path'] site = context.registers[:site] config_dir = (site.config['code_dir'] || 'downloads/code').sub(/^\//,'') @code_dir = File.join(site.source, config_dir) begin = code = File.open(@file).read.encode("UTF-8") code = CodeHighlighter.select_lines(code, ) highlight(code, ) rescue => e failure(e) end end |