From 1adeabcebbfdd78f1425d94703a01e5daf44f0a0 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Fri, 31 Oct 2014 23:38:55 +0000 Subject: Initial lisp-to-org converter --- stack-lto.el | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 stack-lto.el (limited to 'stack-lto.el') diff --git a/stack-lto.el b/stack-lto.el new file mode 100644 index 0000000..6d8ea6d --- /dev/null +++ b/stack-lto.el @@ -0,0 +1,85 @@ +;;; stack-core.el --- lisp-to-org conversion functions for stack-mode -*- lexical-binding: t; -*- + +;; Copyright (C) 2014 Artur Malabarba + +;; Author: Artur Malabarba +;; Keywords: help, hypermedia, mail, news, tools + + +;; 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 . + +;;; Commentary: + +;;; Code: + + +;;; Requirements +(require 'stack-core) +(require 'json) + +(defun stack-lto--question (data) + "Return question DATA in a format acceptable by `org-element-interpret-data'. +DATA is a list of cons cells representing a question, as received +by the API and read by `json-read'." + `(headline (:title ,(cdr (assoc 'title data)) + :level 1 + :tags ,(mapcar #'identity (cdr (assoc 'tags data)))) + (section () + (headline (:title "Question" :level 2) + ,@(stack-lto--question-answer data)) + ,@(mapcar #'stack-lto--answer (cdr (assoc 'answers data)))))) + +(defun stack-lto--answer (data) + "Return answer DATA in a format acceptable by `org-element-interpret-data'. +DATA is a list of cons cells representing a question, as received +by the API and read by `json-read'." + ;; Right now this doesn't do anything special. But it should check + ;; whether the answer is accepted. How do we display that? + `(headline (:title "Answer" :level 2) + ,@(stack-lto--question-answer data))) + +(defvar stack-lto-body-src-block + '(:language "markdown" :switches nil :parameters nil :hiddenp nil) + "Properties used on the src-block which represents the body.") + +(defun stack-lto--question-answer (data) + "Process and return the elements of DATA which questions and answers have in common." + `((property-drawer nil ,(stack-lto--properties data)) + (src-block (:value ,(stack-lto--body data) + . ,stack-lto-body-src-block)))) + +(defun stack-lto--body (data) + "Get and cleanup `body_markdown' from DATA." + (concat + (replace-regexp-in-string + "\r\n" "\n" (cdr (assoc 'body_markdown data))) + "\n")) + +(defvar stack-lto-comment-item + '(:bullet "- " :checkbox nil :counter nil :hiddenp nil) + "Properties used on the items which represent comments.") + +(defun stack-lto--comment (data) + "" + (let* ((owner (cdr (assoc 'owner data))) + (owner-name (cdr (assoc 'display_name owner)))) + `(item (:tag ,owner-name . ,stack-lto-comment-item) + (paragraph () ,(cdr (assoc 'body_markdown data)))))) + +(defun stack-lto--properties (data) + "" + nil) + +(provide 'stack-lto) +;;; stack-core.el ends here -- cgit v1.2.3 From a52d9b70bb37e95f72d8699919e5a3881ecbf560 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sat, 1 Nov 2014 09:38:47 +0000 Subject: Add comments to stack-lto --- stack-lto.el | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'stack-lto.el') diff --git a/stack-lto.el b/stack-lto.el index 6d8ea6d..9b7ac67 100644 --- a/stack-lto.el +++ b/stack-lto.el @@ -49,25 +49,30 @@ by the API and read by `json-read'." `(headline (:title "Answer" :level 2) ,@(stack-lto--question-answer data))) -(defvar stack-lto-body-src-block - '(:language "markdown" :switches nil :parameters nil :hiddenp nil) +(defvar stack-lto-body-src-block + '(:language "markdown" :switches nil :parameters nil :hiddenp nil) "Properties used on the src-block which represents the body.") (defun stack-lto--question-answer (data) "Process and return the elements of DATA which questions and answers have in common." - `((property-drawer nil ,(stack-lto--properties data)) - (src-block (:value ,(stack-lto--body data) - . ,stack-lto-body-src-block)))) + (let ((comments + (mapcar #'stack-lto--comment (cdr (assoc 'comments data))))) + `(;; Body as a src block (really NOT nice). + (src-block (:value ,(stack-lto--body data) + . ,stack-lto-body-src-block)) + ;; Comments as descriptive lists. If there are no comments, an + ;; empty list would throw an error. + ,@(when comments `((plain-list (:type descriptive) ,comments)))))) (defun stack-lto--body (data) "Get and cleanup `body_markdown' from DATA." - (concat + (concat (replace-regexp-in-string "\r\n" "\n" (cdr (assoc 'body_markdown data))) "\n")) (defvar stack-lto-comment-item - '(:bullet "- " :checkbox nil :counter nil :hiddenp nil) + '(:bullet "- " :checkbox nil :counter nil :hiddenp nil) "Properties used on the items which represent comments.") (defun stack-lto--comment (data) @@ -77,9 +82,5 @@ by the API and read by `json-read'." `(item (:tag ,owner-name . ,stack-lto-comment-item) (paragraph () ,(cdr (assoc 'body_markdown data)))))) -(defun stack-lto--properties (data) - "" - nil) - (provide 'stack-lto) ;;; stack-core.el ends here -- cgit v1.2.3 From 556202fe458fc8a45425aeaba68c0532988969eb Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sat, 1 Nov 2014 15:47:39 +0000 Subject: Add comments and html renderer --- stack-lto.el | 57 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) (limited to 'stack-lto.el') diff --git a/stack-lto.el b/stack-lto.el index 9b7ac67..776e4bb 100644 --- a/stack-lto.el +++ b/stack-lto.el @@ -27,6 +27,7 @@ ;;; Requirements (require 'stack-core) (require 'json) +(require 'shr) (defun stack-lto--question (data) "Return question DATA in a format acceptable by `org-element-interpret-data'. @@ -37,7 +38,7 @@ by the API and read by `json-read'." :tags ,(mapcar #'identity (cdr (assoc 'tags data)))) (section () (headline (:title "Question" :level 2) - ,@(stack-lto--question-answer data)) + ,(stack-lto--question-answer data)) ,@(mapcar #'stack-lto--answer (cdr (assoc 'answers data)))))) (defun stack-lto--answer (data) @@ -47,23 +48,40 @@ by the API and read by `json-read'." ;; Right now this doesn't do anything special. But it should check ;; whether the answer is accepted. How do we display that? `(headline (:title "Answer" :level 2) - ,@(stack-lto--question-answer data))) - -(defvar stack-lto-body-src-block - '(:language "markdown" :switches nil :parameters nil :hiddenp nil) - "Properties used on the src-block which represents the body.") + ,(stack-lto--question-answer data))) (defun stack-lto--question-answer (data) "Process and return the elements of DATA which questions and answers have in common." (let ((comments (mapcar #'stack-lto--comment (cdr (assoc 'comments data))))) - `(;; Body as a src block (really NOT nice). - (src-block (:value ,(stack-lto--body data) - . ,stack-lto-body-src-block)) + `(,(if stack-lto-body-src-block + ;; Body as a src block (really NOT nice). + `(src-block (:value ,(stack-lto--body data) + . ,stack-lto-body-src-block)) + ;; Body as html. Nicer... + (list 'paragraph () (stack-lto--body-rendered data))) ;; Comments as descriptive lists. If there are no comments, an ;; empty list would throw an error. ,@(when comments `((plain-list (:type descriptive) ,comments)))))) + +;;; Body rendering +(defvar stack-lto-body-src-block + '(:language "markdown" :switches nil :parameters nil :hiddenp nil) + "Properties used on the src-block which represents the body. +If this is nil, rendered html is used for the body instead.") + +(defface stack-lto-body + '((((background light)) :background "Grey90") + (((background dark)) :background "Grey10")) + "Face used on the body content of questions and answers." + :group 'stack-mode-faces) + +(defcustom stack-lto-bullet (if (char-displayable-p ?•) " •" " -") + "Bullet used on the display of html lists." + :type 'string + :group 'stack-mode) + (defun stack-lto--body (data) "Get and cleanup `body_markdown' from DATA." (concat @@ -71,6 +89,27 @@ by the API and read by `json-read'." "\r\n" "\n" (cdr (assoc 'body_markdown data))) "\n")) +;; We need to add padding in case the body contains a * at column 1 +;; (which would break org-mode). +(defvar stack-lto--padding + (propertize "  " 'display " ") + "Left-padding added to each line of a body.") + +(defun stack-lto--body-rendered (data) + "Get and cleanup `body' from DATA. +Render it with `shr-render-region'." + (propertize + (with-temp-buffer + (insert (cdr (assoc 'body data))) + (let ((shr-bullet stack-lto-bullet)) + (shr-render-region (point-min) (point-max))) + (goto-char (point-min)) + (while (null (eobp)) + (insert stack-lto--padding) + (forward-line 1)) + (buffer-string)) + 'font-lock-face 'stack-lto-body)) + (defvar stack-lto-comment-item '(:bullet "- " :checkbox nil :counter nil :hiddenp nil) "Properties used on the items which represent comments.") -- cgit v1.2.3 From 5b61e791b0e68ec1ab045bba391b4b4d2054e006 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 3 Nov 2014 01:52:12 +0000 Subject: Rename a varialbe --- stack-lto.el | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'stack-lto.el') diff --git a/stack-lto.el b/stack-lto.el index 776e4bb..c44ad9a 100644 --- a/stack-lto.el +++ b/stack-lto.el @@ -54,10 +54,10 @@ by the API and read by `json-read'." "Process and return the elements of DATA which questions and answers have in common." (let ((comments (mapcar #'stack-lto--comment (cdr (assoc 'comments data))))) - `(,(if stack-lto-body-src-block + `(,(if stack-lto--body-src-block ;; Body as a src block (really NOT nice). `(src-block (:value ,(stack-lto--body data) - . ,stack-lto-body-src-block)) + . ,stack-lto--body-src-block)) ;; Body as html. Nicer... (list 'paragraph () (stack-lto--body-rendered data))) ;; Comments as descriptive lists. If there are no comments, an @@ -66,9 +66,9 @@ by the API and read by `json-read'." ;;; Body rendering -(defvar stack-lto-body-src-block +(defvar stack-lto--body-src-block '(:language "markdown" :switches nil :parameters nil :hiddenp nil) - "Properties used on the src-block which represents the body. + "Properties used on the markdown src-block which represents the body. If this is nil, rendered html is used for the body instead.") (defface stack-lto-body -- cgit v1.2.3 From 96be4647af62f9dd6e85a6f9025527f5f69a6a22 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 4 Nov 2014 13:47:28 +0000 Subject: require 'org --- stack-lto.el | 1 + 1 file changed, 1 insertion(+) (limited to 'stack-lto.el') diff --git a/stack-lto.el b/stack-lto.el index c44ad9a..45871a0 100644 --- a/stack-lto.el +++ b/stack-lto.el @@ -28,6 +28,7 @@ (require 'stack-core) (require 'json) (require 'shr) +(require 'org) (defun stack-lto--question (data) "Return question DATA in a format acceptable by `org-element-interpret-data'. -- cgit v1.2.3 From d790ae03a10086098ab1038e5008db7488facd95 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 4 Nov 2014 15:40:40 +0000 Subject: Abolish the HTML renderer. To recover its code, see the tag old-html-renderer. --- stack-lto.el | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) (limited to 'stack-lto.el') diff --git a/stack-lto.el b/stack-lto.el index 45871a0..af1be9f 100644 --- a/stack-lto.el +++ b/stack-lto.el @@ -27,7 +27,6 @@ ;;; Requirements (require 'stack-core) (require 'json) -(require 'shr) (require 'org) (defun stack-lto--question (data) @@ -37,10 +36,8 @@ by the API and read by `json-read'." `(headline (:title ,(cdr (assoc 'title data)) :level 1 :tags ,(mapcar #'identity (cdr (assoc 'tags data)))) - (section () - (headline (:title "Question" :level 2) - ,(stack-lto--question-answer data)) - ,@(mapcar #'stack-lto--answer (cdr (assoc 'answers data)))))) + ,(stack-lto--question-answer data) + ,@(mapcar #'stack-lto--answer (cdr (assoc 'answers data))))) (defun stack-lto--answer (data) "Return answer DATA in a format acceptable by `org-element-interpret-data'. @@ -55,12 +52,9 @@ by the API and read by `json-read'." "Process and return the elements of DATA which questions and answers have in common." (let ((comments (mapcar #'stack-lto--comment (cdr (assoc 'comments data))))) - `(,(if stack-lto--body-src-block - ;; Body as a src block (really NOT nice). - `(src-block (:value ,(stack-lto--body data) - . ,stack-lto--body-src-block)) - ;; Body as html. Nicer... - (list 'paragraph () (stack-lto--body-rendered data))) + `(;; Body as a src block (really NOT nice). + (src-block (:value ,(stack-lto--body data) + . ,stack-lto--body-src-block)) ;; Comments as descriptive lists. If there are no comments, an ;; empty list would throw an error. ,@(when comments `((plain-list (:type descriptive) ,comments)))))) @@ -69,8 +63,7 @@ by the API and read by `json-read'." ;;; Body rendering (defvar stack-lto--body-src-block '(:language "markdown" :switches nil :parameters nil :hiddenp nil) - "Properties used on the markdown src-block which represents the body. -If this is nil, rendered html is used for the body instead.") + "Properties used on the markdown src-block which represents the body.") (defface stack-lto-body '((((background light)) :background "Grey90") @@ -78,8 +71,10 @@ If this is nil, rendered html is used for the body instead.") "Face used on the body content of questions and answers." :group 'stack-mode-faces) +;;; This is not used ATM since we got rid of HTML. But it can be used +;;; once we start extending markdown mode. (defcustom stack-lto-bullet (if (char-displayable-p ?•) " •" " -") - "Bullet used on the display of html lists." + "Bullet used on the display of lists." :type 'string :group 'stack-mode) @@ -96,21 +91,6 @@ If this is nil, rendered html is used for the body instead.") (propertize "  " 'display " ") "Left-padding added to each line of a body.") -(defun stack-lto--body-rendered (data) - "Get and cleanup `body' from DATA. -Render it with `shr-render-region'." - (propertize - (with-temp-buffer - (insert (cdr (assoc 'body data))) - (let ((shr-bullet stack-lto-bullet)) - (shr-render-region (point-min) (point-max))) - (goto-char (point-min)) - (while (null (eobp)) - (insert stack-lto--padding) - (forward-line 1)) - (buffer-string)) - 'font-lock-face 'stack-lto-body)) - (defvar stack-lto-comment-item '(:bullet "- " :checkbox nil :counter nil :hiddenp nil) "Properties used on the items which represent comments.") -- cgit v1.2.3