In Emacs, how to add line number for src block by CSS?

40 views Asked by At

in the following src block example:

#+begin_src python -r -n :results output :exports both
print("Hello")
print("I need line number for these code")
print("But I don't need line number when I copy code")
#+end_src

the corresponding exported html result looks like:

enter image description here

when I select, copy these code and paste to IDE to run it, I have to delete all line number. I want to ommit the line number when I copy them.


I try to use CSS to solve these problem:

pre {
   margin-top: 5px;
   margin-bottom: 20px;
   border: #66c 1px solid;
   padding: 0.5em;
   white-space: pre;
   /* background: #bce;
   background: transparent url("images/bg.jpg") repeat fixed; */
   background: #FFF;
   color: black;
   text-align: left;
   counter-reset: line; 
}

if I remove -n paramenter in src block, then 


pre .linenr::before {
    content: counter(line);
    counter-increment: line;
    padding-right: 1em;
    color: #999;
    border-right: 1px solid #ddd;
    margin-right: 1em;
    display: inline-block;
}

now, the exported html look like:

enter image description here

Can anyone please help me solve this problem?

2

There are 2 answers

0
letranger On BEST ANSWER

I roughly solved part of the problem using the following CSS.


pre {
   margin-top: 5px;
   margin-bottom: 20px;
   border: #66c 1px solid;
   padding: 0.5em;
   white-space: pre-wrap; 
   background: #FFF;
   color: black;
   text-align: left;
   counter-reset: line; 
}

pre .linenr:not(.org-src-container) {
    content: counter(line);
    counter-increment: line;
    padding-right: 1em;
    color: #999;
    color: #20b2aa;
    border-right: 1px solid #ddd;
    border-right: 1px dashed #66cdaa;
    margin-right: 1em;
    display: inline;
}

pre .linenr.org-src-container {
    display: none;
}

.src .linenr {
    user-select: none; 
}

1
Y. E. On

To disable lines numbering in the exported source block, remove the -n switch from the #+begin_src line.

The -n switch does exactly what you're trying to avoid: numbers the lines of the source block. For more information on this, see (info "(org) Literal Examples").

As of today, it states:

Both in ‘example’ and in ‘src’ snippets, you can add a ‘-n’ switch to the end of the ‘#+BEGIN’ line, to get the lines of the example numbered. The ‘-n’ takes an optional numeric argument specifying the starting line number of the block. If you use a ‘+n’ switch, the numbering from the previous numbered snippet is continued in the current one. The ‘+n’ switch can also take a numeric argument. This adds the value of the argument to the last line of the previous block to determine the starting line number.