Skip to content

Commit 071b332

Browse files
committed
Better support for XML and HTML
Keep <?xml and <!doctype declarations.
1 parent fbdca00 commit 071b332

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

main.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"log"
2525
"os"
2626
"path/filepath"
27+
"strings"
2728
"sync"
2829
"time"
2930
)
@@ -155,6 +156,12 @@ func addLicense(path string, fmode os.FileMode, typ string, data *copyrightData)
155156
return ioutil.WriteFile(path, b, fmode)
156157
}
157158

159+
var head = []string{
160+
"#!", // shell script
161+
"<?xml", // XML declaratioon
162+
"<!doctype", // HTML doctype
163+
}
164+
158165
func hashBang(b []byte) []byte {
159166
var line []byte
160167
for _, c := range b {
@@ -163,8 +170,11 @@ func hashBang(b []byte) []byte {
163170
break
164171
}
165172
}
166-
if bytes.HasPrefix(line, []byte("#!")) {
167-
return line
173+
first := strings.ToLower(string(line))
174+
for _, h := range head {
175+
if strings.HasPrefix(first, h) {
176+
return line
177+
}
168178
}
169179
return nil
170180
}

testdata/expected/file.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!doctype html>
12
<!--
23
Copyright 2016 Google Inc.
34
@@ -14,5 +15,4 @@
1415
limitations under the License.
1516
-->
1617

17-
<!doctype html>
1818
<p>Hello World!</p>

testdata/expected/file2.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
2+
<!--
3+
Copyright 2016 Google Inc.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<root>
19+
<one>one</one>
20+
<two/>
21+
</root>

testdata/initial/file2.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
2+
<root>
3+
<one>one</one>
4+
<two/>
5+
</root>

0 commit comments

Comments
 (0)