Exemplo n.º 1
0
def cnxml_view(request):
    check_login(request)
    form = Form(request, schema=CnxmlSchema)
    save_dir = os.path.join(request.registry.settings['transform_dir'], request.session['upload_dir'])
    cnxml_filename = os.path.join(save_dir, 'index.cnxml')
    transformerror = request.session.get('transformerror')

    # Check for successful form completion
    if 'cnxml' in request.POST and form.validate():
        cnxml = form.data['cnxml']

        # Keep sure we use the standard python ascii string and encode Unicode to xml character mappings
        if isinstance(cnxml, unicode):
            cnxml = cnxml.encode('ascii', 'xmlcharrefreplace')        

        # get the list of files from upload.zip if it exists
        files = []
        zip_filename = os.path.join(save_dir, 'upload.zip')
        if os.path.exists(zip_filename):
            zip_archive = zipfile.ZipFile(zip_filename, 'r')
            for filename in zip_archive.namelist():
                if filename == 'index.cnxml':
                    continue
                fp = zip_archive.open(filename, 'r')
                files.append((filename, fp.read()))
                fp.close()

        try:
            files = get_files_from_zipfile(os.path.join(save_dir, 'upload.zip'))
            save_cnxml(save_dir, cnxml, files)
            validate_cnxml(cnxml)
        except ConversionError as e:
            return render_conversionerror(request, e.msg)

        # Return to preview
        return HTTPFound(location=request.route_url('preview'), request=request)

    # Read CNXML
    try:
        with open(cnxml_filename, 'rt') as fp:
            cnxml = fp.read()
    except IOError:
        raise HTTPNotFound('index.cnxml not found')

    # Clean CNXML
    cnxml = clean_cnxml(cnxml)
    cnxml = cnxml.decode('utf-8')
    cnxml = unicode(cnxml)

    return {
        'codemirror': True,
        'form': FormRenderer(form),
        'cnxml': cnxml,
        'transformerror': transformerror,
    }
Exemplo n.º 2
0
def preview_save(request):
    check_login(request)
    html = request.POST['html']
    if isinstance(html, unicode):
        html = html.encode('ascii', 'xmlcharrefreplace')

    save_dir = os.path.join(request.registry.settings['transform_dir'],
                            request.session['upload_dir'])
    # Save new html file from preview area
    save_and_backup_file(save_dir, 'index.html', html)

    conversionerror = ''

    # Get the title from aloha html. We have to do this using a separate
    # parse operation, because aloha_to_etree below does not give us a
    # tree on which path() works. A bug or this developer is just stumped.
    tree = etree.fromstring(html, etree.HTMLParser())
    try:
        edited_title = tree.xpath('/html/head/title/text()')[0]
        request.session['title'] = edited_title
    except IndexError:
        if not request.session.get('title', None):
            request.session['title'] = 'Untitled Document'

    #transform preview html to cnxml
    cnxml = None
    try:
        tree = aloha_to_etree(html)  #1 create structured HTML5 tree
        canonical_html = etree.tostring(tree, pretty_print=True)
        cnxml = etree_to_valid_cnxml(tree, pretty_print=True)
    except Exception as e:
        #return render_conversionerror(request, str(e))
        conversionerror = str(e)

    if cnxml is not None:
        save_and_backup_file(save_dir, 'index.cnxml', cnxml)
        save_and_backup_file(save_dir, 'index.structured.html', canonical_html)
        files = get_files_from_zipfile(os.path.join(save_dir, 'upload.zip'))
        save_zip(save_dir, cnxml, canonical_html, files)
        try:
            validate_cnxml(cnxml)
        except ConversionError as e:
            #return render_conversionerror(request, str(e))
            conversionerror = str(e)

    response = Response(json.dumps({'saved': True, 'error': conversionerror}))
    response.content_type = 'application/json'
    return response
Exemplo n.º 3
0
def preview_save(request):
    check_login(request)
    html = request.POST['html']
    if isinstance(html, unicode):
        html = html.encode('ascii', 'xmlcharrefreplace')        

    save_dir = os.path.join(request.registry.settings['transform_dir'],
        request.session['upload_dir'])
    # Save new html file from preview area
    save_and_backup_file(save_dir, 'index.html', html)

    conversionerror = ''

    # Get the title from aloha html. We have to do this using a separate
    # parse operation, because aloha_to_etree below does not give us a
    # tree on which path() works. A bug or this developer is just stumped.
    tree = etree.fromstring(html, etree.HTMLParser())
    try:
        edited_title = tree.xpath('/html/head/title/text()')[0]
        request.session['title'] = edited_title
    except IndexError:
        if not request.session.get('title', None):
            request.session['title'] = 'Untitled Document'

    #transform preview html to cnxml
    cnxml = None
    try:
        tree = aloha_to_etree(html)           #1 create structured HTML5 tree
        canonical_html = etree.tostring(tree, pretty_print=True)
        cnxml = etree_to_valid_cnxml(tree, pretty_print=True)
    except Exception as e:
        #return render_conversionerror(request, str(e))
        conversionerror = str(e)

    if cnxml is not None:
        save_and_backup_file(save_dir, 'index.cnxml', cnxml)
        save_and_backup_file(save_dir, 'index.structured.html', canonical_html)
        files = get_files_from_zipfile(os.path.join(save_dir, 'upload.zip'))
        save_zip(save_dir, cnxml, canonical_html, files)
        try:
            validate_cnxml(cnxml)
        except ConversionError as e:
            #return render_conversionerror(request, str(e))
            conversionerror = str(e)

    response = Response(json.dumps({'saved': True, 'error': conversionerror}))
    response.content_type = 'application/json'
    return response
def preview_save(request):
    check_login(request)
    html = request.POST['html']
    if isinstance(html, unicode):
        html = html.encode('ascii', 'xmlcharrefreplace')        

    save_dir = os.path.join(request.registry.settings['transform_dir'],
        request.session['upload_dir'])
    # Save new html file from preview area
    save_and_backup_file(save_dir, 'index.html', html)

    conversionerror = ''

    #transform preview html to cnxml
    cnxml = None
    try:
        structured_html = aloha_to_html(html)           #1 create structured HTML5
        # parse the new title from structured HTML
        tree = etree.fromstring(structured_html, etree.HTMLParser())
        try:
            edited_title = tree.xpath('/html/head/title/text()')[0]
            request.session['title'] = edited_title
        except:
            request.session['title'] = 'Untitled Document'

        cnxml = html_to_valid_cnxml(structured_html)    #2 create cnxml from structured HTML5
    except Exception as e:
        #return render_conversionerror(request, str(e))
        conversionerror = str(e)

    if cnxml is not None:
        save_and_backup_file(save_dir, 'index.cnxml', cnxml)
        files = get_files_from_zipfile(os.path.join(save_dir, 'upload.zip'))
        save_zip(save_dir, cnxml, html, files)
        try:
            validate_cnxml(cnxml)
        except ConversionError as e:
            #return render_conversionerror(request, str(e))
            conversionerror = str(e)

    response = Response(json.dumps({'saved': True, 'error': conversionerror}))
    response.content_type = 'application/json'
    return response