View Single Post
Old 12-24-2013, 02:53 PM   #378
eschwartz
Ex-Helpdesk Junkie
eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.eschwartz ought to be getting tired of karma fortunes by now.
 
eschwartz's Avatar
 
Posts: 19,421
Karma: 85400180
Join Date: Nov 2012
Location: The Beaten Path, USA, Roundworld, This Side of Infinity
Device: Kindle Touch fw5.3.7 (Wifi only)
Latest plugboard -- this is the biggest monster of all. It took longer than I expected, but ONLY because,... I have taken the liberty of combining the famous series-stripping plugboard into this, so you can just get rid of those custom columns.

(If you would look at the template guide on the calibre manual, it actually shows that set of templates, but includes "The following program produces the same results as the original recipe, using only one custom column to hold the results of a program that computes the special title value:" and then gives the second part of my program.)

EDIT: In red is the series-stripping plugboard, which replaces the custom columns. In bold underlined green is the part of it that actually has the same text as the custom columns.

Here goes:

Code:
program:
#	save stuff before " - "
start=list_item(field('tags'),-2, ' - ');
#	save stuff after it
end=list_item(field('tags'),-1, ' - ');
#	divides 'end' into two, at '/'
part3=list_item(end,-2,'/');
part4=list_item(end,-1,'/');
#	divides last element for multi-word
part3alt=list_item(part4,-2,' ');
part4=list_item(part4,-1,' ');

#		return a value
thetags=strcat(
#		add first initial
	shorten(start,1,'',0),
#		add first separator if first initial not empty
	test(start,' - ',''),
#		add second initial
	shorten(part3,1,'',0),
#		add second separator if third not empty
	test(part3,'/',''),
#		if last section had ' ' not '/', use these two lines instead
##			add third initial part 1
	shorten(part3alt,1,'',0),
##			if third initial part 1 exists, add ' ' separator
	test(part3alt,' ',''),
#		add third initial
	shorten(part4,1,'',0),
);


#		compute the equivalent of the composite fields
#		and store them in local variables
	stripped = re(field('series'), '^(A|The|An)\s+', '');
	shortened = shorten(stripped, 4, '-' ,4);
	initials = re(stripped, '[^\w]*(\w?)[^\s]+(\s|$)', '\1');

#		Format the series index. Ends up as empty if there is no
#		series index. Note that leading and trailing spaces will be
#		removed by the formatter, so we cannot add them here.
#		We will do that in the strcat below. Also note that because
#		we are in 'program' mode, we can freely use curly brackets
#		in strings, something we cannot do in template mode.
	s_index = template('{series_index:0>2.0f}');

#		print(stripped, shortened, initials, s_index);

#		Now concatenate all the bits together. The switch picks
#		between initials and shortened, depending on whether
#		there is a space in stripped. We then add the brackets
#		around s_index if it is not empty. My version this is not
#		the last bit, but we save this as "theseries" for later use.
	theseries=strcat(
		switch( stripped,
			'.\s', initials,
			'.', shortened,
			field('series')),
		test(s_index, strcat(' [', s_index, '] '), ''));

#		put all the bits together, and return a monster value
	strcat(
		thetags,
		' - ',
		theseries,
		' - ',
		finish_formatting(
			field('series_index'),
			'0>2s',
			' [',
			'] '
		),
		' - ',
		substr(field('#purchased1'),0,3)
	)
Simplified into one line, to go straight into the plugboard box:

Code:
program:start=list_item(field('tags'),-2, ' - ');end=list_item(field('tags'),-1, ' - ');part3=list_item(end,-2,'/');part4=list_item(end,-1,'/');part3alt=list_item(part4,-2,' ');part4=list_item(part4,-1,' ');thetags=strcat(shorten(start,1,'',0),test(start,' - ',''),shorten(part3,1,'',0),test(part3,'/',''),shorten(part3alt,1,'',0),test(part3alt,' ',''),shorten(part4,1,'',0),);stripped = re(field('series'), '^(A|The|An)\s+', '');shortened = shorten(stripped, 4, '-' ,4);initials = re(stripped, '[^\w]*(\w?)[^\s]+(\s|$)', '\1');s_index = template('{series_index:0>2.0f}');theseries=strcat(switch( stripped,'.\s', initials,'.', shortened,field('series')),test(s_index, strcat(' [', s_index, '] '), ''));strcat(thetags,' - ',theseries,' - ',field('title'),' - ',finish_formatting(field('series_index'),'0>2s',' [','] '),' - ',substr(field('#purchased1'),0,3))

Last edited by eschwartz; 12-24-2013 at 04:56 PM.
eschwartz is offline   Reply With Quote