Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 09-14-2022, 10:30 AM   #1
01100001
Enthusiast
01100001 began at the beginning.
 
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
Question Display tags as an unordered list (HTML) using a custom column

I created a custom column of type Column built from other columns and intend to use it to display Tags as an unordered HTML list.

By unordered HTML list, I mean something like this:

Code:
<ul>
    <li>Tag1</li>
    <li>Tag2</li>
    <li>Tag3</li>
</ul>
I'd like to ask for help w.r.t. the code required in the Template field of the custom column.

What I've tried

I don't know python, but using the documentation and some googling, I came up with this:

Code:
program:
  before = '<ul>';
  after = '</ul>';
  j = '';
  for i in 'tags':
    j += i;
  rof;
  before + j + after;
Then turned it into a one-liner and added it to the Template field of the custom column creation dialog box.

Code:
program: before = '<ul>'; after = '</ul>'; j = ''; for i in 'tags': j += i; rof; before + j + after;
I also made sure to check the Show as HTML in Book details option.

The Error

Here's the output I see for the custom column/field in the Book details sidebar:

Code:
TEMPLATE ERROR Formatter: Expected an expression, found '=' near '+' on line 1
Any help is most appreciated!
01100001 is offline   Reply With Quote
Old 09-14-2022, 10:44 AM   #2
01100001
Enthusiast
01100001 began at the beginning.
 
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
My code blocks were missing li tags, here's me fixing that (not that it helped my situation, but it helps keep things clear):

Code:
program:
  before = '<ul>';
  after = '</ul>';
  j = '';
  for i in 'tags':
    j += '<li>' + i + '</li>';
  rof;
  before + j + after;
Code:
program: before = '<ul>'; after = '</ul>'; j = ''; for i in 'tags': j += '<li>' + i + '</li>'; rof; before + j + after;
I have also tried using field('tags') (instead of 'tags') and strcat(before, j, after); (instead of before + j + after;) in my code. Still same error.
01100001 is offline   Reply With Quote
Advert
Old 09-14-2022, 11:42 AM   #3
01100001
Enthusiast
01100001 began at the beginning.
 
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
Should've posted this in the Library Management forum, please move the thread if possible. Sorry for the oversight.
01100001 is offline   Reply With Quote
Old 09-14-2022, 12:05 PM   #4
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
Posts: 29,809
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Moved
theducks is offline   Reply With Quote
Old 09-14-2022, 02:03 PM   #5
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by 01100001 View Post
My code blocks were missing li tags, here's me fixing that (not that it helped my situation, but it helps keep things clear):

Code:
program:
  before = '<ul>';
  after = '</ul>';
  j = '';
  for i in 'tags':
    j += '<li>' + i + '</li>';
  rof;
  before + j + after;
Code:
program: before = '<ul>'; after = '</ul>'; j = ''; for i in 'tags': j += '<li>' + i + '</li>'; rof; before + j + after;
I have also tried using field('tags') (instead of 'tags') and strcat(before, j, after); (instead of before + j + after;) in my code. Still same error.
Probably isn't exactly the same error.

You have several problems:
  • The template language doesn't contain the += operator.
  • The string concatenation operator is & not +.
  • 'tags' is a constant string. You should use $tags, field('tags'), or raw_field('tags').
  • Semicolons are statement separators, not terminators. For example, using a semicolon before rof or fi is an error. It is an error to put one at the end of the program, but tolerated.

Here is a corrected version:
Code:
program:
  before = '<ul>';
  after = '</ul>';
  j = '';
  for i in $tags:
    j = j & '<li>' & i & '</li>'
  rof;
  before & j & after
Comment: I don't see the point of the before and after variables. Why not do
Code:
program:
  j = '';
  for i in $tags:
    j = j & '<li>' & i & '</li>'
  rof;
  '<ul>' & j & '</ul>'
chaley is offline   Reply With Quote
Advert
Old 09-14-2022, 07:19 PM   #6
01100001
Enthusiast
01100001 began at the beginning.
 
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
Works beautifully, except the tags are just text, not links (the way tags are usually clickable, each allowing you to filter the Book list upon clicking). How do I fix that?

My bad, I see that my example didn't show it. This is what I am after:

Code:
<ul>
    <li><a href="...">Tag1</a></li>
    <li><a href="...">Tag2</a></li>
    <li><a href="...">Tag3</a></li>
</ul>
Will attempt to fix this myself in the meantime and if I do come up with a solution, I will be sure to post it here.

Thank you very much for your time!

EDIT: Unfortunately, no luck so far.

