aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/package/diff.scm
blob: ff0b75a25fbe0beb51e455a48422d3386cc747c5 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
;;; diff.scm  --  A document difference highlighting package.
;;; -*- coding: iso-8859-1 -*-
;;;
;;; Copyright 2007  Ludovic Court�s <ludo@gnu.org>
;;;
;;;
;;; This file is part of Skribilo.
;;;
;;; Skribilo 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.
;;;
;;; Skribilo 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 Skribilo.  If not, see <http://www.gnu.org/licenses/>.

(define-module (skribilo package diff)
  :use-module (diff)
  :use-module (srfi srfi-1)
  :use-module (srfi srfi-14)
  :use-module (srfi srfi-39)
  :use-module (ice-9 optargs)

  :use-module (skribilo ast)
  :use-module (skribilo lib)
  :use-module (skribilo engine)
  :use-module (skribilo writer)
  :autoload   (skribilo output)        (output)
  :autoload   (skribilo reader)        (*document-reader*)
  :autoload   (skribilo module)        (make-user-module)
  :autoload   (skribilo resolve)       (resolve!)
  :autoload   (skribilo evaluator)     (evaluate-ast-from-port)
  :autoload   (skribilo biblio)        (*bib-table* make-bib-table)
  :use-module (skribilo package base)
  :use-module (skribilo utils syntax)

  :export (make-diff-document
           make-diff-document-from-files))

(skribilo-module-syntax)

;;; Author: Ludovic Court�s
;;;
;;; Commentary:
;;;
;;; This package provides facilities to automatically produce documents where
;;; changes from a previous version of the document are highlighted.
;;;
;;; Warning: This is very experimental at this stage!
;;;
;;; Code:



;;;
;;; Markup.
;;;

