12

I want to be able to highlight a section of a web page and copy it to the clipboard then save it to my local disk as markdown. I need an efficient way to do that.

My current cumbersome method is:

  1. highlight section and copy to clipboard
  2. open Libre Office Writer
  3. paste into Writer
  4. save Writer doc as HTML
  5. open terminal
  6. cd to the directory where I saved the HTML
  7. pandoc -s -r html /home/me/a/b/mydoc.html -o /home/me/a/b/mydoc.md

Obviously, I need a better method! Any suggestions?

Anthon
  • 78,313
  • 42
  • 165
  • 222
MountainX
  • 17,168
  • 59
  • 155
  • 264
  • Can you refer to a page that you might want to copy in this manner? – slm Jun 06 '13 at 02:52
  • It could be any web page I happen to be browsing. Example: http://money.cnn.com/2013/06/05/technology/mobile/android-nexus-experience/?source=cnn_bin – MountainX Jun 06 '13 at 03:32
  • You're selecting the webpage via the source page, right? Or pieces of it anyway. – slm Jun 06 '13 at 10:50
  • As StephaneChazelas mentioned in the comments below, I am just selecting text from Firefox (or other browser) normally. I am NOT going to the source view. – MountainX Jun 06 '13 at 19:46
  • I believe the [getting HTML source or rich text from the X clipboard](http://stackoverflow.com/questions/3261379/getting-html-source-or-rich-text-from-the-x-clipboard) request on [Stackverflow](http://stackoverflow.com/) may provide guidance in obtaining what you're looking for. – tink Jun 06 '13 at 01:12

1 Answers1

15

With a recent version of xclip (the -t option was added in 2010 but not released until version 0.13 in 2016, so in 2013 you'd have had to get it from subversion, or use the one packaged in Debian).

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

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

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

Which you can do in a loop with:

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.

@tink also has a useful link to a similar question on StackOverflow where you can find how to implement it in python.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • The assumption is that you're selecting a web pages' actual source, not just browsing to it, correct? – slm Jun 06 '13 at 10:49
  • @slm, no. In browsers like firefox or chrome, when you select and/or copy some text in a (rendered) web page, the browser sets the selection both as a string (for text applications to use) and as html (for applications that understand it like libreoffice to use). Those are called targets. `xclip -selection clipboard -t TARGETS` will list the targets/formats that Firefox sets after you copy some text from there. – Stéphane Chazelas Jun 06 '13 at 10:53
  • Is this a new feature in `xclip`? I get a -t: No such file or dir. I'm using ver: 0.12. – slm Jun 06 '13 at 10:58
  • It was added in 2010 (see [commit](http://sourceforge.net/p/xclip/code/81/)), but it looks like it was never released. It's available in Debian's `xclip` though. – Stéphane Chazelas Jun 06 '13 at 11:02
  • 1
    @StephaneChazelas Awesome solution! I got xclip from here: http://pkgs.org/debian-sid/debian-main-amd64/xclip_0.12+svn84-2_amd64.deb/download/ and it installed in Kubuntu 12.04 with no issues at all. This is exactly the type of solution I hoped for. Great! – MountainX Jun 06 '13 at 19:44
  • 2
    @slm: My working version of xclip (installed from the link above) also shows version 0.12, but the deb is named xclip_0.12+svn84-2_amd64.deb. This one does include the -t option and that option is described in its man page. But the man page doesn't show the text/html option, and I probably would not have figured any of this out on my own. – MountainX Jun 06 '13 at 19:49
  • @MountainX - agreed. I saw no mention of the text/html anywhere. He's a machine with the answers 8-). – slm Jun 06 '13 at 19:54
  • BTW, I made the following bash alias `alias clipdown='xclip -o -selection clipboard -t text/html | pandoc -r html -o ~/Downloads/clipdown.md` and I also added a command/action based on that to KDE's Klipper so I can very easy get my markdown from any clipboard selection. – MountainX Jun 06 '13 at 20:08
  • I've been using this solution today and it works perfectly in every situation I have tried. It is awesome. (I also made a KDE global shortcut key for it.) Thanks again! – MountainX Jun 07 '13 at 05:39
  • I'm happy to report that xclip_0.12+svn84..... is also in Ubuntu Trusty Tahr 14.04 LTS, and perhaps some earlier versions (but not Precise). Thanks, all. – nealmcb Dec 10 '14 at 23:16