![]() |
#1 |
Member
![]() Posts: 10
Karma: 10
Join Date: Jun 2010
Device: NONE
|
Need help! Template to shorten series name
Hi - I need help to shorten the series name using a template/plugboard!
I want to create a custom column using the following book title - but I am having trouble shortening the series name: (essentially, I want to use (approx) the first 2 words of the series name) [SHORTENED SERIES NAME] - series index# - book title I want to shorten the series name in a particular way - basically it would require 2 steps: a) I want to strip off the first word (if it is an article - a/an/the) I can do this with a custom column/using template syntax: #stripped_series. Template: {series:re(^(A|The|An)\s+,)||} b) then I want to use the first 2 words (approx) of the series name BUT THIS IS WHERE I GET STUCK... I want to use the first 2 words of the series name BUT if the following words appear, they should be included but not counted as one of the '2' words: any article (a/an/the), conjunction (and/or/&), preposition (of, from..etc), AND any word ending in '.' (full stop) ie any short form like Mr., St. etc. EXAMPLES: 1) Lord of the Rings and other Tolkien Stories WOULD APPEAR AS Lord of the Rings 2) Kincaid & James WOULD APPEAR AS Kincaid & James 3) Mr.Fox & Mrs.Chicken mysteries WOULD APPEAR AS Mr. Fox & Mrs. Chicken 4) D.I. Helen Grace series WOULD APPEAR AS D.I.Helen Grace Can anyone help me with a template that will perform STEP B? (I tried using python syntax strip.string_variable to no avail...I'm just stumped).... Thanks so much.... PERSISTENCE |
![]() |
![]() |
![]() |
#2 |
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 487
Karma: 32554
Join Date: May 2014
Location: Canada
Device: Kobo Libra Colour
|
Please don't start multiple threads on the same subject. Bump it up if you want it more visible.
|
![]() |
![]() |
![]() |
#3 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,916
Karma: 7176769
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
The only practical way to do what you want is to use a loop, which the template language does not support. You can do it by writing a custom template function in python, the content of I leave as an exercise for the reader.
|
![]() |
![]() |
![]() |
#4 |
Member
![]() Posts: 10
Karma: 10
Join Date: Jun 2010
Device: NONE
|
Hi chaley -
Sorry, I guess I'm too much of a noob to fully understand your advice. I'd be glad to do this, but need some help..could you help me get started (with syntax)? If I have some assistance to get started I can go from there...I just need a helping hand. Thanks, in advance...I'll await word. -Persistence |
![]() |
![]() |
![]() |
#5 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,916
Karma: 7176769
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Custom functions are written in pure python, using the editor available from Preferences / Template Functions. The dialog provides some help for setting things up. Here is a sample template function that returns the first word of a series if there is one, the empty string otherwise. Code:
def evaluate(self, formatter, kwargs, mi, locals): series = mi.get('series') if series: return series.split()[0] return '' |
|
![]() |
![]() |
![]() |
#6 |
Member
![]() Posts: 10
Karma: 10
Join Date: Jun 2010
Device: NONE
|
Hi Charles -
I wouldn't want you to write the procedure for me - but I am asking for your help (when you have time). I am a physician. I studied programming years ago. I don't know python syntax, but I am familiar with the process of writing a short program. I know that I'll need to include a loop that evaluates each word in the series starting with the first word, and 'decides' whether to discard /keep the word. The loop would terminate after collecting /keeping 3 words (or when there are no more words to evaluate), whichever comes first. I saw your example but it lost me from the first line, sadly...likely because of my lack of familiarity with python syntax. Python documentation was of little help. I just need some help to explain what each line does in your example (or if possible, in an example that relates fairly closely to what I need to do) - so I can hopefully mix/match and create a function that does what I need. I would be grateful for any help you can offer. Thanks for taking the time to reply to me. Persistence |
![]() |
![]() |
![]() |
#7 |
Resident Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 75,783
Karma: 134321338
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Using your Lord of the Rings example, what you are asking for would come out Lord of the.
The solution is to put in the series name you want and create a custom column to put in the full series name. |
![]() |
![]() |
![]() |
Tags |
calibre, custom column, plugboard, series, template |
Thread Tools | Search this Thread |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Truncate series name using template -- Help!! (use first 3 words) | PERSISTENCE | Library Management | 0 | 01-14-2017 05:17 AM |
Calibre Template and Series | sehlat | Calibre | 4 | 09-16-2013 11:21 AM |
Save Template, {series} etc Always Empty | dbareis | Library Management | 13 | 07-01-2013 07:27 PM |
How to shorten device filename with series info? | Larla | Library Management | 16 | 01-08-2012 01:22 PM |
save file template for series | bucsie | Calibre | 2 | 11-08-2010 02:07 AM |