And I noticed something very important. A comma is assumed as the separator of values! So if I were to use an authors-like custom column (which I am; but simplified it for the example), where & is the separator, it gets messy. (Because values are split at commas instead of &'s when generating/identifying list items.)

I am not sure if anything can be done about this anymore. Would be great if there's a solution though.

Last edited by 01100001; 09-15-2022 at 07:15 AM. Reason: Posted an update
01100001 is offline   Reply With Quote
Old 09-15-2022, 10:43 AM   #7
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by 01100001 View Post
EDIT: Unfortunately, no luck so far.

And I noticed something very important. A comma is assumed as the separator of values! So if I were to use an authors-like custom column (which I am; but simplified it for the example), where & is the separator, it gets messy. (Because values are split at commas instead of &'s when generating/identifying list items.)

I am not sure if anything can be done about this anymore. Would be great if there's a solution though.
It would help if you read the General Program Mode section of the calibre manual. There you would see that you can specify the separator to use in a for loop.

You don't say what the link text should be or where it comes from so I use "link_text" in this example.
Code:
program:
	j = '';
	for i in $#people separator '&':
	    j = j & '<li><a href="' & 'link_text' & '">' & i & '</a></li>'
	rof;
	'<ul>' & j & '</ul>'
chaley is offline   Reply With Quote
Old 09-15-2022, 11:02 AM   #8
01100001
Enthusiast
01100001 began at the beginning.
 
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
Quote:
Originally Posted by chaley View Post
You don't say what the link text should be or where it comes from...
I did, but I may not have been clear enough. I want the list to function just like tags. What do tags link to? I do not know, but they do appear as links in the Book details pane. When we click on a tag, Calibre's Book list filters to show books tagged with it. Makes sense? (I don't know how else to say it. ) That's what I want in my list generated from tags too.

EDIT: I wasn't referring to General Program Mode section because I read somewhere that it works only in specific places (or not everywhere). So I didn't bother, sorry.

Last edited by 01100001; 09-15-2022 at 11:04 AM. Reason: Added missing answer
01100001 is offline   Reply With Quote
Old 09-15-2022, 11:10 AM   #9
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by 01100001 View Post
I did, but I may not have been clear enough. I want the list to function just like tags. What do tags link to? I do not know, but they do appear as links in the Book details pane. When we click on a tag, Calibre's Book list filters to show books tagged with it. Makes sense? (I don't know how else to say it. ) That's what I want in my list generated from tags too.
Sorry, but I am confused. Where exactly do you want to see and click on these links? How does this improve upon what is already in book details, where you already see links for anything that supports them?
chaley is offline   Reply With Quote
Old 09-15-2022, 12:05 PM   #10
01100001
Enthusiast
01100001 began at the beginning.
 
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
Should've explained with an example in first place. My fault; I didn't see it as necessary.

How tags look by default in the details pane/sidebar:

Spoiler:


They are in blue, and they are essentially (internal) links, right? And what do they link to? When you click on each tag, Calibre filters/searches the books list to show books assigned to that particular tag.

The older code you provided:

Code:
program:
  j = '';
  for i in $tags:
    j = j & '<li>' & i & '</li>'
  rof;
  '<ul>' & j & '</ul>'
What it does is this:

Spoiler:


So where are the links for each term in our custom column/field as shown in the default tags field for each tag? I want that function.

So that it looks like this:

Spoiler:


And when clicked, the links should function like the default tags do when they are clicked on.

Hope I am clear this time. In case I am not, please don't hesitate to ask for any missing specifics.

Again, thank you for taking the time!

EDIT: To answer your question

Quote:
Originally Posted by chaley View Post
How does this improve upon what is already in book details, where you already see links for anything that supports them?
This isn't about tags. It's just an example. I'll be using this for a tags-like custom column called Filenames. Since I want it to behave like tags (for my specific usecase), let's just assume we are dealing with tags.

Now, the way tags are displayed by default isn't appealing to me. I want the terms displayed as a list so they are easier to parse at a glance. (In my specific case, filenames are long, and multiple filenames would be easier to parse if they were shown as a list.)

And why tags-like column and not something else? Because I want the links-like terms that tags have. Just right-click and you can quickly copy the link text (in my case filename). For normal text, I'd have to manually select the text, make sure no line breaks/endings are selections and then copy the text.

Just trying to optimize things for my workflow here.

Last edited by 01100001; 09-15-2022 at 01:47 PM. Reason: Placed images in spoilers
01100001 is offline   Reply With Quote
Old 09-15-2022, 12:39 PM   #11
un_pogaz
Chalut o/
un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.
 
un_pogaz's Avatar
 
Posts: 410
Karma: 145324
Join Date: Dec 2017
Device: Kobo
Ah, to this you will probably look the url scheme (if you have activated them at installation)

For a search on the current library: calibre://search/_?q=query

I can't go further than recalling this feature. I hope it will work.

