From 503eff16703c9bd06cdd4c157503a85d1feee7b1 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Wed, 22 Sep 2021 07:31:16 +0200 Subject: [PATCH] refactor: move cff code to library --- BibtexbrowserTest.php | 26 +++++++++++++++++++++++++- bibtex-to-cff.php | 26 +------------------------- bibtexbrowser.php | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/BibtexbrowserTest.php b/BibtexbrowserTest.php index 5a08cfb..daff67b 100755 --- a/BibtexbrowserTest.php +++ b/BibtexbrowserTest.php @@ -823,7 +823,31 @@ class BibtexbrowserTest extends PHPUnit_Framework_TestCase { } - + function test_cff() { + + $btb = new BibDataBase(); + $btb->load('bibacid-utf8.bib'); + $entry = $btb->bibdb['arXiv-1807.05030']; + $expected = "cff-version: 1.2.0\n". + "# CITATION.cff created with https://github.com/monperrus/bibtexbrowser/\n". + "preferred-citation:\n". + " title: \"A Comprehensive Study of Pseudo-tested Methods\"\n". + " year: \"2018\"\n". + " authors:\n". + " - family-names: Vera-Pérez\n". + " given-names: Oscar Luis\n". + " - family-names: Danglot\n". + " given-names: Benjamin\n". + " - family-names: Monperrus\n". + " given-names: Martin\n". + " - family-names: Baudry\n". + " given-names: Benoit\n"; + + $this->assertEquals($expected,$entry->toCFF()); + + } + + } // end class @copy('bibtexbrowser.local.php.bak','bibtexbrowser.local.php'); diff --git a/bibtex-to-cff.php b/bibtex-to-cff.php index 2ec050f..b795e0b 100755 --- a/bibtex-to-cff.php +++ b/bibtex-to-cff.php @@ -21,31 +21,7 @@ function bibtexbrowser_cff($arguments) { $current_entry = $db->getEntryByKey($arguments[$i+1]); } } - // now we have $current_entry - echo "cff-version: 1.2.0"."\n"; - echo "# CITATION.cff created with https://github.com/monperrus/bibtexbrowser/"."\n"; - echo "preferred-citation:"."\n"; - echo " title: \"".$current_entry->getTitle()."\""."\n"; - if ($current_entry->hasField("doi")) { - echo " doi: \"".$current_entry->getField("doi")."\""."\n"; - } - if ($current_entry->hasField("year")) { - echo " year: \"".$current_entry->getField("year")."\""."\n"; - } - if ($current_entry->hasField("journal")) { - echo " type: article\n"; - echo " journal: \"".$current_entry->getField("journal")."\""."\n"; - } - if ($current_entry->hasField("booktitle")) { - echo " type: conference-paper\n"; - echo " conference: \"".$current_entry->getField("booktitle")."\""."\n"; - } - echo " authors:"."\n"; - foreach ($current_entry->getFormattedAuthorsArray() as $author) { - $split = splitFullName($author); - echo " - family-names: ".$split[1]."\n"; - echo " given-names: ".$split[0]."\n"; - } + echo $current_entry->toCFF(); } bibtexbrowser_cff($argv); diff --git a/bibtexbrowser.php b/bibtexbrowser.php index b5ac58b..368bb06 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -1980,7 +1980,38 @@ class BibEntry { return $matches; } -} // enc class BibEntry + /** returns in the citation file format, tailored for github */ + function toCFF() { + $result = ""; + $result .= "cff-version: 1.2.0"."\n"; + $result .= "# CITATION.cff created with https://github.com/monperrus/bibtexbrowser/"."\n"; + $result .= "preferred-citation:"."\n"; + $result .= " title: \"".$this->getTitle()."\""."\n"; + if ($this->hasField("doi")) { + $result .= " doi: \"".$this->getField("doi")."\""."\n"; + } + if ($this->hasField("year")) { + $result .= " year: \"".$this->getField("year")."\""."\n"; + } + if ($this->hasField("journal")) { + $result .= " type: article\n"; + $result .= " journal: \"".$this->getField("journal")."\""."\n"; + } + if ($this->hasField("booktitle")) { + $result .= " type: conference-paper\n"; + $result .= " conference: \"".$this->getField("booktitle")."\""."\n"; + } + $result .= " authors:"."\n"; + foreach ($this->getFormattedAuthorsArray() as $author) { + $split = splitFullName($author); + $result .= " - family-names: ".$split[1]."\n"; + $result .= " given-names: ".$split[0]."\n"; + } + return $result; + } + + +} // end class BibEntry class RawBibEntry extends BibEntry { function setField($name, $value) {