8

Github has a convenient way to show some lines as highlighted in the code, like so.

I'd like to know if there's a way to setup something similar with org-mode code blocks when exporting (in my use case, to HTML), so I can show a longer code block for context yet point the reader quickly towards the lines that were added or modified.

I haven't found anything obvious in the documentation. I don't know where I would start the research to implement this myself.

Can this be done? Has it already been implemented? If not, can I implement this myself? Where would I start?

I have found that org-mode exports to HTML and provides a .code-highlighted class, but that seems to be only for the (ref:xx) tools: hover over a reference, the corresponding code gets highlighted.

1
  • There is no way without writing some code I think. You probably need to mark up your code block with something that looks like a comment, and then use an org-filter to modify the output with the html markup required for highlighting. Commented Feb 23, 2016 at 0:10

1 Answer 1

2

Based on the coderef label syntax I use a (hl:line) pseudo label that gets replaced by an org-export-filter afunction. Referenced lines are wrapped in a div with a style attribute:

(defun org-html-hl-line (src backend info)
  (when (org-export-derived-backend-p backend 'html)
    (replace-regexp-in-string
     "\\([^\n]+\\)(hl:line)\n"
     "<div style=\"background-color: rgb(248, 238, 199);\">\\1\n</div>" src)))

(add-to-list 'org-export-filter-src-block-functions
             'org-html-hl-line)

Note that labels have to be at the end of the line as shown in the following example:

(functionp &rest form)           (hl:line)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.