-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbibyfi.satyh
529 lines (483 loc) · 14.7 KB
/
bibyfi.satyh
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
% vim: foldmethod=marker
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Original author: T. Suwa (github: gfngfn) %
% Original repository: https://github.com/gfngfn/cs-thesis %
% %
% Author: Nakano Masaki<[email protected]> %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@require: vdecoset
@require: option
% global types {{{
type bibyfi-article =
(|
author : string list;
title : string;
journal : string;
year : string;
pages : (string * string);
% option
volume : string option;
number : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-book =
(|
author : string list;
title : string;
publisher : string;
year : string;
% option
volume : string option;
series : string option;
address : string option;
edition : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-booklet =
(|
title : string;
% option
author : (string list) option;
howpublished : string option;
address : string option;
year : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-incollection =
(|
author : string list;
title : string;
booktitle : string;
year : string;
% option
editor : (string list) option;
pages : (string * string);
organization : string option;
publisher : string option;
address : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-inproceedings =
(|
author : string list;
title : string;
booktitle : string;
year : string;
% option
editor : (string list) option;
pages : (string * string) option;
organization : string option;
publisher : string option;
address : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-proceedings =
(|
title : string;
year : string;
% option
editor : (string list) option;
publisher : string option;
organization : string option;
address : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-manual =
(|
title : string;
% option
author : (string list) option;
organization : string option;
address : string option;
edition : string option;
month : string option;
year : string option;
note : string option;
key : string option;
|)
type bibyfi-masterthesis =
(|
author : string list;
title : string;
school : string;
year : string;
% option
address : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-phdthesis =
(|
author : string list;
title : string;
school : string;
year : string;
% option
address : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-techreport =
(|
author : string list;
title : string;
institution : string;
year : string;
% option
typeof : string option;
number : string option;
address : string option;
month : string option;
note : string option;
key : string option;
|)
type bibyfi-misc =
(|
% option
author : (string list) option;
title : string option;
howpublished : string option;
month : string option;
year : string option;
note : string option;
key : string option;
|)
type bibyfi-unpublished =
(|
% option
author : string list;
title : string;
note : string;
%option
month : string option;
year : string option;
key : string option;
|)
% }}}
type bibyfi-item =
| Article of bibyfi-article
| Book of bibyfi-book
| Booklet of bibyfi-booklet
| InCollection of bibyfi-incollection
| InProceedings of bibyfi-inproceedings
| Manual of bibyfi-manual
| MasterThesis of bibyfi-masterthesis
| PhDThesis of bibyfi-phdthesis
| Misc of bibyfi-misc
| Proceedings of bibyfi-proceedings
| TechReport of bibyfi-techreport
| Unpublished of bibyfi-unpublished
| WildCard of inline-text
type bibyfi-entry-element =
(|
not-tail : inline-text -> inline-text;
tail : inline-text -> inline-text;
|)
type bibyfi-citestyle =
| CiteAsNumber
| CiteAsAuthors
| CiteAsAuthorsEtAl
| CiteAsAuthorsPlus
type bib-config = (|
sort-references: bool;
citestyle: bibyfi-citestyle;
name-shrink: bool;
|)
module BiByFi: sig
direct \cite : [string list] inline-cmd
direct +makebibliography : [bib-config?; context -> int -> bibyfi-item -> block-boxes; (string * bibyfi-item) list] block-cmd
direct \BiByFi : [] inline-cmd
val shrink-name : string -> string
val join-authors : inline-text -> inline-text -> inline-text -> inline-text list -> inline-text
val make-entry : context -> inline-boxes -> inline-boxes -> block-boxes
val join-elements : inline-text -> inline-text -> inline-text -> (bibyfi-entry-element option) list -> inline-text
val lift-elem : inline-text -> bibyfi-entry-element option
val map-elem : inline-text option -> bibyfi-entry-element option
val custom-sep : inline-text -> inline-text -> bibyfi-entry-element option -> bibyfi-entry-element option
end = struct
let bib-left-padding = 20pt
% internal functions {{{
let-rec merge xs ys = match (xs, ys) with
| (x :: xs, y :: ys) ->
if x < y
then x :: y :: merge xs ys
else y :: x :: merge xs ys
| (_ :: _, []) -> xs
| ([], _ :: _) -> ys
| ([], []) -> []
let-rec split l = match l with
| x :: y :: l -> (
let (xs, ys) = split l in
(x :: xs, y :: ys))
| x :: [] ->
([x], [])
| [] ->
([], [])
let-rec sort l =
match l with
| [] -> []
| [x] -> [x]
| [x;y] -> if x < y then [x;y] else [y;x]
| _ ->
let (xs, ys) = split l in
let xs = sort xs in
let ys = sort ys in
merge xs ys
let int-of-string s =
let digit-of-string s = match s with
| `0` -> Some(0)
| `1` -> Some(1)
| `2` -> Some(2)
| `3` -> Some(3)
| `4` -> Some(4)
| `5` -> Some(5)
| `6` -> Some(6)
| `7` -> Some(7)
| `8` -> Some(8)
| `9` -> Some(9)
| _ -> None
in
let-rec f exp s =
let l = string-length s in
match l with
| 0 -> None
| 1 ->
(Option.map (fun d -> exp * d) (digit-of-string (string-sub s (l - 1) 1)))
| _ ->
Option.bind
(f (exp * 10) (string-sub s 0 (l - 1)))
(fun n -> Option.map (fun d -> n + exp * d) (digit-of-string (string-sub s (l - 1) 1)))
in
f 1 s
% }}}
let shrinkable codepoint =
(codepoint >= 97 && codepoint <= 122)
|| (codepoint >= 65 && codepoint <= 90)
% helpers for style file {{{
let-rec shrink-name name =
let splitter = regexp-of-string `[ ]+` in
let splited = split-on-regexp splitter name in
let shrink n =
let exploded = string-explode n in
let initial = (match exploded with
| i :: _ -> (if shrinkable i
then Some(string-unexplode [i])
else None)
| _ -> None)
in
match initial with
| Some(initial) -> initial ^ `. `#
| None -> n ^ #` `#
in
let-rec f name = match name with
| [] -> ` `
| [(_, n)] -> n
| (_, n) :: last -> (shrink n) ^ f last
in
f splited
let join-authors sep-for-double sep sep-last authors =
match List.reverse authors with
| [] -> {}
| it :: [] -> it
| it2 :: it1 :: [] -> {#it1;#sep-for-double;#it2;}
| it-last :: it-rest ->
let its = it-rest
|> List.reverse
|> List.fold-left (fun acc it -> {#acc;#it;#sep;}) {}
in
{#its;#sep-last;#it-last;}
let make-entry ctx ib-num ib-main =
let ib =
inline-skip (0pt -' get-natural-width ib-num)
++ ib-num ++ ib-main ++ inline-fil
in
let pads = (20pt, 0pt, 0pt, 0pt) in
block-frame-breakable ctx pads VDecoSet.empty (fun ctx -> line-break true true ctx ib)
let lift-elem e = Some
(|
not-tail = fun sep -> {#e;#sep;};
tail = fun periodo -> {#e;#periodo;};
|)
let map-elem e = Option.map (fun e ->
(|
not-tail = fun sep -> {#e;#sep;};
tail = fun periodo -> {#e;#periodo;};
|)) e
let custom-sep sep periodo e = Option.map (fun e ->
(|
not-tail = fun _ -> e#not-tail sep;
tail = fun _ -> e#tail periodo;
|)) e
let-rec shrink l = match l with
| [] -> []
| (None) :: tail -> shrink tail
| (Some(x)) :: tail -> x :: shrink tail
let join-elements sep periodo space elements =
match elements |> shrink |> List.reverse with
| [] -> {}
| [elem] ->
elem#tail periodo
| elem-last :: elements ->
let it-body = List.fold-left (fun acc elem -> (
let it-elem = elem#not-tail sep in
{#it-elem;#space;#acc;})) {} elements
in
let tail = elem-last#tail periodo in
{#it-body;#tail;}
% }}}
% implementations {{{
let-mutable cite-logs <- []
let-inline ctx \BiByFi =
let size = get-font-size ctx in
let f = read-inline ctx in
let fd = ctx |> set-manual-rising (0pt -' (size *' 0.25)) |> read-inline in
let ib =
f {BiB} ++ kern (size *' 0.05) ++ fd{Y} ++ f{F} ++ kern(size *' 0.05) ++ fd{I}
in
script-guard Latin (no-break ib)
let join-references-smartly =
List.fold-left-adjacent (fun acc ref-n ref-n-prev ref-n-next -> (
match (ref-n-prev, ref-n-next) with
| (_, None) -> acc ^ arabic ref-n
| (None, Some(n-next)) ->
if n-next == ref-n + 1
then (arabic ref-n) ^ `–`
else (arabic ref-n) ^ `, `#
| (Some(n-prev), Some(n-next)) ->
if n-next == ref-n + 1 && n-prev + 1 == ref-n
% nothing to do because `–` was printed
then acc
else (
if n-next == ref-n + 1
% 1, 3-4
then acc ^ (arabic ref-n) ^ `–`
% 1, 3
else acc ^ (arabic ref-n) ^ `, `#
)
)) ` `
let-inline ctx \cite labels =
let sacc =
labels
|> List.fold-left (fun (numbers, fullcites) label -> (
let () = cite-logs <- (
if List.fold-left (fun acc cited -> (string-same cited label) || acc) false !cite-logs
then !cite-logs
else label :: !cite-logs
)
in
match get-cross-reference (`__bibyfi:` ^ label) with
| None -> (numbers, `unresolved` :: fullcites)
| Some(l) -> (match (int-of-string l) with
| Some(i) -> (i :: numbers, fullcites)
| None -> (numbers, l :: fullcites))
))
([], [])
|> (fun (numbers, fullcites) -> (
let numebers-str =
match numbers with
| [] -> ` `
| _ -> (numbers |> sort |> join-references-smartly)
in
let fullcites-str =
let-rec join cites = match cites with
| [] -> ` `
| c :: [] -> c
| c :: last -> c ^ `, `# ^ join last
in
join fullcites
in
numebers-str ^ fullcites-str))
in
inline-skip 2pt ++ read-inline ctx (embed-string (`[` ^ sacc ^ `]`))
let-rec sort-same f src refs =
let pick label lst = lst |> List.filter (fun l -> f l label) |> List.nth 0 in
match refs with
| [] -> []
| label1 :: tl -> (match pick label1 src with
| Some (l) -> l :: sort-same f src tl
| None -> sort-same f src tl)
let default-config = (|
sort-references = true;
citestyle = CiteAsNumber;
name-shrink = true;
|)
let authors-of-item i style name-shrink item =
let f author year = match (author, year) with
| (Some author, Some year) -> Some (author, year)
| _ -> None
in
let authors = match item with
| Article(r) -> f (Some r#author) (Some r#year)
| Book(r) -> f (Some r#author) (Some r#year)
| Booklet(r) -> f r#author r#year
| InCollection(r) -> f (Some r#author) (Some r#year)
| InProceedings(r) -> f (Some r#author) (Some r#year)
| Manual(r) -> f r#author r#year
| MasterThesis(r) -> f (Some r#author) (Some r#year)
| PhDThesis(r) -> f (Some r#author) (Some r#year)
| Misc(r) -> f r#author r#year
| Proceedings(r) -> None
| TechReport(r) -> f (Some r#author) (Some r#year)
| Unpublished(r) -> f (Some r#author) r#year
| WildCard(r) -> None
in
(match style with
| CiteAsNumber -> None
| _ -> (
authors
|> Option.map (fun (authors, year) -> (
let authors = List.map shrink-name authors |> List.reverse in
match authors with
| [] -> ` `
| n1 :: [] -> n1
| n1 :: n2 :: [] -> (
match style with
| CiteAsAuthorsPlus -> n1 ^ `+`
| _ -> n1 ^ #` and `# ^ n2)
| n1 :: ns -> (
match style with
| CiteAsAuthors -> (
match List.reverse authors with
| nlast :: ns ->
(List.fold-left (^) `, `# ns) ^ #` and `# ^ nlast
| _ -> ` `)
| CiteAsAuthorsEtAl ->
n1 ^ #` et al.`
| CiteAsAuthorsPlus ->
n1 ^ `+`
| CiteAsNumber -> ` `
)) ^ #` `# ^ year)))
|> Option.from (arabic (i + 1))
let-block ctx +makebibliography ?:cfg theme bibs =
let cfg = Option.from default-config cfg in
let bibs =
let sorted =
sort-same (fun (cited, _) label -> string-same label cited) bibs !cite-logs
|> List.reverse
in
if cfg#sort-references then sorted else bibs
in
let () =
bibs |> List.iteri (fun i (label, item) -> (
register-cross-reference (`__bibyfi:` ^ label) (authors-of-item i cfg#citestyle cfg#name-shrink item)
))
in
bibs |> List.fold-lefti (fun i bbacc (_, bibitem) -> (
bbacc +++ theme ctx (i + 1) bibitem
)) block-nil
% }}}
end