09-14-2022, 11:30 AM | #1 |
Enthusiast
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
|
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> 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; Code:
program: before = '<ul>'; after = '</ul>'; j = ''; for i in 'tags': j += i; rof; before + j + after; 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 |
09-14-2022, 11:44 AM | #2 |
Enthusiast
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; |
Advert | |
|
09-14-2022, 12:42 PM | #3 |
Enthusiast
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.
|
09-14-2022, 01:05 PM | #4 |
Well trained by Cats
Posts: 30,451
Karma: 58055868
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
Moved
|
09-14-2022, 03:03 PM | #5 | |
Grand Sorcerer
Posts: 12,032
Karma: 7257323
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
You have several problems:
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 Code:
program: j = ''; for i in $tags: j = j & '<li>' & i & '</li>' rof; '<ul>' & j & '</ul>' |
|
Advert | |
|
09-14-2022, 08:19 PM | #6 |
Enthusiast
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> 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 08:15 AM. Reason: Posted an update |
09-15-2022, 11:43 AM | #7 | |
Grand Sorcerer
Posts: 12,032
Karma: 7257323
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
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>' |
|
09-15-2022, 12:02 PM | #8 | |
Enthusiast
Posts: 33
Karma: 10
Join Date: Sep 2022
Device: Microsoft Surface Pro 7, Apple iPad
|
Quote:
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 12:04 PM. Reason: Added missing answer |
|
09-15-2022, 12:10 PM | #9 | |
Grand Sorcerer
Posts: 12,032
Karma: 7257323
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
|
|
09-15-2022, 01:05 PM | #10 | |
Enthusiast
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>' 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:
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 02:47 PM. Reason: Placed images in spoilers |
|
09-15-2022, 01:39 PM | #11 |
Chalut o/
Posts: 421
Karma: 145424
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 01:41 PM. |
09-15-2022, 01:43 PM | #12 |
Enthusiast
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.
|
09-15-2022, 02:14 PM | #13 |
Chalut o/
Posts: 421
Karma: 145424
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"> I will continue on my side to solve the remaining problems. |
09-15-2022, 02:17 PM | #14 |
Grand Sorcerer
Posts: 12,032
Karma: 7257323
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() 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>' Example: for the tag "Fantasy & Magic" the template generates the URL Code:
calibre://search/_hex_-4c6962726172792e746573745f736d616c6c?eq=746167733a3d22222246616e746173792026204d61676963222222 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. |
09-15-2022, 02:39 PM | #15 |
Enthusiast
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 02:42 PM. Reason: Put huge images into spoilers |
Tags |
calibe, calibre 6.4, custom columns, html, template language |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Merging custom column into Tags | MimiRose113 | Library Management | 1 | 03-31-2021 03:59 AM |
Custom column from tags | jelzin | Library Management | 4 | 03-15-2021 03:30 PM |
How can I boolean OR several tags into one custom column? | bmix | Library Management | 3 | 01-09-2015 04:57 AM |
Download tags to a custom column | atjnjk | Library Management | 8 | 01-18-2012 09:02 AM |
Help with template for custom column from tags | africalass | Library Management | 2 | 07-16-2011 12:47 PM |