summaryrefslogtreecommitdiff
path: root/fi/blog/index.org
blob: 740897b2515a60a1884e14ec47a8206423f5619c (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
#+TITLE: Blogin sisällys

#+name: code:index-utils
#+begin_src emacs-lisp :exports none
  (defun get-org-file-property (path property)
    "Gets PROPERTY of org file at PATH. Returns nil if not found."
    (with-temp-buffer (setq case-fold-search t)
                      (insert-file-contents path)
                      (if (search-forward (concat "#+" property ":") nil t)
                          (string-trim-left (buffer-substring (point) (line-end-position)))
                        nil)))

  (defun get-org-file-title (name)
    "Gets the title of the org file. Returns nil if not found."
    (get-org-file-property name "TITLE"))

  (defun get-file-date (path)
    "Gets the date of the file at PATH.
  For an Org file where the date option is specified, it is used.
  Otherwise the file modification date is used.
  The date is returned in textual form, YYYY-MM-DD."
    (or (if (string= (file-name-extension path) "org")
            (get-org-file-property path "DATE")
          nil)
        (format-time-string "%Y-%m-%d"
                            (file-attribute-modification-time
                             (file-attributes (concat "./" path))))))
#+end_src

#+begin_src emacs-lisp :exports results :noweb yes

  <<code:index-utils>>

  (defun generate-entry 
      (name)
    "Generates an index entry for file NAME."
    (let ((extension (file-name-extension name)) (bname (file-name-sans-extension name)))
      (concat "[[./"
              name
              "]["
              (or
               (if (string= extension "org")
                   (get-org-file-title (concat "./" name))
                 nil)
               bname)
              "]] ("
              (get-file-date (concat "./" name))
              ") "
              (or (if (string= extension "org")
                      (get-org-file-property (concat "./" name) "SUBTITLE")
                    nil)
                  "Alaotsikkoa ei saatavilla"))))

  (let ((files
         (seq-filter (lambda (name)
                       (not (or
                             (string-suffix-p "~" name)
                             (string-suffix-p "#" name)
                             (string-prefix-p "#" name)
                             (string-prefix-p "." name)
                             (string= "index.org" name))))
                     (sort (directory-files "./")
                           :key (lambda (f) (get-file-date f))
                           :reverse t))))
    (seq-map (lambda (name) `(,(generate-entry name))) files))
#+end_src

#+RESULTS:
| [[./sivujen-uudistus.org][Verkkosivujeni uudistus]] (2026-06-14) Mitä sivuilleni on tapahtunut ja miksi |
| [[./tekstinsyotto.html][tekstinsyotto]] (2025-07-28) Alaotsikkoa ei saatavilla                        |
| [[./yksinkertaisesti-monipuolinen.txt][yksinkertaisesti-monipuolinen]] (2024-05-06) Alaotsikkoa ei saatavilla        |