From 946798f41b367117704d35a8180941bac768402d Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Tue, 29 Sep 2015 22:56:53 +0200 Subject: [PATCH] fixes bug in generated hyperlinks. Closes #40. --- bibtexbrowser-test.php | 15 +++++++++++++++ bibtexbrowser.php | 11 ++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index 6c009a3..ba25471 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -193,6 +193,21 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertEquals('[pdf]',$first_entry->getUrlLink()); } + // https://github.com/monperrus/bibtexbrowser/issues/40 + function test_doi_url() { + $test_data = fopen('php://memory','x+'); + fwrite($test_data, "@Article{Baldwin2014Quantum,Doi={10.1103/PhysRevA.90.012110},Url={http://link.aps.org/doi/10.1103/PhysRevA.90.012110}}" + ); + fseek($test_data,0); + $btb = new BibDataBase(); + $btb->update_internal("inline", $test_data); + $first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]]; + $this->assertEquals('
@Article{Baldwin2014Quantum,Doi={10.1103/PhysRevA.90.012110},Url={http://link.aps.org/doi/10.1103/PhysRevA.90.012110}}
',$first_entry->toEntryUnformatted()); + } + + + + } // end class ?> diff --git a/bibtexbrowser.php b/bibtexbrowser.php index 6ced37d..9d460dc 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1662,13 +1662,22 @@ class BibEntry { $entry = htmlspecialchars($this->getFullText()); // Fields that should be hyperlinks + // the order matters $hyperlinks = array('url' => '%O', 'file' => '%O', 'pdf' => '%O', 'doi' => 'http://dx.doi.org/%O', 'gsid' => 'http://scholar.google.com/scholar?cites=%O'); + $vals = array(); foreach ($hyperlinks as $field => $url) { if ($this->hasField($field)) { $href = str_replace('%O', $this->getField($field), $url); // this is not a parsing but a simple replacement - $entry = str_replace($this->getField($field), ''.$this->getField($field).'', $entry); + $entry = str_replace($this->getField($field), '___'.$field.'___', $entry); + $vals[$field] = $href; + } + } + foreach ($vals as $field => $href) { + if ($this->hasField($field)) { + // this is not a parsing but a simple replacement + $entry = str_replace('___'.$field.'___', ''.$this->getField($field).'', $entry); } }