forked from renard/o-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
o-blog-grid.el
134 lines (106 loc) · 3.33 KB
/
o-blog-grid.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
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
;;; o-blog-grid.el --- Publish grid in o-blog
;; Copyright © 2012 Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
;; Author: Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
;; Keywords: emacs,
;; Created: 2012-01-23
;; Last changed: 2012-02-10 23:38:16
;; Licence: WTFPL, grab your copy here: http://sam.zoy.org/wtfpl/
;; This file is NOT part of GNU Emacs.
;;; Commentary:
;;
;;; Code:
(eval-when-compile
(require 'org nil t))
(defcustom o-blog-grid-header
"<div class=\"row %s\"><div class=\"%s\">"
"HTML fragment header to be used when publishing a grid using
`o-blog-publish-grid' using `format' with grid options as
parameter. The grid should be closed with `o-blog-grid-footer'."
:type 'string
:group 'o-blog)
(defcustom o-blog-grid-footer
"</div></div>"
"HTML fragment footer to be used when publishing a grid using
`o-blog-publish-grid' as it. This closes `o-blog-grid-header'."
:type 'string
:group 'o-blog)
(defcustom o-blog-grid-column
"</div><div class=\"%s\">"
"HTML middle fragment to be used when publishing a grid. This
string is used for next column in `o-blog-publish-grid' using
`format' with column configuration as parameter."
:type 'string
:group 'o-blog)
(defun o-blog-grig-compute-args (a1 a2)
"Compute A1 and A2."
(let* ((i2c (lambda (x)
(let ((x (if x (string-to-int x) 0)))
(cond
((= 0 x) "")
((< 0 x) (format "span%d" x))
((> 0 x) (format "offset%d" (- x))))))))
(format "%s %s"
(funcall i2c a1)
(funcall i2c a2))))
;;;###autoload
(defun o-blog-publish-grid ()
"Publish a grid in HTML mode.
The default replacement text could be changed using variables
`o-blog-grid-header', `o-blog-grid-footer' and
`o-blog-grid-title'."
(save-match-data
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(let ((case-fold-search t))
(while (re-search-forward
"^#\\+BEGIN_O_BLOG_ROW:?[ \t]+\\(.*\\)"
nil t)
(org-narrow-to-subtree)
(goto-char (point-at-bol))
(let* ((args (when (stringp (match-string 1))
(split-string (match-string-no-properties 1)))))
(insert
"#+BEGIN_HTML\n"
(format o-blog-grid-header
(or (nth 2 args) "")
(o-blog-grig-compute-args
(nth 0 args)
(nth 1 args)))
"\n#+END_HTML\n")
(delete-region (point) (point-at-eol))
(save-excursion
(unless
(re-search-forward "^#\\+END_O_BLOG_ROW" nil t)
(error "#+END_O_BLOG_ROW not found in %s@%s." (buffer-file-name)
(point)))
(beginning-of-line)
(insert
"\n#+BEGIN_HTML\n"
o-blog-grid-footer
"\n#+END_HTML\n")
(delete-region (point) (point-at-eol))
(setq end (point)))
(save-excursion
(while (re-search-forward
"^#\\+O_BLOG_ROW_COLUMN:?[ \t]+\\(-?[0-9]+\\)\\([ \t]+\\(-?[0-9]+\\)\\)?"
nil t)
(goto-char (point-at-bol))
(insert
"#+BEGIN_HTML\n"
(format o-blog-grid-column
(o-blog-grig-compute-args
(match-string 1)
(match-string 2)))
"\n#+END_HTML\n")
(delete-region (point) (point-at-eol)))))
(widen)))))))
(add-to-list
'org-structure-template-alist
'("og" "#+begin_o_blog_row ?\n\n#+end_o_blog_row"))
(add-to-list
'org-structure-template-alist
'("ogr" "#+o_blog_row_column ?\n\n"))
(add-hook 'o-blog-html-plugins-hook 'o-blog-publish-grid)
(provide 'o-blog-grid)