aboutsummaryrefslogtreecommitdiff
path: root/ennu-html.el
blob: 877e561d66af0f449134666cb5c485bb714596e8 (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
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
;; -*- lexical-binding: t -*-

(require 'ox)
(require 'subr-x)
(require 'xmlgen)

(defun expand-file-name* (name default-directory)
  (expand-file-name name (concat "/" default-directory)))

(org-export-define-derived-backend 'ennu-html 'html
  :translate-alist
  '((inner-template . ennu-html-inner-template))
  :options-alist
  '((:html-inline-image-rules
     nil nil '(("image" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\|svg\\)\\'")) t)
    (:summary "SUMMARY" nil nil parse)
    (:thumbnail "THUMBNAIL" nil nil t)
    (:translation-group "TRANSLATION_GROUP" nil nil t)))

(defun ennu-html-inner-template (contents info)
  (concat
   ;; Table of contents
   (let ((depth (plist-get info :with-toc)))
     (when depth (org-html-toc depth info)))
   ;; Beginning of h-entry
   "<article class=\"h-entry\">"
   ;; Title
   (format "<h1 class=\"p-name\">%s</h1>\n"
           (org-export-data (plist-get info :title) info))
   ;; Author and date
   (let ((author (when (plist-get info :with-author)
                   (plist-get info :author)))
         (date (when (plist-get info :with-date)
                 (org-export-get-date info))))
     (when (or author date)
       (xmlgen `(p "Published"
                   ,@(when author
                       `(" by "
                         (a :class "p-author h-card"
                            :href ,(ennu--absolute-uri "")
                            ,(car (plist-get info :author)))))
                   ,@(when date
                       `(" on "
                         (time :class "dt-published"
                               :datetime ,(org-export-get-date info "%Y-%m-%d 12:00:00")
                               ,(org-export-get-date info "%B %d, %Y"))))))))
   ;; Interlanguage language links
   (format "<p>In other languages: %s</p>"
           (mapconcat
            (lambda (translation)
              (let ((lang (ennu-post-language translation))
                    (slug (ennu-post-slug translation)))
                (replace-regexp-in-string
                 "<a " (format "<a hreflang=\"%s\" " lang)
                 (ennu-export-post slug lang (plist-get info :backend)))))
            (plist-get info :translations)
            ", "))
   ;; Tags
   (format "<p>Tags: %s</p>"
           (mapconcat
            (lambda (tag)
              (replace-regexp-in-string
               "<a " "<a class=\"p-category\" "
               (ennu-export-tag tag nil (plist-get info :backend))))
            (plist-get info :filetags) ", "))
   ;; Summary
   (format "<div class=\"p-summary\">%s</div>"
           (org-export-data (plist-get info :summary) info))
   ;; Document contents
   (format "<div class=\"e-content\">%s</div>" contents)
   ;; Footnotes section
   (org-html-footnote-section info)
   "</article>"))

;; TODO: Pass title through org-export-data-with-backend or something
;; similar in order to export org syntax in title
(defun ennu-export-post (path desc backend)
  (let ((filename (concat (expand-file-name path (ennu-setting :posts-directory))
                      ".org")))
    (xmlgen `(a :href ,(expand-file-name*
                        path (ennu-setting :posts-directory))
                ,(or desc (ennu-post-title (ennu-read-post filename)))))))

(org-link-set-parameters
 "post" :export 'ennu-export-post)

(defun ennu-export-image (path desc backend)
  (let ((img `(img :src ,(expand-file-name*
                          (ennu-image-output-filename
                           path (ennu-setting :default-image-width))
                          (ennu-setting :images-directory)))))
    (xmlgen (if (ennu-setting :image-link-width)
                `(a :href ,(expand-file-name*
                            (ennu-image-output-filename
                             path (ennu-setting :image-link-width))
                            (ennu-setting :images-directory))
                    ,img)
              img))))

(org-link-set-parameters
 "image" :export 'ennu-export-image)

(defun ennu-export-thumbnail (path desc backend)
  (xmlgen
   `(img :src ,(expand-file-name*
                (ennu-image-output-filename
                 path (ennu-setting :thumbnail-image-width))
                (ennu-setting :images-directory)))))

(org-link-set-parameters
 "thumbnail" :export 'ennu-export-thumbnail)

(defun ennu-export-video (path desc backend)
  (let ((video-directory (ennu-setting :video-directory)))
    (xmlgen
     `(video :src ,(expand-file-name* path video-directory)
             :poster ,(expand-file-name* (ennu-video-poster path) video-directory)
             :preload "none"
             :controls ""))))

(org-link-set-parameters
 "video" :export 'ennu-export-video)

(defun ennu-export-static (path desc backend)
  (xmlgen
   `(a :href ,(expand-file-name* path (ennu-setting :static-directory))
       ,desc)))

(org-link-set-parameters
 "static" :export 'ennu-export-static)

(org-link-set-parameters
 "tangle" :export 'ennu-export-static)

(defun ennu-export-tag (tag desc backend)
  (xmlgen
   `(a :href ,(expand-file-name* tag (ennu-setting :tag-directory))
       ,(or desc tag))))

(org-link-set-parameters
 "tag" :export 'ennu-export-tag)

(provide 'ennu-html)