Returns a wysiwyg editor (CKEditor) to embed in html pages.

    Fall back to a simple textarea when the library is not installed,
    or when the user's browser is not compatible with the editor, or
    when 'enable' is False, or when javascript is not enabled.

    NOTE that the output also contains a hidden field named
    'editor_type' that contains the kind of editor used, 'textarea' or
    'ckeditor'.

    Based on 'editor_type' you might want to take different actions,
    like replace CRLF with <br/> when editor_type equals to
    'textarea', but not when editor_type equals to 'ckeditor'.

    @param name: *str* the name attribute of the returned editor

    @param id: *str* the id attribute of the returned editor (when
        applicable)

    @param content: *str* the default content of the editor.

    @param textual_content: *str* a content formatted for the case where the
        wysiwyg editor is not available for user. When not
        specified, use value of 'content'

    @param width: *str* width of the editor in an html compatible unit:
        Eg: '400px', '50%'.

    @param height: *str* height of the editor in an html compatible unit:
        Eg: '400px', '50%'.

    @param enabled: *bool* if the wysiwyg editor is return (True) or if a
        simple texteara is returned (False)

    @param file_upload_url: *str* the URL used to upload new files via the
        editor upload panel. You have to implement the
        handler for your own use. The URL handler will get
        form variables 'File' as POST for the uploaded file,
        and 'Type' as GET for the type of file ('file',
        'image', 'flash', 'media')
        When value is not given, the file upload is disabled.

    @param toolbar_set: *str* the name of the toolbar layout to
        use. CKeditor comes by default with 'Basic' and
        'Default'. To define other sets, customize the
        config file in
        /opt/cds-invenio/var/www/ckeditor/invenio-ckconfig.js

    @param custom_configurations_path: *str* value for the CKeditor config
        variable 'CustomConfigurationsPath',
        which allows to specify the path of a
        file that contains a custom configuration
        for the editor. The path is relative to
        /opt/invenio/var/www/

    @return: the HTML markup of the editor

