Skip to content

Tips And Tricks

Mogens Lund edited this page Jul 31, 2017 · 1 revision

Some small snippets to extend and improve the behavior of Liquid. And as inspiration.

Adding all editor functions as typeahead

Add the code snippet below to your .liq file. Restart Liquid or just evaluate (e) the snippet. This will enable typeahead search for editor functions like (editor/end-of-line) to be inserted into the editor. That will be useful when editing the .liq or makeing extensions to Liquid.

(doseq [n (keys (ns-publics 'dk.salza.liq.editor))]
  (editor/add-snippet (str "(editor/" n ")")))

Pipe-like threading

The namespace dk.salza.liq.tools.cshell contains different functions for doing operations similar to ls and grep. Most of them takes a parameters and a string or parameters and a sequence. This allows sequencing the commands in a sort of pipeline using the ->> macro. Here is an example:

ls takes a folder as string and returns a sequence of subfolders and files.
rex takes a regular expression and a sequence of string and filters those using the regular expression.
p just prints a sequence of strings one row at the time. So to print the files and folders below "/tmp" containing "abc" evaluate the following:

(->> "/tmp" ls (rex "abc") p)

frex will filter a list of files names based on regular expression search inside the files.
flrex will filter a list of files names based on regular expression search inside the files and produce a sequence of pairs [filename matching-line].
lsr is like ls, but traverses the whole tree recursively.

Use inline blob of data inside code

Suppose you have some nicely formatted data that is human readable, but you want to manipulate it inside a markdown document. Add the function below to your .liq file or just evaluate it to load it into liquid:

(defn blob
  [keyw]
  (or (second
        (str/split
          (editor/get-content)
          (re-pattern (str "\n\\:" (name keyw) "\n?"))))
      ""))

Write a document with the following content:

This is a markdown document. To get a comma separated list of emails (`e`) evaluate the this code:

(str/join "," (re-seq #"\S+@\S+" (blob :emaillist)))

Data below is nicely listed and can be put at the end of the file or whereever, to get out of the way. The part to retrieve is tagget with the keyword :emaillist.

You can also get a list of persons by evaluating: (re-seq #"[^\n]+(?=:)" (blob :emaillist))

:emaillist
Peter Foo: [email protected]
Allan Bar: [email protected]
Anna James: [email protected]
:emaillist

Go have fun!