From 8c0750cdb6d100083f09c326d050763762347c76 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Sat, 6 Aug 2016 11:59:39 +0200 Subject: [PATCH] improves links to author's web page --- bibtexbrowser-documentation.wiki | 10 ++++++++ bibtexbrowser-test.php | 24 +++++++++++++++--- bibtexbrowser.php | 42 +++++++++++++++++++++----------- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/bibtexbrowser-documentation.wiki b/bibtexbrowser-documentation.wiki index fc011e7..4d5a86e 100755 --- a/bibtexbrowser-documentation.wiki +++ b/bibtexbrowser-documentation.wiki @@ -494,6 +494,16 @@ include('bibtexbrowser.php'); ?> +====How to add hyperlink to the authors' web pages?==== + +You must add a ''@string'' block in the bibtex file, which contains the links. The key is a concatenation of ''hp_'' and then ''FirstNameLastName'' (no space). For instance: + +
+@String { 
+  hp_ericfoo      = {http://www.foo.de/},
+  hp_joebar      = {http://www.joebar.me/},
+}
+
 
 =====Related tools=====
 
diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php
index 50a5639..26fbe3b 100755
--- a/bibtexbrowser-test.php
+++ b/bibtexbrowser-test.php
@@ -217,7 +217,7 @@ class BTBTest extends PHPUnit_Framework_TestCase {
   
   function test_math_cal() {
     $test_data = fopen('php://memory','x+');
-    fwrite($test_data, "@book{aKey,title={{A Book{} $\mbox{foo}$ tt $\boo{t}$}} ,author={Martin Monperrus},publisher={Springer},year=2009}\n".
+    fwrite($test_data, "@book{aKey,title={{A Book $\mbox{foo}$ tt $\boo{t}$}} ,author={Martin Monperrus},publisher={Springer},year=2009}\n".
     "@String{x=2008}\n"
     );
     fseek($test_data,0);
@@ -458,8 +458,26 @@ class BTBTest extends PHPUnit_Framework_TestCase {
         $this->assertEquals("Meyer, Heribert", $authors[0]);
         $this->assertEquals("Advanced Air and Ground Research Team", $authors[1]);
         $this->assertEquals("Foo Bar", $authors[2]);
-   }
-   
+    }
+    
+    function test_latex2html() {
+        $this->assertEquals('é', latex2html("\'e"));    
+    }
+
+    function test_homepage_link() {
+        $bibtex = "@string{hp_MartinMonperrus={http://www.monperrus.net/martin},hp_FooAcé={http://example.net/}},@article{aKey61,title={An article Book},author = {Martin Monperrus and Foo Acé and Monperrus, Martin}}\n";
+        $test_data = fopen('php://memory','x+');
+        fwrite($test_data, $bibtex);
+        fseek($test_data,0);
+        $db = new BibDataBase();
+        $db->update_internal("inline", $test_data);
+        $entry = $db->getEntryByKey('aKey61');
+        $authors = $entry->getFormattedAuthorsArray();
+        $this->assertEquals('Martin Monperrus', $authors[0]);
+        $this->assertEquals('Foo Acé', $authors[1]);
+        $this->assertEquals('Monperrus, Martin', $authors[2]);
+    }
+
 } // end class
 
 ?>
diff --git a/bibtexbrowser.php b/bibtexbrowser.php
index c70cbc3..04a8de7 100755
--- a/bibtexbrowser.php
+++ b/bibtexbrowser.php
@@ -918,7 +918,7 @@ class BibDBBuilder extends ParserDelegate {
     // we add it to the string database
     else if ($this->currentEntry->getType()=='string') {
       foreach($this->currentEntry->fields as $k => $v) {
-        $k!=Q_INNER_TYPE and $this->stringdb[$k]=new StringEntry($k,$v,$this->filename);
+        $k!=Q_INNER_TYPE and $this->stringdb[$k] = new StringEntry($k,$v,$this->filename);
       }
     }
 
@@ -933,11 +933,11 @@ class BibDBBuilder extends ParserDelegate {
       if ($type == 'CURLYTOP') {
         foreach (preg_split('/\s+and\s+/i', $value) as $author) {
           if (strlen($author)>0) {
-            $this->currentEntry->addAuthor($author);
+            $this->currentEntry->addAuthor($author, $this);
           }
         }
       } else { // 'CURLYONE', nnon-breakable
-        $this->currentEntry->addAuthor($value);
+        $this->currentEntry->addAuthor($value, $this);
       }
     }
   }
@@ -1074,10 +1074,6 @@ function latex2html($line) {
   $line = str_replace('\\k{a}','ą',$line);
   $line = str_replace('\\\'{c}','ć',$line);
 
-// clean out extra tex curly brackets, usually used for preserving capitals
-  $line = str_replace('}','', $line);
-  $line = str_replace('{','', $line);
-
   // we restore the math env
   for($i = 0; $i < count($maths); $i++) {
     $line = str_replace('__MATH'.$i.'__', $maths[$i], $line);
@@ -1211,12 +1207,19 @@ class BibEntry {
       
       // 4. transform existing encoded character in the new format
       if (function_exists('mb_convert_encoding') && OUTPUT_ENCODING != BIBTEX_INPUT_ENCODING) {
-        $vaue = mb_convert_encoding($value, OUTPUT_ENCODING, BIBTEX_INPUT_ENCODING);
+        $value = mb_convert_encoding($value, OUTPUT_ENCODING, BIBTEX_INPUT_ENCODING);
       }
 
+      // clean extra tex curly brackets, usually used for preserving capitals
+      $value = preg_replace('/^\{/','', $value);
+      $value = preg_replace('/\}$/','', $value);
+      
     } else {
       //echo "xx".$value."xx\n";
     }
+    
+
+
     $this->fields[$name] = $value;
   }
 
@@ -1358,8 +1361,13 @@ class BibEntry {
   }
 
   /** adds an author to this entry */
-  function addAuthor($author) {
+  function addAuthor($author, $db) {
     $this->author_array[] = trim($author);
+    
+    $homepage_key = $this->getHomePageKey($author);
+    if (isset($db->stringdb[$homepage_key])) {
+        $this->constants[$homepage_key] = $db->stringdb[$homepage_key]->value;
+    }
   }
   
   /** Returns the authors of this entry. If "author" is not given,
@@ -1550,6 +1558,11 @@ class BibEntry {
     return $this->formatAuthor($authors[0]) . $etal;
   }
 
+  function getHomePageKey($author) {
+  
+    return strtolower('hp_'.preg_replace('/ /', '', $this->formatAuthorCanonical(latex2html($author))));
+  }
+  
   /** add the link to the homepage if it is defined in a string
    *  e.g. @string{hp_MartinMonperrus="http://www.monperrus.net/martin"}
    *  The string is a concatenation of firstname, lastname, prefixed by hp_
@@ -1558,11 +1571,11 @@ class BibEntry {
    */
   function addHomepageLink($author) {
     // hp as home page
-    // accents are handled normally
+    // accents are normally handled 
     // e.g. @STRING{hp_Jean-MarcJézéquel="http://www.irisa.fr/prive/jezequel/"}
-    $homepage = strtolower('hp_'.preg_replace('/ /', '', $author));
-    if (isset($_GET[Q_DB]->stringdb[$homepage]))
-      $author=''.$author.'';
+    $homepage = $this->getHomePageKey($author);
+    if (isset($this->constants[$homepage]))
+      $author=''.$author.'';
     return $author;
   }
 
@@ -1580,7 +1593,7 @@ class BibEntry {
   function getFormattedEditors() {
     $editors = array();
     foreach ($this->getEditors() as $editor) {
-      $editors[]=$this->addHomepageLink($this->formatAuthor($editor));
+      $editors[]=$this->formatAuthor($editor);
     }
     if (bibtexbrowser_configuration('USE_COMMA_AS_NAME_SEPARATOR_IN_OUTPUT')) {$sep = '; ';} else {$sep = ', ';}
     if (FORCE_NAMELIST_SEPARATOR !== '') {$sep = FORCE_NAMELIST_SEPARATOR;}
@@ -1828,6 +1841,7 @@ class BibEntry {
   function getConstants() {
     $result='';
     foreach ($this->constants as $k=>$v) {
+      if (preg_match('/^hp_/',$k)) continue;
       $result.='@string{'.$k.'="'.$v."\"}\n";
     }
     return $result;