From 1b7c6cceb47138170db88c3bfb30ac15114cd214 Mon Sep 17 00:00:00 2001 From: Matthieu Guillaumin Date: Wed, 19 Mar 2014 17:56:37 +0100 Subject: [PATCH] bib2links is now encapsulated to allow user-defined functions to be used via bibtexbrowser.local.php --- bibtexbrowser.php | 149 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 115 insertions(+), 34 deletions(-) diff --git a/bibtexbrowser.php b/bibtexbrowser.php index e4a1e95..1bad98e 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -87,6 +87,8 @@ function bibtexbrowser_configure($key, $value) { @define('BIBTEXBROWSER_NO_DEFAULT', false); +// BIBTEXBROWSER_LINK_STYLE defines which function to use to display the links of a bibtex entry +@define('BIBTEXBROWSER_LINK_STYLE','bib2links_default'); // do we add [bibtex] links ? // suggested by Sascha Schnepp @define('BIBTEXBROWSER_BIBTEX_LINKS',true); @@ -1053,12 +1055,75 @@ class BibEntry { return BIBTEXBROWSER_URL.'?'.createQueryString(array(Q_KEY=>$this->getKey(), Q_FILE=>$this->filename)); } - /** returns a "[pdf]" link if relevant */ + /** Read the bibtex field $bibfield and return a link with icon or text + * e.g. given the bibtex entry: @article{myarticle, pdf={myarticle.pdf}}, + * $bibtexentry->getLink('pdf') creates a link to myarticle.pdf using the text '[pdf]'. + * $bibtexentry->getLink($bibfield,$icon,$def) specifies the icon url $icon and default text $def. + */ + function getLink($bibfield,$iconurl=NULL,$def=NULL) { + $show = true; + if ( $bibfield === 'pdf' || $bibfield === 'url' ) { $show = BIBTEXBROWSER_PDF_LINKS; }; + if ($def==NULL) { $def=$bibfield; } + $str = $this->getIconOrTxt($def,$iconurl); + if ( $show && $this->hasField($bibfield)) { + return ' '.$str.''; + } + return ''; + } + + /* a few specializations of getLink */ + + /** returns a "[url]" link if relevant. modified to exploit the new method, while keeping backward compatibility */ function getUrlLink() { - if ($this->hasField('url')) return ' [pdf]'; + return $this->getLink('url'); + } + + /** returns a "[bib]" link if relevant */ + function getBibLink($iconurl=NULL) { + if (BIBTEXBROWSER_BIBTEX_LINKS) { + $bibstr = $this->getIconOrTxt('bibtex',$iconurl); + $href = 'href="'.$this->getURL().'"'; + $link = "getKey()."\" {$href}>$bibstr"; + return $link; + } else { + return ''; + } + } + + + /** DOI are a special kind of links, where the url depends on the doi */ + function getDoiLink($iconurl=NULL) { + $str = $this->getIconOrTxt('doi',$iconurl); + if (BIBTEXBROWSER_DOI_LINKS && $this->hasField('doi')) { + return ' '.$str.''; + } return ''; } + /** GS are a special kind of links, where the url depends on the google scholar id */ + function getGSLink($iconurl=NULL) { + $str = $this->getIconOrTxt('cites',$iconurl); + // Google Scholar ID + if (BIBTEXBROWSER_GSID_LINKS && $this->hasField('gsid')) { + return ' '.$str.''; + } + return ''; + } + + /** replace [$ext] with an icon whose url is defined in a string + * e.g. getIconOrTxt('pdf') will print '[pdf]' + * or getIconOrTxt('pdf','http://link/to/icon.png') will use the icon linked by the url, or print '[pdf'] + * if the url does not point to a valid file (using the "alt" property of the "img" html tag) + */ + function getIconOrTxt($txt,$iconurl=NULL) { + if ( $iconurl==NULL ) { + $str='['.$txt.']'; + } else { + $str='['.$txt.']'; + } + return $str; + } + /** Reruns the abstract */ function getAbstract() { if ($this->hasField('abstract')) return $this->getField('abstract'); @@ -1236,6 +1301,16 @@ class BibEntry { return $this->formatAuthor($authors[0]) . $etal; } + /** + * Returns a compacted string form of author names by throwing away + * all author names except for the first one and appending ", et al." + */ + function getVeryCompactedAuthors(){ + $authors = $this->getRawAuthors(); + $etal = count($authors) > 1 ? ', et al.' : ''; + list($firstname, $lastname) = splitFullName($authors[0]); + return $lastname . $etal; + } /** add the link to the homepage if it is defined in a string * e.g. @string{hp_MartinMonperrus="http://www.monperrus.net/martin"} @@ -1391,7 +1466,7 @@ class BibEntry { break; } $result .= bib2html($this); - $result .= $this->bib2links(); + $result .= bib2links($this); switch(BIBTEXBROWSER_LAYOUT) { // close row case 'list': $result .= ''."\n"; @@ -1476,37 +1551,6 @@ class BibEntry { return ''; } - /** returns a collection of links for the given bibtex entry - * e.g. [bibtex] [doi][pdf] - */ - function bib2links() { - $href = 'href="'.$this->getURL().'"'; - - $str = ''; - - if (BIBTEXBROWSER_BIBTEX_LINKS) { - // we add biburl and title to be able to retrieve this important information - // using Xpath expressions on the XHTML source - $str .= " getKey()."\" {$href}>[bibtex]"; - } - - if (BIBTEXBROWSER_PDF_LINKS) { - // returns an empty string if no url present - $str .= $this->getUrlLink(); - } - - if (BIBTEXBROWSER_DOI_LINKS && $this->hasField('doi')) { - $str .= ' [doi]'; - } - - // Google Scholar ID - if (BIBTEXBROWSER_GSID_LINKS && $this->hasField('gsid')) { - $str .= ' [cites]'; - } - - return $str; - } - /** @@ -1581,6 +1625,37 @@ function get_HTML_tag_for_layout() { return $tag; } +/** returns a collection of links for the given bibtex entry + * e.g. [bibtex] [doi][pdf] + */ +function bib2links_default(&$bibentry) { + $href = 'href="'.$bibentry->getURL().'"'; + + $str = ''; + + if (BIBTEXBROWSER_BIBTEX_LINKS) { + // we add biburl and title to be able to retrieve this important information + // using Xpath expressions on the XHTML source + $str .= " getKey()."\" {$href}>[bibtex]"; + } + + if (BIBTEXBROWSER_PDF_LINKS) { + // returns an empty string if no url present + $str .= $bibentry->getUrlLink(); + } + + if (BIBTEXBROWSER_DOI_LINKS && $bibentry->hasField('doi')) { + $str .= ' [doi]'; + } + + // Google Scholar ID + if (BIBTEXBROWSER_GSID_LINKS && $bibentry->hasField('gsid')) { + $str .= ' [cites]'; + } + + return $str; +} + /** prints the header of a layouted HTML, depending on BIBTEXBROWSER_LAYOUT e.g. */ function print_header_layout() { @@ -1598,6 +1673,12 @@ function bib2html(&$bibentry) { return $function($bibentry); } +/** this function encapsulates the user-defined name for bib2links */ +function bib2links(&$bibentry) { + $function = BIBTEXBROWSER_LINK_STYLE; + return $function($bibentry); +} + /** encapsulates the user-defined sections. @nodoc */ function _DefaultBibliographySections() { $function = BIBLIOGRAPHYSECTIONS;