Golf - シンプルなテンプレート スキームを実装します。
展開は次のとおりです。
- %KEY% -> 値
- %% -> %
コマンドライン引数:
key=value
ARG1:例のようなスタイルでフォーマットされた辞書ファイル- ARG2: テンプレートファイル
ここで私のあまりゴルフの試み(python):261文字。
import sys
dd = dict([ll.split("=",2) for ll in open( sys.argv[1],'r') if len(ll.split("=", 2)) == 2])
tt = "".join([ ll for ll in open( sys.argv[2],'r')])
sys.stdout.write("".join([(((s == "") and "%") or ((s in dd) and dd[s]) or s) for s in tt.split("%")]))
DICT
NAME=MyName
ODDS=100
テンプレート
I, %NAME% am %ODDS% %% sure that that this a waste of time.
結果
I, My Name am 100 % sure that this is a waste of time.
はい、これは欠陥のあるテンプレートシステムであり、より短くより良い実装のために「スナップ」していることを認識しています。