避免 groovy 模板中出现新的换行符
Avoid new break line in groovy template
我有 YAML file
和我的配置名称 applications.yaml,此数据将是我的绑定:
applications:
- name: service1
port: 8080
path: /servier1
- name: service2
port: 8081
path: /service2
然后我有一个模板文件applications.config:
<% applications.each { application -> %>
ApplicationName: <%= application.name %>
<% } $ %>
并将所有内容放在一起:
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()
现在的问题是:这个过程的输出是:
ApplicationName: service1
ApplicationName: service2
但我想要这个:
ApplicationName: service1
ApplicationName: service2
我不知道为什么会有那些多余的空格。我想删除它们,但我看不到这些 新行或断行.
是如何、何时或是什么放置的
谢谢。
查看您的模板,例如来自记事本++(JSP 语言):
在第一行的末尾<% applications.each { application -> %>
有一个CRLF
将在每个Application
之前打印出来
并且在下一行中另一个 CRLF
将在每个 Application
之后打印。
所以每两个应用之间有两个 CRLF
我观察到的(从答案、评论和一些实践中)是: 我们在文件 applications.config
中创建的所有 new Lines
都被视为 new line
在输出中。正如 daggett 所说,这是默认设置。
所以在这里我只想展示可能的 config
文件格式,可以按照你的要求应用一些 conditional logic
并且对我来说看起来不错。例如:if()
application.config :
<% applications.each { application ->
if (application.valid) {%>\
Type :<%=application.valid%>
ApplicationName:<%=application.name%>
Path:<%=application.path%>
Port:<%=application.port%>
<%} else{%>\
--------------------
Found invalid Application : <%= application.name %>
--------------------\
<%}}%>
application.yaml
applications:
- name: service1
port: 8080
path: /servier1
valid: true
- name: service2
port: 8081
path: /service2
valid: false
code.groovy
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()
输出:
Type :true
ApplicationName:service1
Path:/servier1
Port:8080
--------------------
Found invalid Application : service2
--------------------
避免换行的唯一方法是像下面那样修复您的模板
<% applications.each { application -> %>ApplicationName: <%= application.name %>
<% } $ %>
结果
$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2
或者像下面这样处理代码中的换行符
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
def output = template.toString().replaceAll(/^\n|\n$/, "").replaceAll(/\n{2,}/,"\n")
println output
结果
$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2
我有 YAML file
和我的配置名称 applications.yaml,此数据将是我的绑定:
applications:
- name: service1
port: 8080
path: /servier1
- name: service2
port: 8081
path: /service2
然后我有一个模板文件applications.config:
<% applications.each { application -> %>
ApplicationName: <%= application.name %>
<% } $ %>
并将所有内容放在一起:
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()
现在的问题是:这个过程的输出是:
ApplicationName: service1
ApplicationName: service2
但我想要这个:
ApplicationName: service1
ApplicationName: service2
我不知道为什么会有那些多余的空格。我想删除它们,但我看不到这些 新行或断行.
是如何、何时或是什么放置的谢谢。
查看您的模板,例如来自记事本++(JSP 语言):
在第一行的末尾<% applications.each { application -> %>
有一个CRLF
将在每个Application
并且在下一行中另一个 CRLF
将在每个 Application
之后打印。
所以每两个应用之间有两个 CRLF
我观察到的(从答案、评论和一些实践中)是: 我们在文件 applications.config
中创建的所有 new Lines
都被视为 new line
在输出中。正如 daggett 所说,这是默认设置。
所以在这里我只想展示可能的 config
文件格式,可以按照你的要求应用一些 conditional logic
并且对我来说看起来不错。例如:if()
application.config :
<% applications.each { application ->
if (application.valid) {%>\
Type :<%=application.valid%>
ApplicationName:<%=application.name%>
Path:<%=application.path%>
Port:<%=application.port%>
<%} else{%>\
--------------------
Found invalid Application : <%= application.name %>
--------------------\
<%}}%>
application.yaml
applications:
- name: service1
port: 8080
path: /servier1
valid: true
- name: service2
port: 8081
path: /service2
valid: false
code.groovy
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()
输出:
Type :true
ApplicationName:service1
Path:/servier1
Port:8080
--------------------
Found invalid Application : service2
--------------------
避免换行的唯一方法是像下面那样修复您的模板
<% applications.each { application -> %>ApplicationName: <%= application.name %>
<% } $ %>
结果
$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2
或者像下面这样处理代码中的换行符
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)
String template_content = new File('applications.config').text
def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
def output = template.toString().replaceAll(/^\n|\n$/, "").replaceAll(/\n{2,}/,"\n")
println output
结果
$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2