forked from bartuer/ydiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlize.rkt
217 lines (170 loc) · 6.18 KB
/
htmlize.rkt
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
;; ydiff - a language-aware tool for comparing programs
;; Copyright (C) 2011-2013 Yin Wang (yinwang0@gmail.com)
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
#lang racket
(require "utils.rkt")
(require "structs.rkt")
(provide (all-defined-out))
;-------------------------------------------------------------
; HTML generation
;-------------------------------------------------------------
;----------------- utils ----------------
(define qs
(lambda (x)
(let ([x (cond
[(symbol? x) (symbol->string x)]
[(number? x) (number->string x)]
[(string? x) x])])
(string-append "'" x "'"))))
(define line
(lambda (port . s)
(display (string-append (apply string-append s) "\n") port)))
(define change-tags
(lambda (changes side)
(let loop ([cs changes] [tags '()])
(cond
[(null? cs) tags]
[else
(let ([key (if (eq? side 'left)
(Change-old (car cs))
(Change-new (car cs)))])
(cond
[(or (not key)
(= (Node-start key) (Node-end key)))
(loop (cdr cs) tags)]
[(and (Change-old (car cs)) (Change-new (car cs)))
(let ([startTag (Tag (link-start (car cs) side)
(Node-start key) -1)]
[endTag (Tag "</a>" (Node-end key) (Node-start key))])
(loop (cdr cs) (cons endTag (cons startTag tags))))]
[else
(let ([startTag (Tag (span-start (car cs) side)
(Node-start key) -1)]
[endTag (Tag "</span>" (Node-end key) (Node-start key))])
(loop (cdr cs) (cons endTag (cons startTag tags))))]))]))))
(define apply-tags
(lambda (s tags)
(let ([tags (sort tags tag-sort-fn)])
(let loop ([tags tags] [curr 0] [out '()])
(cond
[(null? tags)
(cond
[(< curr (string-length s))
(loop tags (add1 curr) (cons (escape (string-ref s curr)) out))]
[else
(apply string-append (reverse out))])]
[else
(cond
[(< curr (Tag-idx (car tags)))
(loop tags (add1 curr) (cons (escape (string-ref s curr)) out))]
[else
(loop (cdr tags) curr (cons (Tag-tag (car tags)) out))])])))))
;; get the CSS class for the change
(define change-class
(lambda (change)
(cond
[(mov? change) "m"] ; move
[(del? change) "d"]
[(ins? change) "i"]
[else "u"]))) ; unchanged
(define link-start
(lambda (change side)
(let ([cls (change-class change)]
[me (if (eq? side 'left)
(Change-old change)
(Change-new change))]
[other (if (eq? side 'left)
(Change-new change)
(Change-old change))])
(string-append
"<a id=" (qs (uid me))
" tid=" (qs (uid other))
" class=" (qs cls)
">"))))
(define span-start
(lambda (change side)
(string-append "<span class=" (qs (change-class change)) ">")))
(define tag-sort-fn
(lambda (t1 t2)
(cond
[(= (Tag-idx t1) (Tag-idx t2))
(> (Tag-start t1) (Tag-start t2))]
[else
(< (Tag-idx t1) (Tag-idx t2))])))
(define *escape-table*
'((#\" . """)
(#\' . "'")
(#\< . "<")
(#\> . ">")))
(define escape
(lambda (c)
(cond
[(assq c *escape-table*) => cdr]
[else (char->string c)])))
; getting the base name of a path/file name
; (base-name "mk/mk-c.scm") => mk-c
(define base-name
(lambda (fn)
(let loop ([i (- (string-length fn) 1)]
[start -1]
[end (- (string-length fn) 1)])
(cond
[(= i 0)
(substring fn i end)]
[(eq? (string-ref fn i) #\.)
(loop (sub1 i) start i)]
[(eq? (string-ref fn i) #\/)
(substring fn (add1 i) end)]
[else
(loop (sub1 i) start end)]))))
(define html-header
(lambda (port)
(line port "<html>")
(line port "<head>")
(line port "<META http-equiv=\"Content-Type\""
" content=\"text/html; charset=utf-8\">")
(line port "<LINK href=\"diff.css\""
" rel=\"stylesheet\" type=\"text/css\">")
(line port "<script type=\"text/javascript\""
" src=\"nav.js\"></script>")
(line port "</head>")
(line port "<body>")))
(define html-footer
(lambda (port)
(line port "</body>")
(line port "</html>")))
(define write-html
(lambda (port text side)
(line port (string-append "<div id=\"" side "\" class=\"src\">"))
(line port "<pre>")
(if (string=? side "left")
(line port "<a id='leftstart' tid='rightstart'></a>")
(line port "<a id='rightstart' tid='leftstart'></a>"))
(line port text)
(line port "</pre>")
(line port "</div>")))
(define htmlize
(lambda (changes file1 file2 text1 text2)
(letv ([tags1 (change-tags changes 'left)]
[tags2 (change-tags changes 'right)]
[tagged-text1 (apply-tags text1 tags1)]
[tagged-text2 (apply-tags text2 tags2)]
[out-file (string-append (base-name file1) "-"
(base-name file2) ".html")]
[port (open-output-file out-file
#:mode 'text
#:exists 'replace)])
(html-header port)
(write-html port tagged-text1 "left")
(write-html port tagged-text2 "right")
(html-footer port)
(close-output-port port))))