You can control where/how truncation happens using the built-in template function shorten(val, left chars, middle text, right chars).
The function documentation is:
Quote:
Return a shortened version of val, consisting of `left chars` characters from the beginning of val, followed by `middle text`, followed by `right chars` characters from the end of the string. `Left chars` and `right chars` must be integers. For example, assume the title of the book is `Ancient English Laws in the Times of Ivanhoe`, and you want it to fit in a space of at most 15 characters. If you use {title:shorten(9,-,5)}, the result will be `Ancient E-anhoe`. If the field's length is less than left chars + right chars + the length of `middle text`, then the field will be used intact. For example, the title `The Dome` would not be changed.
|
The idea is that if filenames can be truncated because of path length then *you* decide where the truncation occurs. This ensures that the resulting values are predictable and consistent.
You can also choose to use only the first author, not all of them.
For example, this template uses the first 30 characters of the first author's name and the first 120 characters of the title, separated by a dash.
Code:
{authors:'shorten(sublist($, 0, 1, '&'), 30, '', 0)'} - {title:shorten(120,,0)}
You can get more exotic and use the first 15 characters of the first N authors separated however you want, then the title shortened however you want. This is easiest done with a
General Program Mode template using a 'for' statement to loop through the authors. For example, the following template includes the first 4 authors shortened to 15 characters then a dash then the title shortened to 100 characters.
Code:
program:
res = '';
sep = '';
for a in sublist($authors, 0, 4, '&') separator '&':
res = res & sep & shorten(a, 15, '', 0);
sep = ' & '
rof;
res & ' - ' & shorten($title, 100, '', 0)
NB: this template works in calibre 5.39 or later because it used the new '&' concatenation operator. If necessary you could back up to V5.10 (or so) by using the strcat() function.