英文:
How to wrap each line with a div in Golang?
问题
我有一个文本段落。
我想要用一个 div 标签包裹每一行。
例如,如果文本字符串看起来像这样:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Nam eget lectus porta metus aliquam tincidunt sed vel enim.
那么我想要像这样包裹它:
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit.</div>
<div><br/></div>
<div>Nam eget lectus porta metus aliquam tincidunt sed vel enim.</div>
有人可以告诉我正确的方法吗?
谢谢
英文:
I have a text paragraph.
I would like to wrap each line with a div.
For example if the text string looks like this
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Nam eget lectus porta metus aliquam tincidunt sed vel enim.
Then I would like to wrap it like this
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit.</div>
<div><br/></div>
<div>Nam eget lectus porta metus aliquam tincidunt sed vel enim.</div>
Can someone tell me what is the proper way?
Thanks
答案1
得分: 2
你可以使用bytes.Buffer
和bufio.NewScanner
,像下面这个工作示例代码一样:
package main
import "fmt"
import "strings"
import "bufio"
import "bytes"
func main() {
r := strings.NewReader(`Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Nam eget lectus porta metus aliquam tincidunt sed vel enim.`)
buf := &bytes.Buffer{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
s := scanner.Text()
if len(s) == 0 {
s = `<br/>`
}
buf.WriteString(`<div>` + s + `</div>
`)
}
fmt.Println(buf.String())
}
输出结果:
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit.</div>
<div><br/></div>
<div>Nam eget lectus porta metus aliquam tincidunt sed vel enim.</div>
你可以使用bytes.Buffer
和html/template
,但是你需要将<div></div>
替换为<div><br/></div>
,可以使用以下工作示例代码:
package main
import "fmt"
import "bytes"
import "html/template"
import "strings"
func main() {
r := []string{`Lorem ipsum dolor sit amet,`,
`consectetur adipiscing elit.`,
``,
`Nam eget lectus porta metus aliquam tincidunt sed vel enim.`}
buf := &bytes.Buffer{}
t := template.Must(template.New("template").Parse("{{range .}}<div>{{.}}</div>\n{{end}}"))
t.Execute(buf, r)
s := buf.String()
s = strings.Replace(s, `<div></div>`, `<div><br/></div>`, -1)
fmt.Println(s)
}
输出结果:
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit.</div>
<div><br/></div>
<div>Nam eget lectus porta metus aliquam tincidunt sed vel enim.</div>
英文:
You may use bytes.Buffer
and bufio.NewScanner
, like this working sample code:
<!-- language: lang-golang -->
package main
import "fmt"
import "strings"
import "bufio"
import "bytes"
func main() {
r := strings.NewReader(`Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Nam eget lectus porta metus aliquam tincidunt sed vel enim.`)
buf := &bytes.Buffer{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
s := scanner.Text()
if len(s) == 0 {
s = `<br/>`
}
buf.WriteString(`<div>` + s + `</div>
`)
}
fmt.Println(buf.String())
}
output:
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit.</div>
<div><br/></div>
<div>Nam eget lectus porta metus aliquam tincidunt sed vel enim.</div>
You may use bytes.Buffer
and html/template
,
but you have <div></div>
instead of <div><br/></div>
, then you may use
s = strings.Replace(s, `<div></div>`, `<div><br/></div>`, -1)
like this working sample code:
<!-- language: lang-golang -->
package main
import "fmt"
import "bytes"
import "html/template"
import "strings"
func main() {
r := []string{`Lorem ipsum dolor sit amet,`,
`consectetur adipiscing elit.`,
``,
`Nam eget lectus porta metus aliquam tincidunt sed vel enim.`}
buf := &bytes.Buffer{}
t := template.Must(template.New("template").Parse("{{range .}}<div>{{.}}</div>\n{{end}}"))
t.Execute(buf, r)
s := buf.String()
s = strings.Replace(s, `<div></div>`, `<div><br/></div>`, -1)
fmt.Println(s)
}
output:
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit.</div>
<div><br/></div>
<div>Nam eget lectus porta metus aliquam tincidunt sed vel enim.</div>
答案2
得分: 1
生成HTML片段的代码如下所示,它会正确转义HTML内容:
package main
import (
"html/template"
"os"
)
func main() {
data := []string{
"first",
"second",
"<style>body{background-color:lime;}</style>",
"<script>alert('evil')</script>",
}
htmlTemplate := "{{range .}}<div>{{.}}</div>\n{{end}}"
tmpl := template.Must(template.New("").Parse(htmlTemplate))
tmpl.Execute(os.Stdout, data)
}
请注意,这是一个Go语言的代码示例,用于生成HTML片段。
英文:
For the part of generating the HTML snippet from a slice of lines, you should use the following code, which takes care of properly escaping the HTML.
package main
import (
"html/template"
"os"
)
func main() {
data := []string{
"first",
"second",
"<style>body{background-color:lime;}</style>",
"<script>alert('evil')</script>",
}
htmlTemplate := "{{range .}}<div>{{.}}</div>\n{{end}}"
tmpl := template.Must(template.New("").Parse(htmlTemplate))
tmpl.Execute(os.Stdout, data)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论