alphapapa / org-web-tools

View, capture, and archive Web pages in Org-mode
GNU General Public License v3.0
647 stars 33 forks source link

`org-element-at-point` cannot be used in non-Org buffer #56

Closed mooseyboots closed 1 year ago

mooseyboots commented 1 year ago

i recently updated my packages, now i receive an error when i try to use org-web-tools-read-url-as-org.

the backtrace i see looks like this (w url and html args substituted):

error("`org-element-at-point' cannot be used in non-Org buffer %S (%s)" #<buffer  *temp*> fundamental-mode)
org-element-at-point(nil cached)
org-before-first-heading-p()
org-back-to-heading-or-point-min(t)
org-get-property-block()
org-at-property-p()
org-web-tools--remove-custom_id_properties()
org-web-tools--clean-pandoc-output()
org-web-tools--html-to-org-with-pandoc(my-urls-html)
org-web-tools--url-as-readable-org(my-url)

i wonder if when updating, org was updated, and the behaviour of org-element-at-point has been changed?

alphapapa commented 1 year ago

Yes, Org 9.6.something changed a lot of things related to org-element. This is probably a limitation we'll have to deal with now. But it probably won't be hard to fix.

I can't promise to get to this soon, so patches welcome.

Thanks for reporting.

yantar92 commented 1 year ago

Adam Porter @.***> writes:

Yes, Org 9.6.something changed a lot of things related to org-element. This is probably a limitation we'll have to deal with now. But it probably won't be hard to fix.

This particular error is not about changes in org-element. It is about running Org parser in non-Org buffers, which can either lead to parser failing completely or returning garbage. Because of numerous abuses that caused "mysterious" errors, I made parser explicitly throw an error in non-Org buffers.

-- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at https://orgmode.org/. Support Org development at https://liberapay.com/org-mode, or support my work at https://liberapay.com/yantar92

mooseyboots commented 1 year ago

i don't know the library's code, but i just tried adding (org-mode) to org-web-tools--html-to-org-with-pandoc (just before calling the offending functions) and it un-broke it for me:

(defun org-web-tools--html-to-org-with-pandoc (html &optional selector)
  "Return string of HTML converted to Org with Pandoc.
When SELECTOR is non-nil, the HTML is filtered using
`esxml-query' SELECTOR and re-rendered to HTML with
`org-web-tools--dom-to-html', which see."
  (when selector
    (setq html (->> (with-temp-buffer
                      (insert html)
                      (libxml-parse-html-region 1 (point-max)))
                    (esxml-query selector)
                    ;; MAYBE: Should probably use `shr-dom-print' instead.
                    (org-web-tools--dom-to-html))))
  (with-temp-buffer
    (insert html)
    (unless (zerop (call-process-region (point-min) (point-max) "pandoc"
                                        t t nil
                                        (org-web-tools--pandoc-no-wrap-option)
                                        "-f" "html-raw_html-native_divs" "-t" "org"))
      ;; TODO: Add error output, see org-protocol-capture-html
      (error "Pandoc failed"))
    (org-mode)
    (org-web-tools--clean-pandoc-output)
    (buffer-string)))

i'm just not sure if that's all there is to it?