File: bibliography.tcl

package info (click to toggle)
scid 1%3A4.3.0.cvs20120311-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 24,232 kB
  • sloc: tcl: 135,238; cpp: 47,810; ansic: 5,339; sh: 2,882; python: 278; makefile: 105; perl: 86
file content (356 lines) | stat: -rw-r--r-- 12,586 bytes parent folder | download
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
###
### bibliography.tcl: part of Scid.
### Copyright (C) 2010 Alexander Wagner
###
### $Id: bibliography.tcl,v 4.3 2011/02/13 18:12:02 arwagner Exp $
###
### Last change: <Tue, 2010/07/13 23:08:01 arwagner ingata>
###
### Handle Bib pgn headers to show bibliographic references for a
### given game. The bibliographic database should be available in
### BibTeX format. The ref header should be of the form:
###
### [Bib "<bibtexkey>, <additional info> ; <bibtexkey>, <additional info>]
###
### E.g.
###     [Bib "Botvinnik:1960, No. 27 p.57"]
###
### Refering to the BibTeX record:
###
###     @BOOK{Botvinnik:1960,
###       title = {{O}ne hundred selected games},
###       publisher = {Dover Pub.},
###       year = {1960},
###       author = {Botvinnik, M. M.},
###       pages = {272},
###       address = {New York},
###       keywords = {Game Collection},
###       refid = {1106902}
###     }

###
#======================================================================

set m .menu.tools

# $m add command -label "Database information..." -command {::metadata::show}

#======================================================================
#
#======================================================================
namespace eval Bibliography {

	set literature ""

image create photo tb_bibliography -data {
	R0lGODlhEAAPAMZrAAAAAA4ODg0RFRYdJB0gIyUlJSInLiIsNC4vNC41OzM7Qi89STw8PDpETkFG
	TEJHS0JHTEVITD1KV0VJTj5MVzFOazJOa0ZLUDJPbElMTjNQbjRRbUtOUTVSbk5OTkVQXDpXdTxY
	dE5WXT1ZdD9adVZXWUJceFhbX0Rgfk5fcUlgeEVhfU1gcVteYUpielhgaUhkgltiaU5lfFNnfGJl
	aU1ph2JmaU9riWVpbmFqc1lsgFNtimdqbVJujFNvjV1vg2NvfF1xe15xhGBze1h0knBxc1p2k114
	lGx5cmB7mHF+d2V/m2aAnG5/kGuFoXSFloKIdHqKm36Pn4+UmYKYsY2gtqChgZ6lr5qqvJ6ruZut
	v6Sxv66wsqqyuqu6ya+7ybu9wMvMze7WgN3h5Obm6OTn6efo6evt7u7v8O/w8PDx8v//////////
	/////////////////////////////////////////////////////////////////////////yH+
	EUNyZWF0ZWQgd2l0aCBHSU1QACH5BAEKAH8ALAAAAAAQAA8AAAekgH+Cg4SFhQYAiYqKhgFYRzsr
	IyEkJikAhABeSzdinlAWKhSFBVpGVkMaFWIdKSKYggBbTD0oSp5ILhIZRYMMVUQ1QWIVFRszCSWw
	AF9OPjAgGMUyCzywfx5USTcoxMUsCg/LV1JRT01CPz86HyfXAC8IExwcEQ4QFzg07zk2XGZk0qA5
	E2bKtT8EEjUAkmVMGTVgWhwctOhAjC4TDf0RkGhAoUAAOw==
}

if {$png_image_support} {
	image create photo tb_bibliography -data {
		iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAYAAADtc08vAAACYElEQVR42q2TS0hUcRSHv7l3nHFG
		HXykk4KTmIObbGVpJG1aFSFFVEK7WojkplZBaEGbFolCEUhZSIkRlWXpYiaJsqRMsxoKo8YHSfkY
		argzdx733rn/FjoSItSiA2dzOOc7L37wn81eXrm1ExB/c0dOfjMgWdYAGlwuV+/Fa/cZfjtJTI2h
		RCMkEnGEAFmyIEsy2XKS/u52ANm6BtDbddeP7+UHlIjKjRNxQAayudwf5cFIDHd+DuHQdwHsBMw/
		AQe9Xi/hSAw1nuDA9gSnunQCwQimmWLoQiGPRjUyRJJcpw1gBLCkV5AB49bDpzyfCBL6GUZRFKo8
		SVoaHAC03lSYWsgkMh80s6U4lV7vvY72tsPpCY7X1dWx8CuOEo2h6zrFeSlaGhzsPr203EGW2FTk
		ZG5pjuJyDx3tbYcAW3oC0ed/zZPRSeYXQ6iqihJJH08AUOrOY3ZynNqqCjqvXALYBQxLwP76+noC
		wXnUWBzDMDAMg8dns1aLAYSuYjWTfAlOmSuhYQALIK739ImZxbDF0A00XUfTNMJKFGEuA4xUitDc
		VwqyZG73dJNhz2zVk4nzAFYAn99n+TTxJuUuKZFtGTIAmqZjmMtntlqtlHk8pAwd4GO6eBXgcDqp
		rtkm79u7hx21NQhT4HBmIgSkTJOZ6WlevBrjZHMjFkk6J0xzdTW5tGyz85l/IGdifMz9LaSqgc+z
		Ns1ix70hFyEEDrudqelZBgcHCLx/B0IcWU8DBUATcBUYAn4AonxLtXb0WJNxp9+nA8Lpyj/zL4La
		uPKixpy8wq60eIpKK/zrJf8GjDkZQZW6Gi0AAAAASUVORK5CYII=
	}
}

