On Sun, 27 Dec 2015 19:33:24 -0800 (PST)
Chris George <
techn...@gmail.com> wrote:
> Hello All,
>
> Some time ago I asked Terry about being able to copy rich text from a
> browser window and paste rst. I was pointed to a solution that uses
> xclip and pandoc and have been using it since by running it in a
> command window and copy/pasting the output into Leo. This worked as I
> only needed to do this on rare occasions. I recently switched over to
> using markdown and the command line to make this happen works just
> great. I find myself using this capability more and more often though.
>
> xclip -o -selection clipboard -t text/html | pandoc -r html -w
> markdown
> My question is: How do I automate this in Leo? It would be nice to be
> able to have this command run on all pastes (Ctrl-v) that have a
> markdown file as a destination, with plain text as a fallback if the
> clipboard provides something that isn't text/html.
"all pastes (Ctrl-v) that have a markdown file as a destination" would
require a bit more thought. Here's a simple(*) solution you could
deploy as a @button, just stick it in a node called `@button h2m`,
reload or click the script-button button the first time (it will appear
automatically on reloads).
--- cut here ---
from subprocess import Popen, PIPE
from leo.core.leoQt import QtGui
clipboard = QtGui.QApplication.clipboard()
html = unicode(clipboard.text("html"))
if html:
cmd = "pandoc -r html -w markdown".split()
proc = Popen(cmd, stdout=PIPE, stdin=PIPE)
markdown, errors = proc.communicate(html.encode("utf-8"))
i = c.frame.body.wrapper.getInsertPoint()
c.frame.body.wrapper.insert(i, markdown)
else:
g.es("No HTML to convert")
--- cut here ---
For my setup I get a lot of extra chatter in the markdown, like
<span class="Apple-converted-space">Â </span>
but maybe there are pandoc settings to stop that.
(*) ok, this seemed simple, but wasn't.
xclip -o -selection clipboard -t text/html
returns nothing when executed in the shell when there's no html on the
clipboard, but when execute from Popen without html, it never returns.
Hence using Qt to access the clipboard, with related encoding issues.
Cheers -Terry