Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Sigil > Plugins

Notices

Reply
 
Thread Tools Search this Thread
Old 08-09-2017, 05:06 AM   #1
WS64
WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.
 
WS64's Avatar
 
Posts: 660
Karma: 506380
Join Date: Aug 2010
Location: Germany
Device: Kobo Aura / PB Lux 2 / Bookeen Frontlight / Kobo Mini / Nook Color
Rename html files

How can I rename a HTML file within an epub with a Sigil-plugin?
I tried
os.rename('../Text/Section0001.xhtml',...)
But I get a "file not found" error.
Is os.rename not suitable here (does it go to my harddrive maybe instead of the epub?), or how would I address a file called Section0001.xhtml in the Text folder of an epub?
WS64 is offline   Reply With Quote
Old 08-09-2017, 08:53 AM   #2
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 7,637
Karma: 5433388
Join Date: Nov 2009
Device: many
It is not easy. Renaming an xhtml file to some other extension involves walking the complete set of files and fixing all internal links, all toc.ncx entries, opf manifest entries. This ability is built into Sigil itself. So if you want to rename files, even multiple files, you should do this inside of Sigil before invoking an edit plugin.

FYI, The Sigil plugin system uses a copy on change approach and so you are not working with files internal to Sigil, just copies. This is to prevent a plugin from crashing Sigil. Instead the modified files are passed back to Sigil who then checks them and if okay merges the changed files in.

Please see the Plugin documentation and the sample plugin for use of the iterators for each file types and how manifest ids are used to access files.

Ask if you have questions.
KevinH is offline   Reply With Quote
Old 08-09-2017, 09:05 AM   #3
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 27,547
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Also keep in mind that it's very easy to rename .html file to .xhtml (even in bulk) from within Sigil's book Browser. Just in case that's the primary function of your plugin.

For bulk renaming, highlight multiple files in the Book Browser, then right-click to find the Rename action and enter ".xhtml" (without the quotes) in the dialog to change all the highlighted files' extensions to .xhtml
DiapDealer is offline   Reply With Quote
Old 08-09-2017, 09:18 AM   #4
WS64
WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.
 
WS64's Avatar
 
Posts: 660
Karma: 506380
Join Date: Aug 2010
Location: Germany
Device: Kobo Aura / PB Lux 2 / Bookeen Frontlight / Kobo Mini / Nook Color
Actually I do not want to edit file extensions but the name itself.
The idea is to rename the files to the content of some tags, e.g. title or h4 or whatever, if necessary with a numbering behind. (e.g. Section0001-xhtml -> MyTitle.xhtml, Section0002.xhtml -> TitleOfTheNextHTMLFIle.xhtml and so on)

For the plugin documentation, I can read the names of the files very easy (bk.text_iter()), but I have not the slightest idea where to find the actual file to do the renaming.

From your testme3 plugin, can it be that renaming can be done easiest by
- adding a new file
- copying content from old file to new file
- mapping the new file to the id of the old file (this is the point I am not sure about!)
- deleting the old file

This way I guess I do not have to bother with TOC entries and so on, but I may be completely wrong here.
WS64 is offline   Reply With Quote
Old 08-09-2017, 09:22 AM   #5
WS64
WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.
 
WS64's Avatar
 
