feat: only display [pdf] if it is really a pdf, else use field name (#96)
This commit is contained in:
committed by
Martin Monperrus
parent
6c9daef93e
commit
4387468008
@@ -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('<a href="myarticle.pdf">[pdf]</a>',$first_entry->getPdfLink());
|
||||
$this->assertEquals('<a href="myarticle.pdf"><img class="icon" src="pdficon.png" alt="[pdf]" title="pdf"/></a>',$first_entry->getLink('pdf','pdficon.png'));
|
||||
$this->assertEquals('<a href="myarticle.pdf">[see]</a>',$first_entry->getLink('pdf',NULL,'see'));
|
||||
$second_entry=$btb->bibdb[array_keys($btb->bibdb)[1]];
|
||||
$this->assertEquals('<a href="myarticle.pdf">[pdf]</a>',$second_entry->getPdfLink());
|
||||
$third_entry=$btb->bibdb[array_keys($btb->bibdb)[2]];
|
||||
$this->assertEquals('<a href="myarticle.xyz">[url]</a>',$third_entry->getPdfLink());
|
||||
}
|
||||
|
||||
// see https://github.com/monperrus/bibtexbrowser/pull/14
|
||||
|
||||
+25
-8
@@ -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; };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user