loading...

First post, picolisp script

cess11 profile image PNS11 Updated on ・1 min read

Today I made a oneliner for converting a set of ISO-coded XML files in the current directory to UTF-8.

(for X (dir) (and (match '(@A "." "x" "m" "l") (chop X))(and (out "sh.sh" (prinl "#! /bin/sh"))(out "+sh.sh" (prinl (pack "iconv -f ISO-8859-1 -t UTF-8 " X " > utf8_" X)))(call 'sh "sh.sh"]

A more readable, commented version could look like this:

(for X (dir) # for every file name in this directory
    (and 
        (match '(@A "." "x" "m" "l") (chop X)) # check if *.xml
        (and # it is or this is skipped, so...
            (out "sh.sh" # we write a new file
                (prinl "#! /bin/sh")) # with a hashbang 
            (out "+sh.sh" # and then add an instruction
                (prinl 
                    (pack "iconv -f ISO-8859-1 -t UTF-8 " X " > utf8_" X)))
            (call 'sh "sh.sh"] # and finally run it 

Now this isn't very lispy, but it can quickly be turned into a more functional style.

(de qconv (Lst) # '(FromCode ToCode OutPrefix InSuffix FileList) 
    (for X (last Lst)
        (and 
            (match '(@A ~(chop (cadddr Lst))) (chop X))
            (and 
                (out "sh.sh" 
                    (prinl "#! /bin/sh"))
                (out "+sh.sh" 
                    (prinl 
                        (pack "iconv -f " (car Lst) "-t " (cadr Lst) " " X " > " (caddr Lst) X)))
                )))
    (call 'rm "sh.sh"]

Still quite ugly though.

Posted on by:

cess11 profile

PNS11

@cess11

Yeah, well, lisps and then some.

Discussion

markdown guide
 

Have you found a way to make it nicer yet ?