View Single Post
Old 02-15-2023, 09:40 AM   #2
isarl
Addict
isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.isarl ought to be getting tired of karma fortunes by now.
 
Posts: 293
Karma: 2534928
Join Date: Nov 2022
Location: Canada
Device: Kobo Aura 2
It is possible to do everything that you want within Calibre and its template language(s). The user manual gives an example of searching for book series and counting the results (see link at bottom of this comment). Once you have the number of books in a given series, the field width you need can be easily calculated with a bit of math. For instance, you want the number of digits needed to represent the largest index number in the series. For the sake of example, suppose that number is 12. First we take the base-10 logarithm: log10(12). In this case we get a number a little bit more than 1, about 1.079. Now, we know that 12 has two digits. So we could just round this up: ceil(log10(12)) gives us two. However, consider now a series with 10 books: log10(10) = 1, and ceil(1) = 1. So this formula chokes every time we add a new digit (it will likewise be wrong for 100, for 1000, etc.). Instead, we want: floor(log10(n))+1. Let's test our examples: floor(log10(12)) = 1, 1+1 = 2. And floor(log10(10)) = 1, 1+1=2. What about just before adding another digit?: floor(log10(9)) = 0, 0+1 = 1.

I will leave it as an exercise to you how to calculate the maximum number of digits you need after the decimal place, but supposing that you've already done so, I will refer to that number as n_decimals. (One way to do this would be to do string operations on the indices; subset the index number string to only the part after the decimal and then take its length, for instance.)

So to summarize, given a series of size n_books, you can calculate n_digits = floor(log10(n_books)) + 1 and then you can construct your format strings like strcat('0', n_digits, '.', n_decimals, 'f').

Hope this helps to unstick you! Happy organizing!

Edit: adding link to manual page. This is the page with an example of searching for series by length. Search for the phrase, “For example this template search uses this function and its companion to find all series with only one book”.

Last edited by isarl; 02-15-2023 at 09:47 AM.
isarl is offline   Reply With Quote