Motivation

Calibre viewer can be configured with a custom css style. This is done in

Calibre -> Preferences -> Styles

Which presents you with a css file to edit using the calibre file editor. The editor is plain and limiting. It is much more convenient to use a proper editor to edit a local file for this purpose. Unfortunately, calibre does not allow this as an option. The following thread Custom CSS Stylesheets for Calibre's Viewer on www.mobileread.com contains lots of useful css code, but using the default calibre file editor is extremely painful, not least of which is because you have to open and close it every time you wish to make an edit.

Solution

The file calibre-css.sh contains a shell script to update the calibre css stylesheet using a css file of your choice. Edit the file calibre-css.sh and set CSS_FILE and CALIBRE_CONFIG_DIRECTORY accordingly. CSS_FILE is the local css file you wish to use. CALIBRE_CONFIG_DIRECTORY is the default calibre configuration location, this is set to system default.

Now you can run calibre-css.sh to inject your css stylesheet file into calibre. NOTE: your calibre viewer should not be running when you do this, since we are changing the viewers stylesheet.

All the work in calibre-css.sh is done by a one liner, the last line of the file, which is explained below:

jq --arg val "$(cat "$CSS_FILE")" '."session_data"."user_stylesheet" = $val' "$config_file" >"$tmp" && mv "$tmp" "$config_file"

--arg val "$(cat "$CSS_FILE")" : sets the variable val to the contents of $CSS_FILE

'."session_data"."user_stylesheet" = $val' : jq program to run. It assigns the variable "session_data"."user_stylesheet" to the value of the variable val, i.e. the contents of $CSS_FILE

"$config_file" : is the source file

> "$tmp" : send the output of jq to a temporary file

&& mv "$tmp" "$config_file" : and if all goes well replace the original with the temporary file

In closing calibre.css is the style sheet I use. It's an amalgamation of various sources, that mimics the solarized color scheme.