forked from ardumont/markdown-toc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown-toc.el
302 lines (258 loc) · 10.4 KB
/
markdown-toc.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
;;; markdown-toc.el --- A simple TOC generator for markdown file
;; Copyright (C) 2014-2017 Antoine R. Dumont
;; Author: Antoine R. Dumont
;; Maintainer: Antoine R. Dumont
;; URL: http://github.com/ardumont/markdown-toc
;; Created: 24th May 2014
;; Version: 0.1.2
;; Keywords: markdown, toc, tools,
;; Package-Requires: ((markdown-mode "2.1") (dash "2.11.0") (s "1.9.0"))
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Generate a TOC from a markdown file: M-x markdown-toc-generate-toc
;; This will compute the TOC at insert it at current position.
;; Update existing TOC: C-u M-x markdown-toc-generate-toc
;; Here is a possible output:
;; <!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-refresh-toc -->
;; **Table of Contents**
;; - [some markdown page title](#some-markdown-page-title)
;; - [main title](#main-title)
;; - [Sources](#sources)
;; - [Marmalade (recommended)](#marmalade-recommended)
;; - [Melpa-stable](#melpa-stable)
;; - [Melpa (~snapshot)](#melpa-~snapshot)
;; - [Install](#install)
;; - [Load org-trello](#load-org-trello)
;; - [Alternative](#alternative)
;; - [Git](#git)
;; - [Tar](#tar)
;; - [another title](#another-title)
;; - [with](#with)
;; - [some](#some)
;; - [heading](#heading)
;;
;; <!-- markdown-toc end -->
;; Install - M-x package-install RET markdown-toc RET
;;; Code:
(require 's)
(require 'dash)
(require 'markdown-mode)
(defconst markdown-toc--toc-version "0.1.2" "Current version installed.")
(defgroup markdown-toc nil
"A simple TOC generator for markdown file."
:group 'markdown)
(defun markdown-toc-log-msg (args)
"Log message ARGS."
(apply #'message (format "markdown-toc - %s" (car args)) (cdr args)))
;;;###autoload
(defun markdown-toc-version ()
"Markdown-toc version."
(interactive)
(message "markdown-toc version: %s" markdown-toc--toc-version))
(defalias 'markdown-toc/version 'markdown-toc-version)
(defun markdown-toc--compute-toc-structure-from-level (level menu-index)
"Given a LEVEL and a MENU-INDEX, compute the toc structure."
(when menu-index
(let* ((fst (car menu-index))
(tail (cdr menu-index))
(ttail (if (integerp tail) nil (cdr tail))))
(cons `(,level . ,fst)
(--mapcat
(markdown-toc--compute-toc-structure-from-level (+ 1 level) it)
ttail)))))
(defun markdown-toc--compute-toc-structure (imenu-index)
"Given a IMENU-INDEX, compute the TOC structure."
(--mapcat (markdown-toc--compute-toc-structure-from-level 0 it) imenu-index))
(defun markdown-toc--symbol (sym n)
"Compute the repetition of a symbol SYM N times as a string."
(--> n
(-repeat it sym)
(s-join "" it)))
(defconst markdown-toc--protection-symbol "09876543214b825dc642cb6eb9a060e54bf8d69288fbee49041234567890"
"Implementation detail to protect some punctuation characters
when converting to link.")
(defun markdown-toc--to-link (title count)
"Given a TITLE, return the markdown link associated."
(format "[%s](#%s%s)" title
(->> title
downcase
(replace-regexp-in-string "-" markdown-toc--protection-symbol)
(replace-regexp-in-string "[[:punct:]]" "")
(replace-regexp-in-string markdown-toc--protection-symbol "-")
(s-replace " " "-"))
(if (> count 0)
(concat "-" (number-to-string count))
"")))
(defun markdown--count-duplicate-titles (toc-structure)
"Counts the number of times each title appeared in the toc structure and adds
it to the TOC structure."
(-map-indexed (lambda (index n)
(let* ((indent (car n))
(title (cdr n))
(count (--count (string= title (cdr it))
(-take (+ index 1) toc-structure))))
(list indent title (- count 1))))
toc-structure))
(defun markdown-toc--to-markdown-toc (level-title-toc-list)
"Given LEVEL-TITLE-TOC-LIST, a list of pair level, title, return a TOC string."
(->> level-title-toc-list
markdown--count-duplicate-titles
(--map (let ((nb-spaces (* 4 (car it)))
(title (car (cdr it)))
(count (car (cdr (cdr it)))))
(format "%s%s %s"
(markdown-toc--symbol " " nb-spaces)
markdown-toc-list-item-marker
(markdown-toc--to-link title count))))
(s-join "\n")))
(defcustom markdown-toc-list-item-marker
"-"
"List item marker that should be used.
Example: '-' for unordered lists or '1.' for ordered lists."
:type '(choice
(string :tag "Unordered list header" "-")
(string :tag "Ordered list header" "1."))
:group 'markdown-toc)
(defcustom markdown-toc-header-toc-start
"<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-refresh-toc -->"
"Beginning delimiter comment."
:group 'markdown-toc)
(defcustom markdown-toc-header-toc-title
"**Table of Contents**"
"Title comment on TOC header."
:group 'markdown-toc)
(defcustom markdown-toc-header-toc-end
"<!-- markdown-toc end -->"
"Ending delimiter comment."
:group 'markdown-toc)
(defun markdown-toc--toc-already-present-p ()
"Determine if a TOC has already been generated.
Return the end position if it exists, nil otherwise."
(save-excursion
(goto-char (point-min))
(re-search-forward markdown-toc-header-toc-start nil t)))
(defun markdown-toc--toc-start ()
"Compute the toc's starting point."
(save-excursion
(goto-char (markdown-toc--toc-already-present-p))
(point-at-bol)))
(defun markdown-toc--toc-end ()
"Compute the toc's end point."
(save-excursion
(goto-char (point-min))
(re-search-forward markdown-toc-header-toc-end nil t)))
(defun markdown-toc--generate-toc (toc-structure)
"Given a TOC-STRUCTURE, compute a new toc."
(-> toc-structure
markdown-toc--to-markdown-toc
markdown-toc--compute-full-toc))
(defun markdown-toc--delete-toc (&optional replace-toc-p)
"Delets a TOC."
(let ((region-start (markdown-toc--toc-start))
(region-end (markdown-toc--toc-end)))
(delete-region region-start (1+ region-end))
(when replace-toc-p
(goto-char region-start))))
(defun markdown-toc--compute-full-toc (toc)
"Given the TOC's content, compute the full toc with comments and title."
(format "%s\n%s\n\n%s\n\n%s\n"
markdown-toc-header-toc-start
markdown-toc-header-toc-title
toc
markdown-toc-header-toc-end))
(defcustom markdown-toc-user-toc-structure-manipulation-fn
(lambda (toc-structure) toc-structure)
"User crafted function to manipulate toc-structure as user sees fit.
The toc-structure has the following form:
'((0 . \"some markdown page title\")
(0 . \"main title\")
(1 . \"Sources\")
(2 . \"Marmalade (recommended)\")
(2 . \"Melpa-stable\")
(2 . \"Melpa (~snapshot)\")
(1 . \"Install\")
(2 . \"Load org-trello\")
(2 . \"Alternative\")
(3 . \"Git\")
(3 . \"Tar\")
(0 . \"another title\")
(1 . \"with\")
(1 . \"some\")
(1 . \"heading\"))
If the user wanted to remove the first element, it could for
example define the following function:
(custom-set-variables
'(markdown-toc-user-toc-structure-manipulation-fn 'cdr))
Default to identity function (do nothing)."
:group 'markdown-toc)
;;;###autoload
(defun markdown-toc-generate-toc (&optional replace-toc-p)
"Generate a TOC for markdown file at current point.
Deletes any previous TOC.
If called interactively with prefix arg REPLACE-TOC-P, replaces previous TOC."
(interactive "P")
(save-excursion
(when (markdown-toc--toc-already-present-p)
;; when toc already present, remove it
(markdown-toc--delete-toc t))
(->> (markdown-imenu-create-nested-index)
markdown-toc--compute-toc-structure
(funcall markdown-toc-user-toc-structure-manipulation-fn)
markdown-toc--generate-toc
insert)))
;;;###autoload
(defun markdown-toc-generate-or-refresh-toc ()
"Generate a TOC for markdown file at current point or refreshes an already generated TOC."
(interactive)
(markdown-toc-generate-toc t))
;;;###autoload
(defun markdown-toc-refresh-toc ()
"Refreshes an already generated TOC."
(interactive)
(when (markdown-toc--toc-already-present-p)
(markdown-toc-generate-toc t)))
;;;###autoload
(defun markdown-toc-delete-toc ()
"Deletes a previously generated TOC."
(interactive)
(save-excursion
(markdown-toc--delete-toc t)))
(defalias 'markdown-toc/generate-toc 'markdown-toc-generate-toc)
(defun markdown-toc--bug-report ()
"Compute the bug report for the user to include."
(->> `("Please:"
"- Describe your problem with clarity and conciceness (cf. https://www.gnu.org/software/emacs/manual/html_node/emacs/Understanding-Bug-Reporting.html)"
"- Explicit your installation choice (melpa, marmalade, el-get, tarball, git clone...)."
"- Report the following message trace inside your issue."
""
"System information:"
,(format "- system-type: %s" system-type)
,(format "- locale-coding-system: %s" locale-coding-system)
,(format "- emacs-version: %s" (emacs-version))
,(format "- markdown-toc version: %s" markdown-toc--toc-version)
,(format "- markdown-toc path: %s" (find-library-name "markdown-toc")))
(s-join "\n")))
(defun markdown-toc-bug-report (&optional open-url)
"Display a bug report message.
When OPEN-URL is filled, with universal argument (`C-u') is used,
opens new issue in markdown-toc's github tracker."
(interactive "P")
(when open-url
(browse-url "https://github.com/ardumont/markdown-toc/issues/new"))
(markdown-toc-log-msg (list (markdown-toc--bug-report))))
(provide 'markdown-toc)
;;; markdown-toc.el ends here