Posts: 660
Karma: 506380
Join Date: Aug 2010
Location: Germany
Device: Kobo Aura / PB Lux 2 / Bookeen Frontlight / Kobo Mini / Nook Color
@DiapDealer, thanks, this part of the bulk-renaming was indeed new to me, and I even missed exactly that the other day.
This case here, however, is a bit different, see above.
WS64 is offline   Reply With Quote
Old 08-09-2017, 09:25 AM   #6
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,583
Karma: 22735033
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by WS64 View Post
How can I rename a HTML file within an epub with a Sigil-plugin?
You can't rename html files with the Sigil plugin API. You'll have to delete the file and add it as a new file with the desired name. (However, this method can only be used if the file doesn't contain any link target ids referenced by other .xhtml files, unless your script will also take care of this.)

For example:

Spoiler:
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def run(bk):
    manifest_id = 'Section0001.xhtml'
    new_file_name = 'new.xhtml'
    
    # get original spine order
    original_spine = bk.getspine()

    # get file contents
    data = bk.readfile(manifest_id)

    # delete file
    bk.deletefile(manifest_id)

    # add new file with the same file contents
    bk.addfile(manifest_id, new_file_name, data, "application/xhtml+xml")

    # restore original spine order
    bk.setspine(original_spine)

    return 0

def main():
    print('I reached main when I should not have\n')
    return -1

if __name__ == "__main__":
    sys.exit(main())


The above code will rename Section0001.xhtml to new.xhtml.

If you prefer to use pure Python code, you can use bk.copy_book_contents_to() to copy the current epub to a temporary folder and then use os.rename() etc. As with the first method, you'll have to update all link targets with your script.
Doitsu is offline   Reply With Quote
Old 08-09-2017, 01:13 PM   #7
WS64
WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.
 
WS64's Avatar
 
Posts: 660
Karma: 506380
Join Date: Aug 2010
Location: Germany
Device: Kobo Aura / PB Lux 2 / Bookeen Frontlight / Kobo Mini / Nook Color
@Doitsu, your code renames the file associated with the id (not the name) Section0001.xhtml to new.xhtml.
Not a problem, just thought I mention it in case someone else trys it.

But thanks, that helped a lot!
WS64 is offline   Reply With Quote
Old 08-09-2017, 01:25 PM   #8
DiapDealer
Grand Sorcerer
DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.DiapDealer ought to be getting tired of karma fortunes by now.
 
DiapDealer's Avatar
 
Posts: 27,547
Karma: 193191846
Join Date: Jan 2010
Device: Nexus 7, Kindle Fire HD
Just remember that if the "renamed" file is linked to in any other html ToC/NAV/NCX or index/footnote page, you're going to have to update those links manually with your plugin. But you can always use a validator and/or run a report to find any links with missing targets after the plugin is done.

Last edited by DiapDealer; 08-09-2017 at 01:29 PM.
DiapDealer is offline   Reply With Quote
Old 08-09-2017, 10:23 PM   #9
slowsmile
Witchman
slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.slowsmile ought to be getting tired of karma fortunes by now.
 
Posts: 628
Karma: 788808
Join Date: May 2013
Location: Philippines
Device: Android S5
@WS64...Not to sure what you want to do here. Renaming a text file in Sigil shouldn't be a problem.

But if you just want to rename your files in Sigil, do the following:

*In the Sigil Book Browser right click on the file you want to rename. Rename your file. Then hit enter.

*Now check the content.opf file in Sigil. Check that the file name has also updated in the <manifest> section. And if that file is also in your TOC then check that the file has also updated in the toc.ncx in Sigil.
slowsmile is offline   Reply With Quote
Old 08-10-2017, 01:15 AM   #10
WS64
WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.WS64 ought to be getting tired of karma fortunes by now.
 
WS64's Avatar
 
Posts: 660
Karma: 506380
Join Date: Aug 2010
Location: Germany
Device: Kobo Aura / PB Lux 2 / Bookeen Frontlight / Kobo Mini / Nook Color
Slowsmile, thanks, but I want to bulk-update the filenames, e.g. rename them all after the first appearance of the <h4> tag. Or the <title> tag.
And I want to do things like numbering the files but keep the rest of their filenames. And I don't mean Section0001 to Section0017 but things like 01_FirstChapter, 02_Interlude, 03_something else.

But actually I have all that right now already, now I have to check how to let the user communicate with the plugin to select which tag, if he wants numbering and so on.

My questions here are all answered so far, thanks all!
WS64 is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mass rename files r2d256 Calibre 6 11-21-2015 10:18 PM
automatically rename HTML files u238110 Sigil 16 03-20-2015 11:36 PM
How Do I Rename Files? cassieking Library Management 10 04-17-2014 01:35 AM
rename files richreads Sigil 2 01-01-2013 04:53 PM
rename files kawayama Sigil 9 12-22-2011 12:30 AM


All times are GMT -4. The time now is 10:01 PM.


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