-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg2html
More file actions
172 lines (141 loc) · 5.15 KB
/
org2html
File metadata and controls
172 lines (141 loc) · 5.15 KB
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
#!/usr/bin/env bash
# @file org2html
# @brief Org-Mode files support for CGit about pages and text blobs rendering.
# @copyright 2023-2024 Alexandre Martos <contact@amartos.fr>
# @license GPLv3
CGIT_APPDIR="${CGIT_APPDIR:-/usr/share/cgit}"
CGIT_CONFIG="${CGIT_CONFIG:-/etc/cgitrc}"
CGIT_CACHE="${CGIT_CACHE:-/var/cache/cgit}"
if [ -f "$CGIT_CONFIG" ] && grep -q cache-root "$CGIT_CONFIG"
then
CGIT_CACHE=$(awk 'BEGIN {FS="="} /^cache-root/ {print $2}' "$CGIT_CONFIG")
fi
CACHE="$CGIT_CACHE/org2html"
ORG2HTML_CSS_PATH="${ORG2HTML_CSS_PATH:-$CGIT_APPDIR/css/org2html.css}"
INIT="$CACHE/init.el"
usage() {
echo "usage: $(basename $0) [-hvw] < FILE.org"
}
version() {
echo "org2html v1.1.0"
}
license() {
cat <<EOF
$(version) Copyright (C) 2023-2024 Alexandre Martos <contact@amartos.fr>
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 of the License, 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 this program. If not, see <https://www.gnu.org/licenses/>.
EOF
}
phelp() {
cat <<EOF
$(usage)
This program converts the content of an Org-Mode file to HTML (body
only). It is designed to be used by CGit as a converter for filters.
[INPUT]
The script takes the Org-Mode file content through STDIN.
[OPTIONS]
-h print this help
-v print the version number
-w print the short license text
[ENVIRONMENT]
CGIT_CONFIG CGit config file path (defaults to /etc/cgitrc)
CGIT_APPDIR CGit root directory (defaults to /usr/share/cgit)
CGIT_CACHE CGit cache directory (defaults to /var/cache/cgit)
ORG2HTML_CSS_PATH CSS file path (defaults to CGIT_APPDIR/css/org2html.css)
[CONFIGURATION]
The CGit configuration values this script uses (if defined):
cache-root CGit cache root path (supersedes CGIT_CACHE)
[BUGS]
In case of issues please contact the maintainer at:
https://github.com/amartos/cgit-org2html/issues
[COPYRIGHT]
$(license)
EOF
}
while getopts "hvw" arg
do
case $arg in
h) phelp; exit 0; ;;
v) version; exit 0; ;;
w) license; exit 0; ;;
*) ;;
esac
done
mkdir -p "$CACHE"
[ ! -f "$INIT" ] && cat > "$INIT"<<EOF
(require 'org)
(require 'ox-html)
(defun org-html-src-block (src-block _contents info)
"Transcode a SRC-BLOCK element from Org to HTML.
CONTENTS holds the contents of the item. INFO is a plist holding
contextual information.
This version is the same as the ox-html.el function, except that the
code is enclosed in <pre><code> tags (and not only <pre>), and the
language classes used for the code blocks are those used by prism.js
and highlight.js"
(if (org-export-read-attribute :attr_html src-block :textarea)
(org-html--textarea-block src-block)
(let* ((lang (org-element-property :language src-block))
(code (org-html-format-code src-block info))
(label (let ((lbl (org-html--reference src-block info t)))
(if lbl (format " id='%s'" lbl) "")))
(klipsify (and (plist-get info :html-klipsify-src)
(member lang '("javascript" "js"
"ruby" "scheme" "clojure" "php" "html")))))
(if (not lang) (format "<pre class='example'%s>\n%s</pre>" label code)
(format "<div class='org-src-container'>\n%s%s\n</div>"
;; Build caption.
(let ((caption (org-export-get-caption src-block)))
(if (not caption) ""
(let ((listing-number
(format
"<span class='listing-number'>%s </span>"
(format
(org-html--translate "Listing %d:" info)
(org-export-get-ordinal
src-block info nil #'org-html--has-caption-p)))))
(format "<label class='org-src-name'>%s%s</label>"
listing-number
(org-trim (org-export-data caption info))))))
;; Contents.
(if klipsify
(format "<pre><code class='lang lang-%s'%s%s>%s</code></pre>"
lang
label
(if (string= lang "html")
" data-editor-type='html'"
"")
code)
(format "<pre><code class='lang lang-%s'%s>%s</code></pre>"
lang label code)))))))
(defun org-html-example-block (example-block _contents info)
"Transcode a EXAMPLE-BLOCK element from Org to HTML.
CONTENTS is nil. INFO is a plist holding contextual
information.
This version is designed to give the exact same output as
org-html-src-block."
(org-html-src-block example-block _contents info))
(setq org-html-self-link-headlines t)
(defun user/org/cgit-org2html ()
(org-html-export-as-html nil nil nil t nil)
(princ (buffer-string))
)
EOF
STDIN="$(cat)"
SHA="$CACHE/$(echo $STDIN | sha256sum | cut -d' ' -f1)"
if [ ! -f "$SHA.html" ]
then
echo "$STDIN" > "$SHA.org"
emacs -Q --batch "$SHA.org" -l "$INIT" -f user/org/cgit-org2html --kill 1>"$SHA.html" 2>/dev/null
rm "$SHA.org"
fi
echo "<style>$(cat $ORG2HTML_CSS_PATH)</style>"
cat "$SHA.html"