Example #1
0
def web_edit(doc=None, actions=[], title='', ajax_handlers={}):
    """
    Launches webbrowser editor
    Params :
        - doc: MarkdownDocument instance to edit
        - actions: list of ('action_name', action_handker) to be displayed as buttons in web interface

            action_handler is a function that receives MarkdownDocument as uniquqe parameter and must return a tuple, example : 

            def action(markdown_document):
                html_result = '<h1>Done</h1>'
                kill_editor = True
                return html_result, kill_editor

        - title: html code to insert above the editor
        - ajax_handlers: map of 'ajax_req_path':ajax_handler_func to handle your own ajax requests
    """

    default_actions = [('Preview', action_preview), ('Close', action_close)]

    if not doc:
        doc = MarkdownDocument()

    if doc.input_file or doc.output_file:
        default_actions.insert(0, ('Save', action_save))

    PORT = 8000
    httpd = HTTPServer(("", PORT), EditorRequestHandler)

    print('Opening a browser page on : http://localhost:' + str(PORT))
    webbrowser.open('http://localhost:' + str(PORT))

    httpd._running = True
    httpd._document = doc
    httpd._in_actions = default_actions
    httpd._out_actions = actions
    httpd._html_head = title or doc.input_file and '&nbsp;<span class="glyphicon glyphicon-file"></span>&nbsp;<span>%s</span>' % os.path.basename(
        doc.input_file) or ''
    httpd._ajax_handlers = ajax_handlers
    while httpd._running:
        httpd.handle_request()
def web_edit(doc = None, actions=[], title='', ajax_handlers={}):
    """
    Launches webbrowser editor
    Params :
        - doc: MarkdownDocument instance to edit
        - actions: list of ('action_name', action_handker) to be displayed as buttons in web interface

            action_handler is a function that receives MarkdownDocument as uniquqe parameter and must return a tuple, example : 

            def action(markdown_document):
                html_result = '<h1>Done</h1>'
                kill_editor = True
                return html_result, kill_editor

        - title: html code to insert above the editor
        - ajax_handlers: map of 'ajax_req_path':ajax_handler_func to handle your own ajax requests
    """

    default_actions = [('Preview',action_preview), ('Close',action_close)]

    if not doc:
        doc = MarkdownDocument()

    if doc.input_file or doc.output_file:
        default_actions.insert(0, ('Save',action_save))

    PORT = 8000
    httpd = HTTPServer(("", PORT), EditorRequestHandler)
    
    print('Opening a browser page on : http://localhost:'+str(PORT))
    webbrowser.open('http://localhost:' + str(PORT))

    httpd._running = True
    httpd._document = doc
    httpd._in_actions = default_actions
    httpd._out_actions = actions
    httpd._html_head = title or doc.input_file and '&nbsp;<span class="glyphicon glyphicon-file"></span>&nbsp;<span>%s</span>' % os.path.basename(doc.input_file) or ''
    httpd._ajax_handlers = ajax_handlers
    while httpd._running:
        httpd.handle_request()