Example #1
0
def polish_one(ebook, opts, report):
    rt = lambda x: report('\n### ' + x)
    jacket = None

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        smarten_punctuation(ebook, report)
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        embed_all_fonts(ebook, stats, report)
        report('')

    if opts.subset:
        rt(_('Subsetting embedded fonts'))
        subset_all_fonts(ebook, stats.font_stats, report)
        report('')

    if opts.remove_unused_css:
        rt(_('Removing unused CSS rules'))
        remove_unused_css(ebook, report)
        report('')
Example #2
0
def polish(file_map, opts, log, report):
    rt = lambda x: report('\n### ' + x)
    st = time.time()
    for inbook, outbook in file_map.iteritems():
        report(_('## Polishing: %s')%(inbook.rpartition('.')[-1].upper()))
        ebook = get_container(inbook, log)
        jacket = None

        if opts.subset:
            stats = StatsCollector(ebook)

        if opts.opf:
            rt(_('Updating metadata'))
            update_metadata(ebook, opts.opf)
            jacket = find_existing_jacket(ebook)
            if jacket is not None:
                replace_jacket(ebook, jacket)
                report(_('Updated metadata jacket'))
            report(_('Metadata updated\n'))

        if opts.cover:
            rt(_('Setting cover'))
            set_cover(ebook, opts.cover, report)
            report('')

        if opts.jacket:
            rt(_('Inserting metadata jacket'))
            if jacket is None:
                if add_or_replace_jacket(ebook):
                    report(_('Existing metadata jacket replaced'))
                else:
                    report(_('Metadata jacket inserted'))
            else:
                report(_('Existing metadata jacket replaced'))
            report('')

        if opts.remove_jacket:
            rt(_('Removing metadata jacket'))
            if remove_jacket(ebook):
                report(_('Metadata jacket removed'))
            else:
                report(_('No metadata jacket found'))
            report('')

        if opts.smarten_punctuation:
            rt(_('Smartening punctuation'))
            smarten_punctuation(ebook, report)
            report('')

        if opts.subset:
            rt(_('Subsetting embedded fonts'))
            subset_all_fonts(ebook, stats.font_stats, report)
            report('')

        ebook.commit(outbook)
        report('-'*70)
    report(_('Polishing took: %.1f seconds')%(time.time()-st))
Example #3
0
def polish_one(ebook, opts, report, customization=None):
    rt = lambda x: report('\n### ' + x)
    jacket = None
    changed = False
    customization = customization or CUSTOMIZATION.copy()

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        changed = True
        rt(_('Updating metadata'))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_('Updated metadata jacket'))
        report(_('Metadata updated\n'))

    if opts.cover:
        changed = True
        rt(_('Setting cover'))
        set_cover(ebook, opts.cover, report)
        report('')

    if opts.jacket:
        changed = True
        rt(_('Inserting metadata jacket'))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_('Existing metadata jacket replaced'))
            else:
                report(_('Metadata jacket inserted'))
        else:
            report(_('Existing metadata jacket replaced'))
        report('')

    if opts.remove_jacket:
        rt(_('Removing metadata jacket'))
        if remove_jacket(ebook):
            report(_('Metadata jacket removed'))
            changed = True
        else:
            report(_('No metadata jacket found'))
        report('')

    if opts.smarten_punctuation:
        rt(_('Smartening punctuation'))
        if smarten_punctuation(ebook, report):
            changed = True
        report('')

    if opts.embed:
        rt(_('Embedding referenced fonts'))
        if embed_all_fonts(ebook, stats, report):
            changed = True
        report('')

    if opts.subset:
        rt(_('Subsetting embedded fonts'))
        if subset_all_fonts(ebook, stats.font_stats, report):
            changed = True
        report('')

    if opts.remove_unused_css:
        rt(_('Removing unused CSS rules'))
        if remove_unused_css(
                ebook, report, remove_unused_classes=customization['remove_unused_classes'], merge_rules=customization['merge_identical_selectors']):
            changed = True
        report('')

    if opts.compress_images:
        rt(_('Losslessly compressing images'))
        if compress_images(ebook, report)[0]:
            changed = True
        report('')

    return changed
Example #4
0
def polish_one(ebook, opts, report, customization=None):
    rt = lambda x: report("\n### " + x)
    jacket = None
    changed = False
    customization = customization or CUSTOMIZATION.copy()

    if opts.subset or opts.embed:
        stats = StatsCollector(ebook, do_embed=opts.embed)

    if opts.opf:
        changed = True
        rt(_("Updating metadata"))
        update_metadata(ebook, opts.opf)
        jacket = find_existing_jacket(ebook)
        if jacket is not None:
            replace_jacket(ebook, jacket)
            report(_("Updated metadata jacket"))
        report(_("Metadata updated\n"))

    if opts.cover:
        changed = True
        rt(_("Setting cover"))
        set_cover(ebook, opts.cover, report)
        report("")

    if opts.jacket:
        changed = True
        rt(_("Inserting metadata jacket"))
        if jacket is None:
            if add_or_replace_jacket(ebook):
                report(_("Existing metadata jacket replaced"))
            else:
                report(_("Metadata jacket inserted"))
        else:
            report(_("Existing metadata jacket replaced"))
        report("")

    if opts.remove_jacket:
        rt(_("Removing metadata jacket"))
        if remove_jacket(ebook):
            report(_("Metadata jacket removed"))
            changed = True
        else:
            report(_("No metadata jacket found"))
        report("")

    if opts.smarten_punctuation:
        rt(_("Smartening punctuation"))
        if smarten_punctuation(ebook, report):
            changed = True
        report("")

    if opts.embed:
        rt(_("Embedding referenced fonts"))
        if embed_all_fonts(ebook, stats, report):
            changed = True
        report("")

    if opts.subset:
        rt(_("Subsetting embedded fonts"))
        if subset_all_fonts(ebook, stats.font_stats, report):
            changed = True
        report("")

    if opts.remove_unused_css:
        rt(_("Removing unused CSS rules"))
        if remove_unused_css(ebook, report, remove_unused_classes=customization["remove_unused_classes"]):
            changed = True
        report("")

    return changed