This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-readme.el
76 lines (70 loc) · 2.47 KB
/
generate-readme.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
;; -*- lexical-binding: t -*-
;; Generate README.org from init.el
(require 'package)
(setq package-archives nil)
(package-initialize)
;; Don't create a backup of README.org
(setq-default make-backup-files nil)
(require 'thingatpt)
(require 'org-make-toc)
(let* ((outfile "README.org")
(src-buf (find-file-noselect "init.el"))
(out-buf (create-file-buffer outfile))
(standard-output out-buf))
(princ "* Emacs Configuration
This is my Emacs configuration.
** Table of contents
:PROPERTIES:
:TOC: siblings
:END:\n")
(with-current-buffer src-buf
(widen)
(goto-char (point-min))
(when (string-match-p (rx "-*-") (thing-at-point 'line))
(forward-line 1))
(while (< (point) (point-max))
(cond
((looking-at (rx eol))
(forward-line 1))
((looking-at (rx ";;" (+ ";")))
(let* ((line (thing-at-point 'line))
(h (string-match (rx ";;" (group (+ ";")) (group (+ anything))) line))
(level (length (match-string 1 line)))
(heading (match-string 2 line)))
(mapc #'princ (list (make-string (1+ level) ?\*) " " heading))
(forward-line 1)))
((looking-at (rx ";;"))
(let* ((start (point))
(end (save-excursion
(re-search-forward (rx bol (or ";;;" "("))
nil t)
(1- (line-beginning-position))))
(substr (replace-regexp-in-string
(rx bol (+ ";") (* space))
""
(buffer-substring-no-properties start end))))
(mapc #'princ (list substr "\n\n"))
(goto-char (1+ end))))
((looking-at "(")
(let* ((start (point))
(end (progn
(while (looking-at (rx (* (or space "\n")) "("))
(forward-sexp 1))
(point)))
(str (buffer-substring-no-properties start end)))
(mapc #'princ
(list "#+begin_src emacs-lisp\n"
str
"\n#+end_src\n\n"))
(when (re-search-forward (rx bol ";") nil t)
(backward-char 1))))
(t
(error "Unexpected input: " (thing-at-point 'line))))))
(setq standard-output t)
(with-current-buffer out-buf
(org-mode)
(org-make-toc)
(write-file outfile))
(kill-buffer src-buf)
(kill-buffer out-buf)
(message "%s has been generated." outfile))