Last edited by un_pogaz; 09-15-2022 at 12:41 PM.
un_pogaz is offline   Reply With Quote
Old 09-15-2022, 12:43 PM   #12
01100001
Enthusiast
01100001 began at the beginning.
 
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
@un_pogaz Yep, I am aware of that, but I am looking to see if there's a built-in, less error-prone way to go about it. Esp. because I'd have to code the URLs in which the search terms must be encoded right.
01100001 is offline   Reply With Quote
Old 09-15-2022, 01:14 PM   #13
un_pogaz
Chalut o/
un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.un_pogaz understands the importance of being earnest.
 
un_pogaz's Avatar
 
Posts: 410
Karma: 145324
Join Date: Dec 2017
Device: Kobo
I don't really know how it works, so if more advanced people can confirm or have better ideas, thanks.
Here is a cleaner and more functional version.
calibre://search/_?q=tags:%22=tag%22
(the %22 are important to have the " in the query.)
Code:
<a href="calibre://search/_?q=tags:%22=' & i & '%22">
This should do the vast majority of case you are looking for and if there are any bugs left, it should be a solid base to work from.
I will continue on my side to solve the remaining problems.
un_pogaz is offline   Reply With Quote
Old 09-15-2022, 01:17 PM   #14
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: 11,742
Karma: 6997045
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I now understand what you are trying to do. With a recent calibre 6 release it can be done with a template and a custom template function. The template constructs calibre search URLs. The template function is used to hex-encode the search parameters.

First, the template function: you add template functions in Preferences / Template functions. Click the second tab. Enter my_to_hex (or whatever you want, but don't use to_hex) in the function box. Enter 1 (one) in the argument count box. Enter the following in the program code box
Code:
def evaluate(self, formatter, kwargs, mi, locals, val):
		return val.encode().hex()
This screen capture shows what I mean.

Click image for larger version

Name:	Clipboard01.jpg
Views:	61
Size:	137.4 KB
ID:	196551

Assuming you used my_to_hex above, the template is
Code:
program:
	j = '';
	for i in $tags:
		j = j & '<li><a href="' & 'calibre://search/_hex_-' &
			my_to_hex(current_library_name()) & '?eq=' & 
			my_to_hex('tags:="""' & i & '"""') & 
			'">' & i & '</a></li>'
	rof;
	'<ul>' & j & '</ul>'
The template constructs a calibre search URL as described in the calibre manual. The hex encoding is required because both the library name and the query string can contain almost anything, and trying to do HTML escapes is a losing proposition. The encoded search uses super-quotes to avoid needing to escape anything in the search string.

Example: for the tag "Fantasy & Magic" the template generates the URL
Code:
calibre://search/_hex_-4c6962726172792e746573745f736d616c6c?eq=746167733a3d22222246616e746173792026204d61676963222222
The links appear in calibre like this

Click image for larger version

Name:	Clipboard02.jpg
Views:	62
Size:	15.3 KB
ID:	196552

Using calibre links isn't the fastest thing in the world, but they do work. And there isn't any other way I can think of to do it.

You may ask "Why not use to_hex()"? I am going to add that as a built-in template function. Have a custom function with the same name will make things break.
chaley is offline   Reply With Quote
Old 09-15-2022, 01:39 PM   #15
01100001
Enthusiast
01100001 began at the beginning.
 
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
Wow, so close. Here's the difference that I hope the images will convey:

Image 1: Showing the default Filenames tags-like column/field, followed by the custom Filenames2 column/field generated based on it.

Spoiler:


Now, I right-click on the filename/link in the first column/field, and these are the options I see (selected is the one I want):

Spoiler:


Here's what I get when I right-click on the filename/link in the 2nd (custom) column/field:

Spoiler:


This defeats the purpose I wanted the links for in the first place, as I explained in an earlier post. https://www.mobileread.com/forums/sh...8&postcount=10

Looks like I've reached the end of the rope here, as this appears to be a UI limitation.

I will see if I can come up with a different idea, 'cause this isn't going to fit in my workflow, which is unfortunate considering the time everyone took to help me out.

Thank you so very much!

Last edited by chaley; 09-15-2022 at 01:42 PM. Reason: Put huge images into spoilers
01100001 is offline   Reply With Quote
Reply

Tags
calibe, calibre 6.4, custom columns, html, template language


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging custom column into Tags MimiRose113 Library Management 1 03-31-2021 02:59 AM
Custom column from tags jelzin Library Management 4 03-15-2021 02:30 PM
How can I boolean OR several tags into one custom column? bmix Library Management 3 01-09-2015 03:57 AM
Download tags to a custom column atjnjk Library Management 8 01-18-2012 08:02 AM
Help with template for custom column from tags africalass Library Management 2 07-16-2011 11:47 AM


All times are GMT -4. The time now is 10:38 AM.


MobileRead.com is a privately owned, operated and funded community.