'Function to parse the source code
of the internals page and write the
required functions onto output.txt'

def generate_pages(self, page_name):

    elements = self.elements
    repeated = self.repeated
    description = self.description
    title_chck = self.titles
    if (page_name == 'internals'):
        soup = self.internals_page
    elif (page_name == 'events'):
        soup = self.events_page
    else:
        soup = self.exceptions_page
    dls = soup.find_all('dl')
    for k in dls:
        curr_dd = k.find('dd')
        section = '<section    class="prog__container">{}</section>'
        code = '<pre><code>{}</code></pre>'
        desc = '{}'
        header = '<pre><code>{}</code></pre>'
        code_done = 0
        txt = ''
        p = ''
        first_p = ''
        is_first_p = 1
        for elem in curr_dd:
            if ((elem is None) or (str(type(elem)) == "<class    'bs4.element.NavigableString'>")):
                continue
            if (elem.name == 'dl'):
                break
            elif ((elem.name == 'div') and (('highlight-default' in elem['class']) or ('event-signatures' in elem['class']))):
                strg = ''
                if ('event-signatures' in elem['class']):
                    ptag = elem.find('p')
                    p += self.replace_unicodes(self.get_paragraph_contents(ptag))
                    divtag = elem.find('div', {'class': 'highlight-default'})
                    strg = divtag.text
                else:
                    strg = elem.text
                p = self.replace_unicodes(p)
                strg = self.replace_unicodes(strg)
                txt += (desc.format(p) + code.format(strg))
                p = ''
                is_first_p = 0
            elif ((elem.name == 'div') and (('admonition' in elem['class']) or ('versionadded' in elem['class']) or ('versionchanged' in elem['class']))):
                continue
            elif ((elem.name == 'div') and ('seealso' in elem['class'])):
                break
            elif (elem.name == 'table'):
                p += self.get_table_contents(elem)
                p = self.replace_unicodes(p)
            elif (elem.name == 'ul'):
                p += self.replace_unicodes(self.get_ul_contents(elem))
            elif (elem.name == 'p'):
                p += self.replace_unicodes(self.get_paragraph_contents(elem))
                if is_first_p:
                    first_p += elem.text
                    is_first_p = 0
            elif (elem is not None):
                try:
                    p += elem.text
                except:
                    p += elem
        cur_dt = k.find('dt')
        head = ''
        for d in cur_dt:
            if ((d is None) or (str(type(d)) == "<class    'bs4.element.NavigableString'>")):
                head += d.string
                continue
            if ((d.name == 'a') and (d['class'][0] == 'headerlink')):
                break
            head += d.string
        head = head.replace(u'\u2192', '&#8594;')
        head = head.replace('\n', '')
        header = header.format(head)
        p = self.replace_unicodes(p)
        txt += desc.format(p)
        section = section.format((header + txt))
        cur_code = cur_dt.find('code', {'class': 'descname'})
        title = ''
        write = 0
        class_list = cur_dt['id'].split('.')
        for s in class_list:
            if ((s != 'sqlalchemy') and (s != 'orm')):
                title += (s + '.')
        title = title[0:(len(title) - 1)]
        if (cur_code.text in title_chck):
            to_write = (title + ' DCTB ')
            if (cur_code.text in repeated):
                repeated[cur_code.text].append(title)
            else:
                repeated[cur_code.text] = [title]
            write = 1
        else:
            to_write = (cur_code.text + ' DCTB ')
            title_chck[cur_code.text] = title
        to_write += 'A DCTB '
        to_write += (' DCTB ' * 9)
        to_write += (section + ' DCTB ')
        to_write += ((('http://docs.sqlalchemy.org/en/latest/orm/' + page_name) + '.html#') + cur_dt['id'])
        to_write += '\n'
        if write:
            elements[title] = to_write
            description[title] = self.replace_unicodes(first_p)
            description[title] = self.remove_newline(description[title])
        else:
            elements[cur_code.text] = to_write
            description[cur_code.text] = self.replace_unicodes(first_p)
            description[cur_code.text] = self.remove_newline(description[cur_code.text])
