diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php
index e37aff6..1f6b84e 100755
--- a/bibtexbrowser-test.php
+++ b/bibtexbrowser-test.php
@@ -339,7 +339,7 @@ class BTBTest extends PHPUnit_Framework_TestCase {
function test_link_configuration() {
bibtexbrowser_configure('BIBTEXBROWSER_LINKS_TARGET','_self');
$test_data = fopen('php://memory','x+');
- fwrite($test_data, "@book{aKey,pdf={myarticle.pdf}}\n"
+ fwrite($test_data, "@book{aKey,pdf={myarticle.pdf}}\n@book{bKey,url={myarticle.pdf}}\n@book{cKey,url={myarticle.xyz}}\n"
);
fseek($test_data,0);
$btb = new BibDataBase();
@@ -349,6 +349,10 @@ class BTBTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('[pdf]',$first_entry->getPdfLink());
$this->assertEquals('
',$first_entry->getLink('pdf','pdficon.png'));
$this->assertEquals('[see]',$first_entry->getLink('pdf',NULL,'see'));
+ $second_entry=$btb->bibdb[array_keys($btb->bibdb)[1]];
+ $this->assertEquals('[pdf]',$second_entry->getPdfLink());
+ $third_entry=$btb->bibdb[array_keys($btb->bibdb)[2]];
+ $this->assertEquals('[url]',$third_entry->getPdfLink());
}
// see https://github.com/monperrus/bibtexbrowser/pull/14
diff --git a/bibtexbrowser.php b/bibtexbrowser.php
index aacbf76..0ed2f63 100755
--- a/bibtexbrowser.php
+++ b/bibtexbrowser.php
@@ -123,6 +123,7 @@ if (defined('ENCODING')) {
// do we add [bibtex] links ?
@define('BIBTEXBROWSER_BIBTEX_LINKS',true);
// do we add [pdf] links ?
+// if the file extention is not .pdf, the field name (pdf, url, or file) is used instead
@define('BIBTEXBROWSER_PDF_LINKS',true);
// do we add [doi] links ?
@define('BIBTEXBROWSER_DOI_LINKS',true);
@@ -1315,29 +1316,45 @@ class BibEntry {
return $link;
}
- /** same as `getPdfLink`, kept for backward compatibility */
- function getUrlLink($iconurl, $label) {
- return $this->getPdfLink($iconurl, $label);
+ /** kept for backward compatibility */
+ function getPdfLink($iconurl = NULL, $label = NULL) {
+ return $this->getUrlLink($iconurl);
}
/** returns a "[pdf]" link for the entry, if possible.
Tries to get the target URL from the 'pdf' field first, then from 'url' or 'file'.
+ Performs a sanity check that the file extension is 'pdf' or 'ps' and uses that as link label.
+ Otherwise (and if no explicit $label is set) the field name is used instead.
*/
- function getPdfLink($iconurl = NULL, $label = 'pdf') {
+ function getUrlLink($iconurl = NULL) {
if ($this->hasField('pdf')) {
- return $this->getLink('pdf', $iconurl, $label);
+ return $this->getAndRenameLink('pdf', $iconurl);
}
if ($this->hasField('url')) {
- return $this->getLink('url', $iconurl, $label);
+ return $this->getAndRenameLink('url', $iconurl);
}
// Adding link to PDF file exported by Zotero
// ref: https://github.com/monperrus/bibtexbrowser/pull/14
if ($this->hasField('file')) {
- return $this->getLink('file', $iconurl, $label);
+ return $this->getAndRenameLink('file', $iconurl);
}
return "";
}
+ /** See description of 'getUrlLink'
+ */
+ function getAndRenameLink($bibfield, $iconurl=NULL) {
+ $extension = strtolower(pathinfo(parse_url($this->getField($bibfield),PHP_URL_PATH),PATHINFO_EXTENSION));
+ switch ($extension) {
+ // overriding the label if it's a known extension
+ case 'html': return $this->getLink($bibfield, $iconurl, 'html'); break;
+ case 'pdf': return $this->getLink($bibfield, $iconurl, 'pdf'); break;
+ case 'ps': return $this->getLink($bibfield, $iconurl, 'ps'); break;
+ default:
+ return $this->getLink($bibfield, $iconurl, $bibfield);
+ }
+ }
+
/** DOI are a special kind of links, where the url depends on the doi */
@@ -2005,7 +2022,7 @@ function bib2links_default($bibentry) {
}
if (BIBTEXBROWSER_PDF_LINKS) {
- $link = $bibentry->getPdfLink();
+ $link = $bibentry->getUrlLink();
if ($link != '') { $links[] = $link; };
}