aboutsummaryrefslogtreecommitdiff
path: root/src/guile/skribilo/package/lncs.scm
blob: 2e6bb217c52f93d059e2312bd4bec67de047492b (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
;;; lncs.scm  --  The Skribilo style for LNCS articles.
;;;
;;; Copyright 2003, 2004  Manuel Serrano
;;; Copyright 2007, 2015, 2018, 2020  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 lncs)
  #:use-module (skribilo ast)
  #:use-module (skribilo engine)
  #:use-module (skribilo writer)
  #:autoload   (skribilo output)         (output)
  #:use-module (skribilo package base)
  #:autoload   (skribilo utils keywords) (the-options the-body)
  #:autoload   (skribilo biblio template)(output-bib-entry-template
                                         make-bib-entry-template/default)
  #:autoload   (skribilo biblio author)  (bib-sort/first-author-last-name
                                          abbreviate-author-first-names/family-first
                                          abbreviate-first-names)
  #:autoload   (skribilo evaluator)      (evaluate-document)

  #:use-module (skribilo lib)
  #:use-module (skribilo utils syntax)

  #:use-module (ice-9 optargs)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-13)

  #:export (abstract references))

(skribilo-module-syntax)

;;; Author: Manuel Serrano, Ludovic Courtès
;;;
;;; Commentary:
;;;
;;; This module provides support for writing articles for the ``Lecture Notes
;;; in Computer Science'' series (LNCS) published by Springer-Verlag.
;;;
;;; Since Springer provides a LaTeX class (called `llncs') and expects you to
;;; submit articles only in LaTeX, this module tries hard to use native LaTeX
;;; constructs when the LaTeX engine is being used, so that you can pass the
;;; generated TeX file to Springer-Verlag and make them happy.
;;;
;;; Code:


;*---------------------------------------------------------------------*/
;*    LaTeX global customizations                                      */
;*---------------------------------------------------------------------*/
(let ((le (find-engine 'latex)))
   (engine-custom-set! le 'documentclass "\\documentclass{llncs}")
   (engine-custom-set! le 'class-has-chapters? #f)
   ;; &latex-author
   (markup-writer '&latex-author le
      :action (lambda (n e)
		 (define (&latex-inst-body n)
		    (let ((affiliation (markup-option n :affiliation))
			  (address (markup-option n :address)))
		       (when affiliation (output affiliation e) (display ", "))
		       (when address
			  (for-each (lambda (a) (output a e) (display " "))
				    address)
			  (newline))))
		 (define (&latex-inst-n i)
		    (display "\\institute{\n")
		    (&latex-inst-body (car i))
		    (for-each (lambda (n)
				 (display "\\and\n")
				 (&latex-inst-body n))
			      (cdr i))
		    (display "}\n"))
		 (define (&latex-author-1 n)
		    (display "\\author{\n")
		    (output n e)
		    (display "}\n"))
		 (define (&latex-author-n n)
		    (display "\\author{\n")
		    (output (car n) e)
		    (for-each (lambda (a)
				 (display " and ")
				 (output a e))
			      (cdr n))
		    (display "}\n"))
		 (let ((body (markup-body n)))
                    (define (institute=? n1 n2)
                      (let ((aff1 (markup-option n1 :affiliation))
                            (add1 (markup-option n1 :address))
                            (aff2 (markup-option n2 :affiliation))
                            (add2 (markup-option n2 :address)))
                        (and (equal? aff1 aff2) (equal? add1 add2))))
                    (define (search-institute n i j)
                      (cond
                       ((null? i)
                        #f)
                       ((institute=? n (car i))
                        j)
                       (else
                        (search-institute n (cdr i) (- j 1)))))

		    (cond
		       ((is-markup? body 'author)
			(markup-option-add! n 'inst 1)
			(&latex-author-1 body)
			(&latex-inst-n (list body)))
		       ((and (list? body)
			     (every (lambda (b) (is-markup? b 'author))
                                    body))
			(if (null? (cdr body))
			    (begin
			       (markup-option-add! (car body) 'inst 1)
			       (&latex-author-1 (car body))
			       (&latex-inst-n body))
			    ;; collect the institutes
			    (let loop ((ns body)
				       (is '())
				       (j 1))
			       (if (null? ns)
				   (begin
				      (&latex-author-n body)
				      (&latex-inst-n (reverse! is)))
				   (let* ((n (car ns))
					  (si (search-institute n is (- j 1))))
				      (if (integer? si)
					  (begin
					     (markup-option-add! n 'inst si)
					     (loop (cdr ns) is j))
					  (begin
					     (markup-option-add! n 'inst j)
					     (loop (cdr ns)
						   (cons n is)
						   (+ 1 j)))))))))
		       (else
			(skribe-error 'author
				      "Invalid 'lncs' author"
				      body))))))
   ;; author
   (let ((old-author (markup-writer-get 'author le)))
      (markup-writer 'author le
         :options (writer-options old-author)		     
         :action (lambda (n e)
		    (let ((name (markup-option n :name))
			  (title (markup-option n :title))
			  (inst (markup-option n 'inst)))
		       (if name (output name e))
		       (if title (output title e))
		       (if inst (format #t "\\inst{~a}\n" inst)))))))

;*---------------------------------------------------------------------*/
;*    HTML global customizations                                       */
;*---------------------------------------------------------------------*/
(let ((he (find-engine 'html)))
   (markup-writer '&html-lncs-abstract he
      :action (lambda (n e)
		 (let* ((bg (or (engine-custom e 'abstract-background)
				"#cccccc"))
			(exp (p (center (color :bg bg :width 90. 
					   (markup-body n))))))
		    (evaluate-document exp e)))))

;*---------------------------------------------------------------------*/
;*    abstract ...                                                     */
;*---------------------------------------------------------------------*/
(define-markup (abstract :rest opt :key postscript)
   (let ((w (markup-writer-get 'lncs-abstract (*current-engine*))))
     (if (writer? w)
         (new markup
            (markup 'lncs-abstract)
            (options (the-options opt))
            (loc  &invocation-location)
            (body (the-body opt)))
         (let ((a (new markup
                     (markup '&html-lncs-abstract)
                     (body (the-body opt)))))
           (list (if postscript
                     (chapter :number #f :toc #f :title "Postscript Download"
                              postscript))
                 (chapter :number #f :toc #f :title "Abstract" a)
                 (chapter :number #f :toc #f :title "Table of Contents"
                          (toc :subsection #t)))))))

;*---------------------------------------------------------------------*/
;*    references ...                                                   */
;*---------------------------------------------------------------------*/
(define* (references :key (sort #f))
   (let ((sort-proc (or sort bib-sort/first-author-last-name)))
     (list "\n\n"
           (if (engine-format? "latex")
               (font :size -1
                     (flush :side 'left
                            (the-bibliography :sort sort-proc)))
               (chapter :title "References"
                        (font :size -1
                              (the-bibliography :sort sort-proc)))))))

(define (bib-entry-template kind)
  ;; Return the LNCS bibliography entry template for KIND.
  (case kind
    ((techreport)
     `(author ": " (or title url documenturl) ". "
              ;; TRANSLATORS: The next few msgids are fragments of
              ;; bibliography items.
              ,(G_ "Technical Report") " " number
              (", " institution)
              (", " address)
              (", " pages)
              (" (" year ")")))
    ((article)
     `(author ": " (or title url documenturl) ". "
              ,(G_ "In: ") journal ", " volume
              ("(" number ")") ", "
              (address ", ")
              ("pp. " pages) (" (" year ")")))
    ((inproceedings)
     '(author ": " (or title url documenturl) ". "
              ,(G_ "In: ") booktitle ", "
              (series)
              ("(" number "), ")
              (publisher ", ")
              ("pp. " pages)
              (" (" year ")")))
    ((book)
     '((or author editor) ": "
       (or title url documenturl) ". "
       publisher
       (", " address)
       (", " month)
       ", " year
       (", pp. " pages)))
    ((inbook)
     `(author ": " (or title url documenturl) ". "
              ,(G_ "In: ") booktitle ", " publisher
              (", " editor " (" ,(G_ "editor") ")")
              (", " ,(G_ "Chapter ") chapter)
              (", pp. " pages)
              (" (" year ")")))
    ((phdthesis)
     `(author ": " (or title url documenturl)
              ", " ,(G_ "PhD Thesis")
              (", " (or school institution))
              (", " address)
              (", " month)
              (if month " " ", ") year))
    ((misc)
     '(author ": " (or title url documenturl) ". "
              (institution ", ")
              (publisher ", ")
              (address ", ")
              (month " ") ("(" year ")")
              (" " url)))
    (else
     '(author ": " (or title url documenturl) ". "
              (publisher ", ")
              (address ", ")
              (month " ")
              (", pp. " pages)
              (" (" year ")")))))


;;;
;;; Writers for LaTeX's `llncs' document class.
;;;

(when-engine-is-loaded 'latex
  (lambda ()
    (let ((latex (find-engine 'latex)))

      ;; Use the `abstract' command provided by the `llncs' class.
      (markup-writer 'lncs-abstract latex
         :before "\n\\begin{abstract}\n"
         :after  "\n\\end{abstract}\n")


      ;; Use the native bibliography system (BibTeX).

      (markup-writer 'bib-ref latex
         :options '(:text :bib)
         :action (lambda (n e)
                   (let ((entry (handle-ast (markup-body n))))
                     (format #t "\\cite{~a}" (markup-ident entry)))))

      (markup-writer 'bib-ref+ latex
         :options '(:text :bib :sort-bib-refs)
         :action (lambda (n e)
                   (let ((entries   (map (lambda (bib-ref)
                                           (handle-ast (markup-body bib-ref)))
                                         (markup-body n)))
                         (sort-proc (markup-option n :sort-bib-refs)))
                     (format #t "\\cite{~a}"
                             (string-join (map markup-ident
                                               (if (procedure? sort-proc)
                                                   (sort entries sort-proc)
                                                   entries))
                                          ",")))))

      (markup-writer '&the-bibliography latex
         :before (lambda (n e)
                   (let ((count (length (markup-body n))))
                     (format #t "\\begin{thebibliography}{~a}\n"
                             count)))
         :after  "\\end{thebibliography}\n")

      (markup-writer '&bib-entry-body
         :action (lambda (n e)
                   (let* ((kind (markup-option n 'kind))
                          (template (bib-entry-template kind)))
                     (output-bib-entry-template n e template))))

      (markup-writer '&bib-entry latex
         :action (lambda (n e)
                   (display "%\n\\bibitem[")
                   (output (markup-option n :title) e)
                   (format #t "]{~a}\n" (markup-ident n))
                   (output n e (markup-writer-get '&bib-entry-body e)))
         :after "\n%\n")

      ;; Abbreviate author first names like this: "Stallman, R.".
      (markup-writer '&bib-entry-author
         :action (lambda (n e)
                   (let ((names (markup-body n)))
                     (evaluate-document
                      (if (string? names)
                          (abbreviate-first-names
                           names
                           abbreviate-author-first-names/family-first)
                          names)
                      e))))

      ;; Journal and book titles must not be italicized.
      (markup-writer '&bib-entry-booktitle
         :action (lambda (n e)
                   (let ((title (markup-body n)))
                     (evaluate-document title e))))

      (markup-writer '&bib-entry-journal
         :action (lambda (n e)
                   (evaluate-document (markup-body n) e))))))

;;; lncs.scm ends here