	#----------------------------------------------------------------------
	# Take a ref structure and turn it into a simplified version of
	# ISBD reference output
	#----------------------------------------------------------------------
	proc Ref2ISBD { Ref Biblio} {

		set w .showRef

		set key [lindex $Ref 0]
		set type ""
		set address   ""; set author    ""; set editor    ""; set number    "";
		set isbn      ""; set issn      ""; set issue     ""; set pages     "";
		set publisher ""; set title     ""; set volume    ""; set year      "";
		set edition   ""; set keywords  "";

		foreach tag { type address author editor number isbn issn issue pages publisher title volume year edition keywords } {
			set $tag [lindex [lindex $Ref [lsearch -exact -index 0 $Ref $tag]] 1]
		}
		regsub -all -nocase { and }    $author "; " author
		set isbd ""

		$w.f.tree insert {} end -id $key -text "$type: $author ($key)" -open true
		$w.f.tree insert $key end -id "l2$key" -text "$author" -tags {author}
		$w.f.tree insert $key end -id "l3$key" -text "$title"  -tags {title}
		$w.f.tree insert $key end -id "l4$key" -text "$Biblio" -tags {reference}
		if {$editor != ""} {
			$w.f.tree insert $key end -id "l5$key" -text "Ed. $editor"
		}
		if {$edition != ""} {
			$w.f.tree insert $key end -id "l6$key" -text  "$edition"
		}
		$w.f.tree insert $key end -id "l7$key" -text  "$address : $publisher, $year. - "
		if {$pages != ""} {
			$w.f.tree insert $key end -id "l8$key" -text  "$pages"
		}
		if {$isbn != ""} {
			$w.f.tree insert $key end -id "l9$key" -text  "$isbn"
		} elseif {$issn != ""} {
			$w.f.tree insert $key end -id "l9$key" -text  "$issn"
		}
		if {$keywords != ""} {
			$w.f.tree insert $key end -id "l0$key" -text  "$keywords"
		}

		### set isbd "\[$type\]\n"
		### set isbd "$author\n$title / $author.\n"
		### if {$editor != ""} {
		### 	set isbd "$isbd Ed. $editor\n"
		### }
		### set isbd "$isbd $edition\n"
		### set isbd "$isbd $address : $publisher, $year. - "
		### set isbd "$isbd $pages\n$isbn\n\n$keywords"

		# Simplified ISBD for book
		if {$type == "BOOK"} {
			set isbd "$author:\n$title / $author. Ed. $editor.\n- $edition -\n$address : $publisher, $year. - $pages\n$isbn\n\n$keywords\n"
		} elseif {$type == "PERIODICAL"} {
			# Simplified ISBD for journal
			set isbd "\[Journal\]\n$title / publ. $editor\n$address : $publisher, $year. - $pages\n$issn\n\n$keywords\n"
		} else {
			# use book format by default, should be improved
			set isbd "\[$type\]\n$author:\n$title / $author. Ed. $editor.\n - $edition - $address : $publisher, $year. - $pages\n$isbn ($issn)\n\n$keywords\n"
		}
		### set isbd "$isbd\n------------------------------------------------------------\n";

		return $isbd
	}

