Reply to topic

Save the HTML content from Klipper (KDE clipboard) to a file

User avatar MountainX
Registered Member
Posts
54
Karma
0
OS
If I copy HTML content to the KDE clipboard, I can paste it into an HTML-aware editor and it retains the HTML formatting. That's expected behavior. And I need a command line method of doing the same (or similar). Or I need a way to create an action/command in Klipper to accomplish the goal.

Here is the first (non-working) solution I tried so far:

Code: Select all
qdbus org.kde.klipper /klipper getClipboardHistoryItem 0 > ~/Downloads/temp/clipboard.html


It saves the HTML contents as plain text.

Next I used "Action Options" (section 3.3 of Klipper handbook).

Code: Select all
echo %s > ~/Downloads/temp/clipboard.html


That also saves the HTML contents as plain text.

I need a command that will let me save the HTML as HTML source. For example, say I hightlight and copy some content from a web page about running ("The Couch to 5k Plan"), I want the file I save to contain that HTML like this:

Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
        <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
        <TITLE></TITLE>
        <META NAME="GENERATOR" CONTENT="LibreOffice 3.5  (Linux)">
        <META NAME="CREATED" CONTENT="20130606;2395500">
        <META NAME="CHANGED" CONTENT="20130606;2401600">
        <STYLE TYPE="text/css">
        <!--
                @page { margin: 0.79in }
                P { margin-bottom: 0.08in }
                A:link { so-language: zxx }
        -->
        </STYLE>
</HEAD>
<BODY LANG="en-US" DIR="LTR">
<P ALIGN=LEFT><STRONG><FONT COLOR="#000066">The Couch to 5k Plan</FONT></STRONG></P>
<P ALIGN=LEFT><A HREF="http://runningforbeginners.com/" TARGET="_blank">Running
for Beginners</A><BR>The first place to visit for anyone looking to
start running. Provides a heap of good advice for those wanting to
run 5k plus links to Couch to 5k resources.</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
</BODY>

That's what I want.

Instead, what I get with either of the two methods above is this:

Code: Select all
The Couch to 5k Plan

Running for Beginners
The first place to visit for anyone looking to start running. Provides a heap of good advice for those wanting to run 5k plus links to Couch to 5k resources.

That's not what I want.
User avatar bcooksley
Administrator
Posts
19765
Karma
87
OS
Unusual. I just copied a basic HTML fragment and it was output without alteration by both these commands:
Code: Select all
qdbus org.kde.klipper /klipper getClipboardContents
qdbus org.kde.klipper /klipper getClipboardContents


Which application are you copying the HTML code from?


KDE Sysadmin
[img]http://forum.kde.org/content/bcooksley_sig.png[/img]
User avatar MountainX
Registered Member
Posts
54
Karma
0
OS
bcooksley wrote:Unusual. I just copied a basic HTML fragment and it was output without alteration by both these commands:
Code: Select all
qdbus org.kde.klipper /klipper getClipboardContents
qdbus org.kde.klipper /klipper getClipboardContents


Which application are you copying the HTML code from?


I don't see any difference in the two commands you listed.

I did not say I was copying HTML code. I am copying HTML content from the browser. I just highlight a section of text (may or may not include images, links, etc.). Then I use CTRL-C to copy it to the clipboard.
User avatar MountainX
Registered Member
Posts
54
Karma
0
OS
Here's the best solution I have so far. But it doesn't use Klipper. I'm still looking for a Klipper solution.

http://unix.stackexchange.com/a/78416/15010

Answer

With a recent version of [`xclip`](http://xclip.sourceforge.net) (the `-t` option [was added in 2010](http://sourceforge.net/p/xclip/code/81/) but not released yet AFAICT, so you'd need to get it from subversion, or use the one packaged in Debian).

Code: Select all
xclip -o -selection clipboard -t text/html | pandoc -r html -w markdown


And if you want to make that back into the clipboard:

Code: Select all
xclip -o -selection clipboard -t text/html | pandoc -r html -w markdown | xclip -i -selection clipboard


Which you can do in a loop with:
Code: Select all
    while :; do
      xclip -o -selection clipboard -t text/html | pandoc -r html -w markdown | xclip -i -selection clipboard -quiet
    done


The second `xclip`, with `-quiet` will block until something else claims the CLIPBOARD selection, that is until you select something else somewhere.

That way, you can copy back and forth between your browser and whatever you're pasting the markdown in.

Answered by [Stephane Chazelas](http://unix.stackexchange.com/users/225 ... e-chazelas)
User avatar bcooksley
Administrator
Posts
19765
Karma
87
OS
Oops. What I had meant to post was:
Code: Select all
qdbus org.kde.klipper /klipper getClipboardContents
qdbus org.kde.klipper /klipper getClipboardHistoryItem 0


I can confirm the issue you mentioned. Unfortunately it can't be easily solved unless Klipper adds support for explicitly requesting a certain mimetype - which is what the xclip command above does. Klipper likely defaults to text/plain, rather than text/html which is what you need in this case.


KDE Sysadmin
[img]http://forum.kde.org/content/bcooksley_sig.png[/img]

 
Reply to topic

Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], kde-zagzigger, wcstarks, Yahoo [Bot]