This commit is contained in:
Justin Abrahms 2021-08-13 15:09:02 +00:00
parent 1e4cabf98a
commit 7897f8eb84

View File

@ -39,20 +39,20 @@
:menu-entry :menu-entry
'(?g "Export to Gemini" '(?g "Export to Gemini"
((?b "To buffer" ((?b "To buffer"
(lambda (a s v b) (lambda (a s v b)
(org-gemini-export-to-buffer a s v b nil))) (org-gemini-export-to-buffer a s v b nil)))
(?f "To file" (?f "To file"
(lambda (a s v b) (lambda (a s v b)
(org-gemini-export-to-file a s v b nil))))) (org-gemini-export-to-file a s v b nil)))))
:translate-alist '((code . org-gemini-code-inline) :translate-alist '((code . org-gemini-code-inline)
(paragraph . org-gemini-paragraph) (paragraph . org-gemini-paragraph)
(headline . org-gemini-headline) (headline . org-gemini-headline)
(link . org-gemini-link) (link . org-gemini-link)
(section . org-gemini-section) (section . org-gemini-section)
(src-block . org-gemini-code-block) (src-block . org-gemini-code-block)
(item . org-gemini-item) (item . org-gemini-item)
(quote-block . org-gemini-quote-block) (quote-block . org-gemini-quote-block)
(template . org-gemini-template))) (template . org-gemini-template)))
(defun org-gemini-paragraph (_paragraph contents _info) (defun org-gemini-paragraph (_paragraph contents _info)
"CONTENTS is the text of the paragraph." "CONTENTS is the text of the paragraph."
@ -76,7 +76,7 @@
"EXAMPLE-BLOCK is a codeblock. INFO is a plist." "EXAMPLE-BLOCK is a codeblock. INFO is a plist."
(org-remove-indentation (org-remove-indentation
(format "```\n%s```" (format "```\n%s```"
(org-export-format-code-default example-block info)))) (org-export-format-code-default example-block info))))
(defun org-gemini--describe-links (links _width info) (defun org-gemini--describe-links (links _width info)
"Describe links is the footer-portion of the link data. "Describe links is the footer-portion of the link data.
@ -92,10 +92,10 @@ It's output just before each section. LINKS is a list of each link. INFO is a
(path (if is-org-file-link (path (if is-org-file-link
(concat (file-name-sans-extension (org-element-property :path link)) ".gmi") (concat (file-name-sans-extension (org-element-property :path link)) ".gmi")
raw-path)) raw-path))
(desc (org-element-contents link)) (desc (org-element-contents link))
(anchor (org-export-data (anchor (org-export-data
(or desc (org-element-property :raw-link link)) (or desc (org-element-property :raw-link link))
info))) info)))
(format "=> %s %s\n" path anchor))) (format "=> %s %s\n" path anchor)))
links "") links "")
(when (car links) (when (car links)
@ -118,20 +118,20 @@ org-gemini--describe-links"
CONTENTS is the contents of the section. INFO is a plist holding CONTENTS is the contents of the section. INFO is a plist holding
contextual information." contextual information."
(let ((links (let ((links
(and (plist-get info :ascii-links-to-notes) (and (plist-get info :ascii-links-to-notes)
;; Take care of links in first section of the document. ;; Take care of links in first section of the document.
(not (org-element-lineage section '(headline))) (not (org-element-lineage section '(headline)))
(org-gemini--describe-links (org-gemini--describe-links
(org-ascii--unique-links section info) (org-ascii--unique-links section info)
(org-ascii--current-text-width section info) (org-ascii--current-text-width section info)
info)))) info))))
(org-remove-indentation (org-remove-indentation
(if (not (org-string-nw-p links)) contents (if (not (org-string-nw-p links)) contents
(concat (org-element-normalize-string contents) "\n\n" links)) (concat (org-element-normalize-string contents) "\n\n" links))
;; Do not apply inner margin if parent headline is low level. ;; Do not apply inner margin if parent headline is low level.
(let ((headline (org-export-get-parent-headline section))) (let ((headline (org-export-get-parent-headline section)))
(if (or (not headline) (org-export-low-level-p headline info)) 0 (if (or (not headline) (org-export-low-level-p headline info)) 0
(plist-get info :ascii-inner-margin)))))) (plist-get info :ascii-inner-margin))))))
(defun org-gemini--build-title (defun org-gemini--build-title
(element info _text-width &optional _underline _notags toc) (element info _text-width &optional _underline _notags toc)
@ -139,13 +139,13 @@ contextual information."
ELEMENT is an org-element. TOC is whether to show the table of contents. INFO is unimportant." ELEMENT is an org-element. TOC is whether to show the table of contents. INFO is unimportant."
(let ((number (org-element-property :level element)) (let ((number (org-element-property :level element))
(text (text
(org-trim (org-trim
(org-export-data (org-export-data
(if toc (if toc
(org-export-get-alt-title element info) (org-export-get-alt-title element info)
(org-element-property :title element)) (org-element-property :title element))
info)))) info))))
(format "%s %s" (make-string number ?#) text))) (format "%s %s" (make-string number ?#) text)))
@ -158,49 +158,49 @@ holding contextual information."
;; of the template. ;; of the template.
(unless (org-element-property :footnote-section-p headline) (unless (org-element-property :footnote-section-p headline)
(let* ((low-level (org-export-low-level-p headline info)) (let* ((low-level (org-export-low-level-p headline info))
(width (org-ascii--current-text-width headline info)) (width (org-ascii--current-text-width headline info))
;; Export title early so that any link in it can be ;; Export title early so that any link in it can be
;; exported and seen in `org-ascii--unique-links'. ;; exported and seen in `org-ascii--unique-links'.
(title (org-gemini--build-title headline info width (not low-level))) (title (org-gemini--build-title headline info width (not low-level)))
;; Blank lines between headline and its contents. ;; Blank lines between headline and its contents.
;; `org-ascii-headline-spacing', when set, overwrites ;; `org-ascii-headline-spacing', when set, overwrites
;; original buffer's spacing. ;; original buffer's spacing.
(pre-blanks (pre-blanks
(make-string (or (car (plist-get info :ascii-headline-spacing)) (make-string (or (car (plist-get info :ascii-headline-spacing))
(org-element-property :pre-blank headline) (org-element-property :pre-blank headline)
0) 0)
?\n)) ?\n))
(links (and (plist-get info :ascii-links-to-notes) (links (and (plist-get info :ascii-links-to-notes)
(org-gemini--describe-links (org-gemini--describe-links
(org-ascii--unique-links headline info) width info))) (org-ascii--unique-links headline info) width info)))
;; Re-build contents, inserting section links at the right ;; Re-build contents, inserting section links at the right
;; place. The cost is low since build results are cached. ;; place. The cost is low since build results are cached.
(body (body
(if (not (org-string-nw-p links)) contents (if (not (org-string-nw-p links)) contents
(let* ((contents (org-element-contents headline)) (let* ((contents (org-element-contents headline))
(section (let ((first (car contents))) (section (let ((first (car contents)))
(and (eq (org-element-type first) 'section) (and (eq (org-element-type first) 'section)
first)))) first))))
(concat (and section (concat (and section
(concat (org-element-normalize-string (concat (org-element-normalize-string
(org-export-data section info)) (org-export-data section info))
"\n\n")) "\n\n"))
links links
(mapconcat (lambda (e) (org-export-data e info)) (mapconcat (lambda (e) (org-export-data e info))
(if section (cdr contents) contents) (if section (cdr contents) contents)
"")))))) ""))))))
;; Deep subtree: export it as a list item. ;; Deep subtree: export it as a list item.
(if low-level (if low-level
(let* ((bullets (cdr (assq (plist-get info :ascii-charset) (let* ((bullets (cdr (assq (plist-get info :ascii-charset)
(plist-get info :ascii-bullets)))) (plist-get info :ascii-bullets))))
(bullet (bullet
(format "%c " (format "%c "
(nth (mod (1- low-level) (length bullets)) bullets)))) (nth (mod (1- low-level) (length bullets)) bullets))))
(concat bullet title "\n" pre-blanks (concat bullet title "\n" pre-blanks
;; Contents, indented by length of bullet. ;; Contents, indented by length of bullet.
(org-ascii--indent-string body (length bullet)))) (org-ascii--indent-string body (length bullet))))
;; Else: Standard headline. ;; Else: Standard headline.
(concat title "\n" pre-blanks body))))) (concat title "\n" pre-blanks body)))))
(defun org-gemini-template (contents info) (defun org-gemini-template (contents info)
"Return complete document string after GEMINI conversion. "Return complete document string after GEMINI conversion.