Created
June 9, 2010 00:34
-
-
Save osima/430860 to your computer and use it in GitHub Desktop.
ブログエントリーに作成日マクロを追加
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// {cdate:yyyy-mm-dd} 形式のタイムスタンプを必要なら追加する. | |
// | |
class BlogUtils { | |
static def ENC = 'UTF-8' | |
boolean hasCdate( File f ){ | |
boolean found=false | |
if( f && f.exists() ){ | |
f.newReader(ENC).eachLine{ line-> | |
(line =~ /^\{cdate:/).find() ? found=true : null | |
} | |
} | |
found | |
} | |
String getCdateString(){ | |
def c=Calendar.getInstance(); | |
new java.text.SimpleDateFormat("yyyy-MM-dd").format( c.getTime() ); | |
} | |
String touchCdate( String text ){ | |
def br = System.getProperty('line.separator') | |
def sb = ''<<'' | |
sb << br | |
sb << '{cdate:' + getCdateString() + '}' | |
sb << br | |
sb << text | |
sb.toString() | |
} | |
void save( File f, String text ){ | |
if( f ){ | |
def w = f.newWriter(ENC) | |
w.write( text ) | |
w.close() | |
} | |
} | |
} | |
def cli = new CliBuilder() | |
cli.f(argName:'filename', required:true ,args:1 , 'entry file name') | |
def options=cli.parse(args) | |
def f = null | |
if( options.f ){ | |
f = new File(options.f) | |
} | |
if( f && f.exists() ){ | |
def util = new BlogUtils() | |
if( util.hasCdate( f )==false ){ | |
def text = f.getText(BlogUtils.ENC) | |
util.save( f, util.touchCdate( text ) ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment