View Single Post
Old 09-19-2014, 11:52 AM   #582
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)
Quote:
Originally Posted by DMee View Post
Getting there :-)

I changed formulas to

stripped = re(field('series'), '^(A|The|An)\s+', '');
shortened = shorten(stripped, 8, '-' ,6);
initials = re(stripped, '[\s]?([^\s]{1,3})[^\s]+(\s|$)', '\1');

But summats wrong

Worlds Without End
becomes
WorWitEn

Missing "d"?
Well, chaley has already answered this.

The previous template assumed only the first letter would be captured, and matched the following letters outside the capture. But it required there to be letters outside, under the assumption that any word would have at least two letters. So at least one letter at the end would not be included.

hence chaley's fix.

Quote:
Also is it poss to have it as

Wor-Wit-End
@chaley, cool use of list_re() -- I am still trying to get my head around using them. Well, list_re_group() specifically. I'm sure they will eventually click into place though.

Thanks, and thank you for all these recent extensions to template programming! They will no doubt make things easier as soon as I get the hang of them.

Quote:
Also, is it feasible to add in the code we rejigged when doing series with fractions? you had a prog to decide which formula to use to reformat the series link properly depending on size of highest number and presence of fractions :-)
You mean this?

PHP Code:
program:

ser_num test(
    
field('#complex_series'),
    
finish_formatting(
        
field('series_index'),
        
'_>7.2f',
        
' [',
        
'] '
    
),
    
finish_formatting(
        
field('series_index'),
        
'_>4d',
        
' [',
        
'] '
    
),
);

strcat(
    
field('series'),
    
ser_num,
    
field('title')

Um, lessee...

Ah, it's easy. Just replace s_index.

PHP Code:
program:
#       compute the equivalent of the composite fields and store them in local variables
stripped re(field('series'), '^(A|The|An)\s+''');
shortened shorten(stripped4'-' ,4);
initials re(list_re(stripped' ''[\s]?([^\s]{1,3})[^\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.
#
#       Using the new ser_num calculation from https://www.mobileread.com/forums/showpost.php?p=2916022&postcount=548
s_index test(
    
field('#complex_series'),
    
finish_formatting(
        
field('series_index'),
        
'_>7.2f',
        
' [',
        
'] '
    
),
    
finish_formatting(
        
field('series_index'),
        
'_>4d',
        
' [',
        
'] '
    
),
);

#       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. Finally, add the title. As this is the last function in
#       the program, its value will be returned.
strcat(
    switch(
        
stripped,
        
'.\s',
        
initials,
        
'.',
        
shortened,
        
field('series')
    ),
    
test(
        
s_index,
        
strcat(' ['s_index'] '),
        
''
    
),
    
field('title')
); 
EDIT: added chaley's fix to the new template.

Using the second fix, to get "Wor-Wit-End" style.

Last edited by eschwartz; 09-19-2014 at 12:07 PM. Reason: remembered to use chaley's fix.
eschwartz is offline   Reply With Quote