def convert_book_to_html (book_subdir, lang, book_name, book_text):
    lang=str(lang)
    dirname = os.path.dirname
    PROJECT_ROOT = dirname(dirname(os.path.realpath(__file__)))
    TEXT_DIR = os.path.join(PROJECT_ROOT, '..', 'texts')
    output_dir = os.path.join(TEXT_DIR, book_subdir, lang.upper())
    if (os.path.exists(output_dir)==False):
              os.makedirs(output_dir)
     
    chapters=re.split("\[Chapter\]", book_text, flags=re.I) 
#    chapters=book_text.split("[Chapter]")
    chap_num=1
    for chapter in (chapters):
        if re.search("\w", chapter, re.UNICODE):     #  no blank chapters
            convert_chapter_to_html (output_dir, lang, book_name,
                                 chapter, chap_num)
            chap_num+=1
    # add to db
    dj_book=""
    if (BookInfo.objects.filter(
            title="%s" % book_name,
             chaps="%d" % chap_num).exists()
        == False):
        dj_book = BookInfo(title="%s" % book_name,
                           chaps="%d" % chap_num)
        dj_book.save()
    if (dj_book == ""):
        dj_book= BookInfo.objects.get(title="%s" % book_name)
    tran_lang = Languages.objects.get(abbr="%s" % lang.lower())
    if (BookTranslation.objects.filter(
            book_id=dj_book, language_id=tran_lang).exists()
        == False):
        dj_tran = BookTranslation (
            book_id=dj_book, language_id=tran_lang)
        dj_tran.save()
def main():
    global book_translations
    global BookInfo
    global BookTranslation
    for tran in book_translations:
        book = BookInfo.objects.get(title="%s" % tran[0])
        lang = Languages.objects.get(abbr="%s" % tran[1])
        if (BookTranslation.objects.filter(
                book_id=book, language_id=lang).exists()
            == False):
            dj_tran = BookTranslation (book_id=book, language_id=lang)
            dj_tran.save()