-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathob-git-permalink.el
126 lines (105 loc) · 4.53 KB
/
ob-git-permalink.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
;;; ob-git-permalink.el --- Import GitHub code given a permalink -*- lexical-binding: t -*-
;; Copyright (C) 2022 kijima Daigo
;; Author: kijima Daigo <[email protected]>
;; Version: 0.1.0
;; Keywords: docs convenience
;; Package-Requires: ((emacs "25.1"))
;; URL: https://github.com/kijimaD/ob-git-permalink
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Import GitHub code given a permalink.
;; #+begin_src git-permalink
;; https://github.com/emacs-mirror/emacs/blob/a4dcc8b9a94466c792be3743760a4a45cf6e1e61/lisp/emacs-lisp/ring.el#L48-L52
;; #+end_src
;; ↓ evaluate(C-c)
;; (defun ring-p (x)
;; "Return t if X is a ring; nil otherwise."
;; (and (consp x) (integerp (car x))
;; (consp (cdr x)) (integerp (cadr x))
;; (vectorp (cddr x))))
;;; Code:
(require 'ob)
(add-to-list 'org-babel-tangle-lang-exts '("git-permalink"))
(defun ob-git-permalink-parser-github (url)
"Parse GitHub URL and return result hash."
(let* ((hash (make-hash-table)))
(string-match "http[s]?://github.com/\\(.*?\\)/\\(.*?\\)/blob/\\(.*?\\)/\\(.*\\)#L\\(.*?\\)\\(?:-L\\(.*?\\)\\)?$" url)
(puthash 'user (match-string 1 url) hash)
(puthash 'repo (match-string 2 url) hash)
(puthash 'githash (match-string 3 url) hash)
(puthash 'path (match-string 4 url) hash)
(puthash 'start (string-to-number (match-string 5 url)) hash)
(puthash 'end (if (match-string 6 url)
(string-to-number (match-string 6 url))
nil) hash)
hash))
(defun ob-git-permalink-parser (url)
"Choose parser by URL."
(cond ((string-match-p "^http[s]?://github.com" url) 'ob-git-permalink-parser-github)
(t (error "Not found parser"))))
(defun ob-git-permalink-build-link (hash-table)
"Build link with HASH-TABLE."
(when (not (hash-table-p hash-table))
(error "Argument Error: HASH is not hash table"))
(let* ((user (gethash 'user hash-table))
(repo (gethash 'repo hash-table))
(githash (gethash 'githash hash-table))
(path (gethash 'path hash-table)))
(format "https://raw.githubusercontent.com/%s/%s/%s/%s" user repo githash path)))
(defun ob-git-permalink-request (url start end)
"Get code from raw file URL and trim between START and END."
(let* ((lines)
(current-line start)
(buffer (url-retrieve-synchronously url)))
(with-current-buffer buffer
;; remove request header
(goto-char (point-min))
(re-search-forward "^$")
(delete-region (point) (point-min))
(kill-line)
;; trim line
(forward-line (- current-line 1))
(push (buffer-substring-no-properties (line-beginning-position) (line-end-position)) lines)
(forward-line 1)
(when end
(while (< (line-number-at-pos) (+ end 1))
(progn
(push (buffer-substring-no-properties (line-beginning-position) (line-end-position)) lines)
(forward-line 1)))))
(mapconcat (function (lambda (s) (format "%s" s)))
(reverse lines)
"\n")))
(defun ob-git-permalink-get-code (url)
"Get code by URL."
(let* ((parser (ob-git-permalink-parser url))
(hash-table (funcall parser url))
(request-url (ob-git-permalink-build-link hash-table))
(start (gethash 'start hash-table))
(end (if (gethash 'start hash-table)
(gethash 'end hash-table)
start)))
(ob-git-permalink-request request-url start end)))
;;;###autoload
(defun org-babel-execute:git-permalink (body params)
"Resolve BODY permalink and insert source code.
If PARAMS url is specified, the parameter is used."
(let* ((params-url (cdr (assq :url params)))
(url (if params-url
params-url
body))
(code (ob-git-permalink-get-code url)))
(with-temp-buffer
(insert code)
(buffer-substring (point-min) (point-max)))))
(provide 'ob-git-permalink)
;;; ob-git-permalink.el ends here