trivial-string-template

2020-12-20

A trivial string template library, inspired by Python's string.Template

Upstream URL

github.com/macdavid313/trivial-string-template

Author

Tianyu Gu, David Gu

License

MIT
README
Trivial-String-TemplateTravis-CI Status Coverage Status

A trivial string template library, inspired by Python's string.Template; you can check around the Python's functionality here.

1Usage

1.1substitute

(substitute "$who likes $what" :who "tim" :what "kung pao")
;; => "tim likes kung pao"

You can specify the `delimiter` character by lexcially binding the special variable `\*delimiter\*`:

(let ((*delimiter* #\%))
  (substitute "%who likes %what" :who "tim" :what "kung pao"))
;; => "tim likes kung pao"

You can escape a `delimiter` character:

(substitute "Give $who $$100" :who "tim")
;; => "Give tim $100"

And you can use '{}' to set a so called `placeholder`:

(substitute "$who likes ${what}--is it for real?" :who "tim" :what "kung pao")
;; => "tim likes kung pao--is it for real?"

1.2safe-substitute

(safe-substitute "$who likes $what" :what "kung pao")
;; => "$who likes kung pao"

1.3template

It will produce a template class, which is funcallable. Some accessors methods are exported, by which you can modify the state of a template and don't you worry about any confilcts because every accessor will automatically adjust and then make itself consistent.

(defvar *tmpl* (template "$who likes $what"))
(funcall *tmpl* :who "a" :what "b")
;; => "a likes b"
(setf (source-string *tmpl*) "Give $who $$100")
(funcall *tmpl* :who "me")
;; => "Give me $100"

1.4define-template

Just a wrapper around template class.

(define-template tmpl (:delimiter #\& :safe t) "&who likes &what")
;; => #<TRIVIAL-STRING-TEMPLATE:TEMPLATE {1003B1FC7B}>
(tmpl :who tim) ;; => "tim likes &what"

2TODOTO DO List [66%]

  • Provide a more detailed APIs documenatation.
  • More tests.
  • Improvements.

3Author

  • David Gu (macdavid313@gmail.com)

4License

Licensed under the MIT License.

Dependencies (5)

  • alexandria
  • closer-mop
  • cl-ppcre
  • proc-parse
  • prove

Dependents (0)

    • GitHub
    • Quicklisp