From c610b461895d6b15079958900b267e2d15738558 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sun, 10 Apr 2022 23:54:29 +0200 Subject: [PATCH] Add ox-conlang-tsx.el Preliminary work for now --- .gitignore | 14 +++++ ox-conlang-tsx.el | 150 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 .gitignore create mode 100644 ox-conlang-tsx.el diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b3736c --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# Compiled +*.elc + +# Packaging +.cask + +# Backup files +*~ + +# Undo-tree save-files +*.~undo-tree + +# Flycheck +flycheck_* \ No newline at end of file diff --git a/ox-conlang-tsx.el b/ox-conlang-tsx.el new file mode 100644 index 0000000..4356548 --- /dev/null +++ b/ox-conlang-tsx.el @@ -0,0 +1,150 @@ +;;; ox-conlang-tsx.el --- NextJS exporter for org-mode -*- lexical-binding: t -*- + +;; Author: Lucien Cartier-Tilet +;; Maintainer: Lucien Cartier-Tilet +;; Version: 0.1.0 +;; Package-Requires: ((emacs "26.1") (org "9.5")) +;; Homepage: https://labs.phundrak.com/phundrak/ox-conlang-tsx.el +;; Keywords: outlines + + +;; 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 . + + +;;; Commentary: + +;; This package exports org files and buffers to a TSX format meant to +;; be used by NextJS (https://nextjs.org/). + +;;; Code: + +(require 'ox-publish) +(require 'ox-html) + +(org-export-define-derived-backend 'conlang-tsx 'html + :menu-entry + '(?C "Conlang export" + ((?T "To temporary buffer" + (lambda (a s v _) (org-conlang-tsx-export-as-tsx a s v))) + (?t "To TSX file" + (lambda (a s v _) (org-conlang-tsx-export-to-tsx a s v))) + (?o "To TSX file and open" + (lambda (a s v b) + (if a (org-conlang-tsx-export-to-tsx t s v) + (org-open-file (org-conlang-tsx-export-to-tsx nil s v))))))) + + :options-alist + '((:conlang-logo "LOGO" nil nil t)) + + :translate-alist + '((link . org-conlang-tsx-link))) + + + +;; Transcode Functions + +;; Links +(defun org-conlang-tsx-link (link desc info) + "Transcode a LINK object from Org to TSX. +The link object is described by DESC and INFO." + (let* ((html (org-html-link link desc info)) + (href-regex "href=\"\\([^\"]+\\)") + (href (save-match-data + (string-match href-regex html) + (substring html (match-beginning 1) (match-end 1)))) + (a-link (replace-regexp-in-string " *href=\"[^\"]+\"" "" html))) + (format "%s" href a-link))) + + + +;;; Interactive Functions + +;;;###autoload +(defun org-conlang-tsx-export-as-tsx (&optional async subtreep visible-only) + "Export current buffer to a Conlang flavored TSX buffer. + +If narrowing is active in the current buffer, only export its +narrowed part. + +If a region is active, export that region. + +A non-nil optional argument ASYNC means the process should happen +asynchronously. The resulting buffer should be accessible through +the `org-export-stack' interface. + +When optional argument SUBTREEP is non-nil, export the sub-tree +at point, extracting information from the headline properties +first. + +When optional argument VISIBLE-ONLY is non-nil, don't export +contents of hidden elements. + +Export is done in a buffer named \"*Org Conlang TSX Export*\", +which will be displayed when +`org-export-show-temporary-export-buffer' is non-nil." + (interactive) + (org-export-to-buffer 'conlang-tsx "*Org Conlang TSX Export*" + async subtreep visible-only nil nil (lambda () (text-mode)))) + + +;;;###autoload +(defun org-conlang-tsx-convert-region-to-tsx () + "Assume the current region has org-mode syntax, and convert it to Conlang flavored TSX. + +This can be used in any buffer. For example, you can write an +itemized list in org-mode syntax in a TSX buffer and use +this command to convert it." + (interactive) + (org-export-replace-region-by 'conlang-tsx)) + + +;;;###autoload +(defun org-conlang-tsx-export-to-tsx (&optional async subtreep visible-only ext-plist) + "Export current buffer to a Conlang flavored TSX file. + +If narrowing is active in the current buffer, only export its +narrowed part. + +If a region is active, export that region. + +A non-nil optional argument ASYNC means the process should happen +asynchronously. The resulting file should be accessible through +the `org-export-stack' interface. + +When optional argument SUBTREEP is non-nil, export the sub-tree +at point, extracting information from the headline properties +first. + +When optional argument VISIBLE-ONLY is non-nil, don't export +contents of hidden elements. + +Return output file's name." + (interactive) + (let* ((outfile (org-export-output-file-name ".tsx" subtreep))) + (org-export-to-file 'conlang-tsx outfile async subtreep visible-only))) + +;;;###autoload +(defun org-conlang-tsx-publish-to-tsx (plist filename pub-dir) + "Publish an org file to Conlang TSX. +FILENAME is the filename of the Org file to be published. PLIST +is the property list for the given project. PUB-DIR is the +publishing directory. +Return output file name." + (org-publish-org-to 'conlang-tsx filename ".tsx" plist pub-dir)) + +(provide 'ox-conlang-tsx) + +;;; ox-conlang-tsx.el ends here