-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjira-doc.el
93 lines (76 loc) · 3.31 KB
/
jira-doc.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
;;; jira-doc.el --- Jira Doc -*- lexical-binding: t -*-
;; Copyright (C) 2025 Pablo González Carrizo
;; Author: Pablo González Carrizo <[email protected]>
;; Created: 2025-02-16
;; This file is NOT part of GNU Emacs.
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; Manage Altassian Document Format
;; See https://developer.atlassian.com/cloud/jira/platform/apis/document/
;; Not all the kinds of blocks are supported yet, only the most common ones.
;;; Code:
;; these blocks contain the content property
(defconst jira-doc--top-level-blocks
'("blockquote" "bulletList" "codeBlock" "expand" "heading" "mediaGroup"
"mediaSingle" "orderedList" "panel" "paragraph" "rule" "table"
"multiBodiedExtension"))
;; these blocks contain the content property
(defconst jira-doc--child-blocks
'("listItem" "media" "nestedExpand" "tableCell" "tableHeader"
"tableRow" "extensionFrame"))
(defconst jira-doc--inline-blocks
'("date" "emoji" "hardBreak" "inlineCard" "mention" "status"
"text" "mediaInline"))
(defun jira-doc--list-to-str (items sep)
"Concatenate ITEMS with SEP."
(mapconcat #'identity items sep))
(defun jira-doc--format-inline-block(block)
"Format inline BLOCK to a string."
(let ((type (alist-get 'type block))
(text (alist-get 'text block)))
(cond ((string= type "hardBreak") "\n")
((string= type "inlineCard")
(let* ((url (alist-get 'url (alist-get 'attrs block))))
(buttonize url `(lambda (data) (interactive) (browse-url ,url)))))
(text (format "%s " text)))))
(defun jira-doc--format-content-block(block)
"Format content BLOCK to a string."
(let* ((type (alist-get 'type block))
(sep (if (string= type "paragraph") "" "\n"))
(prefix (if (string= type "listItem") " - " "")))
(cond
((string= type "table")
"\n<TABLES NOT SUPPORTED BY jira.el>\n")
(t (concat prefix
(jira-doc--list-to-str
(mapcar (lambda (b) (jira-doc--format-block b))
(alist-get 'content block))
sep))))))
(defun jira-doc--format-block(block)
"Format BLOCK to a string."
(let ((type (alist-get 'type block)))
(if (or (member type jira-doc--top-level-blocks)
(member type jira-doc--child-blocks))
(jira-doc--format-content-block block)
(jira-doc--format-inline-block block))))
(defun jira-doc-format (doc)
"Format DOC in Jira Document Format to a string."
(let* ((content (alist-get 'content doc)))
(jira-doc--list-to-str
(mapcar (lambda (block) (jira-doc--format-block block)) content)
"\n")))
(provide 'jira-doc)
;;; jira-doc.el ends here