trivial-string-template

2016-10-31
Travis-CI Status Coverage Status

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

Usage

substitute

(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?"

safe-substitute

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

template

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"

define-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"

TODO TO DO List [66%]

  • [X] Provide a more detailed APIs documenatation.
  • [X] More tests.
  • [ ] Improvements.

Author

  • David Gu (macdavid313@gmail.com)

License

Licensed under the MIT License.

Author
David Gu
License
MIT