def get_html_text_editor(
        name,
        id=None,
        content='',
        textual_content=None,
        width='300px',
        height='200px',
        enabled=True,
        file_upload_url=None,
        toolbar_set="Basic",
        custom_configurations_path='/js/ckeditor/invenio-ckeditor-config.js',
        ln=None):
    """
    Returns a wysiwyg editor (CKEditor) to embed in html pages.

    Fall back to a simple textarea when the library is not installed,
    or when the user's browser is not compatible with the editor, or
    when 'enable' is False, or when javascript is not enabled.

    NOTE that the output also contains a hidden field named
    'editor_type' that contains the kind of editor used, 'textarea' or
    'ckeditor'.

    Based on 'editor_type' you might want to take different actions,
    like replace CRLF with <br/> when editor_type equals to
    'textarea', but not when editor_type equals to 'ckeditor'.

    @param name: *str* the name attribute of the returned editor

    @param id: *str* the id attribute of the returned editor (when
        applicable)

    @param content: *str* the default content of the editor.

    @param textual_content: *str* a content formatted for the case where the
        wysiwyg editor is not available for user. When not
        specified, use value of 'content'

    @param width: *str* width of the editor in an html compatible unit:
        Eg: '400px', '50%'.

    @param height: *str* height of the editor in an html compatible unit:
        Eg: '400px', '50%'.

    @param enabled: *bool* if the wysiwyg editor is return (True) or if a
        simple texteara is returned (False)

    @param file_upload_url: *str* the URL used to upload new files via the
        editor upload panel. You have to implement the
        handler for your own use. The URL handler will get
        form variables 'File' as POST for the uploaded file,
        and 'Type' as GET for the type of file ('file',
        'image', 'flash', 'media')
        When value is not given, the file upload is disabled.

    @param toolbar_set: *str* the name of the toolbar layout to
        use. CKeditor comes by default with 'Basic' and
        'Default'. To define other sets, customize the
        config file in
        /opt/cds-invenio/var/www/ckeditor/invenio-ckconfig.js

    @param custom_configurations_path: *str* value for the CKeditor config
        variable 'CustomConfigurationsPath',
        which allows to specify the path of a
        file that contains a custom configuration
        for the editor. The path is relative to
        /opt/invenio/var/www/

    @return: the HTML markup of the editor
    """
    ln = default_ln(ln)

    if textual_content is None:
        textual_content = content

    editor = ''

    if enabled and ckeditor_available:
        # Prepare upload path settings
        file_upload_script = ''
        if file_upload_url is not None:
            file_upload_script = ''',
            filebrowserLinkUploadUrl: '%(file_upload_url)s',
            filebrowserImageUploadUrl: '%(file_upload_url)s?type=Image',
            filebrowserFlashUploadUrl: '%(file_upload_url)s?type=Flash'
            ''' % {'file_upload_url': file_upload_url}

        # Prepare code to instantiate an editor
        editor += '''
        <script type="text/javascript" language="javascript">//<![CDATA[
        /* Load the script only once, or else multiple instance of the editor on the same page will not work */
        var INVENIO_CKEDITOR_ALREADY_LOADED
            if (INVENIO_CKEDITOR_ALREADY_LOADED != 1) {
                document.write('<script type="text/javascript" src="%(CFG_SITE_URL)s/vendors/ckeditor/ckeditor.js"><\/script>');
                INVENIO_CKEDITOR_ALREADY_LOADED = 1;
            }
        //]]></script>
        <input type="hidden" name="editor_type" id="%(id)seditortype" value="textarea" />
        <textarea rows="100" cols="80" id="%(id)s" name="%(name)s" style="width:%(width)s;height:%(height)s">%(textual_content)s</textarea>
        <textarea rows="100" cols="80" id="%(id)shtmlvalue" name="%(name)shtmlvalue" style="display:none;width:%(width)s;height:%(height)s">%(html_content)s</textarea>
        <script type="text/javascript">//<![CDATA[
          var CKEDITOR_BASEPATH = '/ckeditor/';

          CKEDITOR.replace( '%(name)s',
                            {customConfig: '%(custom_configurations_path)s',
                            toolbar: '%(toolbar)s',
                            width: '%(width)s',
                            height:'%(height)s',
                            language: '%(ln)s'
                            %(file_upload_script)s
                            });

        CKEDITOR.on('instanceReady',
          function( evt )
          {
            /* If CKeditor was correctly loaded, display the nice HTML representation */
            var oEditor = evt.editor;
            editor_id = oEditor.id
            editor_name = oEditor.name
            var html_editor = document.getElementById(editor_name + 'htmlvalue');
            oEditor.setData(html_editor.value);
            var editor_type_field = document.getElementById(editor_name + 'editortype');
            editor_type_field.value = 'ckeditor';
            var writer = oEditor.dataProcessor.writer;
            writer.indentationChars = ''; /*Do not indent source code with tabs*/
            oEditor.resetDirty();
            /* Workaround: http://dev.ckeditor.com/ticket/3674 */
             evt.editor.on( 'contentDom', function( ev )
             {
             ev.removeListener();
             evt.editor.resetDirty();
             } );
            /* End workaround */
          })

        //]]></script>
        ''' % \
            {'textual_content': cgi.escape(textual_content),
             'html_content': content,
             'width': width,
             'height': height,
             'name': name,
             'id': id or name,
             'custom_configurations_path': custom_configurations_path,
             'toolbar': toolbar_set,
             'file_upload_script': file_upload_script,
             'CFG_SITE_URL': cfg['CFG_SITE_URL'],
             'ln': ln}

    else:
        # CKedior is not installed
        textarea = '<textarea rows="100" cols="80" %(id)s name="%(name)s" style="width:%(width)s;height:%(height)s">%(content)s</textarea>' \
            % {'content': cgi.escape(textual_content),
               'width': width,
               'height': height,
               'name': name,
               'id': id and ('id="%s"' % id) or ''}
        editor += textarea
        editor += '<input type="hidden" name="editor_type" value="textarea" />'

    return editor