2017 © Pedro Peláez
 

library citeproc-php

Full-featured CSL processor (https://citationstyles.org)

image

seboettg/citeproc-php

Full-featured CSL processor (https://citationstyles.org)

  • Monday, July 30, 2018
  • by seboettg
  • Repository
  • 5 Watchers
  • 23 Stars
  • 20,233 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 9 Forks
  • 2 Open issues
  • 25 Versions
  • 24 % Grown

The README.md

citeproc-php

Latest Stable Version Total Downloads License Build Status Code Coverage Scrutinizer Code Quality Code Intelligence Status PHP PHP PHP PHP, (*1)

citeproc-php is a full-featured CSL 1.0.1 processor that renders bibliographic metadata into html formatted citations or bibliographies using CSL stylesheets. citeproc-php renders bibliographies as well as citations (except of Citation-specific Options)., (*2)

Citation Style Language CSL

The Citation Style Language (CSL) is an XML-based format to describe the formatting of citations, notes and bibliographies, offering:, (*3)

  • An open format
  • Compact and robust styles
  • Extensive support for style requirements
  • Automatic style localization
  • Infrastructure for style distribution and updating
  • Thousands of freely available styles (Creative Commons BY-SA licensed)

For additional documentation of CSL visit http://citationstyles.org., (*4)

Installing citeproc-php

The recommended way to install citeproc-php is through Composer., (*5)

$ curl -sS https://getcomposer.org/installer | php

Add the following lines to your composer.json file in order to add required program libraries as well as CSL styles and locales:, (*6)

{
    "name": "vendor-name/program-name",
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "citation-style-language/locales",
                "version":"1.0.0",
                "source": {
                    "type": "git",
                    "url": "https://github.com/citation-style-language/locales.git",
                    "reference": "master"
                }
            }
        },
        {
            "type": "package",
            "package": {
                "name": "citation-style-language/styles",
                "version":"1.0.0",
                "source": {
                    "type": "git",
                    "url": "https://github.com/citation-style-language/styles.git",
                    "reference": "master"
                }
            }
        }
    ],
    "require": {
        "citation-style-language/locales":"@dev",
        "citation-style-language/styles":"@dev",
        "seboettg/citeproc-php": "^2"
    }
}

Next, run the Composer command to install the latest stable version of citeproc-php and its dependencies:, (*7)

$ php composer.phar install --no-dev

After installing, you need to require Composer's autoloader:, (*8)

require 'vendor/autoload.php';

You can then later update citeproc-php using composer:, (*9)

bash $ composer.phar update --no-dev, (*10)

If you have trouble using composer you will find further information on https://getcomposer.org/doc/., (*11)

How to use citeproc-php

citeproc-php renders bibliographical metadata into html formatted citations or bibliographies using a stylesheet which defines the citation rules., (*12)

Get the metadata of your publications

Create a project folder:, (*13)

$ mkdir mycslproject
$ cd mycslproject

First, you need json formatted metadata array of publication's metadata. There are a lot of services that supports CSL exports. For instance BibSonomy, Zotero, Mendeley. If you don't use any of these services, you can use the following test data for a first step., (*14)

[
    {
        "author": [
            {
                "family": "Doe", 
                "given": "James", 
                "suffix": "III"
            }
        ], 
        "id": "item-1", 
        "issued": {
            "date-parts": [
                [
                    "2001"
                ]
            ]
        }, 
        "title": "My Anonymous Heritage", 
        "type": "book"
    },
    {
        "author": [
            {
                "family": "Anderson", 
                "given": "John"
            }, 
            {
                "family": "Brown", 
                "given": "John"
            }
        ], 
        "id": "ITEM-2", 
        "type": "book",
        "title": "Two authors writing a book"
    }
]

Copy this into a file in your project root and name that file metadata.json., (*15)

Build a first simple script

render(json_decode($data), "bibliography");
```

You can also render citations instead of bibliographies:

```php
echo $citeProc->render(json_decode($data), "citation");
```

### Filter Citations ###

Since version 2.1 you have also the possibility to apply a filter so that just specific citations appear.

```php

