1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
# GoHTML - HTML formatter for Go
[](https://app.wercker.com/project/bykey/926cf3edc004271539be40d705d037bd)
[](http://godoc.org/github.com/yosssi/gohtml)
GoHTML is an HTML formatter for [Go](http://golang.org/). You can format HTML source codes by using this package.
## Example
Example Go source code:
```go
package main
import (
"fmt"
"github.com/yosssi/gohtml"
)
func main() {
h := `<!DOCTYPE html><html><head><title>This is a title.</title><script type="text/javascript">
alert('aaa');
if (0 < 1) {
alert('bbb');
}
</script><style type="text/css">
body {font-size: 14px;}
h1 {
font-size: 16px;
font-weight: bold;
}
</style></head><body><form><input type="name"><p>AAA<br>BBB></p></form><!-- This is a comment. --></body></html>`
fmt.Println(gohtml.Format(h))
}
```
Output:
```html
<!DOCTYPE html>
<html>
<head>
<title>
This is a title.
</title>
<script type="text/javascript">
alert('aaa');
if (0 < 1) {
alert('bbb');
}
</script>
<style type="text/css">
body {font-size: 14px;}
h1 {
font-size: 16px;
font-weight: bold;
}
</style>
</head>
<body>
<form>
<input type="name">
<p>
AAA
<br>
BBB>
</p>
</form>
<!-- This is a comment. -->
</body>
</html>
```
## Ouput Formatted HTML with Line No
You can output formatted HTML source codes with line no by calling `FormatWithLineNo`:
```go
package main
import (
"fmt"
"github.com/yosssi/gohtml"
)
func main() {
h := `<!DOCTYPE html><html><head><title>This is a title.</title><script type="text/javascript">
alert('aaa');
if (0 < 1) {
alert('bbb');
}
</script><style type="text/css">
body {font-size: 14px;}
h1 {
font-size: 16px;
font-weight: bold;
}
</style></head><body><form><input type="name"><p>AAA<br>BBB></p></form><!-- This is a comment. --></body></html>`
fmt.Println(gohtml.FormatWithLineNo(h))
}
```
Output:
```sh
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>
5 This is a title.
6 </title>
7 <script type="text/javascript">
8 alert('aaa');
9 if (0 < 1) {
10 alert('bbb');
11 }
12 </script>
13 <style type="text/css">
14 body {font-size: 14px;}
15 h1 {
16 font-size: 16px;
17 font-weight: bold;
18 }
19 </style>
20 </head>
21 <body>
22 <form>
23 <input type="name">
24 <p>
25 AAA
26 <br>
27 BBB>
28 </p>
29 </form>
30 <!-- This is a comment. -->
31 </body>
32 </html>
```
## Format Go html/template Package's Template's Execute Result
You can format [Go html/template package](http://golang.org/pkg/html/template/)'s template's execute result by passing `Writer` to the `tpl.Execute`:
```go
package main
import (
"os"
"text/template"
"github.com/yosssi/gohtml"
)
func main() {
tpl, err := template.New("test").Parse("<html><head></head><body>{{.Msg}}</body></html>")
if err != nil {
panic(err)
}
data := map[string]interface{}{"Msg": "Hello!"}
err = tpl.Execute(gohtml.NewWriter(os.Stdout), data)
if err != nil {
panic(err)
}
}
```
Output:
```html
<html>
<head>
</head>
<body>
Hello!
</body>
</html>
```
## Docs
* [GoDoc](https://godoc.org/github.com/yosssi/gohtml)
|