emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Max Nikulin <manikulin@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Re: Raw Org AST snippets for "impossible" markup
Date: Mon, 3 Jan 2022 21:34:16 +0700	[thread overview]
Message-ID: <sqv1ha$s5g$1@ciao.gmane.io> (raw)
In-Reply-To: <87y24txvlv.fsf@posteo.net>

[-- Attachment #1: Type: text/plain, Size: 1899 bytes --]

On 10/12/2021 05:27, Juan Manuel Macías wrote:
> Juan Manuel Macías writes:
> 
>> Jumping into the "real world", how about these two examples of nested emphasis?
> 
> By the way, what do you think about allowing the use of some kind of
> aliases, so that the aspect is less verbose?

I have no particular opinion concerning aliases, but certainly they 
should not work through string search and replace when parsed tree is 
available.

>    (defun orgia--transform-path (path)
>      (with-temp-buffer
>        (insert path)
>        (mapc (lambda (el)
> 	      (orgia-replace (concat "(" (car el) "::") (concat "(" (cadr el) " () ")))

By the way, is there any problem with `replace-regexp-in-string'?

See the attached file for definitions of some helper functions. Final setup:

#+begin_src elisp :results silent
   (setq orgia-demo-alias-alist
	'((b . bold)
	  (i . italic)
	  (s . strike-through)
	  (_ . underline)))

   (defun orgia-demo-alias-post-filter (node &optional _children)
     (when (listp node)
       (let ((sym (and (symbolp (car node))
		      (assq (car node) orgia-demo-alias-alist))))
	(when sym
	  (setcar node (cdr sym)))))
     node)

   (defun orgia-demo-alias (tree)
     (orgia-transform-tree-deep tree nil #'orgia-demo-alias-post-filter))
#+end_src

#+begin_src elisp :results silent
   (require 'ox)
   (add-to-list 'org-export-filter-parse-tree-functions 
#'orgia-parse-tree-filter)
   (org-link-set-parameters "orgia")

   (require 'ob-org)
   (add-to-list 'orgia-transform-functions #'orgia-demo-alias)
#+end_src

And a bit modified your test sample:

#+begin_src org :results latex :results replace
   [[orgia:(i nil "The English versions of the " (b nil (i () "Iliad")) 
" and the " (b () (i ()
   "Odyssey")))]]
#+end_src

#+RESULTS:
#+begin_export latex
\emph{The English versions of the \textbf{\emph{Iliad}} and the 
\textbf{\emph{Odyssey}}}
#+end_export

[-- Attachment #2: orgia-draft.el --]
[-- Type: text/x-emacs-lisp, Size: 2080 bytes --]

(defvar orgia-transform-functions nil)

(defun orgia-default-pre-filter (node)
  "Returns (node . children)"
  (if (listp node)
      (cons node node)
    (cons node nil)))

(defun orgia-transform-tree-deep (tree &optional pre-filter post-filter)
  "Deep-first walk."
  ;; Queue items: ((node-cell . children) . next-list)
  (let* ((pre-filter (or pre-filter #'orgia-default-pre-filter))
	 (top (list tree))
	 (queue (list (cons (cons top top) top))))
    (while queue
      (let* ((item (pop queue))
	     (next-list (cdr item)))
	(if (not next-list)
	    ;; post; skip POST-FILTER for the list wrapping TREE
	    (when (and queue post-filter)
	      (let* ((node-cell-children (car item))
		     (children (cdr node-cell-children)))
		(setcar (car node-cell-children)
			(funcall post-filter
				 (caar node-cell-children)
				 children))))
	  ;; pre
	  (setcdr item (cdr next-list))
	  (push item queue)
	  (let* ((node-children
		  (funcall pre-filter (car next-list)))
		 (node (car node-children))
		 (children (cdr node-children)))
		(setcar next-list node)
		(push (cons (cons next-list children) children) queue)))))
    (car top)))

(defun orgia-element-replace (current new destructive?)
  (if (eq current new)
      current
    (let* ((lst? (and (listp new) (not (symbolp (car new)))))
	   (new-lst (if lst?
			(if destructive? (nconc new) (reverse new))
		      (list new))))
      (dolist (element new-lst)
	(org-element-insert-before element current)))
    (org-element-extract-element current)
    new))

(defun orgia--transform-link (data)
  (if (not (string-equal "orgia" (org-element-property :type data)))
      data
    (let* ((path (org-element-property :path data)))
      (if (not (eq ?\( (aref path 0)))
	  (or path (org-element-contents data))
	(let ((tree (read path)))
	  (dolist (f orgia-transform-functions tree)
	    (setq tree (funcall f tree))))))))

(defun orgia-parse-tree-filter (data _backend info)
  (org-element-map data 'link
    (lambda (data)
      (orgia-element-replace data (orgia--transform-link data) t))
    info nil nil t)
  data)

  reply	other threads:[~2022-01-03 14:35 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-02 10:50 Org-syntax: Intra-word markup Denis Maier
2021-12-02 11:18 ` Ihor Radchenko
2021-12-02 11:30   ` Juan Manuel Macías
2021-12-02 11:36     ` Denis Maier
2021-12-02 12:01       ` Ihor Radchenko
2021-12-02 11:42     ` Marco Wahl
2021-12-02 11:50       ` Denis Maier
2021-12-02 12:10         ` Ihor Radchenko
2021-12-02 12:40           ` Denis Maier
2021-12-02 12:54             ` Ihor Radchenko
2021-12-02 13:14               ` Juan Manuel Macías
2021-12-02 13:28                 ` Denis Maier
2021-12-02 12:48           ` Max Nikulin
2021-12-02 12:02       ` Ihor Radchenko
2021-12-02 12:00     ` Ihor Radchenko
     [not found]       ` <87r1avtdjy.fsf@ucl.ac.uk>
2021-12-02 12:27         ` Denis Maier
2021-12-02 13:06           ` Eric S Fraga
2021-12-02 12:28       ` Denis Maier
2021-12-02 12:55         ` Ihor Radchenko
2021-12-02 11:58 ` Timothy
2021-12-02 12:26   ` Denis Maier
2021-12-02 13:07     ` Ihor Radchenko
2021-12-02 15:51       ` Max Nikulin
2021-12-02 18:11         ` Tom Gillespie
2021-12-02 19:09           ` Juan Manuel Macías
2021-12-04 13:07             ` Org-syntax: emphasis and not English punctuation Max Nikulin
2021-12-04 16:42               ` Juan Manuel Macías
2021-12-02 20:47           ` Org-syntax: Intra-word markup Denis Maier
2021-12-02 22:44             ` Samuel Wales
2021-12-03 14:53           ` Max Nikulin
2021-12-03 23:51           ` Tim Cross
2021-12-04 15:01             ` Max Nikulin
2021-12-05 23:34               ` Russell Adams
2021-12-05 23:37             ` Russell Adams
2021-12-06  1:39               ` Samuel Wales
2021-12-02 19:03       ` Nicolas Goaziou
2021-12-02 19:34         ` Juan Manuel Macías
2021-12-02 23:05           ` Nicolas Goaziou
2021-12-02 23:24             ` Juan Manuel Macías
2021-12-03 14:24         ` Max Nikulin
2021-12-03 15:01           ` Juan Manuel Macías
2021-12-04 15:57           ` Denis Maier
2021-12-04 17:53             ` Tom Gillespie
2021-12-04 18:37               ` John Kitchin
2021-12-04 21:16                 ` Juan Manuel Macías
2021-12-06 10:57                 ` Raw Org AST snippets for "impossible" markup Max Nikulin
2021-12-06 15:45                   ` Juan Manuel Macías
2021-12-06 16:56                     ` Juan Manuel Macías
2021-12-08 13:09                     ` Max Nikulin
2021-12-08 23:19                       ` Juan Manuel Macías
2021-12-08 23:35                         ` John Kitchin
2021-12-09  7:01                           ` Juan Manuel Macías
2021-12-09 14:56                             ` Max Nikulin
2021-12-09 16:11                               ` Juan Manuel Macías
2021-12-09 22:27                                 ` Juan Manuel Macías
2022-01-03 14:34                                   ` Max Nikulin [this message]
2021-12-04 19:04               ` Org-syntax: Intra-word markup Timothy
2021-12-04 21:48                 ` Tom Gillespie
2021-12-06 10:59                   ` Max Nikulin
2022-01-28 14:52                   ` Max Nikulin
2022-01-29  3:13                     ` Ihor Radchenko
2022-01-29 13:05                       ` Juan Manuel Macías
2022-02-02 15:28                         ` Max Nikulin
2022-02-02 20:01                           ` Juan Manuel Macías
2022-02-03 12:10                             ` Max Nikulin
2021-12-06 11:01               ` Denis Maier
2022-01-28 12:12 ` [PATCH] Intra-word markup: \relax Max Nikulin
2022-01-28 13:13   ` Juan Manuel Macías
2022-02-02 15:42     ` Max Nikulin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='sqv1ha$s5g$1@ciao.gmane.io' \
    --to=manikulin@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).