Currently, for notetaking tools like Evernote and Quiver, I can directly copy HTML content from my favorite browser and then paste them into the app, with all the formatting + link preserved. However in orgmode it seems that all the formatting info is lost.

I've seen somebody suggest using eww to browse the web and copy the content via eww-org. However that is really tedious(I don't think there would be a lot of people browsing the web using eww instead of modern browsers nowadays. I'll have to open that link again in eww and do the copying, not to mention sometimes eww doesn't render the contents nicely).

Is it possible to let Emacs directly parse the copied HTML when pasting? Even if there's no existing tool for that yet, is it feasible to make one?

This is almost the only thing that stops me from switching to orgmode from other notetaking tools.

  • Please clarify what you mean by "directly parse the copied HTML" – mankoff May 5 '15 at 1:56
  • @mankoff OK I guess I wasn't clear enough in my description. What I want is for example if the original HTML had <b>text</b> then after I Cmd + C on it, it can be converted to *text* in org mode by some means when pasting. Or if no, at least preserve the original HTML code so that I could view them in their original proper format later. The current situation is somehow only plain text will be rendered. – xji May 5 '15 at 11:21
  • For example, we have here <p>I've seen somebody <a href="http://emacs.stackexchange.com/q/7171/115">suggest using <code>eww</code> to browse the web and copy the content via <code>eww-org</code></a>. However that is really tedious(I don't think there would be a lot of people browsing the web using <code>eww</code> instead of modern browsers nowadays. I'll have to open that link again in <code>eww</code> and do the copying, not to mention sometimes <code>eww</code> doesn't render the contents nicely). </p>. If I copy this paragraph, I want to be able to reproduce its formatting in orgmode. – xji May 5 '15 at 11:22
  • 1
    @mankoff Wonderful! It worked! You're the man! I think you could even consider submitting it as an Emacs package etc. haha. This could make org mode so much more user-friendly. Actually I like it more with the formatting without intermediate RTF conversion because it preserves more info. For example #+BEGIN_QUOTE and #+BEGIN_EXAMPLE in your answer would not be preserved with the additional conversion. – xji May 5 '15 at 14:20
  • 1
    @incandescentman That's just attempt to strip some formatting information, e.g. some CSS within the web page. You may try it yourself and see what difference it makes. – xji Apr 4 '17 at 9:13
up vote 10 down vote accepted

is it feasible to make one?

Since this is emacs, yes.

My approach is to use a 3rd party tools that can take HTML and convert to plain text or even directly to Org format. I think this is an ugly hack, and there may be better ways to do this, but it looks like it works for my test cases.

(defun kdm/html2org-clipboard ()
  "Convert clipboard contents from HTML to Org and then paste (yank)."
  (interactive)
  (kill-new (shell-command-to-string "osascript -e 'the clipboard as \"HTML\"' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))' | pandoc -f html -t json | pandoc -f json -t org"))
  (yank))

Unfortunately, HTML is incredibly complex now - no longer some simple hand-written tags. This complex HTML tagging requires the complicated shell command above. It does the following:

  1. osascript gets the HTML text from the clipboard. It is hex encoded, so
  2. perl converts the hex to a string
  3. We could convert that HTML to Org directly with pandoc, but the HTML is full of complicated tags and therefore produces a ton of Org code. In order to simply the HTML to the minimal set of tags needed to capture the formatting, I
  4. Convert the HTML to json, and then
  5. Convert the json to Org (these two steps simplify the HTML).

Note that osascript is for MacOS. To modify steps 1-2 for Linux, replace the argument of shell-command-to-string with

"xclip -o -t text/html | pandoc -f html -t json | pandoc -f json -t org"

In any case, the output of the pandoc command is returned to emacs, and inserted into the buffer.

Bind the new Emacs command to a key similar to "paste" but that means "paste-and-convert-from-html" to you, and it should work.

Alternatively, if you don't want to think about which paste command to use, here is a Linux version that will convert HTML when that is available on the clipboard and will otherwise fall back to plain text:

"xclip -o -t TARGETS | grep -q text/html && (xclip -o -t text/html | pandoc -f html -t json | pandoc -f json -t org) || xclip -o"
  • One addition: It seems that pandoc automatically uses Non-breaking space quite a lot instead of normal space when converting formatted inline text(bold, italics, code etc.), which are not recognized by orgmode by default. You'd have to add it ( ) to org-emphasis-regexp-components in order for those texts to be formatted correctly in orgmode. – xji Jun 18 '15 at 14:57
  • notably the the "released" version of xclip does not support option -t; so xclip must be built from github. Also, you might need to pipe pandoc input and output through iconv utf-8 – malcook Mar 10 '16 at 17:51
  • xclip is on OS X also (perhaps only w/ X11 and/or Developer Tools installed?), so the improved answer could work on OS X too. – mankoff Mar 11 '16 at 9:33
  • @JIXiang How would I modify the accepted answer so that it also converts non-breaking spaces to normal spaces? – incandescentman Apr 3 '17 at 23:36
  • 1
    @incandescentman I originally modified org-mode's package file so that it recognizes non-breaking space as a separator. However it turned out to be tedious with version changes. I then raised an issue on pandoc's repo which you can search about. Essentially you can use a "filter" in pandoc to perform automatic substitution. But that sometimes also fails. So now I just mostly manually select the pasted content and perform a substitution. My last substitution is almost always this one so I just scroll up my substitution history and apply. – xji Apr 4 '17 at 6:56

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.