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
def main():
    try:
        import argparse
    except ImportError:
        print("The 'argparse' distribution is needed "
              "to run from the commandline")
        print("Recommendation: Use the 'cmdline_support'' requirement extra.")
        return 2

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('document', nargs=1,
                        type=argparse.FileType('r'),
                        default=sys.stdin,
                        help="the filesystem location of the html document")
    args = parser.parse_args()

    result = html_to_valid_cnxml(args.document[0])

    print(result)

    return 0
예제 #3
0
def main():
    try:
        import argparse
    except ImportError:
        print("The 'argparse' distribution is needed "
              "to run from the commandline")
        print("Recommendation: Use the 'cmdline_support'' requirement extra.")
        return 2

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('document',
                        nargs=1,
                        type=argparse.FileType('r'),
                        default=sys.stdin,
                        help="the filesystem location of the html document")
    args = parser.parse_args()

    result = html_to_valid_cnxml(args.document[0])

    print(result)

    return 0