help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Need best way to pre-process text with Emacs Lisp


From: Jean Louis
Subject: Need best way to pre-process text with Emacs Lisp
Date: Thu, 22 Apr 2021 13:38:27 +0300

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 via email to

[Prev in Thread] Current Thread [Next in Thread]