Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Eldev
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
;; Uncomment some calls below as needed for your project.
;(eldev-use-package-archive 'gnu)
;(eldev-use-package-archive 'nongnu)
;(eldev-use-package-archive 'melpa)
(eldev-use-package-archive 'melpa)
82 changes: 82 additions & 0 deletions lotion-api.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
;;; lotion-api.el --- Lotion api -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022

;; URL: https://github.com/sienic/lotion
;; Version: 0.1.0
;; Keywords: org-mode, notion
;; Package-Requires: ((emacs "26.1") (org "9.4")

;; 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 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/>.
;;
;;; Commentary:
;;
;; Lotion api
;;
;;; Code:

(defun lotion-api-page-fetch (uuid &optional callback)
(lotion-api--get (format "/v1/pages/%s" uuid) callback))

(defun lotion-api-blocks-fetch (uuid &optional callback)
(lotion-api--get (format "/v1/blocks/%s/children" uuid) callback))

(defun lotion-api-block-patch (uuid payload &optional callback)
(lotion-api--patch (format "/v1/blocks/%s" uuid) payload callback))

(defun lotion-api--get (path &optional callback)
(lotion-api--request
:path path
:callback callback
:payload '(("page_size . 100"))))

(defun lotion-api--patch (path payload &optional callback)
(lotion-api--request
:path path
:method "PATCH"
:callback callback
:payload (json-encode payload)
:headers '(("Content-Type" . "application/json"))))

(cl-defun lotion-api--request (&key path method callback payload headers)
"Performs a request to notion"
(let ((type (or method "GET")))
(request (concat "https://" lotion-default-host path)
:type type
:parser 'json-read
:data payload
:headers (append headers `(("Notion-Version" . "2022-02-22")
("Authorization" . ,(concat "Bearer " (lotion-token)))))
:success (cl-function
(lambda (&key data &allow-other-keys)
(if callback (funcall callback data nil))
(setq my/data data)))
:error (cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
(if callback (funcall callback nil error-thrown))
(message "Got error: %S" error-thrown))))))

(defun alist-get-in (alist symbols)
"Navigate an ALIST via SYMBOLS.
Numbers in SYMBOLS are considered indeces of sequences."
(if symbols
(if-let ((index (and (numberp (car symbols))
(car symbols))))
(alist-get-in (nth index (append alist nil)) (cdr symbols))
(alist-get-in (alist-get (car symbols) alist) (cdr symbols)))
alist))

(provide 'lotion-api)
;;; lotion-api.el ends here
60 changes: 60 additions & 0 deletions lotion-oom.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
;;; lotion-oom.el --- Lotion org object model -*- lexical-binding: t; -*-

;;
;; Copyright (C) 2022

;; URL: https://github.com/sienic/lotion
;; Version: 0.1.0
;; Keywords: org-mode, notion
;; Package-Requires: ((emacs "26.1") (org "9.4")

;; 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 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/>.
;;
;;; Commentary:
;;
;; Lotion oom
;;
;;; Code:

(defconst lotion-oom-org-template-lookup
'((page . "* %s") (heading_1 . "** %s") (heading_2 . "*** %s") (paragraph . "%s"))
"Map of org templates per block type")

(defun lotion-oom--template-get (block-type)
"Returns the template for the BLOCK-TYPE"
(cdr (assq block-type lotion-oom-org-template-lookup)))

(defun lotion-oom--page--to-org (page)
"Convert PAGE into an org line"
(format (lotion-oom--template-get (page-type page))
(page-title page)))

(defun lotion-oom--block-to-org (block)
"Convert BLOCK into an org line"
(format (lotion-oom--template-get (block-type block))
(block-content block)))

(defun lotion-oom--element-to-org (elt)
"Convert an ELT into an org line"
(cond ((page-p elt) (lotion-oom--page--to-org elt))
((block-p elt) (lotion-oom--block-to-org elt))))

(defun lotion-oom-page-to-org (page)
"Converts a page and its children blocks into org syntax"
(string-join
(mapcar #'lotion-oom--element-to-org (cons page (page-blocks page))) "\n"))

(provide 'lotion-oom)
56 changes: 56 additions & 0 deletions lotion-parse.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
;;; lotion-parse.el --- Lotion parse -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022

;; URL: https://github.com/sienic/lotion
;; Version: 0.1.0
;; Keywords: org-mode, notion
;; Package-Requires: ((emacs "26.1") (org "9.4")

;; 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 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/>.
;;
;;; Commentary:
;;
;; Lotion parser
;;
;;; Code:

;; data models
;; block is a object-type block
(cl-defstruct block id type content-type content)
;; a page is a type of block (let's store it separate for now)
(cl-defstruct page id type title blocks)

(defun lotion-parse-page (page-data blocks-data)
"Parses a PAGE-DATA and BLOCKS-DATA from Notion's API into Lotion models"
(make-page :id (alist-get-in page-data '(id))
:type (intern (alist-get-in page-data '(object)))
:title (alist-get-in page-data '(properties Name title 0 plain_text))
:blocks (lotion-parse--blocks blocks-data)))

(defun lotion-parse--blocks (data)
(let ((blocks (alist-get-in data '(results))))
(mapcar #'lotion-parse--block blocks)))

(defun lotion-parse--block (data)
(let* ((id (alist-get-in data '(id)))
(type (intern (alist-get-in data '(type))))
(content-type (intern (alist-get-in data `(,type rich_text 0 type))))
(content (alist-get-in data `(,type rich_text 0 ,content-type content))))
(make-block :id id :type type :content-type content-type :content content)))

(provide 'lotion-parse)
;;; lotion-parse.el ends here
46 changes: 46 additions & 0 deletions lotion-render.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
;;; lotion-render.el --- Lotion render -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022

;; URL: https://github.com/sienic/lotion
;; Version: 0.1.0
;; Keywords: org-mode, notion
;; Package-Requires: ((emacs "26.1") (org "9.4")

;; 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 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/>.
;;
;;; Commentary:
;;
;; Lotion render
;;
;;; Code:

(defun lotion-render-page (page)
"Render PAGE and its contents"
(lotion-render--flush (lotion-oom-page-to-org page)))

(defun lotion-render--flush(content)
"Flushes CONTENT into the lotion buffer"
(save-excursion
(with-current-buffer (get-buffer-create "*lotion*")
(org-mode)
(save-excursion
(delete-region (point-min) (point-max))
(insert content)))
(switch-to-buffer-other-window "*lotion*")))

(provide 'lotion-render)
;;; lotion-render.el ends here
Loading