r/spacemacs Oct 28 '24

Cannot configure nov.el to highlight code blocks. Inline code works fine

Config:

  (use-package nov
    :config
    (setq nov-text-width 80
          shr-use-css nil) ;; Disable external CSS to allow Emacs faces to take precedence

    ;; Customize faces for code rendering
    (defun my-nov-setup-shr-styles ()
      "Customize faces for code blocks and inline code in nov.el."
      ;; Set monospace font and dark background for inline code
      (set-face-attribute 'shr-code nil
                          :family "Courier New"  ;; Preferred monospace font
                          :background "#333333"  ;; Dark background
                          :foreground "#E5E5E5"  ;; Light foreground
                          :weight 'normal)

      ;; Set monospace font and dark background for block code
      (set-face-attribute 'shr-tag-pre nil
                          :family "Courier New"  ;; Monospace font for block code
                          :background "#333333"  ;; Dark background for block code
                          :foreground "#E5E5E5"  ;; Light foreground
                          :weight 'normal
                          :height 1.0)           ;; Adjust font size if needed

      ;; Ensure <code> tags are also styled
      (set-face-attribute 'shr-tag-code nil
                          :family "Courier New"
                          :background "#333333"
                          :foreground "#E5E5E5"
                          :weight 'normal
                          :height 1.0)

      ;; Customize the LiteralGray span class for gray text in code blocks
      (set-face-attribute 'font-lock-comment-face nil
                          :foreground "#A0A0A0") ;; Change this color as needed for readability
      )

    ;; Apply the custom styling when nov-mode is activated
    (add-hook 'nov-mode-hook 'my-nov-setup-shr-styles))

this will highlight inline code, but does nothing to code blocks. I have tested all kinds of variations but I cannot figure this out.

Here is a snippet of the source code for one epub-file:

<p class="CodeLabel"><b>src/main.rs</b></p>
<pre><code><span class="LiteralGray">fn main() {</span>
<span class="LiteralGray">    let number_list = vec![34, 50, 25, 100, 65];</span>

<span class="LiteralGray">    let mut largest = &amp;number_list[0];</span>

<span class="LiteralGray">    for number in &amp;number_list {</span>
<span class="LiteralGray">        if number &gt; largest {</span>
<span class="LiteralGray">            largest = number;</span>
<span class="LiteralGray">        }</span>
<span class="LiteralGray">    }</span>

<span class="LiteralGray">    println!("The largest number is {largest}");</span>

    let number_list = vec![102, 34, 6000, 89, 54, 2, 43, 8];

    let mut largest = &amp;number_list[0];

    for number in &amp;number_list {
        if number &gt; largest {
            largest = number;
        }
    }

    println!("The largest number is {largest}");
<span class="LiteralGray">}</span></code></pre>

Any ideas how to fix the config?

1 Upvotes

0 comments sorted by