From 6933084926a555ce1859d4e62292bee0806b67ea Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sun, 9 Mar 2014 06:30:30 -0400 Subject: [PATCH] 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. --- app/assets/javascripts/pastes.js.coffee | 11 +++++++++-- app/assets/stylesheets/highlight.css | 1 - app/assets/stylesheets/pastes.css.scss | 23 +++++++++++++++++++++++ app/views/pastes/show.html.erb | 5 ++++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/pastes.js.coffee b/app/assets/javascripts/pastes.js.coffee index cf33fea..64a46d8 100644 --- a/app/assets/javascripts/pastes.js.coffee +++ b/app/assets/javascripts/pastes.js.coffee @@ -2,5 +2,12 @@ # All this logic will automatically be available in application.js. # You can use CoffeeScript in this file: http://coffeescript.org/ $(document).on 'ready page:load', -> - $(".highlight pre code").each (i, e) -> - hljs.highlightBlock e + $(".code pre code").each (i, e) -> + hljs.highlightBlock e + numberLines e + +numberLines = (e) -> + $('.line-numbers').html([1..lineCount(e)].join("
")) + +lineCount = (e) -> + $(e).html().split("\n").length - 1 diff --git a/app/assets/stylesheets/highlight.css b/app/assets/stylesheets/highlight.css index 05e32a7..a84b6c6 100644 --- a/app/assets/stylesheets/highlight.css +++ b/app/assets/stylesheets/highlight.css @@ -6,7 +6,6 @@ Original style from softwaremaniacs.org (c) Ivan Sagalaev
<%= @paste.content %>
+
+
+
<%= @paste.content %>
+