예제 #1
0
def start(doc, actions=[], default_action=None):
    all_actions = _as_objects(actions, Action) + [
        Action('Edit again', None, 'e'),
        Action('Preview', None, 'p')
    ]

    if doc.input_file or doc.output_file:
        all_actions.append(Action('Save', MarkdownDocument.save, 's'))
    all_actions.append(Action('Quit', action_close, 'q'))

    action_funcs = dict([(a.key, a) for a in all_actions])
    actions_prompt = [a.key + ' : ' + a.name for a in all_actions]

    keep_running = True
    with tempfile.NamedTemporaryFile(mode='r+', suffix=".html") as f:
        temp = codecs.getwriter('utf8')(f) if sys.version_info[0] < 3 else f
        temp.write(doc.get_html_page())
        temp.flush()
        while keep_running:
            command = default_action or raw_input(
                '''Choose command :\n\n{}\n?: '''.format(
                    '\n'.join(actions_prompt)))

            default_action = None
            if command[:1] == 'e':
                temp.seek(0)
                temp.write(sys_edit(doc).get_html_page())
                temp.truncate()
                temp.flush()
            elif command[:1] == 'p':
                webbrowser.open('file:///{}'.format(temp.name))
            elif command[:1] in action_funcs:
                result, keep_running = action_funcs[command](doc)
def start(doc, actions=[], default_action=None):
    all_actions = _as_objects(actions, Action) + [Action('Edit again', None, 'e'), Action('Preview', None, 'p')]

    if doc.input_file or doc.output_file:
        all_actions.append(Action('Save', MarkdownDocument.save, 's'))
    all_actions.append(Action('Quit', action_close, 'q'))

    action_funcs = dict([(a.key, a) for a in all_actions])
    actions_prompt = [a.key + ' : ' + a.name for a in all_actions]

    keep_running = True
    with tempfile.NamedTemporaryFile(mode='r+', suffix=".html") as f:
        temp = codecs.getwriter('utf8')(f) if sys.version_info[0] < 3 else f
        temp.write(doc.get_html_page())
        temp.flush()
        while keep_running:
            command = default_action or raw_input('''Choose command :\n\n{}\n?: '''.format(
                    '\n'.join(actions_prompt)))

            default_action = None
            if command[:1] == 'e':
                temp.seek(0)
                temp.write(sys_edit(doc).get_html_page())
                temp.truncate()
                temp.flush()
            elif command[:1] == 'p':
                webbrowser.open('file:///{}'.format(temp.name))
            elif command[:1] in action_funcs:
                result, keep_running = action_funcs[command](doc)
예제 #3
0
def start(doc, custom_actions=None, title='', ajax_handlers=None, port=8222):

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

    if doc.input_file or doc.output_file:
        default_actions.insert(0, WebAction('Save', ajax_save, action_template=SAVE_ACTION_TEMPLATE))

    if title:
        html_head = title
    elif doc.input_file or doc.output_file:
        html_head = PAGE_HEADER_TEMPLATE.format(os.path.basename(doc.input_file or doc.output_file))
    else:
        html_head = ''

    app.config.load_dict({
        'autojson': False,
        'myapp': {
            'document': doc,
            'in_actions': default_actions,
            'out_actions': _as_objects(custom_actions, WebAction),
            'html_head': html_head,
            'ajax_handlers': ajax_handlers or {}
            }
        }, make_namespaces=True)

    webbrowser.open('http://localhost:{}'.format(port))

    run(app, host='localhost', port=port, debug=False, reloader=False)
def start(doc, custom_actions=None, title='', ajax_handlers=None, port=8222):

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

    if doc.input_file or doc.output_file:
        default_actions.insert(
            0,
            WebAction('Save', ajax_save, action_template=SAVE_ACTION_TEMPLATE))

    if title:
        html_head = title
    elif doc.input_file or doc.output_file:
        html_head = PAGE_HEADER_TEMPLATE.format(
            os.path.basename(doc.input_file or doc.output_file))
    else:
        html_head = ''

    app.config.load_dict(
        {
            'autojson': False,
            'myapp': {
                'document': doc,
                'in_actions': default_actions,
                'out_actions': _as_objects(custom_actions, WebAction),
                'html_head': html_head,
                'ajax_handlers': ajax_handlers or {}
            }
        },
        make_namespaces=True)

    #Find correct port automatically, to allow multiple sessions
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    while 1:
        try:
            s.bind(('0.0.0.0', port))
            s.close()
            break
        except socket.error as e:
            if e.errno == 98:
                print("Port " + str(port) + " is already in use")
                port = port + 1

    webbrowser.open('http://localhost:{}'.format(port))

    run(app, host='localhost', port=port, debug=False, reloader=False)
예제 #5
0
def start(doc, custom_actions=None, title='', ajax_handlers=None, port=8222):

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

    if doc.input_file or doc.output_file:
        default_actions.insert(0, WebAction('Save', ajax_save, action_template=SAVE_ACTION_TEMPLATE))

    if title:
        html_head = title
    elif doc.input_file or doc.output_file:
        html_head = PAGE_HEADER_TEMPLATE.format(os.path.basename(doc.input_file or doc.output_file))
    else:
        html_head = ''

    app.config.load_dict({
        'autojson': False,
        'myapp': {
            'document': doc,
            'in_actions': default_actions,
            'out_actions': _as_objects(custom_actions, WebAction),
            'html_head': html_head,
            'ajax_handlers': ajax_handlers or {}
            }
        }, make_namespaces=True)

    #Find correct port automatically, to allow multiple sessions
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    while 1:
        try:
            s.bind(('127.0.0.1',port))
            s.close()
            break
        except socket.error as e:
            if e.errno==98:
                print("Port "+str(port)+" is already in use")
                port=port+1
        
    


    webbrowser.open('http://localhost:{}'.format(port))

    run(app, host='localhost', port=port, debug=False, reloader=False)
예제 #6
0
def start(doc, custom_actions=None, title='', ajax_handlers=None, port=8222):

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

    if doc.input_file or doc.output_file:
        default_actions.insert(
            0,
            WebAction('Save', ajax_save, action_template=SAVE_ACTION_TEMPLATE))

    if title:
        html_head = title
    elif doc.input_file or doc.output_file:
        html_head = PAGE_HEADER_TEMPLATE.format(
            os.path.basename(doc.input_file or doc.output_file))
    else:
        html_head = ''

    app.config.load_dict(
        {
            'autojson': False,
            'myapp': {
                'document': doc,
                'in_actions': default_actions,
                'out_actions': _as_objects(custom_actions, WebAction),
                'html_head': html_head,
                'ajax_handlers': ajax_handlers or {}
            }
        },
        make_namespaces=True)

    webbrowser.open('http://localhost:{}'.format(port))

    run(app, host='localhost', port=port, debug=False, reloader=False)