Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
  • Loading branch information
seboettg committed May 7, 2022
2 parents 87c5626 + e3d258d commit dcb9409
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 99 deletions.
12 changes: 10 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ I would suggest the following way:
$ git clone https://github.com/<yourname>/citeproc-php
```
* Setup your preferred IDE
* Run the UnitTests within your IDE
* create a new branch in your forked repo (do not commit your changes in the master branch)
* implement your fix or feature
* Run the UnitTests
* 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
Expand All @@ -48,4 +50,10 @@ class MyNewClassTest extends TestCase
```
* Make sure that your test case covers relevant code parts
* Implement the code until all tests finishing successfully
* Send a pull request
* Send a pull request as follows:
1. create a new pull request and select the forked repo and the new branch as head
2. select the "seboettg/citeproc-php" and the master branch as base
3. select "Allow edits by maintainers"
4. add title and a meaningful description for your PR
5. submit the pull request

113 changes: 40 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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/)
Expand Down Expand Up @@ -95,8 +95,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 +108,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 +176,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 setence
<?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 +282,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,65 +313,32 @@ $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 ##
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
You want to contribute to citeproc-php? Follow these [instructions](CONTRIBUTING.md)!
## Testing ##
Expand All @@ -391,4 +358,4 @@ $ composer test
* [Stud.IP](https://github.com/studip/studip)
* [bibliography Plugin for DokuWiki](https://github.com/gyger/dokuwiki-bibliography)
* [ldbase_citations (Plugin for LDbase)](https://github.com/ldbase/ldbase_citations)
* [Citations - (Drupal module)](https://github.com/fsulib/citations)
* [Citations - (Drupal module)](https://github.com/fsulib/citations)
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@
},
"require": {
"seboettg/collection": "^2.1",
"myclabs/php-enum": "^1.5",
"myclabs/php-enum": "^1.8",
"ext-simplexml": "*",
"ext-json": "*",
"php": ">=7.1",
"php": ">=7.3",
"ext-mbstring": "*"
},
"require-dev": {
"php-coveralls/php-coveralls": "^1",
"phpunit/phpunit": "^7.5",
"squizlabs/php_codesniffer": "^3.5",
"phpmd/phpmd": "^2.8"
"phpunit/phpunit": "^8.0"
},
"suggest": {
"ext-intl": "*",
Expand Down
2 changes: 1 addition & 1 deletion src/CiteProc.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function render($data, $mode = "bibliography", $citationItems = [], $cita
*/
public function init($citationAsArray = false)
{
self::$context = new Context($this);
self::$context = new Context();
self::$context->setLocale(new Locale\Locale($this->lang)); //init locale
self::$context->setCitationsAsArray($citationAsArray);
// set markup extensions
Expand Down
18 changes: 13 additions & 5 deletions src/Rendering/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public function render($data, $citationNumber = null)
if (CiteProc::getContext()->getRenderingState()->getValue() === RenderingState::SUBSTITUTION) {
unset($data->{$this->toRenderTypeValue});
}
$renderedText = $this->applyAdditionalMarkupFunction($data, $renderedText);
if (!CiteProcHelper::isUsingAffixesByMarkupExtentsion($data, $this->toRenderTypeValue)) {
$renderedText = $this->applyAdditionalMarkupFunction($data, $renderedText);
}
break;
case 'macro':
$renderedText = $this->renderMacro($data);
Expand All @@ -125,7 +127,7 @@ public function render($data, $citationNumber = null)
$renderedText = !empty($term) ? $this->applyTextCase($term, $lang) : "";
}
if (!empty($renderedText)) {
$renderedText = $this->formatRenderedText($renderedText);
$renderedText = $this->formatRenderedText($data, $renderedText);
}
return $renderedText;
}
Expand Down Expand Up @@ -239,13 +241,17 @@ private function renderVariable($data, $lang)
}

/**
* @param $renderedText
* @param $data
* @param $renderedText
* @return string
*/
private function formatRenderedText($renderedText)
private function formatRenderedText($data, $renderedText)
{
$text = $this->format($renderedText);
$res = $this->addAffixes($text);
if (CiteProcHelper::isUsingAffixesByMarkupExtentsion($data, $this->toRenderTypeValue)) {
$res = $this->applyAdditionalMarkupFunction($data, $res);
}
if (!empty($res)) {
$res = $this->removeConsecutiveChars($res);
}
Expand All @@ -261,7 +267,9 @@ private function formatRenderedText($renderedText)
private function renderCitationNumber($data, $citationNumber)
{
$renderedText = $citationNumber + 1;
$renderedText = $this->applyAdditionalMarkupFunction($data, $renderedText);
if (!CiteProcHelper::isUsingAffixesByMarkupExtentsion($data, $this->toRenderTypeValue)) {
$renderedText = $this->applyAdditionalMarkupFunction($data, $renderedText);
}
return $renderedText;
}

Expand Down
1 change: 0 additions & 1 deletion src/Styles/AffixesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ protected function initAffixesAttributes(SimpleXMLElement $node)
{
/** @var SimpleXMLElement $attribute */
foreach ($node->attributes() as $attribute) {
/** @var string $name */
$name = (string) $attribute->getName();
$value = (string) $attribute;

Expand Down
35 changes: 33 additions & 2 deletions src/Util/CiteProcHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ public static function applyAdditionMarkupFunction($dataItem, $valueToRender, $r
{
$markupExtension = CiteProc::getContext()->getMarkupExtension();
if (array_key_exists($valueToRender, $markupExtension)) {
$function = $markupExtension[$valueToRender];
if (is_array($markupExtension[$valueToRender]) && array_key_exists('function', $markupExtension[$valueToRender])) {
$function = $markupExtension[$valueToRender]['function'];
} else {
$function = $markupExtension[$valueToRender];
}
if (is_callable($function)) {
$renderedText = $function($dataItem, $renderedText);
}
} elseif (array_key_exists($mode = CiteProc::getContext()->getMode(), $markupExtension)) {
if (array_key_exists($valueToRender, $markupExtension[$mode])) {
$function = CiteProc::getContext()->getMarkupExtension()[$mode][$valueToRender];
if (is_array($markupExtension[$mode][$valueToRender]) && array_key_exists('function', $markupExtension[$mode][$valueToRender])) {
$function = CiteProc::getContext()->getMarkupExtension()[$mode][$valueToRender]['function'];
} else {
$function = CiteProc::getContext()->getMarkupExtension()[$mode][$valueToRender];
}
if (is_callable($function)) {
$renderedText = $function($dataItem, $renderedText);
}
Expand All @@ -54,4 +62,27 @@ public static function cloneArray(array $array)
}
return $newArray;
}

/**
* @param stdClass $dataItem the actual item
* @param string $valueToRender value the has to apply on
* @return bool
*/
public static function isUsingAffixesByMarkupExtentsion($dataItem, $valueToRender)
{
$markupExtension = CiteProc::getContext()->getMarkupExtension();
if (array_key_exists($valueToRender, $markupExtension)) {
if (is_array($markupExtension[$valueToRender]) && array_key_exists('affixes', $markupExtension[$valueToRender])) {
return $markupExtension[$valueToRender]['affixes'];
}
} elseif (array_key_exists($mode = CiteProc::getContext()->getMode(), $markupExtension)) {
if (array_key_exists($valueToRender, $markupExtension[$mode])) {
if (is_array($markupExtension[$mode][$valueToRender]) && array_key_exists('affixes', $markupExtension[$mode][$valueToRender])) {
return CiteProc::getContext()->getMarkupExtension()[$mode][$valueToRender]['affixes'];
}
}
}

return FALSE;
}
}
2 changes: 1 addition & 1 deletion tests/src/CiteProcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CiteProcTest extends TestCase
*/
private $citeProc;

public function setUp()
public function setUp(): void
{
//parent::setU
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ContextTest extends TestCase
*/
private $context;

public function setUp()
public function setUp(): void
{
$style = StyleSheet::loadStyleSheet("din-1505-2");
$this->citeProc = new CiteProc($style, "de-DE");
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Locale/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testFilterTerms()
static::assertEquals("o. J.", $a2->{'single'});
}

protected function setUp()
protected function setUp(): void
{
$this->object = new Locale("de-DE");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Rendering/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GroupTest extends TestCase

private $data = "{\"title\":\"Ein Buch\", \"URL\":\"http://foo.bar\"}";

public function setUp()
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$context = new Context();
Expand Down
Loading

0 comments on commit dcb9409

Please sign in to comment.