all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jean Louis <bugs@gnu.support>
To: Help GNU Emacs <help-gnu-emacs@gnu.org>
Subject: Need best way to pre-process text with Emacs Lisp
Date: Thu, 22 Apr 2021 13:38:27 +0300	[thread overview]
Message-ID: <courier.000000006081522E.00006092@stw1.rcdrun.com> (raw)


I would like to get feedback from experienced Emacs Lisp programmers
on what would be best way to pre-process text so that I can expand
Emacs Lisp values in a text. I am moving from Common Lisp to Emacs
Lisp. And I was using CL-EMB Common Lisp package:
http://mtn-host.prjek.net/projects/cl-emb and it served me well, but I
wish to switch all 4057 pages to use Emacs Lisp instead of Common
Lisp. That way I can move the WRS or Website Revision System to Emacs
for easier management.

My purpose is to make HTML and other template expansions easier and
faster while in same time using Emacs Lisp for processing.

This represents some kind of minimal template:
----------------------------------------------
(setq string "<html>
⦑ (+ 2 2) ⦒

⦑ title ⦒ and hash value ⦑ (gethash 'title hash) ⦒

")

This is the function, maybe I can improve the function with help of
mailing list members:
---------------------
(defun template-eval (string hash)
  (let ((open "⦑")
	(close "⦒"))
    (with-temp-buffer
      (insert string)
      (goto-char 0)
      (while (re-search-forward (rx (literal open)
				    (one-or-more (or blank "\n"))
				    (group (minimal-match (one-or-more anything)))
				    (one-or-more (or blank "\n"))
				    (literal close))
				nil t)
	(let ((value (eval
		      (condition-case nil              ;; I don't know
                                                       ;; how to
                                                       ;; format this
			  (eval
			   (car
			    (read-from-string
			     (buffer-substring-no-properties
			      (1+ (match-beginning 0)) (1- (match-end 0))))))
			(error "")))))
	  (delete-region (match-beginning 0) (match-end 0))
	  (insert (format "%s" value))))
      (buffer-string))))

This is example of how it should work:
--------------------------------------
(setq h (make-hash-table)) ;; we make hash
(puthash 'title "MY TITLE" h) ;; some hash key/value

and functiona evaluates it so far correctly:

(template-eval string h) ⇒ "<html>
4

 and hash value MY TITLE

"

Does anybody see something wrong with this function?

Any suggestions for improvements for the regular expression matching?

Any suggestions for speed optimization?

Strings will be handled that arrive from database, thus none will have
some properties. I am interested in speed as function is invoked
thousands of times when generating HTML pages usually.

This will allow me to use it as Emacs Lisp pre-processor for any kind
of markup language, like Markdown, including Org, restructured text
and similar. I could then expand any text with Emacs Lisp embedded
such as that 2 plus 2 equals⦑ (+ 2 2) ⦒ -- this would be expanded
before processing by let us say Markdown.

I did use this method for many years by using Perl, then Common Lisp
and now I wish to switch all of the WRS or Website Revision System to
Emacs Lisp for processing.

Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/
https://rms-support-letter.github.io/





                 reply	other threads:[~2021-04-22 10:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

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

  git send-email \
    --in-reply-to=courier.000000006081522E.00006092@stw1.rcdrun.com \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.