View Single Post
Old 08-08-2025, 10:12 AM   #2
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Here is a template that (I think) follows your rules. I can't test it because I don't have real data. You can test it in the calibre template tester to see if it generates the right output for various books.

There are two parts to this template, a stored python template and the template to put in the plugboard. The stored template, has_index_over_ten(), is in the attached template export file. Note that you can make the plugboard template a stored template as well, which would make editing easier.

I could test this if you give me a subset of your metadata.db that has all the permutations.

Code:
program:

	# First find the parameters
	has_series = $series != '';
	has_stk_series = $#stk_series != '';
	has_series_universe = $#series_universe != '';
	has_original_title = $#original_title != '';

	# Check if the various series indices are larger than 10.
	# First, the normal series
	series_greater_than_10 = has_index_over_ten('series', $series);

	# Now check stk_series
	stk_series_greater_than_10 = has_index_over_ten('#stk_series', $stk_series);

	# Lastly, series_universe
	series_universe_greater_than_10 = has_index_over_ten('#series_universe', $series_universe);


	# Now select the right template.
	# First check if none of the special cases apply -- the book doesn't
	# have any of series_universe, stk_series, or normal series
	if ! (has_series_universe || has_stk_series || has_series) then
		if has_original_title then
			return $#original_title
		else
			return $title
		fi
	fi;

	# A special case applies.
	# Check series_universe first.
	if has_series_universe then
		if series_universe_greater_than_10 then
			return template('{#series_universe}{#series_universe_index:0>5.2f| [|] }{title}')
		else
			return template('{#series_universe}{#series_universe_index| [|]}{title}')
		fi
	fi;

	# No series universe. Check stk_series
	if has_stk_series then
		if stk_series_greater_than_10 then
			return template('{#stk_series}{#stk_series_index:0>5.2f| [|] }{title}')
		else
			return template('{#stk_series}{#stk_series_index| -|- }{title}')
		fi
	fi;

	# No series_universe or stk_series. Because of the test above we know 
	# it has a series so ...
	if series_greater_than_10 then
		return template('{series}{series_index:0>5.2f| [|] }{title}') 
	else
		return template('{series}{series_index| [|]}{title}')
	fi
Attached Files
File Type: txt has_index_over_ten.txt (611 Bytes, 5 views)
chaley is offline   Reply With Quote