summaryrefslogtreecommitdiffstats
path: root/lib/export-man.el
blob: 03ef8436b76658a4b23c7d8680a2ce83928e770d (plain)
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
;;; export-man.el -- Export man page and filter result

;;; Commentary:
;;;
;;; Exports a man page and filters the result so we can exclude parts of the man
;;; page based on features enabled in the build system.
;;;
;;; The export-man-page function is called from common.mk with --eval

;;; Code:

(require 'ox-man)
(require 'parse-time)

(defvar feature-exclude-tags
  '(("LIBBPF_PERF_BUFFER__CONSUME" . "feat_perfbuf"))
  "Mapping of feature strings to exclude tags for man page export.")

(defvar feature-exclude-regexes
  '(("LIBBPF_PERF_BUFFER__CONSUME" . "--perf-wakeup"))
  "Mapping of feature strings to regexes to filter form export man page.")

(defun get-feature-values (enabled-feats exclude-list)
  "Get feature-tag values for ENABLED-FEATS based on EXCLUDE-LIST."
  (delq nil (mapcar #'(lambda (f)
                      (unless (member (car f) enabled-feats)
                        (cdr f)))
                    exclude-list)))

(defun replace-regexp-in-buffer (regexp replace)
  "Replace REGEXP with REPLACE in buffer."
  (let ((case-fold-search nil))
    (goto-char 0)
    (when (re-search-forward regexp nil t)
      (replace-match replace))))

(defun open-file (filename)
  "Find file FILENAME but complain if it doesn't exist."
  (if (file-exists-p filename)
      (find-file filename)
    (error "File not found: %s" filename)))

(defun get-file-mod-time (filename)
  (let* ((file-modtime (file-attribute-modification-time (file-attributes filename)))
         (git-logtime (ignore-errors (shell-command-to-string
                                      (format "git log -1 --pretty='format:%%cI' -- %s" filename))))
         (git-modtime (ignore-errors (parse-iso8601-time-string git-logtime))))
    (or git-modtime file-modtime)))

(defun filter-post-export (file feat-list version modtime)
  "Post-process exported FILE based on features in FEAT-LIST and VERSION."
  (let ((exclude-regexes (get-feature-values feat-list feature-exclude-regexes))
        (date (format-time-string "%B %_d, %Y" modtime))
        (make-backup-files nil))
    (with-current-buffer (open-file file)
      (mapc #'(lambda (r) (delete-matching-lines r)) exclude-regexes)
      (replace-regexp-in-buffer "DATE" date)
      (replace-regexp-in-buffer "VERSION" version)
      (replace-regexp-in-buffer "^.SH \"\\([^\"]+\\) - \\([^\"]+\\)\""
                                ".SH \"NAME\"\n\\1 \\\\- \\2\n.SH \"SYNOPSIS\"")
      (save-buffer))))

(defun export-man-page (outfile infile enabled-features version)
  "Export man page from INFILE into OUTFILE with ENABLED-FEATURES and VERSION."
  (let* ((feat-list (split-string enabled-features))
         (org-export-exclude-tags (get-feature-values feat-list feature-exclude-tags))
         (modtime (get-file-mod-time infile)))
    (with-current-buffer (open-file infile)
      (org-export-to-file 'man outfile)
      (filter-post-export outfile feat-list version modtime))))

(provide 'export-man)
;;; export-man.el ends here