View Single Post
Old 08-22-2017, 05:51 AM   #212
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
Re: AddKindleMediaQueries problem

(Please see release notes for a description of the above plugin)

Hi everyone...I'm currently working, together with Doitsu, to fix various problems with the above plugin when div tags are used in various formats with the image tag lines.

The current problem -- and the last problem -- that I'm having is that when I run Granny Grump's Irving Washington epub through this plugin I'm still getting one error with this problem which is a missing end </div> tag for just one image only in dual format. All the other images in the ebook which are formatted in exactly the same way are OK. And when I test other ebooks with and without the div tag formatting I never get this problem. So this is both a consistent problem occurring with one image only in one epub and it's also a quirky problem because I can't reproduce this problem in any other epub ebook on test.

I also cannot understand why this missing div tag problem is occurring.

The plugin itself is quite small -- all it does is add the relevant media queries to the selected stylesheet and then dual format all images in the epub HTML files for KF7(in pixels) and KF8(as % values).

Here is the main driver code for the program:

Spoiler:
Code:
def processAllTasks(bk, wdir, t_ids, t_fnames, s_ids, s_fnames):
    print('\n -- Processing automatic tasks...')
    for fname in t_fnames:
        if 'cover.xhtml' in fname or 'TOC.xhtml' in fname:
            continue
        else:    
            fname = os.path.join(wdir, fname)
            reformatImageLayout(wdir, fname)   # put all the possible forms of div/img formatting into a standard input format
            reformatSmallImages(wdir, fname)   # create two separate img lines to enable dual format for Kindle KF7 and KF7 devices
            
            if options.SYS_EXIT == True:
                return(0)
                
            prettifyXHTMLFile(wdir, fname)     # prettify the html display
            fixBrokenTags(wdir, fname)         # fix any broken tags
        
    # now add the media queries to the selected stylesheet
    for file in s_fnames:
        if file == options.SET_CSS_FNAME:      
            file = os.path.join(wdir, file)
            prettifyCSS1(wdir, file)
            addMediaQueries2CSS(wdir, file)
            prettifyCSS2(wdir, file)          
         
    # write css files back to epub         
    writeFiles2Epub(bk, wdir, t_ids, t_fnames)
    writeFiles2CSS(bk, wdir, s_ids, s_fnames)        
    return(0)


And here is the main function that does all the dual formatting for the html image lines in GG's ebook(please also note that there is no div tag formatting at all in this function). This function appears to be working without any problems:

Spoiler:
Code:
def formatImages(wdir, line):
    
    # needed for later dual format processing
    original_line = line
    
    # find the image name & location
    soup = BeautifulSoup(line, 'html.parser')
    tag = soup.find('img')    
    if tag.has_attr('src'):
        text = tag['src']      
    else:
        return(line)  
    
    #get the img file name from the img path     
    file_name = os.path.basename(text)
    file_path = os.path.join(wdir, file_name)
     
    # get the image dimensions
    width, height = getImageSize(file_path)                             # uses the PIL 'image' method to get image dimensions from the bitmap
    perc_width = round(width/int(options.BASE_WIDTH_VALUE) * 100)       # calculates width as a percentage of screen width
    perc_height = round(height/1200 * 100)                              # calculates height as a percentage of screen height
    
    # reduce image widths to 100% screen width 
    # for all outsize images
    if perc_width >= 100:
        perc_width = 100
        
    if perc_height >= 100:
        perc_height = 100
        
    # precaution in case image html name contains spaces(not allowed in epubs)    
    file_name = os.path.split(file_name)[1]
    file_name = file_name.replace(' ', '_')
    
    ### Now dual format the image line ###
    
    # insert the % height and width image values for KF8 devices
    soup = BeautifulSoup(line, 'html.parser')
    img = soup.img
    if img.has_attr('class'):
        del img['class']                      # remove all classes from the img tags 
    if not img.has_attr('style'):             
        if img.has_attr('width'):
            del img['width']
        if img.has_attr('height'):
            del img['height']            
        img['style'] = 'width: ' + str(perc_width) + '%;height: auto;'     # add the calculated % values into the inline style
    else:
        img['style'] = 'width: ' + str(perc_width) + '%;height: auto;'     # delete then re-add the calculated % values into the inline style   
    line = str(soup)
    
    # include css @media classes for kf8 devices only
    liner = BeautifulSoup(line, 'html.parser')
    img1 = soup.img
    if img1.has_attr('class'):
        del img1['class']            # remove any classes
        img1['class'] = 'kf8only'    # add the appropriate @media class
    else:    
        img1['class'] = 'kf8only'    # add the appropriate @media class if there are no classes present
        
    if img1.has_attr('id'):          # remove ids from the image tag -- not required(avoids id duplication errors on Epubcheck)
        del img1['id']    
        
    l = str(soup)
    line2 = l + '\n'
    
    # format pixel image values for mobi(KF7) version only using 
    # the img tag html attributes for height and width in pixels
    soup = BeautifulSoup(original_line, 'html.parser')
    img2 = soup.img
    
    # remove all inline styling from the img tag
    if img2.has_attr('style'):
        del img2['style']
    
    # remove all classes and then add the appropriate @media classes for mobi only
    if img2.has_attr('class'):
        del img2['class']
        img2['class'] = 'mobionly'
    else:
        img2['class'] = 'mobionly'
    
    # remove and then add the appropriate 'px' values for h nd w
    # in pixels for mobi version only 
    if img2.has_attr('width'):
        del img2['width'] 
        img2['width'] = str(width) + 'px'
    else: 
        img2['width'] = str(width) + 'px'   
    
    if img2.has_attr('height'):
        del img2['height']
        img2['height'] = str(height) + 'px'
    else:
        img2['height'] = str(height) + 'px'    

    if img2.has_attr('id'):
        del img2['id']            
    
    # now join these two line as one(they already contain newline end chars)
    line1 = str(soup)
    line = line1 + line2
    
    return(line)


Also, both the reformatDivLayout() and fixBrokenTags() functions were added after we got the missing div tag problem. Similarly the prettifyXHTMLFiles() function also has nothing to do with this problem because I've already used this function in all my other plugins without any problems whatsoever. So these functions have nothing to do with the missing div tag problem.

Below you can also download the new version of this plugin(v0.1.5 -- which is only for test purposes only and not released yet) as well as Doitsu's test epub so that you can see the missing div tag problem on Epubcheck for yourself. The test epub by Granny Grump is called Irving,Washington-LegendOfSleepyHollow-illus-Rackham.epub.
Attached Files
File Type: epub Irving,Washington-LegendOfSleepyHollow-illus-Rackham.epub (4.49 MB, 858 views)
File Type: zip AddKindleMediaQueries_v015.zip (40.2 KB, 653 views)

Last edited by slowsmile; 08-22-2017 at 06:36 AM.
slowsmile is offline   Reply With Quote