如何在Golang中将每一行用div包装起来?

huangapple go评论81阅读模式
英文:

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.Bufferbufio.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.Bufferhtml/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 &quot;fmt&quot;
import &quot;strings&quot;
import &quot;bufio&quot;
import &quot;bytes&quot;

func main() {
	r := strings.NewReader(`Lorem ipsum dolor sit amet,
consectetur adipiscing elit.

Nam eget lectus porta metus aliquam tincidunt sed vel enim.`)

	buf := &amp;bytes.Buffer{}
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		s := scanner.Text()
		if len(s) == 0 {
			s = `&lt;br/&gt;`
		}
		buf.WriteString(`&lt;div&gt;` + s + `&lt;/div&gt;
`)
	}
	fmt.Println(buf.String())
}

output:

&lt;div&gt;Lorem ipsum dolor sit amet,&lt;/div&gt;
&lt;div&gt;consectetur adipiscing elit.&lt;/div&gt;
&lt;div&gt;&lt;br/&gt;&lt;/div&gt;
&lt;div&gt;Nam eget lectus porta metus aliquam tincidunt sed vel enim.&lt;/div&gt;

You may use bytes.Buffer and html/template,
but you have &lt;div&gt;&lt;/div&gt; instead of &lt;div&gt;&lt;br/&gt;&lt;/div&gt;, then you may use

s = strings.Replace(s, `&lt;div&gt;&lt;/div&gt;`, `&lt;div&gt;&lt;br/&gt;&lt;/div&gt;`, -1)

like this working sample code:

<!-- language: lang-golang -->

package main

import &quot;fmt&quot;
import &quot;bytes&quot;
import &quot;html/template&quot;
import &quot;strings&quot;

func main() {
	r := []string{`Lorem ipsum dolor sit amet,`,
		`consectetur adipiscing elit.`,
		``,
		`Nam eget lectus porta metus aliquam tincidunt sed vel enim.`}
	buf := &amp;bytes.Buffer{}
	t := template.Must(template.New(&quot;template&quot;).Parse(&quot;{{range .}}&lt;div&gt;{{.}}&lt;/div&gt;\n{{end}}&quot;))
	t.Execute(buf, r)
	s := buf.String()
	s = strings.Replace(s, `&lt;div&gt;&lt;/div&gt;`, `&lt;div&gt;&lt;br/&gt;&lt;/div&gt;`, -1)
	fmt.Println(s)
}

output:

&lt;div&gt;Lorem ipsum dolor sit amet,&lt;/div&gt;
&lt;div&gt;consectetur adipiscing elit.&lt;/div&gt;
&lt;div&gt;&lt;br/&gt;&lt;/div&gt;
&lt;div&gt;Nam eget lectus porta metus aliquam tincidunt sed vel enim.&lt;/div&gt;

答案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 (
	&quot;html/template&quot;
	&quot;os&quot;
)

func main() {
	data := []string{
		&quot;first&quot;,
		&quot;second&quot;,
		&quot;&lt;style&gt;body{background-color:lime;}&lt;/style&gt;&quot;,
		&quot;&lt;script&gt;alert(&#39;evil&#39;)&lt;/script&gt;&quot;,
	}
	htmlTemplate := &quot;{{range .}}&lt;div&gt;{{.}}&lt;/div&gt;\n{{end}}&quot;

	tmpl := template.Must(template.New(&quot;&quot;).Parse(htmlTemplate))
	tmpl.Execute(os.Stdout, data)
}

huangapple
  • 本文由 发表于 2016年8月23日 03:40:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/39087469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定