Copy rich text, paste markdown

559 views
Skip to first unread message

Chris George

unread,
Dec 27, 2015, 10:33:24 PM12/27/15
to leo-editor
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.

The usual disclaimer about me not being a programmer applies. I use Leo to write, not code.

Chris

Terry Brown

unread,
Dec 28, 2015, 11:01:37 AM12/28/15
to leo-e...@googlegroups.com
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

Chris George

unread,
Dec 28, 2015, 11:31:31 AM12/28/15
to leo-editor
Hi Terry,

When I copy from a web page, I get some strange characters. I have attached an image. This sentence, copied from a webpage in Chrome, renders the following in Leo when I run the script.

In fact, it’s not just okay–it’s inevitable. 

Here is what I get when I copy/paste from Leo to the google groups editor.


In fact, it’s not just okay–it’s inevitable.


What are the encoding issues with Qt and how do I fix it?


Chris

Chris George

unread,
Dec 28, 2015, 2:16:37 PM12/28/15
to leo-editor
Hi Terry,

Apparently the Debian version of the xclip program (.deb file from their repositories) is different from the Ubuntu version (gleaned from a Stackexchange conversation). When I run the string on an image copied in GIMP, it returns an error message, "Error: target text/html not available".

The way to tell if you have the right version or not is to type "man xclip" and see if -t is an option. If it is not, you need to remove xclip and hunt down the Debian version.

Chris
.

On Monday, December 28, 2015 at 8:01:37 AM UTC-8, Terry Brown wrote:

Chris George

unread,
Dec 29, 2015, 5:29:16 PM12/29/15
to leo-editor
I resolved this by coming at it from the other end.

I created a shell script.

pastedown.sh

#!/bin/bash

xclip
-o -selection clipboard -t text/html|pandoc -r html -w markdown|xclip -i -selection clipboard


Then I used the Custom Shortcuts feature in KDE to assign Ctrl-Shift-v to launch it. The script reads the clipboard, converts it to markdown and puts it back in the clipboard and then allows me to paste it into Leo using Ctrl-v as markdown. No encoding errors.

Edward K. Ream

unread,
Dec 31, 2015, 9:51:03 AM12/31/15
to leo-editor


On Tuesday, December 29, 2015 at 5:29:16 PM UTC-5, Chris George wrote:
I resolved this by coming at it from the other end.

I created a shell script.

pastedown.sh

#!/bin/bash

xclip
-o -selection clipboard -t text/html|pandoc -r html -w markdown|xclip -i -selection clipboard


Then I used the Custom Shortcuts feature in KDE to assign Ctrl-Shift-v to launch it. The script reads the clipboard, converts it to markdown and puts it back in the clipboard and then allows me to paste it into Leo using Ctrl-v as markdown. No encoding errors.

Does this thread have any relevance to bug # 201?

Edward

Chris George

unread,
Dec 31, 2015, 11:15:01 AM12/31/15
to leo-editor
Hi Edward,

I don't think so. I use Linux Mint 17.2 KDE.

Leo 5.1-final, build 20151207062203, Mon Dec  7 06:22:03 CST 2015
Git repo info: branch = master, commit = 0d2b4de265f0
Python 2.7.6, PyQt version 4.8.6
linux2

Terry identified it as an encoding issue specific to Qt, but in reading about how to solve this it turns out that KDE reads the clipboard using Qt as well, so I am not sure if that is where the problem lies.

My original "idea" was to enable Leo to read text/html from the clipboard and then convert and paste the string into Leo depending on the @language of the node. For example I could copy part of a web page (which all browsers on linux add to the clipboard as text/html) and paste it as markdown into a markdown node or rst in a rst node or as plain text into node with no @language setting. My solution doesn't address this added functionality in Leo, but it does solve my specific problem, on this specific OS/DE.

Chris 

Terry Brown

unread,
Dec 31, 2015, 1:19:45 PM12/31/15
to leo-e...@googlegroups.com
On Thu, 31 Dec 2015 08:15:01 -0800 (PST)
Chris George <techn...@gmail.com> wrote:

> Hi Edward,
>
> I don't think so. I use Linux Mint 17.2 KDE.
>
> Leo 5.1-final, build 20151207062203, Mon Dec 7 06:22:03 CST 2015
> Git repo info: branch = master, commit = 0d2b4de265f0
> Python 2.7.6, PyQt version 4.8.6
> linux2
>
> Terry identified it as an encoding issue specific to Qt, but in
> reading about how to solve this it turns out that KDE reads the
> clipboard using Qt as well, so I am not sure if that is where the
> problem lies.

I didn't mean to blame Qt specifically, it seemed necessary to use Qt
to read the clipboard because xclip would hang Leo in the Popen call
when HTML was requested and there wasn't any on the clipboard. After
that there was encoding to deal with, but not necessarily anything Qt
was doing wrong.

Cheers -Terry
> > <https://github.com/leo-editor/leo-editor/issues/201>?
> >
> > Edward
> >
>
Reply all
Reply to author
Forward
0 new messages