Skip to content

Commit

Permalink
Merge branch 'release/2.4.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
seboettg committed May 8, 2022
2 parents dcb9409 + 26e7734 commit b6478c9
Show file tree
Hide file tree
Showing 9 changed files with 787 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
composer.lock
build/
review/
.phpunit.result.cache
114 changes: 73 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# citeproc-php #
[![Latest Stable Version](https://poser.pugx.org/seboettg/citeproc-php/v/stable)](https://packagist.org/packages/seboettg/citeproc-php)
[![Total Downloads](https://poser.pugx.org/seboettg/citeproc-php/downloads)](https://packagist.org/packages/seboettg/citeproc-php/stats)
[![Latest Stable Version](https://poser.pugx.org/seboettg/citeproc-php/v/stable)](https://packagist.org/packages/seboettg/citeproc-php)
[![Total Downloads](https://poser.pugx.org/seboettg/citeproc-php/downloads)](https://packagist.org/packages/seboettg/citeproc-php/stats)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://opensource.org/licenses/MIT)
[![Build Status](https://scrutinizer-ci.com/g/seboettg/citeproc-php/badges/build.png?b=master)](https://scrutinizer-ci.com/g/seboettg/citeproc-php/build-status/master)
[![Code Coverage](https://scrutinizer-ci.com/g/seboettg/citeproc-php/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/seboettg/citeproc-php/code-structure/master/code-coverage/src/)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/seboettg/citeproc-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/seboettg/citeproc-php/?branch=master)
[![Code Intelligence Status](https://scrutinizer-ci.com/g/seboettg/citeproc-php/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence)
![PHP](https://img.shields.io/badge/PHP-7.1-green.svg?style=flat)
![PHP](https://img.shields.io/badge/PHP-7.2-green.svg?style=flat)
![PHP](https://img.shields.io/badge/PHP-7.3-green.svg?style=flat)
![PHP](https://img.shields.io/badge/PHP-7.4-green.svg?style=flat)
![PHP](https://img.shields.io/badge/PHP-8.0-green.svg?style=flat)

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](http://docs.citationstyles.org/en/stable/specification.html#citation-specific-options)).

Expand Down Expand Up @@ -95,8 +94,8 @@ If you have trouble using composer you will find further information on [https:/

## How to use citeproc-php ##

citeproc-php renders bibliographical metadata into html formatted citations or bibliographies using a stylesheet which defines the
citation rules.
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 ###
Expand All @@ -108,42 +107,42 @@ $ 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](https://www.bibsonomy.org), [Zotero](https://www.zotero.org/), [Mendeley](https://www.mendeley.com/).
First, you need json formatted metadata array of publication's metadata. There are a lot of services that supports CSL exports. For instance [BibSonomy](https://www.bibsonomy.org), [Zotero](https://www.zotero.org/), [Mendeley](https://www.mendeley.com/).
If you don't use any of these services, you can use the following test data for a first step.

```javascript
[
{
"author": [
{
"family": "Doe",
"given": "James",
"family": "Doe",
"given": "James",
"suffix": "III"
}
],
"id": "item-1",
],
"id": "item-1",
"issued": {
"date-parts": [
[
"2001"
]
]
},
"title": "My Anonymous Heritage",
},
"title": "My Anonymous Heritage",
"type": "book"
},
{
"author": [
{
"family": "Anderson",
"family": "Anderson",
"given": "John"
},
},
{
"family": "Brown",
"family": "Brown",
"given": "John"
}
],
"id": "ITEM-2",
],
"id": "ITEM-2",
"type": "book",
"title": "Two authors writing a book"
}
Expand Down Expand Up @@ -176,15 +175,15 @@ echo $citeProc->render(json_decode($data), "citation");
Since version 2.1 you have also the possibility to apply a filter so that just specific citations appear.

```php
<p>This a wise sentence
<p>This a wise sentence
<?php echo $citeProc->render($data, "citation", json_decode('[{"id":"item-1"}]')); ?>.</p>
<p>This is the most wise setence
<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.
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
Expand Down Expand Up @@ -282,11 +281,11 @@ $citeProc = new CiteProc($style, "en-US", $additionalMarkup);
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
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
<?php
<?php
$additionalMarkup = [
"bibliography" => [
"author" => $authorFunction,
Expand All @@ -313,32 +312,65 @@ $citeProc = new CiteProc($style, "en-US", $additionalMarkup);
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.
To include affixes in a lambda function, you can define the array as follows:
```php
<?php
$additionalMarkup = [
"title" => array(
'function' => $titleFunction,
'affixes' => TRUE),
"author" => array(
'function' => $authorFunction,
'affixes' => TRUE)
];
$citeProc = new CiteProc($style, "en-US", $additionalMarkup);
?>
```
### 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](http://docs.citationstyles.org/en/1.0.1/specification.html#standard-variables)).
* Custom Lambda Functions may be applied on all Name Variables (according to the [CSL specification](http://docs.citationstyles.org/en/1.0.1/specification.html#name-variables)). 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.
* ```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 ##
You want to contribute to citeproc-php? Follow these [instructions](CONTRIBUTING.md)!
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](https://github.com/seboettg/citeproc-php/issues) 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
```bash
$ 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](https://github.com/citation-style-language/test-suite). You can build custom (human-readable) test cases following the described [Fixture layout](https://github.com/citation-style-language/test-suite#fixture-layout).
* Additionally, you have to translate (human-readable) test-cases into json format (machine-readable)
```bash
$ 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
<?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 ##
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
}
},
"require": {
"seboettg/collection": "^2.1",
"seboettg/collection": ">=3.0",
"myclabs/php-enum": "^1.8",
"ext-simplexml": "*",
"ext-json": "*",
"php": ">=7.3",
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
"php-coveralls/php-coveralls": "^1",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5",
"phpmd/phpmd": "^2.8"
},
"suggest": {
"ext-intl": "*",
Expand Down
2 changes: 1 addition & 1 deletion src/Styles/QuotesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function addSurroundingQuotes($text)
$punctuationInQuotes = CiteProc::getContext()->getLocale()->filter("options", "punctuation-in-quote");
$text = $this->replaceOuterQuotes($text, $openQuote, $closeQuote);
if (null !== $punctuationInQuotes || $punctuationInQuotes === false) {
if (preg_match("/([^\.,;]+)([\.,;]{1,})$/", $text, $match)) {
if (preg_match("/^(.+)([\.,;]+)$/", $text, $match)) {
$punctuation = substr($match[2], -1);
if ($this->suffix !== $punctuation) {
$text = $match[1] . substr($match[2], 0, strlen($match[2]) - 1);
Expand Down
3 changes: 0 additions & 3 deletions src/Styles/TextCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ trait TextCaseTrait
protected function initTextCaseAttributes(SimpleXMLElement $node)
{
foreach ($node->attributes() as $attribute) {
/**
* @var string $name
*/
$name = $attribute->getName();
$value = (string) $attribute;

Expand Down
40 changes: 25 additions & 15 deletions src/Util/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Seboettg\CiteProc\Util;

use Seboettg\CiteProc\CiteProc;
use Seboettg\Collection\ArrayList;

/**
* Class StringHelper
Expand Down Expand Up @@ -89,24 +90,33 @@ public static function capitalizeAll($text)
*/
public static function capitalizeForTitle($titleString)
{
if (strlen($titleString) == 0) {
return "";
}
if (preg_match('/(.+[^\<\>][\.:\/;\?\!]\s?)([a-z])(.+)/', $titleString, $match)) {
$titleString = $match[1].StringHelper::mb_ucfirst($match[2]).$match[3];
}

$wordArray = explode(" ", $titleString);

array_walk($wordArray, function (&$word) {
$words = explode("-", $word);
if (count($words) > 1) {
array_walk($words, function (&$w) {
$w = StringHelper::keepLowerCase($w) ? $w : StringHelper::mb_ucfirst($w);
});
$word = implode("-", $words);
}
$word = StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
});

return implode(" ", array_filter($wordArray));
$pattern = "/(\s|\/)/";
if (!preg_match($pattern, $titleString, $matches)) {
return StringHelper::mb_ucfirst($titleString);
}
$delimiter = $matches[1];
$wordArray = preg_split($pattern, $titleString); //explode(" ", $titleString);

$wordList = new ArrayList(...$wordArray);
return $wordList
->map(function(string $word) {
$wordParts = explode("-", $word);
if (count($wordParts) > 1) {
$casedWordParts = [];
foreach ($wordParts as $w) {
$casedWordParts[] = StringHelper::keepLowerCase($w) ? $w : StringHelper::mb_ucfirst($w);
}
$word = implode("-", $casedWordParts);
}
return StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
})
->collectToString($delimiter);
}

/**
Expand Down
Loading

0 comments on commit b6478c9

Please sign in to comment.