This repository has been archived by the owner on Apr 10, 2019. It is now read-only.
/
html.go
117 lines (104 loc) · 2.23 KB
/
html.go
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
package output
import (
"bytes"
"fmt"
"io"
"regexp"
"sort"
"github.com/mibk/dupl/syntax"
)
type HTMLPrinter struct {
iota int
*TextPrinter
}
func NewHTMLPrinter(w io.Writer, fr FileReader) *HTMLPrinter {
fmt.Fprint(w, `<!DOCTYPE html>
<meta charset="utf-8"/>
<title>Duplicates</title>
<style>
pre {
background-color: #FFD;
border: 1px solid #E2E2E2;
padding: 1ex;
}
</style>
`)
return &HTMLPrinter{
TextPrinter: NewTextPrinter(w, fr),
}
}
func (p *HTMLPrinter) Print(dups [][]*syntax.Node) error {
p.iota++
fmt.Fprintf(p.writer, "<h1>#%d found %d clones</h1>\n", p.iota, len(dups))
clones := make([]clone, len(dups))
for i, dup := range dups {
cnt := len(dup)
if cnt == 0 {
panic("zero length dup")
}
nstart := dup[0]
nend := dup[cnt-1]
file, err := p.freader.ReadFile(nstart.Filename)
if err != nil {
return err
}
lineStart, _ := blockLines(file, nstart.Pos, nend.End)
cl := clone{filename: nstart.Filename, lineStart: lineStart}
start := findLineBeg(file, nstart.Pos)
content := append(toWhitespace(file[start:nstart.Pos]), file[nstart.Pos:nend.End]...)
cl.fragment = deindent(content)
clones[i] = cl
}
sort.Sort(byNameAndLine(clones))
for _, cl := range clones {
fmt.Fprintf(p.writer, "<h2>%s:%d</h2>\n<pre>%s</pre>\n", cl.filename, cl.lineStart, cl.fragment)
}
return nil
}
func (*HTMLPrinter) Finish() {}
func findLineBeg(file []byte, index int) int {
for i := index; i >= 0; i-- {
if file[i] == '\n' {
return i + 1
}
}
return 0
}
func toWhitespace(str []byte) []byte {
var out []byte
for _, c := range bytes.Runes(str) {
if c == '\t' {
out = append(out, '\t')
} else {
out = append(out, ' ')
}
}
return out
}
func deindent(block []byte) []byte {
const maxVal = 99
min := maxVal
re := regexp.MustCompile(`(^|\n)(\t*)\S`)
for _, line := range re.FindAllSubmatch(block, -1) {
indent := line[2]
if len(indent) < min {
min = len(indent)
}
}
if min == 0 || min == maxVal {
return block
}
block = block[min:]
Loop:
for i := 0; i < len(block); i++ {
if block[i] == '\n' && i != len(block)-1 {
for j := 0; j < min; j++ {
if block[i+j+1] != '\t' {
continue Loop
}
}
block = append(block[:i+1], block[i+1+min:]...)
}
}
return block
}