All Projects → seboettg → citeproc-php

seboettg / citeproc-php

Licence: MIT license
Full-featured CSL 1.0.1 processor for PHP

Programming Languages

PHP
23972 projects - #3 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to citeproc-php

citeproc-el
A CSL 1.0.2 Citation Processor for Emacs.
Stars: ✭ 75 (+27.12%)
Mutual labels:  citation, csl
Styles
Official repository for Citation Style Language (CSL) citation styles.
Stars: ✭ 2,458 (+4066.1%)
Mutual labels:  citations, csl
Tutorials
Tutorials for creating figures, tables, or other content for AAS Journals.
Stars: ✭ 35 (-40.68%)
Mutual labels:  citation
Rdpack
R package Rdpack provides functions and macros facilitating writing and management of R documentation.
Stars: ✭ 21 (-64.41%)
Mutual labels:  citations
biblatex-sp-unified
An opinionated biblatex implementation of the Unified Stylesheet for Linguistics Journals
Stars: ✭ 35 (-40.68%)
Mutual labels:  citations
citation.vim
Zotero and bibtex citations for Vim
Stars: ✭ 78 (+32.2%)
Mutual labels:  citation
academic-bloggers-toolkit
WordPress plugin providing an all-in-one solution for effective academic blogging.
Stars: ✭ 117 (+98.31%)
Mutual labels:  citation
bookends-tools
Alfred Workflow to Integrate with Bookends, an academic reference manager/bibliography tool for macOS
Stars: ✭ 78 (+32.2%)
Mutual labels:  citation
Chinese-STD-GB-T-7714-related-csl
GB/T 7714相关的csl以及Zotero使用技巧及教程。
Stars: ✭ 1,189 (+1915.25%)
Mutual labels:  csl
PyScholar
A 'supervised' parser for Google Scholar
Stars: ✭ 74 (+25.42%)
Mutual labels:  citation
SemanticCite
Allows to manage citation resources using semantic annotations
Stars: ✭ 22 (-62.71%)
Mutual labels:  citation
journals
Tools and journal metadata for generating dependent CSL styles
Stars: ✭ 40 (-32.2%)
Mutual labels:  csl
gscholar-citations-crawler
Crawl all your citations from Google Scholar
Stars: ✭ 43 (-27.12%)
Mutual labels:  citation
citation-regexes
A collection of regular expressions for matching citations to state, federal, and even international law
Stars: ✭ 20 (-66.1%)
Mutual labels:  citations
AASTeX60
Version 6 of the LaTeX style files and documentation for authoring AAS Journal (AJ/ApJ) articles.
Stars: ✭ 40 (-32.2%)
Mutual labels:  citation
LaTeX-Templates
Document templates composed using LaTeX for my college assignments and projects (Applicable for any other university or college) ✨
Stars: ✭ 18 (-69.49%)
Mutual labels:  citation
cloudcite
The best bibliography generator. No pop-up ads, no gimmicks, no registration required, always secure (SSL, TLS 1.3, HSTS, and CSP), always fast (HTTP/3.0 and Brotli Compression) and 100% Open Source (AGPLv3).
Stars: ✭ 18 (-69.49%)
Mutual labels:  citation
hardcider
🍺 CLI for quickly generating citations for websites and books
Stars: ✭ 17 (-71.19%)
Mutual labels:  citation
Markdown-Templates
Templates for creating scientific, academic and technical documents using Markdown and Pandoc that support equations, code blocks, citations, Unicode symbols and vector graphics.
Stars: ✭ 130 (+120.34%)
Mutual labels:  citations
snowballing
Provides tools for literature snowballing
Stars: ✭ 42 (-28.81%)
Mutual labels:  citations

citeproc-php

Latest Stable Version Total Downloads License Build Status Code Coverage Scrutinizer Code Quality Code Intelligence Status PHP PHP PHP PHP

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).

Citation Style Language CSL

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

  • 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.

Installing citeproc-php

The recommended way to install citeproc-php is through Composer.

$ 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:

{
    "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:

$ php composer.phar install --no-dev

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

You can then later update citeproc-php using composer:

$ composer.phar update --no-dev

If you have trouble using composer you will find further information on https://getcomposer.org/doc/.

How to use citeproc-php

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

Get the metadata of your publications

Create a project folder:

$ 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.

[
    {
        "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.

Build a first simple script

<?php
include "vendor/autoload.php";
use Seboettg\CiteProc\StyleSheet;
use Seboettg\CiteProc\CiteProc;

$data = file_get_contents("metadata.json");
$style = StyleSheet::loadStyleSheet("din-1505-2");
$citeProc = new CiteProc($style);
echo $citeProc->render(json_decode($data), "bibliography");

You can also render citations instead of bibliographies:

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.

<p>This a wise sentence 
<?php echo $citeProc->render($data, "citation", json_decode('[{"id":"item-1"}]')); ?>.</p>
<p>This is the most wise sentence 
<?php echo $citeProc->render($data, "citation", json_decode('[{"id":"item-1"},{"id":"ITEM-2"}]')); ?>.</p>

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.

<?php
include "vendor/autoload.php";
use Seboettg\CiteProc\StyleSheet;
use Seboettg\CiteProc\CiteProc;

$data = file_get_contents("metadata.json");
$style = StyleSheet::loadStyleSheet("harvard-north-west-university");
$citeProc = new CiteProc($style);
$bibliography = $citeProc->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:

$ php -S localhost:8080

Start your Browser and open the URL http://localhost:8080.

Under examples folder you will find another example script.

Advanced usage of citeproc-php

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

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.

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

<?php
include "vendor/autoload.php";
use Seboettg\CiteProc\StyleSheet;
use Seboettg\CiteProc\CiteProc;

$data = file_get_contents("metadata.json");
$style = StyleSheet::loadStyleSheet("elsevier-vancouver");

// pimp the title
$titleFunction = function($cslItem, $renderedText) {
    return '<a href="https://example.org/publication/' . $cslItem->id . '">' . $renderedText . '</a>';
};

//pimp author names
$authorFunction = function($authorItem, $renderedText) {
    if (isset($authorItem->id)) {
        return '<a href="https://example.org/author/' . $authorItem->id . '">' . $renderedText . '</a>';
    }
    return $renderedText;
};
?>

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

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

<?php
$additionalMarkup = [
    "title" => $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.

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:

<?php 
$additionalMarkup = [
    "bibliography" => [
        "author" => $authorFunction,
        "title" => $titleFunction,
        "csl-entry" => function($cslItem, $renderedText) {
            return '<a id="' . $cslItem->id .'" pl-s1">$cslItem->id .'"></a>' . $renderedText;
        }
    ],
    "citation" => [
        "citation-number" => function($cslItem, $renderedText) {
            return '<a pl-s1">$cslItem->id .'">'.$renderedText.'</a>';
        }
    ]
];

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

?>
<p>This ia a wise sentence <?php echo $citeProc->render(json_decode($data), "citation", json_decode('[{"id":"item-1"}]')); ?>.</p>
<h3>Literature</h3>
<?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.

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.

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.

Reporting a Bug

Use the Issue Tracker in order to report a bug.

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.

I would suggest the following way:

  • 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:

$ composer test

Known projects that use citeproc-php

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].