'Generate individual genre HTML file.
Generate an individual genre HTML file. Called from generate_html_by_genres()
Args:
genre (str): genre name
section_head (bool): True if starting section
books (dict): list of books in genre
outfile (str): full pathname to output file
Results:
(file): Genre HTML file written
Returns:
titles_spanned (list): [(first_author, first_book), (last_author, last_book)]'

def generate_html_by_genre(self, genre, section_head, books, outfile):

    soup = self.generate_html_genre_header(genre)
    body = soup.find('body')
    btc = 0
    divTag = Tag(soup, 'div')
    dtc = 0
    if section_head:
        aTag = Tag(soup, 'a')
        aTag['id'] = 'section_start'
        divTag.insert(dtc, aTag)
        dtc += 1
    aTag = Tag(soup, 'a')
    aTag['id'] = ('Genre_%s' % genre)
    divTag.insert(dtc, aTag)
    body.insert(btc, divTag)
    btc += 1
    titleTag = body.find(attrs={'class': 'title'})
    titleTag.insert(0, NavigableString(('%s' % escape(self.get_friendly_genre_tag(genre)))))
    divTag = body.find(attrs={'class': 'authors'})
    dtc = 0
    current_author = ''
    current_series = None
    for book in books:
        if (book['author'] != current_author):
            current_author = book['author']
            non_series_books = 0
            current_series = None
            pAuthorTag = Tag(soup, 'p')
            pAuthorTag['class'] = 'author_index'
            aTag = Tag(soup, 'a')
            if self.opts.generate_authors:
                aTag['href'] = ('%s.html#%s' % ('ByAlphaAuthor', self.generate_author_anchor(book['author'])))
            aTag.insert(0, book['author'])
            pAuthorTag.insert(0, aTag)
            divTag.insert(dtc, pAuthorTag)
            dtc += 1
        if (book['series'] and (book['series'] != current_series)):
            current_series = book['series']
            pSeriesTag = Tag(soup, 'p')
            pSeriesTag['class'] = 'series'
            if (self.opts.fmt == 'mobi'):
                pSeriesTag['class'] = 'series_mobi'
            if self.opts.generate_series:
                aTag = Tag(soup, 'a')
                aTag['href'] = ('%s.html#%s' % ('BySeries', self.generate_series_anchor(book['series'])))
                aTag.insert(0, book['series'])
                pSeriesTag.insert(0, aTag)
            else:
                pSeriesTag.insert(0, NavigableString(('%s' % book['series'])))
            divTag.insert(dtc, pSeriesTag)
            dtc += 1
        if (current_series and (not book['series'])):
            current_series = None
        pBookTag = Tag(soup, 'p')
        pBookTag['class'] = 'line_item'
        ptc = 0
        pBookTag.insert(ptc, self.format_prefix(book['prefix']))
        ptc += 1
        spanTag = Tag(soup, 'span')
        spanTag['class'] = 'entry'
        stc = 0
        aTag = Tag(soup, 'a')
        if self.opts.generate_descriptions:
            aTag['href'] = ('book_%d.html' % int(float(book['id'])))
        args = self.generate_format_args(book)
        if current_series:
            formatted_title = self.formatter.safe_format(self.by_genres_series_title_template, args, (_('error    in') + '    by_genres_series_title_template:'), self.db.new_api.get_proxy_metadata(book['id']))
        else:
            formatted_title = self.formatter.safe_format(self.by_genres_normal_title_template, args, (_('error    in') + '    by_genres_normal_title_template:'), self.db.new_api.get_proxy_metadata(book['id']))
            non_series_books += 1
        aTag.insert(0, NavigableString(escape(formatted_title)))
        spanTag.insert(stc, aTag)
        stc += 1
        pBookTag.insert(ptc, spanTag)
        ptc += 1
        divTag.insert(dtc, pBookTag)
        dtc += 1
    outfile = open(outfile, 'w')
    outfile.write(soup.prettify())
    outfile.close()
    if (len(books) > 1):
        titles_spanned = [(books[0]['author'], books[0]['title']), (books[(-1)]['author'], books[(-1)]['title'])]
    else:
        titles_spanned = [(books[0]['author'], books[0]['title'])]
    return titles_spanned
