ちょっと入出力の練習
連投です.
某H社のtex記法がアレなのでGoogle Chart APIを使っているのですが,
直に打つのは面倒なので,latexの$$←こいつで挟んだ記法からGoogle Chartのイメージを取ってくるhtmlに変換するスクリプヨを書いていたわけです.
まあgauche使います.
いい入出力の練習です.
#!/usr/bin/env gosh ;; google chart に変換 (define (convert-tex port) ;convert-tex :: <port> -> <string>(portから変換後の文字列を出します) (define (foo in-port ls mode) ;modeは$$で挟まれた中にstreamが居るのかを判定しています.一個一個steamからcharを取り出してloopさせます (let ([ch (read-char in-port)]) (define (dispatch!) (cond ((and (eq? ch #\$) mode) (begin (set! ls (cons "<img src=\"http://chart.apis.google.com/chart?cht=tx&chf=bg,s,ffffff&chco=0000ff&chl=" ls)) (set! mode (not mode)))) ((and (eq? ch #\$) (not mode)) (begin (set! ls (cons "\"/>" ls)) (set! mode (not mode)))) (else (set! ls (cons (make-string 1 ch) ls))))) (if (eof-object? ch) ls (begin (dispatch!) (foo in-port ls mode))))) (string-join (reverse (foo port '() #t)) "")) (define (open->convert->write filename) ;fileをopenしてconvertしたものを<filename>-convertedという名前のfileに書き出します (let ([converted-txt (call-with-input-file filename convert-tex)]) (call-with-output-file (string-append filename "-converted") (lambda (oport) (display converted-txt oport) )))) (define (main args) ; mainループ.第一引数はデフォルトで"この"filenameになっているので捨てるのを忘れない (if (null? (cdr args)) (format #t "Convert done!\n") (let ([filename (cadr args)] [rest-args (cdr args)]) (open->convert->write filename) (main rest-args))))
取り合えず,hatena.scmとか名前付けて,diary.txtにtexのソース入れて
./hatena.scm diary.txt
するとコンバートされたのがdiary.txt-convertedに入ります.
昨日の記事はこうやって書きました.
背景とか文字色とか変えたい場合は適宜なんかいじってくらさい.
byebye