View Single Post
Old 11-23-2020, 09:07 PM   #9
EbookMakers
Enthusiast
EbookMakers began at the beginning.
 
Posts: 26
Karma: 38
Join Date: Nov 2019
Location: Paris, France
Device: none
The function for well-behaved people

This function is not interrupted, the ‘return’ is executed. But this 'return' replaces the selected note by the set of notes already encountered, and ordered by a mechanism similar to that of the rascal function. Therefore, after 'merging' and 'returning', we get a single file containing all of the ordered notes.

Code:
# <body[^\n]*\n\K\s*(<h[^>]*>[^<]*</h\d>)?\s*<dl[^>]*>\s*<dt[^>]*>\[<a\b(?:(?!</dl).)+</dl>\s*(?=</body>)

from calibre.gui2.tweak_book import current_container
from calibre.gui2.tweak_book.boss import get_boss
from calibre.ebooks.oeb.polish.split import merge

def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):

    if match is None:	# this is the last passage (all matches found)
        ctnr = current_container()
        # Merge at least 2 files whose name is in the list 'note_files_list'
        # (stored in persistent dict 'data', a parameter of replace()) 
        # into the file whose name is in 'merge_master' (also stored in data)
        if data and len(data['note_files_list']) > 1:
           merge(ctnr, 'text', data['note_files_list'], data['merge_master'])
           get_boss().apply_container_update_to_gui()

    else:

        if 'merge_master' not in data :  
            # data is empty, therefore it's the 1st iteration
            # the list of files is initialized with the current note file
            # The master of merge is the current note file
            data['note_files_list'] = [file_name]
            data['merge_master'] = file_name

            if match.group(1):
                # If group 1 exists, the note contains the title and therefore the note is the 1st note
                data['first'] = True
                data['first_notes'] = match.group()
                data['last_notes'] = ''
            else:
                data['first'] = False
                data['first_notes'] = ''
                data['last_notes'] = match.group()

            # Ask for a passage after the last find (match will be None)
            # Ask for processing the files in the order they appear in the book
            replace.call_after_last_match = True
            replace.file_order = 'spine'
           
        else:
            # Increments the list of files by adding the name of the current file
            # The master of merge becomes the current note file
            data['note_files_list'].append(file_name)
            data['merge_master'] = file_name
            if match.group(1):
                data['first'] = True
            if data['first']:
                # If first is true, the function has already processed the 1st note,
                # we concatenate in first_notes
                data['first_notes'] = data['first_notes'] + match.group()
            else:
                # Otherwise in last_notes
                data['last_notes'] = data['last_notes'] + match.group()

        data['all_notes'] = data['first_notes'] + data['last_notes']
        # print (['note_files_list'], data['merge_master'])
        return data['all_notes']

Last edited by EbookMakers; 11-23-2020 at 09:24 PM.
EbookMakers is offline   Reply With Quote