This a wise sentence render($data, "citation", json_decode('[{"id":"item-1"}]')); ?>.</p> , (*16)

This is the most wise sentence render($data, "citation", json_decode('[{"id":"item-1"},{"id":"ITEM-2"}]')); ?>., (*17)

Bibliography-specific styles using CSS

Some CSL stylesheets use bibliography-specific style options like hanging indents or alignments. To get an effect of these options you can render separated Cascading Stylesheets using CiteProc. You have to insert these styles within the <head> tag of your html output page., (*18)

render(json_decode($data), "bibliography");
$cssStyles = $citeProc->renderCssStyles();
?>
<html>
<head>
    <title>CSL Test</title>
    <style type="text/css" rel="stylesheet">
        <?php echo $cssStyles; ?>
    </style>
</head>
<body>
    <h1>Bibliography</h1>
    <?php echo $bibliography; ?>
</body>
</html>

Now, you can watch and test the output using PHP's internal web server:, (*19)

$ php -S localhost:8080

Start your Browser and open the URL http://localhost:8080., (*20)

Under examples folder you will find another example script., (*21)

Advanced usage of citeproc-php

Since version 2.1, citeproc-php comes with additional features that are not a part of the CSL specifications., (*22)

You can enrich bibliographies and citations with additional HTML tags to inject links (i.e. to set a link to an author's CV), or to add other html markup., (*23)

Use Lambda Functions to setup citeproc-php in order to render advanced citations or bibliographies

id . '">' . $renderedText . '';
};

//pimp author names
$authorFunction = function($authorItem, $renderedText) {
    if (isset($authorItem->id)) {
        return '' . $renderedText . '';
    }
    return $renderedText;
};
?>

As you can see, $titleFunction wraps the title and $authorFunction wraps author's name in a link., (*24)

Assign these functions to its associated CSL variable (in this case title and author) as follows., (*25)

 $titleFunction,
    "author" => $authorFunction
];

$citeProc = new CiteProc($style, "en-US", $additionalMarkup);
?>
<html>
<head>
    <title>CSL Test</title>
</head>
<body>
    <h1>Bibliography</h1>
    <?php echo $citeProc->render(json_decode($data), "bibliography"); ?>
</body>
</html>

You can also use custom Lambda Functions in order to enrich citations with additional HTML markup., (*26)

If you want to restrict citeproc-php to use a custom Lambda Function either for bibliographies or citations, or you want to apply different functions for both, you can define the array as follows:, (*27)

 [
        "author" => $authorFunction,
        "title" => $titleFunction,
        "csl-entry" => function($cslItem, $renderedText) {
            return '' . $renderedText;
        }
    ],
    "citation" => [
        "citation-number" => function($cslItem, $renderedText) {
            return ''.$renderedText.'';
        }
    ]
];

$citeProc = new CiteProc($style, "en-US", $additionalMarkup);

?>


This ia a wise sentence render(json_decode($data), "citation", json_decode('[{"id":"item-1"}]')); ?>., (*28)

Literature

<?php echo $citeProc->render(json_decode($data), "bibliography");

In this example each entry of the bibliography gets an anchor by its id and the citation (in Elsevier-Vancouver style [1]) gets an URL with a fragment by its id. Hence, every citation mark gets a link to its entry in the bibliography. Further examples you will find in the example folder., (*29)

Good to know

  • A custom Lambda Function must have two parameters (function ($item, $renderedValue) { ... }) in their signature and must return a string.
  • The 1st parameter of a custom Lambda Function is the item (either a citation item or a name item. Both of type \stdClass). The 2nd parameter is the rendered result of the associated item.
  • Custom Lambda Functions may be applied on all Standard Variables (according to the CSL specification).
  • Custom Lambda Functions may be applied on all Name Variables (according to the CSL specification). Be aware, just one name item will passed as parameter instead of the full citation item.
  • Custom Lambda Function for Number Variables or Date Variables will be ignored.
  • csl-entry is not a valid variable according to the CSL specifications. citeproc-php use csl-entry to hook in and apply a custom Lambda Function after a whole citation item or bibliography entry is rendered.

