Skip to content

Instantly share code, notes, and snippets.

@shikaiwen
Created June 15, 2018 08:02
Show Gist options
  • Save shikaiwen/cc1cb9f2e42df5854bbffe48f12b5978 to your computer and use it in GitHub Desktop.
Save shikaiwen/cc1cb9f2e42df5854bbffe48f12b5978 to your computer and use it in GitHub Desktop.
Groovy完成文字模板替换
package com.kw.test
import org.apache.commons.collections.CollectionUtils
import org.apache.commons.lang3.StringUtils
String s = '''
2018年04月 上海海隆グループの資本参加を受け、株式会社海隆一創に商号変更しました。
2017年12月 社員数が60名を突破しました。
2017年10月 事業拡大の為資本金を4000万に増資しました。事業拡大の為資本金を4000万に増資しました。
2016年04月 社員数が40名を突破しました。社員数が40名を突破しました。
2015年04月 事業拡大の為資本金を2500万に増資しました。
2014年12月 社員数が20名を突破しました。
2014年09月 事業拡大の為資本金を950万に増資しました。
2014年08月 業務拡大の為東京都新宿区へ移転しました。
2014年03月 東京都千代田区において株式会社一創として会社設立しました。
'''
String renderTemp = '''
<div class="yet sing${index}">
<div class="wec te5"><img src="images/dins${index}.png"/></div>
<div class="i_t iur5">
<h3>${time}</h3>
<p>${title}</p>
</div>
</div>
'''
def engine = new groovy.text.SimpleTemplateEngine()
def template = engine.createTemplate(renderTemp)
int count = 1
s.split("\n").reverseEach {}
def array = s.split("\n")
CollectionUtils.reverseArray(array)
array.eachWithIndex { a,index->
if(StringUtils.isEmpty(a)) return
String pat = "\\ "
def time = a.split(pat)[0]
def title = a.split(pat)[1]
def binding = ["index": count++, "time": time, "title": title]
def make = template.make(binding)
print(make.toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment