MobileRead Forums

MobileRead Forums (https://www.mobileread.com/forums/index.php)
-   Plugins (https://www.mobileread.com/forums/forumdisplay.php?f=268)
-   -   Rename html files (https://www.mobileread.com/forums/showthread.php?t=289426)

WS64 08-09-2017 06:06 AM

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?

KevinH 08-09-2017 09:53 AM

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.

DiapDealer 08-09-2017 10:05 AM

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

WS64 08-09-2017 10:18 AM

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 08-09-2017 10:22 AM

@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.

Doitsu 08-09-2017 10:25 AM

Quote:

Originally Posted by WS64 (Post 3565321)
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.

WS64 08-09-2017 02:13 PM

@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!

DiapDealer 08-09-2017 02:25 PM

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.

slowsmile 08-09-2017 11:23 PM

@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.

WS64 08-10-2017 02:15 AM

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!


All times are GMT -4. The time now is 07:35 PM.

Powered by: vBulletin
Copyright ©2000 - 3.8.5, Jelsoft Enterprises Ltd.
MobileRead.com is a privately owned, operated and funded community.