Skip to content

Commit

Permalink
Merge branch 'jump_code_click' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ongaeshi committed Mar 23, 2012
2 parents 93beee8 + ac6deee commit 93c7c01
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 4 deletions.
132 changes: 130 additions & 2 deletions lib/milkode/cdweb/lib/coderay_html2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,31 @@ def text_token text, kind
super
end

# [ref] CodeRay::Encoders::HTML#finish (coderay-1.0.5/lib/coderay/encoders/html.rb:219)
def finish options
@out = ornament_line_attr(options)
super

unless @opened.empty?
warn '%d tokens still open: %p' % [@opened.size, @opened] if $CODERAY_DEBUG
@out << '</span>' while @opened.pop
@last_opened = nil
end

@out.extend Output
@out.css = @css
if options[:line_numbers]
# Numbering.number! @out, options[:line_numbers], options
HTML2::number! @out, options[:line_numbers], options
end
@out.wrap! options[:wrap]
@out.apply_title! options[:title]

if defined?(@real_out) && @real_out
@real_out << @out
@out = @real_out
end

@out
end

def ornament_line_attr(options)
Expand All @@ -34,10 +56,116 @@ def ornament_line_attr(options)

def line_attr(no, highlight_lines)
r = []
r << "id=\"#{no}\""
r << "id=\"n#{no}\""
r << "class=\"highlight-line\"" if highlight_lines.include?(no)
r.join(" ")
end

# [ref] CodeRay::Encoders::Numberling#number! (coderay-1.0.5/lib/coderay/encoders/numbering.rb:8)
def self.number! output, mode = :table, options = {}
return self unless mode

options = DEFAULT_OPTIONS.merge options

start = options[:line_number_start]
unless start.is_a? Integer
raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
end

anchor_prefix = options[:line_number_anchors]
anchor_prefix = 'line' if anchor_prefix == true
anchor_prefix = anchor_prefix.to_s[/\w+/] if anchor_prefix

anchor_url = options[:line_number_anchor_url] || ""

anchoring =
if anchor_prefix
proc do |line|
line = line.to_s
anchor = anchor_prefix + line
"<a href=\"#{anchor_url}##{anchor}\" name=\"#{anchor}\">#{line}</a>"
end
else
proc { |line| line.to_s } # :to_s.to_proc in Ruby 1.8.7+
end

bold_every = options[:bold_every]
highlight_lines = options[:highlight_lines]
bolding =
if bold_every == false && highlight_lines == nil
anchoring
elsif highlight_lines.is_a? Enumerable
highlight_lines = highlight_lines.to_set
proc do |line|
if highlight_lines.include? line
"<strong class=\"highlighted\">#{anchoring[line]}</strong>" # highlighted line numbers in bold
else
anchoring[line]
end
end
elsif bold_every.is_a? Integer
raise ArgumentError, ":bolding can't be 0." if bold_every == 0
proc do |line|
if line % bold_every == 0
"<strong>#{anchoring[line]}</strong>" # every bold_every-th number in bold
else
anchoring[line]
end
end
else
raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
end

line_count = output.count("\n")
position_of_last_newline = output.rindex(RUBY_VERSION >= '1.9' ? /\n/ : ?\n)
if position_of_last_newline
after_last_newline = output[position_of_last_newline + 1 .. -1]
ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
line_count += 1 if not ends_with_newline
end

case mode
when :inline
max_width = (start + line_count).to_s.size
line_number = start
nesting = []
output.gsub!(/^.*$\n?/) do |line|
line.chomp!
open = nesting.join
line.scan(%r!<(/)?span[^>]*>?!) do |close,|
if close
nesting.pop
else
nesting << $&
end
end
close = '</span>' * nesting.size

line_number_text = bolding.call line_number
indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
line_number += 1
"<span class=\"line-numbers\">#{indent}#{line_number_text}</span>#{open}#{line}#{close}\n"
end

when :table
line_numbers = (start ... start + line_count).map(&bolding).join("\n")
line_numbers << "\n"
line_numbers_table_template = Output::TABLE.apply('LINE_NUMBERS', line_numbers)

output.gsub!(/<\/div>\n/, '</div>')
output.wrap_in! line_numbers_table_template
output.wrapped_in = :div

when :list
raise NotImplementedError, 'The :list option is no longer available. Use :table.'

else
raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
[mode, [:table, :inline]]
end

output
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/milkode/cdweb/lib/coderay_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ def to_html
)
end

def to_html_anchorlink(url)
CodeRay.scan(@content, file_type).
html2(
:wrap => nil,
:line_numbers => :table,
:css => :class,
:highlight_lines => @highlight_lines,
:line_number_start => @line_number_start,
:line_number_anchors => 'n',
:line_number_anchor_url => url
)
end

def file_type
case File.extname(@filename)
when ".el"
Expand Down
6 changes: 4 additions & 2 deletions lib/milkode/cdweb/lib/search_contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,12 @@ def result_match_record(match_group)
coderay.col_limit(COL_LIMIT)
coderay.set_range(first_index..last_index)

url = "/home/" + record_link(record)

<<EOS
<dt class='result-record'><a href='#{"/home/" + record_link(record) + "##{coderay.line_number_start}"}'>#{Util::relative_path record.shortpath, @path}</a></dt>
<dt class='result-record'><a href='#{url + "#n#{coderay.line_number_start}"}'>#{Util::relative_path record.shortpath, @path}</a></dt>
<dd>
#{coderay.to_html}
#{coderay.to_html_anchorlink(url)}
</dd>
EOS
end
Expand Down

0 comments on commit 93c7c01

Please sign in to comment.