(define-markup (deletion :rest args :key loc)
  (new markup
       (markup 'diff:deletion)
       (ident  (gensym "diff:deletion"))
       (loc    (or loc &invocation-location))
       (body   args)))

(define-markup (insertion :rest args :key loc)
  (new markup
       (markup 'diff:insertion)
       (ident  (gensym "diff:insertion"))
       (loc    (or loc &invocation-location))
       (body   args)))

(define-markup (replacement :rest args :key loc)
  (new markup
       (markup 'diff:replacement)
       (ident  (gensym "diff:replacement"))
       (loc    (or loc &invocation-location))
       (body   args)))

(define-markup (unchanged :rest args :key loc)
  (new markup
       (markup 'diff:unchanged)
       (ident  (gensym "diff:unchanged"))
       (loc    (or loc &invocation-location))
       (body   args)))



;;;
;;; Helpers for string diffs.
;;;

(define (coalesce-edits edits)
  ;; Coalesce EDITS (an array of edits as returned by `diff:edits') into a
  ;; list of contiguous changes, each change being denoted by `(CHANGE-KIND
  ;; START END)' where CHANGE-KIND is one of `deletion', `insertion' or
  ;; `replacement'.
  (define (do-coalesce edit-kind edit result)
    (cond ((null? result)
           `((,edit-kind ,edit ,edit)))
          ((eq? (caar result) edit-kind)
           (let ((start (cadr  (car result)))
                 (end   (caddr (car result))))
             (if (= edit (+ end 1))
                 (cons `(,edit-kind ,start ,edit)
                       (cdr result))
                 (cons `(,edit-kind ,edit ,edit)
                       result))))
          (else
           (let ((start (cadr  (car result)))
                 (end   (caddr (car result))))
             (if (and (= start end edit)
                      (not (eq? (caar result) 'replacement)))
                 (do-coalesce 'replacement edit (cdr result))
                 (cons `(,edit-kind ,edit ,edit)
                       result))))))

  (reverse! (fold (lambda (edit result)
                    (if (negative? edit)
                        (let ((edit (- -1 edit)))
                          (do-coalesce 'deletion edit result))
                        (let ((edit (- edit 1)))
                          (do-coalesce 'insertion edit result))))
                  '()
                  (array->list edits))))

(define (add-unchanged edits str-len)
  ;; Add information about unchanged regions to EDITS, a list returned by
  ;; `coalesce-edits'.  STR-LEN should be the length of the _target_ string,
  ;; i.e., the second argument of `diff:edits'.
  (define (coalesce-unchanged start end result)
    (if (null? result)
        `((unchanged ,start ,end))
        (let ((prev-unchanged? (eq? (caar result) 'unchanged))
              (prev-start      (cadr (car result))))
          (if prev-unchanged?
              (cons `(unchanged ,prev-start ,end)
                    (cdr result))
              (cons `(unchanged ,start ,end)
                    result)))))

  (let loop ((edits   edits)
             (result  '())
             (str-pos 0))
    (if (null? edits)
        (reverse! (if (< str-pos str-len)
                      (cons (list 'unchanged str-pos (- str-len 1))
                            result)
                      result))
        (let* ((change (car edits))
               (kind  (car change))
               (start (cadr change))
               (end   (caddr change)))

          (loop (cdr edits)
                (if (memq kind '(insertion replacement))
                    (if (> start str-pos)
                        (cons change
                              (coalesce-unchanged str-pos (- start 1)
                                                  result))
                        (cons change result))
                    (cons change result))
                (if (eq? kind 'deletion)
                    str-pos ;; deletion doesn't change string position
                    (+ end 1)))))))

(define (string-diff-sequences str1 str2)
  ;; Return a "diff sequence" between STR1 and STR2.  The diff sequence is
  ;; alist of 3-element list whose car represent a diff type (a symbol,
  ;; either `unchanged', `replacement', `insertion', or `deletion') and two
  ;; integers denoting where the change took place.  These two integers are
  ;; an indices in STR1 in the case of `deletion', indices in STR2 otherwise.
  (add-unchanged (coalesce-edits (diff:edits str1 str2))
                 (string-length str2)))



;;;
;;; AST diffing.
;;;

(define %undiffable-markups
  ;; List of markups to not diff.
  '(ref url-ref bib-ref bib-ref+ line-ref unref numref
    eq     ;; XXX: not supported because of the `eq-evaluate' thing
    figref ;; non-standard
    mark
    image symbol lout-illustration
    &the-bibliography
    toc
    index &index-entry &the-index &the-index-header))

(define %diffable-options
  ;; List of diffable options.
  '(:title :text))

(define (annotated-string-diff str1 str2)
  ;; Return a list (actually an AST) denoting the differences between STR1
  ;; and STR2.  The returned text is actually that of STR2 augmented with
  ;; `insertion', `deletion', `replacement', and `unchanged' markup.

  (define (space-preserving-substring str start end)
    ;; Return the substring of STR, possibly inserting unbreakable spaces at
    ;; the beginning/end if STR starts/ends in whitespaces.
    (let ((len (- end start)))
      (if (> len 0)
          (let* ((lead (string-ref str start))
                 (lead* (if (char-set-contains? char-set:whitespace
                                                lead)
                            (breakable-space)
                            (string lead))))
            (if (> len 1)
                (let* ((trail (string-ref str (- end 1)))
                       (trail* (if (char-set-contains? char-set:whitespace
                                                       trail)
                                   (breakable-space)
                                   (string trail))))
                  (list lead* (substring str (+ start 1) (- end 1))
                        trail*))
                (list lead* (substring str (+ start 1) end))))
          "")))

  (reverse!
   (fold (lambda (edit result)
           (let ((start (cadr edit))
                 (end   (+ 1 (caddr edit))))
             (cons (case (car edit)
                     ((insertion)
                      (insertion (space-preserving-substring str2 start
                                                             end)))
                     ((deletion)
                      (deletion  (space-preserving-substring str1 start
                                                             end)))
                     ((replacement)
                      (replacement (space-preserving-substring str2 start
                                                               end)))
                     ((unchanged)
                      (unchanged (space-preserving-substring str2 start
                                                             end))))
                   result)))
         '()
         (string-diff-sequences str1 str2))))

(define (make-diff-document ast1 ast2)
  ;; Return a document based on AST2 that highlights differences between AST1
  ;; and AST2, enclosing unchanged parts in `unchanged' markups, etc.  AST2
  ;; is used as the "reference" tree, thus changes from AST1 to AST2 are
  ;; shown in the resulting document.
  (define (undiffable? kind)
    (memq kind %undiffable-markups))

  (define (make-diff-options m1 m2 loop)
    ;; Return a list of options based on that of markup M2.
    (map (lambda (opt+val)
           (let ((opt (car opt+val)))
             (if (memq opt %diffable-options)
                 (cons opt
                       (loop (markup-option m1 opt)
                             (cdr opt+val)))
                 opt+val)))
         (markup-options m2)))


  (let loop ((ast1 ast1)
             (ast2 ast2))
    ;;(format (current-error-port) "diff: ~a ~a~%" ast1 ast2)
    (cond ((string? ast2)
           (if (string? ast1)
               (annotated-string-diff ast1 ast2)
               (insertion ast2)))

          ((document? ast2)
           (let ((ident (or (markup-ident ast2)
                            (ast->string (markup-option ast2 :title))
                            (symbol->string (gensym "document"))))
                 (opts  (markup-options ast2))
                 (class (markup-class ast2))
                 (body  (markup-body ast2)))
             (new document
                  (markup 'document)
                  (ident ident)
                  (class class)
                  (loc   (ast-loc ast2))
                  (options opts)
                  (body (loop (if (markup? ast1)
                                  (markup-body ast1)
                                  ast1)
                              body))
                  (env (list (list 'chapter-counter 0) (list 'chapter-env '())
                             (list 'section-counter 0) (list 'section-env '())
                             (list 'footnote-counter 0)
                             (list 'footnote-env '())
                             (list 'figure-counter 0)
                             (list 'figure-env '()))))))

          ((container? ast2)
           (let ((kind  (markup-markup ast2))
                 (ident (markup-ident ast2))
                 (class (markup-class ast2))
                 (body  (markup-body ast2)))
             (new container
                  (markup  kind)
                  (ident   ident)
                  (class   class)
                  (loc     (ast-loc ast2))
                  (options (if (or (undiffable? kind)
                                   (not (container? ast1)))
                               (markup-options ast2)
                               (make-diff-options ast1 ast2 loop)))
                  (body (if (undiffable? kind)
                            body
                            (loop (if (and (container? ast1)
                                           (is-markup? ast1 kind))
                                      (markup-body ast1)
                                      ast1)
                                  body))))))

          ((markup? ast2)
           (let ((kind  (markup-markup ast2))
                 (ident (markup-ident ast2))
                 (class (markup-class ast2))
                 (body  (markup-body ast2)))
             (new markup
                  (markup  kind)
                  (ident   ident)
                  (class   class)
                  (loc     (ast-loc ast2))
                  (options (if (or (undiffable? kind)
                                   (not (markup? ast1)))
                               (markup-options ast2)
                               (make-diff-options ast1 ast2 loop)))
                  (body (if (undiffable? kind)
                            body
                            (loop (if (is-markup? ast1 kind)
                                      (markup-body ast1)
                                      ast1)
                                  body))))))

          ((command? ast2)
           ;; Leave it untouched.
           (new command
                (loc  (ast-loc ast2))
                (fmt  (command-fmt ast2))
                (body (command-body ast2))))

          ((list? ast2)
           (if (list? ast1)
               (let liip ((ast1 ast1)
                          (ast2 ast2)
                          (result '()))
                 (if (null? ast2)
                     (reverse! result)
                     (liip (if (null? ast1) ast1 (cdr ast1))
                           (cdr ast2)
                           (cons (loop (if (null? ast1) #f (car ast1))
                                       (car ast2))
                                 result))))
               (map (lambda (x)
                      (loop ast1 x))
                    ast2)))

          ((equal? ast1 ast2)
           (unchanged :loc (ast-loc ast2) ast1))

          (else
           (let ((loc (and (ast? ast2) (ast-loc ast2))))
             (insertion :loc loc  ast2))))))



;;;
;;; Public API.
;;;

(define* (make-diff-document-from-files old-file new-file
                                        :key (reader (*document-reader*))
                                             (env '())
                                             (engine (*current-engine*)))
  ;; Return a document similar to NEW-FILE, where differences from OLD-FILE
  ;; are highlighted.
  (let ((ast1
         (parameterize ((*bib-table* (make-bib-table 'doc-1)))
           (skribe-message "diff: loading first document~%")
           (evaluate-ast-from-port (open-input-file old-file)
                                   :reader reader
                                   :module (make-user-module 'skribilo))))
        (ast2
         (parameterize ((*bib-table* (make-bib-table 'doc-2)))
           (skribe-message "diff: loading second document~%")
           (evaluate-ast-from-port (open-input-file new-file)
                                   :reader reader
                                   :module (make-user-module 'skribilo)))))

    (resolve! ast1 engine env)
    (resolve! ast2 engine env)
    (make-diff-document ast1 ast2)))



;;;
;;; Default writers.
;;;

(markup-writer 'diff:deletion (find-engine 'base)
  :action (lambda (n e)
            ;;(color :fg "red" (symbol "middot"))

            ;; Output nothing by default so that the document remains
            ;; readable.
            #f))

(markup-writer 'diff:insertion (find-engine 'base)
  :action (lambda (n e)
            (output (color :fg "green" (markup-body n)) e)))

(markup-writer 'diff:replacement (find-engine 'base)
  :action (lambda (n e)
            (output (color :fg "orange" (markup-body n)) e)))

(markup-writer 'diff:unchanged (find-engine 'base)
  :action (lambda (n e)
            (output (markup-body n) e)))


;;; diff.scm ends here

;;; arch-tag: 69ad10fa-5688-4835-8956-439e44e26847