Beispiel #1
0
def clip(save, edit, browser, overwrite):
    """convert html in the clipboard to markdown"""
    html = clipboard.get_clipboard_html()
    if html is None:
        click.echo('No html in the clipboard')
        return

    if save is None:
        content = html2md.html_to_markdown(html).strip()
        click.echo(content)
        return

    if not save.endswith('.md'):
        click.echo('Note must have extension ".md"')
        return

    note = Note(save)
    if os.path.exists(note.path.abs) and not overwrite:
        click.echo('Note already exists at "{}" (specify `--overwrite` to overwrite)'.format(note.path.abs))
        return

    html = parsers.rewrite_external_images(html, note)
    content = html2md.html_to_markdown(html).strip()
    note.write(content)

    if browser:
        click.launch('http://localhost:{0}/{1}'.format(conf.PORT, note.path.rel))

    if edit:
        click.edit(filename=note.path.abs)
Beispiel #2
0
def save(note, data):
    html = data['html']

    if parsers.remove_html(html):
        path_new = os.path.join(data['new[notebook]'],
                                data['new[title]'] + note.ext)

        # If the title or notebook has changed,
        # move the note by updating its path.
        if note.path.abs != path_new:
            try:
                note.move(path_new)
            except NoteConflictError:
                # 409 = Conflict
                return 'Note already exists', 409

        html = parsers.rewrite_external_images(html, note)

        if note.ext == '.md':
            content = html2md.html_to_markdown(html)

        note.write(content)
        note.clean_resources()

        # Update all connected clients.
        #self.refresh_clients()

        return jsonify({'path': note.path.abs})
    return 'Success', 200
Beispiel #3
0
def save(note, data):
    html = data['html']

    if parsers.remove_html(html):
        path_new = os.path.join(data['new[notebook]'], data['new[title]'] + note.ext)

        # If the title or notebook has changed,
        # move the note by updating its path.
        if note.path.abs != path_new:
            try:
                note.move(path_new)
            except NoteConflictError:
                # 409 = Conflict
                return 'Note already exists', 409

        html = parsers.rewrite_external_images(html, note)

        if note.ext == '.md':
            content = html2md.html_to_markdown(html)

        note.write(content)
        note.clean_resources()

        # Update all connected clients.
        #self.refresh_clients()

        return jsonify({
            'path': note.path.abs
        })
    return 'Success', 200
Beispiel #4
0
def port_evernote(path, to_notebook):
    """
    Ports an exported Evernote html note to a nomadic markdown note,
    porting over the resources folder (if there is one)
    and updating references to it.
    """
    path = path.decode('utf-8')
    basepath, filename = os.path.split(path)
    title, ext = os.path.splitext(filename)

    # Look for an Evernote resources directory.
    abspath = os.path.abspath(path)
    dirname = os.path.dirname(abspath)
    en_rsrc_rel = u'{0}.resources'.format(title)
    en_rsrc_abs = os.path.join(dirname, en_rsrc_rel)

    with open(path, 'r') as html_file:
        html = html_file.read()

    n_rsrc_rel = u'assets/{0}'.format(title)
    n_rsrc_abs = os.path.join(to_notebook.path.abs, n_rsrc_rel)

    if os.path.exists(en_rsrc_abs):
        shutil.move(en_rsrc_abs, n_rsrc_abs)

    markdown = html2md.html_to_markdown(html)

    # Update asset references.
    markdown = markdown.replace(en_rsrc_rel, quote(n_rsrc_rel.encode('utf-8')))
    markdown = markdown.replace(quote_evernote(en_rsrc_rel), quote(n_rsrc_rel.encode('utf-8')))

    path = os.path.join(to_notebook.path.abs, title + '.md')
    with open(path, 'w') as note:
        note.write(markdown.encode('utf-8'))

    return path
Beispiel #5
0
 def test_html_to_markdown(self):
     markdown = html2md.html_to_markdown(html)
     expected = u'_** foobar _ lala ** yum **_ hey hey ** uh oh ** yes **_\n'
     print(markdown)
     self.assertEqual(markdown, expected)
Beispiel #6
0
 def test_html_to_markdown(self):
     markdown = html2md.html_to_markdown(html)
     expected = '_** foobar _ lala ** yum **_ hey hey ** uh oh ** yes **_\n'
     print(markdown)
     self.assertEqual(markdown, expected)