Add line numbers

Line numbers are not supported by highlight.js.  This is hacked in through
JavaScript.  At page load, the number of new line characters are counted in the
code element.  Then, JavaScript loops through and adds a number and line break
to an empty div that is floated left.  This gives the appearence of line
numbers.
This commit is contained in:
Andrew Tomaka 2014-03-09 06:30:30 -04:00
parent ae9aecc2ab
commit 6933084926
4 changed files with 36 additions and 4 deletions

View file

@ -2,5 +2,12 @@
# All this logic will automatically be available in application.js. # All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/ # You can use CoffeeScript in this file: http://coffeescript.org/
$(document).on 'ready page:load', -> $(document).on 'ready page:load', ->
$(".highlight pre code").each (i, e) -> $(".code pre code").each (i, e) ->
hljs.highlightBlock e hljs.highlightBlock e
numberLines e
numberLines = (e) ->
$('.line-numbers').html([1..lineCount(e)].join("<br/>"))
lineCount = (e) ->
$(e).html().split("\n").length - 1

View file

@ -6,7 +6,6 @@ Original style from softwaremaniacs.org (c) Ivan Sagalaev <Maniac@SoftwareManiac
.hljs { .hljs {
display: block; display: block;
padding: 0.5em;
background: #f0f0f0; background: #f0f0f0;
} }

View file

@ -1,3 +1,26 @@
// Place all the styles related to the pastes controller here. // Place all the styles related to the pastes controller here.
// They will automatically be included in application.css. // They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/ // You can use Sass (SCSS) here: http://sass-lang.com/
.paste {
.line-numbers {
float: left;
padding: 10px;
margin: 1px 10px 0 0;
border-right: 1px solid;
line-height: 1.5;
font-size: 12px;
font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
text-align: right;
}
pre {
margin: 0;
padding: 10px;
.code {
margin: 0;
font-size: 12px;
}
}
}

View file

@ -1 +1,4 @@
<div class="highlight"><pre><code><%= @paste.content %></code></pre></div> <div class="paste">
<div class="line-numbers"></div>
<div class="code"><pre><code><%= @paste.content %></code></pre></div>
</div>