eww readable 小优化

目的

eaf-browser 提供了一种很方便的网页阅读形式:

  1. eaf-open-browser 打开一个网页
  2. eaf-py-proxy-insert_or_render_by_eww 转换为 eww.

eaf-browser 转换为 eww 时它的阅读效果比直接用eww 打开网页,开启(eww-readable) 效果更好。

猫哥表示使用了 readability 来抽取网页中的可都部分。

虽然 eaf-browser 还是比较香的。但日常还是习惯,复杂的网页使用Chrome, 阅读文章直接使用 eww ,但eww 默认的(eww-readable)有时候效果不佳,因此希望直接在eww 接入 readability, 能够更好的解析出网页中的可读部分。

实现

发现 readability 有一个命令行工具: readability-cli

而 eww (eww-readable) 的代码也比较简短,可以修改一下它,使用 readability-cli 来重新解析.

(defun eww-readable ()
  "View the main \"readable\" parts of the current web page.
This command uses heuristics to find the parts of the web page that
contains the main textual portion, leaving out navigation menus and
the like."
  (interactive nil eww-mode)
  (let* ((old-data eww-data)
         (eww-temp-html (make-temp-file "eww-temp-html"))
         (dom
          (with-temp-buffer
            (with-temp-file eww-temp-html
              (insert (plist-get old-data :source))
              (decode-coding-region (point-min) (point-max) 'utf-8))
            (insert
             (shell-command-to-string
              (format "readable '%s'" eww-temp-html)))

            (condition-case nil
                (decode-coding-region (point-min) (point-max) 'utf-8)
              (coding-system-error nil))
            (eww--preprocess-html (point-min) (point-max))
            (libxml-parse-html-region (point-min) (point-max))))
         (base (plist-get eww-data :url)))
    (eww-score-readability dom)
    (eww-save-history)
    (eww-display-html nil nil
                      (list 'base
                            (list (cons 'href base))
                            (eww-highest-readability dom))
                      nil
                      (current-buffer))
    (dolist (elem '(:source :url :title :next :previous :up))
      (plist-put eww-data elem (plist-get old-data elem)))
    (eww--after-page-change)))

修改只有两段代码:

(with-temp-file eww-temp-html
  (insert (plist-get old-data :source))
  (decode-coding-region (point-min) (point-max) 'utf-8))


(insert
 (shell-command-to-string
  (format "readable '%s'" eww-temp-html)))

把eww 获取的html 保存到临时文件里,然后eww 需要用到的时候,用 readable 命令处理一下,抽取核心可读内容。

效果

原来网页:左右分栏,核心阅读内容在右边

eww 默认解析效果,eww 没法判断左侧的部分不属于阅读内容,依然渲染出来了。

通过 readability.js 解析后的效果,直接就是主题

最后,折腾一顿,也就是实现了 eaf-browser eaf-py-proxy-insert_or_render_by_eww 的效果,推荐可以直接尝试使用 eaf-browser

4 个赞
(setq eww-retrieve-command '("readable"))

看了下它可以支持直接配置用什么命令来下载网页,这样是不是一样的效果?

3 个赞

盲生,你发现了华点,效果极佳。

这句要放在哪里请问

init.el, 先安装readability

有个小问题,有些文中的图片消失了,有什么args加到readable里能解决吗?

而且文字抽取有些激进,比如 sacha 的blog,只识别出一小段

如果一篇文章分章节的应该怎么办,eww处理后的链接会报错

另问问大家一般是怎么重排的 目前新手做法: 对eww后的文章,先去只读,再设宽度,设visual line mode,之后全选后alt q 是不是效率很低

olivetti

我的 EAF broser 没有 eaf-py-proxy-insert_or_render_by_eww 函数?搜索代码仓库也没看到函数实现啊?如上图。

要切换成 read mode 发现 EAF 有 eaf-py-proxy-toggle_read_mode,运行它,报错:

Traceback (most recent call last):
  File "/Users/c/emacs-config/default/elpa/28.2/develop/eaf-20221129.165543/eaf.py", line 394, in eval_function
    getattr(buffer, function_name)()
AttributeError: 'AppBuffer' object has no attribute 'toggle_read_mode'
Traceback (most recent call last):
  File "/Users/c/emacs-config/default/elpa/28.2/develop/eaf-20221129.165543/eaf.py", line 394, in eval_function
    getattr(buffer, function_name)()
AttributeError: 'AppBuffer' object has no attribute 'toggle_read_mode'
Traceback (most recent call last):
  File "/Users/c/emacs-config/default/elpa/28.2/develop/eaf-20221129.165543/eaf.py", line 394, in eval_function
    getattr(buffer, function_name)()
AttributeError: 'AppBuffer' object has no attribute 'toggle_read_mode'

在仓库中搜索 toggle_read_mode

所以这两个函数:

  1. eaf-py-proxy-insert_or_render_by_eww EAF 没有,为什么这个帖子在讨论?
  2. eaf-py-proxy-toggle_read_mode EAF 没有,为什么 README 中快捷键中

, insert_or_switch_to_reader_mode

分配逗号给它?

eaf-py-proxy-insert_or_ 是 Elisp 自动生成的, 用来区别 Elisp、 Python、 JavaScript 三种函数时自动加上的Elisp前缀, 所以搜索的时候要搜索 render_by_eww

今天尝试用eaf-open-browsr 打开此网页

https://www.cyberciti.biz/faq/install-and-configure-tigervnc-server-on-ubuntu-18-04/

提示 buffer id 一串数 not exist, rebuild eaf buffer

message 里说是找不到一些jpeg图片

粘贴一下日志吧

不是很确定日志指哪个,另外没有复现出来找不到 jpeg 的反馈,现在直接只是

[EAF/browser] Opening https://www.cyberciti.biz/faq/install-and-configure-tigervnc-server-on-ubuntu-18-04/
Quit

以下是 eaf 的信息(应该是日志?)


Traceback (most recent call last):
  File "/Users/tom/.emacs.d/site-lisp/emacs-application-framework/core/utils.py", line 51, in on_signal_received
    self._func(obj, *args, **kwargs)
  File "/Users/tom/.emacs.d/site-lisp/emacs-application-framework/eaf.py", line 153, in new_buffer
    self.create_buffer(buffer_id, url, module_path, arguments)
  File "/Users/tom/.emacs.d/site-lisp/emacs-application-framework/eaf.py", line 171, in create_buffer
    app_buffer = module.AppBuffer(buffer_id, url, arguments)
  File "/Users/tom/.emacs.d/site-lisp/emacs-application-framework/app/browser/buffer.py", line 86, in __init__
    self.buffer_widget.init_dark_mode_js(__file__,
  File "/Users/tom/.emacs.d/site-lisp/emacs-application-framework/core/webengine.py", line 724, in init_dark_mode_js
    self.dark_mode_js = open(os.path.join(os.path.dirname(module_path), "node_modules", "darkreader", "darkreader.js")).read()
FileNotFoundError: [Errno 2] No such file or directory: '/Users/tom/.emacs.d/site-lisp/emacs-application-framework/app/browser/node_modules/darkreader/darkreader.js'
Error with Feature-Policy header: Unrecognized feature: 'payment'.
Error with Feature-Policy header: Unrecognized feature: 'usb'.

最后这两行重复了很多遍

你都没有安装好,当然打不开了

macos

我按提示git pull后,./install-eaf.py --force 了两回,还是失败,提示手动安装,回去看readme没找到对应

用./install-eaf.py --install browser 提示 Already up to date,但是 ./install-eaf.py --force 还是说没安装上

请问这个手动安装正确做法是?

先发一下 install-eaf.py的安装日志?

我不太确定我理解的日志对不对,于是在Emacs里执行了下贴上来


[EAF] ------------------------------------------
[EAF] Installing core dependencies
[EAF] Running pip3 install --user -U epc sexpdata tld lxml mac-app-frontmost PyQt6==6.3.1 PyQt6-Qt6==6.3.1 PyQt6-sip PyQt6-WebEngine==6.3.1 PyQt6-WebEngine-Qt6==6.3.1 @ /Users/Tom/.emacs.d/site-lisp/emacs-application-framework
Requirement already satisfied: epc in /Users/Tom/Library/Python/3.10/lib/python/site-packages (0.0.5)
Requirement already satisfied: sexpdata in /Users/Tom/Library/Python/3.10/lib/python/site-packages (0.0.3)
Requirement already satisfied: tld in /Users/Tom/Library/Python/3.10/lib/python/site-packages (0.12.6)
Requirement already satisfied: lxml in /Users/Tom/Library/Python/3.10/lib/python/site-packages (4.9.1)
Requirement already satisfied: mac-app-frontmost in /Users/Tom/Library/Python/3.10/lib/python/site-packages (2020.12.3)
Requirement already satisfied: PyQt6==6.3.1 in /Users/Tom/Library/Python/3.10/lib/python/site-packages (6.3.1)
Requirement already satisfied: PyQt6-Qt6==6.3.1 in /Users/Tom/Library/Python/3.10/lib/python/site-packages (6.3.1)
Requirement already satisfied: PyQt6-sip in /Users/Tom/Library/Python/3.10/lib/python/site-packages (13.4.0)
Requirement already satisfied: PyQt6-WebEngine==6.3.1 in /Users/Tom/Library/Python/3.10/lib/python/site-packages (6.3.1)
Requirement already satisfied: PyQt6-WebEngine-Qt6==6.3.1 in /Users/Tom/Library/Python/3.10/lib/python/site-packages (6.3.1)
[EAF] Finished installing core dependencies
[EAF] ------------------------------------------
[EAF] ------------------------------------------
[EAF] Installing application dependencies

[EAF] Updating demo to newest version...
[EAF] Running git branch @ app/demo
[EAF] Running git symbolic-ref HEAD @ app/demo
[EAF] Running git pull origin master @ app/demo
From https://github.com/emacs-eaf/eaf-demo
 * branch            master     -> FETCH_HEAD
Already up to date.

[EAF] Updating terminal to newest version...
[EAF] Running git branch @ app/terminal
[EAF] Running git symbolic-ref HEAD @ app/terminal
[EAF] Running git pull origin master @ app/terminal
From https://github.com/emacs-eaf/eaf-terminal
 * branch            master     -> FETCH_HEAD
Already up to date.

[EAF] Updating browser to newest version...
[EAF] Running git branch @ app/browser
[EAF] Running git symbolic-ref HEAD @ app/browser
[EAF] Running git pull origin master @ app/browser
From https://github.com/emacs-eaf/eaf-browser
 * branch            master     -> FETCH_HEAD
Already up to date.

[EAF] Updating file-browser to newest version...
[EAF] Running git branch @ app/file-browser
[EAF] Running git symbolic-ref HEAD @ app/file-browser
[EAF] Running git pull origin master @ app/file-browser
From https://github.com/emacs-eaf/eaf-file-browser
 * branch            master     -> FETCH_HEAD
Already up to date.

[EAF] Updating file-manager to newest version...
[EAF] Running git branch @ app/file-manager
[EAF] Running git symbolic-ref HEAD @ app/file-manager
[EAF] Running git pull origin master @ app/file-manager
From https://github.com/emacs-eaf/eaf-file-manager
 * branch            master     -> FETCH_HEAD
Already up to date.

[EAF] Updating org-previewer to newest version...
[EAF] Running git branch @ app/org-previewer
[EAF] Running git symbolic-ref HEAD @ app/org-previewer
[EAF] Running git pull origin master @ app/org-previewer
From https://github.com/emacs-eaf/eaf-org-previewer
 * branch            master     -> FETCH_HEAD
Already up to date.

[EAF] Installing dependencies for the selected applications

[EAF] Please always ensure the following config are added to your init.el:
(require 'eaf-demo)
(require 'eaf-terminal)
(require 'eaf-browser)
(require 'eaf-file-browser)
(require 'eaf-file-manager)
(require 'eaf-org-previewer)
[EAF] Installation SUCCESS!
[EAF] ------------------------------------------
[EAF] install-eaf.py finished.

[EAF] Please run 'git pull ; ./install-eaf.py' (M-x eaf-install-and-update) to update EAF, applications and their dependencies.
[EAF] Use the flag '--install-new-apps' to install previously uninstalled or new apps.
             

加个--force

之前已经加过

force 过数次,是在命令行下,我猜emacs里执行的命令调用的是一个东西;感觉是安装上了但是没被识别似的

手动安装就是打开 dependences.json ,手动安装系统依赖包和python包

有package.json和index.html文件的目录就npm install一下,只有package.json文件的需要手动 npm install再npm build