feat: only display [pdf] if it is really a pdf, else use field name (#96)

This commit is contained in:
Jens Kober
2019-03-21 09:03:45 +01:00
committed by Martin Monperrus
parent 6c9daef93e
commit 4387468008
2 changed files with 30 additions and 9 deletions
+5 -1
View File
@@ -339,7 +339,7 @@ class BTBTest extends PHPUnit_Framework_TestCase {
function test_link_configuration() { function test_link_configuration() {
bibtexbrowser_configure('BIBTEXBROWSER_LINKS_TARGET','_self'); bibtexbrowser_configure('BIBTEXBROWSER_LINKS_TARGET','_self');
$test_data = fopen('php://memory','x+'); $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); fseek($test_data,0);
$btb = new BibDataBase(); $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">[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"><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')); $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 // see https://github.com/monperrus/bibtexbrowser/pull/14
+25 -8
View File
@@ -123,6 +123,7 @@ if (defined('ENCODING')) {
// do we add [bibtex] links ? // do we add [bibtex] links ?
@define('BIBTEXBROWSER_BIBTEX_LINKS',true); @define('BIBTEXBROWSER_BIBTEX_LINKS',true);
// do we add [pdf] links ? // 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); @define('BIBTEXBROWSER_PDF_LINKS',true);
// do we add [doi] links ? // do we add [doi] links ?
@define('BIBTEXBROWSER_DOI_LINKS',true); @define('BIBTEXBROWSER_DOI_LINKS',true);
@@ -1315,29 +1316,45 @@ class BibEntry {
return $link; return $link;
} }
/** same as `getPdfLink`, kept for backward compatibility */ /** kept for backward compatibility */
function getUrlLink($iconurl, $label) { function getPdfLink($iconurl = NULL, $label = NULL) {
return $this->getPdfLink($iconurl, $label); return $this->getUrlLink($iconurl);
} }
/** returns a "[pdf]" link for the entry, if possible. /** 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'. 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')) { if ($this->hasField('pdf')) {
return $this->getLink('pdf', $iconurl, $label); return $this->getAndRenameLink('pdf', $iconurl);
} }
if ($this->hasField('url')) { if ($this->hasField('url')) {
return $this->getLink('url', $iconurl, $label); return $this->getAndRenameLink('url', $iconurl);
} }
// Adding link to PDF file exported by Zotero // Adding link to PDF file exported by Zotero
// ref: https://github.com/monperrus/bibtexbrowser/pull/14 // ref: https://github.com/monperrus/bibtexbrowser/pull/14
if ($this->hasField('file')) { if ($this->hasField('file')) {
return $this->getLink('file', $iconurl, $label); return $this->getAndRenameLink('file', $iconurl);
} }
return ""; 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 */ /** 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) { if (BIBTEXBROWSER_PDF_LINKS) {
$link = $bibentry->getPdfLink(); $link = $bibentry->getUrlLink();
if ($link != '') { $links[] = $link; }; if ($link != '') { $links[] = $link; };
} }