前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >优化 EWW 浏览 GitHub 体验

优化 EWW 浏览 GitHub 体验

作者头像
飞驰的西瓜
发布2022-07-26 16:49:54
4390
发布2022-07-26 16:49:54
举报
文章被收录于专栏:EmacsTalk

在之前的使用 EWW 阅读技术文章一文中介绍过使用 EWW 的优势,对于文档来说默认的 EWW 体验就很好了,但是对于阅读 GitHub 上的代码时,体验就不是很好了。比如:

默认 EWW 浏览 GitHub 时的界面

可以看到,EWW 展示 GitHub 时充斥了大量类似导航栏之类的内容,比较影响代码阅读。优化思路也很简单,就是尽量用 GitHub 提供的功能,以纯文本的方式来浏览,方式如下:

  1. 1. 对于项目中的每一个文件,都对于一个纯文本的 raw 版本
  2. 2. 在每个 Pull Request/Commit 页面,通过增加 .patch 后缀,可以打开对应的 patch 格式文件

有了上面的指导思路,实现就不复杂了。首要问题就是 URL 重定向,解决代码如下:

代码语言:javascript
复制
(setq my/url-redirect-list `(("^https://github.com/\\(.+\\)/commit/\\(\\w+\\)$" .
                              ;; 针对单个 commit
                              (lambda (url)
                                (format "https://github.com/%s/commit/%s.patch"
                                        (match-string 1 url)
                                        (match-string 2 url))))
                             ("^https://github.com/\\(.+\\)/pull/\\([[:digit:]]+\\)$" .
                              ;; 针对单个 Pull Request
                              (lambda (url)
                                (format "https://github.com/%s/pull/%s.patch"
                                        (match-string 1 url)
                                        (match-string 2 url))))
                             ("^https://github.com/\\(.+\\)/blob/\\(.+\\)" .
                              ;; 针对单个文件
                              (lambda (url)
                                (format "https://github.com/%s/raw/%s"
                                        (match-string 1 url)
                                        (match-string 2 url))))))

(defun my/url-redirect (fn url &rest args)
  (catch 'ret
    (dolist (redirect-rule my/url-redirect-list)
      (let* ((regexp (car redirect-rule))
             (redirect-fn (cdr redirect-rule))
             (inhibit-message t))
        (when-let* ((matched-groups (string-match regexp url)))
          (setq url (funcall redirect-fn url))
          (message "Redirect URL to %s" url)
          (throw 'ret url)))))
  (apply fn url args))

(advice-add 'url-retrieve :around
            'my/url-redirect)

利用 advice 机制,在进行 url-retrieve 访问时,进行规则匹配,匹配成功时进行 URL 改写,如果所有规则都不匹配,则浏览原始 URL。

在能以纯文本格式浏览 GitHub 后,可以根据打开的链接,开启对应的 mode,进行语法高亮,更方便阅读,代码如下:

代码语言:javascript
复制
(defun my/eww-render-hook()
  (let ((url (plist-get eww-data :url)))
    (cond
     ((string-suffix-p ".patch" url) (diff-mode))
     ((string-suffix-p ".el" url) (emacs-lisp-mode))
     ((string-suffix-p ".rs" url) (rust-mode))
     ((string-suffix-p ".go" url) (go-mode))
     (t (when (and (plist-get eww-data :source)
                   ;; 排除微信公众号内的文章
                   (not (string-match-p "weixin\\.qq\\.com" url)))
          (eww-readable))))))

(add-hook 'eww-after-render-hook 'my/eww-render-hook)

测试

这里给出一些链接方便读者测试:

  • • https://github.com/1History/eww-history-ext/blob/master/eww-history-ext.el
  • • https://github.com/1History/1History/commit/757f7a46dd6677d3abbb002471eeaf872ca82e78
  • • https://github.com/1History/eww-history-ext/pull/1
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 EmacsTalk 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档