Skip to content
This repository

added start:LINENO/end:LINENO to include_code #478

Closed
wants to merge 3 commits into from

3 participants

Sebastian Cohnen Hong Xu Brandon Mathis
Sebastian Cohnen
tisba commented

I needed the ability to include just a few lines from a file at a time. So I extended the include_code tag to accept start:X and end:Y. E.g. using {% include_code Title path/file.rb start:5 end:10 %} only lines 5 to 10 (inclusive) are being included.

start and end are both optional - the default to 0 and end of document respectively. If start or end is given, the title of the snipped is extended to reflect the included lines.

Sebastian Cohnen tisba added start:LINENO/end:LINENO to include_code
Added ability to include just a specific range of lines from the specified file. Using `{% include_code Title path/file.rb start:5 end:10 %}` only lines 5 to 10 (inclusive) is being included.
e13cd5b
Sebastian Cohnen
tisba commented

There is one (minor) issue: The line numbers in the rendered result do not reflect the offset. I'll have a look into the highlighting process and try to fix this.

Sebastian Cohnen
tisba commented

Update: I'm having some problems figuring out how to pass linenostart to the Pygments formatter (tmm1/pygments.rb#16). I'm not very good at reading/debugging Python and I'm open for input.

Sebastian Cohnen
tisba commented

Okay, got it.

/cc @imathis

Hong Xu

+1

Brandon Mathis imathis closed this pull request from a commit
Brandon Mathis Improvements to Code embedding features
1. Removed lots of duplication
2. Added warnings and fixed some dim regex
3. New: set a start line number for any code snippet
4. New: set a start, end, or range for include_code
5. New: added option to disable line numbers linenos:false

Fixes #478 #484
e16844a
Brandon Mathis imathis closed this in e16844a
Brandon Mathis
Owner
imathis commented

Thanks for the work on this. I added range:#,# as an option. I think you'll be pleased :)

Sebastian Cohnen
tisba commented

awesome! :)

Neo Choi vampireneo referenced this pull request from a commit in vampireneo/octopress
Brandon Mathis Improvements to Code embedding features
1. Removed lots of duplication
2. Added warnings and fixed some dim regex
3. New: set a start line number for any code snippet
4. New: set a start, end, or range for include_code
5. New: added option to disable line numbers linenos:false

Closes #478 Closes #484
5c9a470
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Mar 11, 2012
Sebastian Cohnen tisba added start:LINENO/end:LINENO to include_code
Added ability to include just a specific range of lines from the specified file. Using `{% include_code Title path/file.rb start:5 end:10 %}` only lines 5 to 10 (inclusive) is being included.
e13cd5b
Mar 26, 2012
Sebastian Cohnen tisba added options hash to HighlightCode#highlight 86fc77b
Sebastian Cohnen tisba pass linenostart option to highlight invokation 39a6b17
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 24 additions and 7 deletions. Show diff stats Hide diff stats

  1. +19 3 plugins/include_code.rb
  2. +5 4 plugins/pygments_code.rb
22 plugins/include_code.rb
@@ -36,6 +36,14 @@ def initialize(tag_name, markup, tokens)
36 36 @filetype = $1
37 37 markup = markup.strip.sub(/lang:\w+/i,'')
38 38 end
  39 + if markup.strip =~ /start:(\d+)/
  40 + @start_line = $1.to_i || 0
  41 + markup = markup.strip.sub(/start:(\d+)/, '')
  42 + end
  43 + if markup.strip =~ /end:(\d+)/
  44 + @end_line = $1.to_i || 0
  45 + markup = markup.strip.sub(/end:(\d+)/, '')
  46 + end
39 47 if markup.strip =~ /(.*)?(\s+|^)(\/*\S+)/i
40 48 @title = $1 || nil
41 49 @file = $3
@@ -57,12 +65,20 @@ def render(context)
57 65 end
58 66
59 67 Dir.chdir(code_path) do
60   - code = file.read
  68 + lines = file.readlines
  69 +
  70 + start_line = @start_line || 1
  71 + end_line = @end_line && @end_line >= start_line ? @end_line : lines.size
  72 + code = lines[(start_line-1)..(end_line-1)].join("")
  73 +
  74 + options = {:linenostart => start_line}
  75 +
61 76 @filetype = file.extname.sub('.','') if @filetype.nil?
62   - title = @title ? "#{@title} (#{file.basename})" : file.basename
  77 + start_end_title = ", line #{start_line} to #{end_line}" if @start_line || @end_line
  78 + title = @title ? "#{@title} (#{file.basename}#{start_end_title})" : file.basename
63 79 url = "/#{code_dir}/#{@file}"
64 80 source = "<figure class='code'><figcaption><span>#{title}</span> <a href='#{url}'>download</a></figcaption>\n"
65   - source += " #{highlight(code, @filetype)}</figure>"
  81 + source += " #{highlight(code, @filetype, options)}</figure>"
66 82 safe_wrap(source)
67 83 end
68 84 end
9 plugins/pygments_code.rb
@@ -6,13 +6,13 @@
6 6 FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)
7 7
8 8 module HighlightCode
9   - def highlight(str, lang)
  9 + def highlight(str, lang, options={})
10 10 lang = 'ruby' if lang == 'ru'
11 11 lang = 'objc' if lang == 'm'
12 12 lang = 'perl' if lang == 'pl'
13 13 lang = 'yaml' if lang == 'yml'
14 14 str = pygments(str, lang).match(/<pre>(.+)<\/pre>/m)[1].to_s.gsub(/ *$/, '') #strip out divs <div class="highlight">
15   - tableize_code(str, lang)
  15 + tableize_code(str, lang, options)
16 16 end
17 17
18 18 def pygments(code, lang)
@@ -29,11 +29,12 @@ def pygments(code, lang)
29 29 end
30 30 highlighted_code
31 31 end
32   - def tableize_code (str, lang = '')
  32 + def tableize_code (str, lang = '', options={})
  33 + options = {:linenostart => 1}.merge(options)
33 34 table = '<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers">'
34 35 code = ''
35 36 str.lines.each_with_index do |line,index|
36   - table += "<span class='line-number'>#{index+1}</span>\n"
  37 + table += "<span class='line-number'>#{index+options[:linenostart]}</span>\n"
37 38 code += "<span class='line'>#{line}</span>"
38 39 end
39 40 table += "</pre></td><td class='code'><pre><code class='#{lang}'>#{code}</code></pre></td></tr></table></div>"

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.