<commit_before>#!/usr/bin/python

import cgi
import Cookie
import uuid
import os
import ebooker


def get_chapter_html(nr, title, content):
    html = """

        <div class="chapter" id="chapter-%(nr)s">
            <label for="chapter-%(nr)s">Chapter %(nr)s</label><br/>
            <input type="text" name="chapter-%(nr)s-name" id="chapter-%(nr)s-name" value="%(title)s"/><br/>
            <textarea name="chapter-%(nr)s-content" id="chapter-%(nr)s-content">%(content)s</textarea>
        </div>
    """ % {'nr': nr, 'title': title, 'content': content}
    return html


cookie_data = {}

if 'HTTP_COOKIE' in os.environ:
    cookies = os.environ['HTTP_COOKIE']
    cookies = cookies.split('; ')

    for cookie in cookies:
        cookie = cookie.split('=')
        cookie_data[cookie[0]] = cookie[1]

if "session_id" not in cookie_data:
    session_id = str(uuid.uuid4())
    cookie = Cookie.SimpleCookie()
    cookie['session_id'] = session_id
    cookie['session_id']['expires'] = 24 * 60 * 60
    print cookie
else:
    session_id = cookie_data["session_id"]

session = ebooker.load_or_create_session(session_id)

#form = cgi.FieldStorage()
#message = form.getvalue("message", "(no message)")

print """Content-type: text/html"""
print
print """
<!DOCTYPE html>
<html>
<head>
    <title>featherlib ebooker</title>
    <meta charset="utf-8"/>
    <link rel="icon" type="image/png" href="favicon.png"/>
    <link href="styles/default-style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
    <h1>featherlib ebooker</h1>
    <h2>Make epubs in one simple form.</h2>

    <p>
        Here's how:
        <ol>
            <li>Add title, author and optionally additional information about the document</li>
            <li>Paste the text for each chapter into the chapter input fields</li>
            <li>Click <i>Make epub</i>, then wait for the file dialog to save your epub</li>
        </ol>
    </p>

    <form id="docForm" method="post" action="index.py">
        <label for="doc-name">Document name</label><br/>
        <input type="text" name="doc-name" id="doc-name"/><br/>

        <label for="doc-author">Author</label><br/>
        <input type="text" name="doc-author" id="doc-author"/><br/>

        <span onClick="showAdditionalDocFields()">Expand</span>
        <div id="additionalDocFields" style="display: none;">
            <label for="doc-css">CSS</label><br/>
            <input type="text" name="doc-css" id="doc-css"/><br/>
        </div>

"""

if "chapters" in session and len(session["chapters"]) > 0:
    for chapter_name in session["chapters"]:
        chapter = session["chapters"][chapter_name]
        print get_chapter_html(chapter["nr"], chapter["title"], chapter["content"])
else:
    print get_chapter_html(1, "")

print """

        <div id="chapterInsertMarker"></div>
        <span class="addChapter" onClick="addChapter()">Add chapter</span><br/>
<commit_msg>Add update of session with POST data.<commit_after>#!/usr/bin/python

import cgi
import Cookie
import uuid
import os
import ebooker


def get_chapter_html(nr, title, content):
    html = """

        <div class="chapter" id="chapter-%(nr)s">
            <label for="chapter-%(nr)s">Chapter %(nr)s</label><br/>
            <input type="text" name="chapter-%(nr)s-name" id="chapter-%(nr)s-name" value="%(title)s"/><br/>
            <textarea name="chapter-%(nr)s-content" id="chapter-%(nr)s-content">%(content)s</textarea>
        </div>
    """ % {'nr': nr, 'title': title, 'content': content}
    return html


def update_session_from_form(session, form):
    doc_name = form.getvalue("doc-name", "")
    session["config"]["book"]["title"] = doc_name
    author = form.getvalue("doc-author", "")
    session["config"]["author"]["name"] = author

    nr_of_chapters = int(form.getvalue("no-of-chapters", 0))
    session["chapters"] = {}
    for i in range(1, nr_of_chapters + 1):
        chapter_name = "ch" + str(i)
        chapter_title = form.getvalue("chapter-%s-name" % str(i), "")
        chapter_content = form.getvalue("chapter-%s-content" % str(i), "")
        session["chapters"][chapter_name] = {"nr": str(i), "title": chapter_title, "content": chapter_content}

    return session


# Now, do the thing
cookie_data = {}

if 'HTTP_COOKIE' in os.environ:
    cookies = os.environ['HTTP_COOKIE']
    cookies = cookies.split('; ')

    for cookie in cookies:
        cookie = cookie.split('=')
        cookie_data[cookie[0]] = cookie[1]

if "session_id" not in cookie_data:
    session_id = str(uuid.uuid4())
    cookie = Cookie.SimpleCookie()
    cookie['session_id'] = session_id
    cookie['session_id']['expires'] = 24 * 60 * 60
    print cookie
else:
    session_id = cookie_data["session_id"]

session = ebooker.load_or_create_session(session_id)

form = cgi.FieldStorage()
if int(form.getvalue("no-of-chapters", 0)) > 0:
    session = update_session_from_form(session, form)

print """Content-type: text/html"""
print
print """
<!DOCTYPE html>
<html>
<head>
    <title>featherlib ebooker</title>
    <meta charset="utf-8"/>
    <link rel="icon" type="image/png" href="favicon.png"/>
    <link href="styles/default-style.css" rel="stylesheet" type="text/css"/>
</head>

<body>
    <h1>featherlib ebooker</h1>
    <h2>Make epubs in one simple form.</h2>

    <p>
        Here's how:
        <ol>
            <li>Add title, author and optionally additional information about the document</li>
            <li>Paste the text for each chapter into the chapter input fields</li>
            <li>Click <i>Make epub</i>, then wait for the file dialog to save your epub</li>
        </ol>
    </p>

    <form id="docForm" method="post" action="index.py">
        <label for="doc-name">Document name</label><br/>
        <input type="text" name="doc-name" id="doc-name"/><br/>

        <label for="doc-author">Author</label><br/>
        <input type="text" name="doc-author" id="doc-author"/><br/>

        <span onClick="showAdditionalDocFields()">Expand</span>
        <div id="additionalDocFields" style="display: none;">
            <label for="doc-css">CSS</label><br/>
            <input type="text" name="doc-css" id="doc-css"/><br/>
        </div>

"""

if "chapters" in session and len(session["chapters"]) > 0:
    for chapter_name in session["chapters"]:
        chapter = session["chapters"][chapter_name]
        print get_chapter_html(chapter["nr"], chapter["title"], chapter["content"])
else:
    print get_chapter_html(1, "", "")

print """

        <div id="chapterInsertMarker"></div>
        <span class="addChapter" onClick="addChapter()">Add chapter</span><br/>

{'fraction_non_alphanumeric': 0.17895056111616622, 'fraction_numerical': 0.005611161662117076, 'mean_word_length': 3.3878908848968727, 'pattern_counts': {'":': 3, '<': 123, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '840416', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2151, 'n_words': 476}