Contribution

citeproc-php is an Open Source project. You can support it by reporting bugs, contributing code or contributing documentation., (*30)

Star the Repo

Developing software is a hard job and one has to spend a lot of time. Every open-source developer is looking forward about esteem for his work. If you use citeproc-php and if you like it, star it and talk about it in Blogs., (*31)

Reporting a Bug

Use the Issue Tracker in order to report a bug., (*32)

Contribute Code

You are a developer and you like to help developing new features or bug fixes? Fork citeproc-php, setup a workspace and send a pull request., (*33)

I would suggest the following way:, (*34)

  • Fork citeproc-php on Github
  • Clone the forked repo
$ git clone https://github.com/<yourname>/citeproc-php
  • Setup your preferred IDE
  • Run the UnitTests within your IDE
  • Write a test case for your issue. My tests are based on the original test-suite. You can build custom (human-readable) test cases following the described Fixture layout.
  • Additionally, you have to translate (human-readable) test-cases into json format (machine-readable)
$ cd <project-root>/tests/fixtures/basic-tests
$ ./processor.py -g
  • create a test function within an already existing test class or create a new test class:
<?php 
namespace Seboettg\CiteProc;
use PHPUnit\Framework\TestCase;

class MyNewClassTest extends TestCase
{
    use TestSuiteTestCaseTrait;
    // ...
    public function testMyBrandNewFunction() 
    {
        //my brand new function is the file name (without file extension)
        $this->_testRenderTestSuite("myBrandNewFunction");
    }
    // ...
}
  • Implement or adapt your code as long as all tests finishing successfully
  • Make sure that your test case covers relevant code parts
  • Send a pull request

Testing

You can also run test cases without IDE:, (*35)

$ composer test

Known projects that use citeproc-php

The Versions

30/07 2018

dev-master

9999999-dev

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

30/07 2018

v2.1.4

2.1.4.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

15/06 2018

v2.1.3

2.1.3.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

01/06 2018

dev-issue-50

dev-issue-50

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

18/04 2018

v2.1.2

2.1.2.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

14/04 2018

dev-issue-49

dev-issue-49

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2018

v2.1.1

2.1.1.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

08/02 2018

dev-issue-47

dev-issue-47

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

23/11 2017

v2.1.0

2.1.0.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

23/11 2017

dev-feature/extended-markup

dev-feature/extended-markup

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

19/11 2017

dev-feature/filter-specific-citation

dev-feature/filter-specific-citation

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

15/11 2017

v2.0.4

2.0.4.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

11/11 2017

v2.0.3

2.0.3.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

04/10 2017

v2.0.2

2.0.2.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

04/10 2017

dev-fatal-error-php56-issue-42

dev-fatal-error-php56-issue-42

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

04/10 2017

dev-substitution-supress-issue-41

dev-substitution-supress-issue-41

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

13/06 2017

dev-version-2.1

dev-version-2.1

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

23/05 2017

v2.0.1

2.0.1.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

11/05 2017

v2.0.0

2.0.0.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

01/05 2017

v2.0.0-beta

2.0.0.0-beta

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

01/04 2017

v2.0.0-alpha2

2.0.0.0-alpha2

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

26/03 2017

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

18/03 2017

v2.0.0-alpha

2.0.0.0-alpha

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

03/11 2016

dev-version2.0

dev-version2.0

Full-featured CSL processor (https://citationstyles.org)

  Sources   Download

MIT

The Requires

 

The Development Requires

07/06 2016

dev-develop

dev-develop

This is an effort to implement a full-featured standalone CSL processor in PHP. The code based on the implementation of Ron Jerome.

  Sources   Download

GPL v3

The Requires

 

The Development Requires