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 | sed 's/ / /g'"))
(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:
osascript
gets the HTML text from the clipboard. It is hex encoded, so
- perl converts the hex to a string
- 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
- Convert the HTML to json, and then
- Convert the json to Org (these two steps simplify the HTML).
- Replace non-standard spaces with standard ones.
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"
<b>text</b>
then after ICmd + C
on it, it can be converted to*text*
inorg 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.<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 inorgmode
.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.