	#----------------------------------------------------------------------
	# Simple BibTeX reader. Does not handle all the funny things of
	# BibTeX like crossref etc. assumes all tags in a single line
	#----------------------------------------------------------------------
	proc ReadBibTeX { bibfile } {
		set fd [open $bibfile]

		set Bibentries {}

		set type ""
		set key  ""
		set address   ""; set author    ""; set editor    ""; set number    "";
		set isbn      ""; set issn      ""; set issue     ""; set pages     "";
		set publisher ""; set title     ""; set volume    ""; set year      "";
		set edition   ""; set keywords  "";

		while { [gets $fd line] >= 0} {
			if {[regexp -- "@" $line]} {
				# begin of dataset
				lappend Bibentries [list \
					"$key" \
					[list "type" $type] \
					[list "title" $title] \
					[list "editor" $editor] \
					[list "author" $author] \
					[list "publisher" $publisher] \
					[list "pages" $pages] \
					[list "address" $address] \
					[list "isbn" $isbn] \
					[list "issn" $issn] \
					[list "volume" $volume] \
					[list "issue" $issue] \
					[list "number" $number] \
					[list "edition" $edition] \
					[list "keywords" $keywords] \
					[list "year"  $year] ]

				regsub {\{.*} $line "" type
				regsub {\@} $type "" type
				## set type [string lower type]
				regsub {.*\{} $line "" key
				regsub {,}    $key  "" key

				set key [string trim $key]
				set type [string trim $type]

				set address   ""; set author    ""; set editor    ""; set number    "";
				set isbn      ""; set issn      ""; set issue     ""; set pages     "";
				set publisher ""; set title     ""; set volume    ""; set year      ""
				set edition   ""; set keywords  "";
			} else {
				regsub -all { "} $line {} line
				regsub -all {" } $line {} line
				regsub -all {,$} $line {} line
				regsub -all {[\{\}]} $line {} line

			}
			if {[regexp -- {[ \t]title[ \t=]} $line]} {
				regsub -all {title .*= } $line {} title
				set title [string trim $title]
			}
			if {[regexp -- {[ \t]author[ \t=]} $line]} { 
				regsub -all {author .*= } $line {} author
				set author [string trim $author]
			}
			if {[regexp -- {[ \t]editor[ \t=]} $line]} { 
				regsub -all {editor .*= } $line {} editor
				set editor [string trim $editor]
			}
			if {[regexp -- {[ \t]year[ \t=]} $line]} {
				regsub -all {year .*= } $line {} year
				set year [string trim $year]
			}
			if {[regexp -- {[ \t]publisher[ \t=]} $line]} {
				regsub -all {publisher .*= } $line {} publisher
				set publisher [string trim $publisher]
			}
			if {[regexp -- {[ \t]address[ \t=]} $line]} {
				regsub -all {address .*= } $line {} address
				set address [string trim $address]
			}
			if {[regexp -- {[ \t]isbn[ \t=]} $line]} {
				regsub -all {isbn.*= } $line {} isbn
				set isbn [string trim $isbn]
			}
			if {[regexp -- {[ \t]issn[ \t=]} $line]} {
				regsub -all {issn.*= } $line {} issn
				set issn [string trim $issn]
			}
			if {[regexp -- {[ \t]issue[ \t=]} $line]} {
				regsub -all {issue.*= } $line {} issue
				set issue [string trim $issue]
			}
			if {[regexp -- {[ \t]volume[ \t=]} $line]} {
				regsub -all {volume.*= } $line {} volume
				set volume [string trim $volume]
			}
			if {[regexp -- {[ \t]number[ \t=]} $line]} {
				regsub -all {number.*= } $line {} number
				set number [string trim $number]
			}
			if {[regexp -- {[ \t]edition[ \t=]} $line]} {
				regsub -all {edition.*= } $line {} edition
				set edition [string trim $edition]
			}
			if {[regexp -- {[ \t]keywords[ \t=]} $line]} {
				regsub -all {keywords.*= } $line {} keywords
				set keywords [string trim $keywords]
			}
		}
		close $fd
		return $Bibentries
	}

	#----------------------------------------------------------------------
	# Extract the Ref header from the current game. Uses bibfile as
	# input for bibliographic information, returns a block of ISBD
	# formatted entries
	#----------------------------------------------------------------------
	proc ExtractRef { bibfile } {

		set w .showRef

		set Extra  [sc_game tags get Extra]
		set extraTagsList [split $Extra "\n"]

		set literature ""

		foreach i $extraTagsList {
			if { [string equal -nocase [lindex $i 0] "Bib" ] } {
				set ref [string range $i 5 end-1]

				regsub -all { ; } $ref {|} ref

				# Tcl uses a list of split chars as second argument :(
				set ref [split $ref "|"]
				
				set BibEntries [::Bibliography::ReadBibTeX $bibfile]

				foreach r $ref {
					regsub -all {, .*} $r {} BibKey
					regsub -all {.*, } $r {} Biblio
					set BibKey [string trim $BibKey]
					set Biblio [string trim $Biblio]
					set idx [lsearch -exact -index 0 $BibEntries $BibKey]
					set BibDta [lindex $BibEntries $idx]

					set isbd [::Bibliography::Ref2ISBD $BibDta $Biblio]
					set literature "$literature\n$isbd\n$Biblio\n\n"
				}
			}
		}
		set lit [string trim $literature]
		return $lit
	}

	#----------------------------------------------------------------------
	# Open a window with an expanded tree widget containing all
	# referenced entities from the BibTeX file.
	#----------------------------------------------------------------------
	proc ShowRef {} {
		global scidDataDir

		set w .showRef

		if { [winfo exists $w] } {
			# recreate the widget with new data
			destroy $w
		}

		toplevel $w
		::setTitle $w "References"
		setWinLocation $w
		setWinSize $w

		ttk::frame $w.f
		pack $w.f -fill both -expand 1

		ttk::treeview  $w.f.tree -xscrollcommand "$w.f.xbar set" -yscrollcommand "$w.f.ybar set" -show tree -selectmode none
		ttk::scrollbar $w.f.xbar -command "$w.f.tree xview" -orient horizontal
		ttk::scrollbar $w.f.ybar -command "$w.f.tree yview" -orient vertical
		$w.f.tree column #0 -minwidth 1200

		$w.f.tree tag configure author -foreground blue
		$w.f.tree tag configure title -foreground blue
		$w.f.tree tag configure reference -foreground red

		pack $w.f.xbar -side bottom -fill x
		pack $w.f.ybar -side right  -fill y
		pack $w.f.tree -side left   -expand 1 -fill both

		# set a global bib file
		set bibfile [file nativename [file join $scidDataDir "Bookshelf.bib"]]
		# ... but overwrite it if a bibliography for the base itself exits
		if {[file readable "[sc_base filename].bib"]} {
			set bibfile "[sc_base filename].bib"
		}
		set ::Bibliography::literature [::Bibliography::ExtractRef $bibfile]

		bind $w <Escape> { destroy  .showRef}
		bind $w <Configure>  {
			recordWinSize .showRef
		}
		# Copy references to clipboard
		bind $w <Control-Insert> { clipboard clear; clipboard append $::Bibliography::literature }
		bind $w <Control-c>      { clipboard clear; clipboard append $::Bibliography::literature }
	}

}

###
### End of file: bibliography.tcl
###