Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSR-2 #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/vendor/
profile.txt
dev.php
/nbproject/*
/vendor/*
3 changes: 2 additions & 1 deletion LICENSE.md → LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (c) 2012-2013 Nils Werner
Original work Copyright (c) 2012-2013 Nils Werner
Modified work Copyright (c) 2014 Nikolai Neff

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ BibtexParser

Bibtex parser for PHP, autoloading- and composer-compliant.

[![Build Status](https://travis-ci.org/audiolabs/bibtexparser.png?branch=master)](https://travis-ci.org/audiolabs/bibtexparser)
[![Build Status](https://travis-ci.org/Gefrierbrand/bibtexparser.svg?branch=master)](https://travis-ci.org/Gefrierbrand/bibtexparser)


## Installation

Add audiolabs/bibtexparser as a requirement to composer.json:
Add flatplane/bibtexparser as a requirement to composer.json:

```json
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Gefrierbrand/bibtexparser"
}
],
"require": {
"audiolabs/bibtexparser": "dev-master"
"flatplane/bibtexparser": "dev-master"
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace AudioLabs\BibtexParser;
namespace de\flatplane\BibtexParser;

class BibtexFormatter
{
static function format($entry)
public static function format($entry)
{
$body = self::buildBody($entry);

Expand All @@ -16,61 +16,55 @@ static function format($entry)
if (!empty($entry[$key])) {

// Arrays need special treatment
if(is_array($entry[$key])) {
if($key == 'author') {
if (is_array($entry[$key])) {
if ($key == 'author') {
$data = implode($entry[$key], ' and ');
}
elseif($key == 'pages' && is_array($entry[$key])) {
} elseif ($key == 'pages' && is_array($entry[$key])) {
$data = $entry[$key]['start'] . '&mdash;' . $entry[$key]['end'];
}
else {
} else {
$data = "";
}
}

// Regular strings are simply used as-is
else {
} else { // Regular strings are simply used as-is
$data = $entry[$key];
}

$body = str_ireplace($placeholder[0][$j], $data, $body);

}

}

return self::removeOptionalFields($body);
}

static function buildBody($entry)
public static function buildBody($entry)
{

$type = strtolower($entry['type']);
if ($type == "article")
if ($type == "article") {
return "{author}<br/> <strong>{title}</strong><br/>{journal}[, {volume}][({number})][: {pages}], {year}. [<br>{dbslinks}]";
elseif ($type == "book")
} elseif ($type == "book") {
return "{author}<br/> <strong>{title}</strong><br/>{publisher}[, ISBN: {isbn}], {year}. [<br>{dbslinks}]";
elseif ($type == "incollection")
} elseif ($type == "incollection") {
return "{author}<br/> <strong>{title}</strong><br/>In[ {editor} (ed.)]: {booktitle}, {publisher}[, {volume}][: {pages}], {year}. [<br>{dbslinks}]";
elseif ($type == "proceedings")
} elseif ($type == "proceedings") {
return "[{author}<br/> ]<strong>{title}</strong><br/>[In {booktitle}, ]{year}. [<br>{dbslinks}]";
elseif ($type == "inproceedings")
} elseif ($type == "inproceedings") {
return "{author}<br/> <strong>{title}</strong><br/>In {booktitle}, {year}. [<br>{dbslinks}]";
elseif ($type == "mastersthesis")
} elseif ($type == "mastersthesis") {
return "{author}<br/> <strong>{title}</strong><br/>[{note},] {school}, {year}. [<br>{dbslinks}]";
elseif ($type == "misc")
} elseif ($type == "misc") {
return "[{author}<br/>][ <strong>{title}</strong><br/>][{howpublished}, ][{note}][, {year}]. [<br>{dbslinks}]";
elseif ($type == "phdthesis")
} elseif ($type == "phdthesis") {
return "{author}<br/> <strong>{title}</strong><br/>PhD Thesis, {school}, {year}. [<br>{dbslinks}]";
elseif ($type == "techreport")
} elseif ($type == "techreport") {
return "{author}<br/> <strong>{title}</strong><br/>Technical Report, [No. {number}, ]{institution}, {year}. [<br>{dbslinks}]";
elseif ($type == "unpublished")
} elseif ($type == "unpublished") {
return "{author}<br/> <strong>{title}</strong><br/>{note}. [<br>{dbslinks}]";
else
} else {
return "{author}<br/> <strong>{title}</strong><br/>{journal}{booktitle}, {year} [<br>{dbslinks}]";
}
}

static function removeOptionalFields($entry)
public static function removeOptionalFields($entry)
{
$save = 10; // just to avoid an inf. loop
do {
Expand Down
115 changes: 115 additions & 0 deletions classes/BibtexParser/BibtexParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace de\flatplane\BibtexParser;

class BibtexParser
{
public static function parseFile($filename)
{
return self::parseLines(file($filename));
}

public static function parseString($data)
{
return self::parseLines(preg_split('/\n/', $data));
}

public static function parseLines($lines)
{
$items = array();
$count = -1;

if (!$lines) {
return;
}

foreach ($lines as $number => $line) {
$line = trim($line);

// empty line
if (!strlen($line)) {
continue;
}

// some funny comment string
if (strpos(strtolower($line), '@string') !== false) {
continue;
}

// pybliographer comments
if (strpos(strtolower($line), '@comment') !== false) {
continue;
}

// normal TeX style comment
if ($line[0] == "%") {
continue;
}

// begins with @, for example @inproceedings{...}
if ($line[0] == "@") {
$count++;
$handle = "";
$value = "";
$data = "";
$start = strpos($line, '@');
$end = strpos($line, '{');
$items[$count] = array();
$items[$count]['raw'] = "";
$items[$count]['type'] = trim(substr($line, 1, $end - 1));
$items[$count]['reference'] = trim(substr($line, $end + 1), ', ');
$items[$count]['lines'] = array('start' => $number + 1, 'end' => $number + 1);
} elseif (substr_count($line, '=') > 0) { // contains =, for example authors = {...}
$start = strpos($line, '=');
$handle = strtolower(trim(substr($line, 0, $start)));
$data = trim(substr($line, $start+1));

if ($handle == 'pages') {
preg_match('%(\d+)\s*\-+\s*(\d+)%', $data, $matches);
if (count($matches) > 2) {
$value = array('start' => $matches[1], 'end' => $matches[2]);
} else {
$value = $data;
}
} elseif ($handle == 'author') {
$value = explode(' and ', $data);
} else {
$value = $data;
}
} else { // neither a new block nor a new field: a following line of a multiline field
if (!is_array($value)) {
$value.= ' ' . $line;
}
}

$items[$count]['raw'] .= $line . "\n";

if ($value != "") {
$items[$count][$handle] = self::cleanup($value);
}
if (count($items) > 0) {
$items[$count]['lines']['end'] = $number + 1;
}
}
return $items;
}

public static function cleanup($value)
{
// call cleanup() recursively if passed an array (authors or pages).
if (is_array($value)) {
return array_map(
array('\de\flatplane\BibtexParser\BibtexParser', 'cleanup'),
$value
);
}

// replace a bunch of LaTeX stuff
$search = array('\"a', '\"A', '\"o', '\"O', '\"u', '\U"', '\ss', '\`e', '\´e',
'\url{', '{', '}', '--', '\"', '\'', '`', '\textbackslash');
$replace = array('ä', 'Ä', 'ö', 'Ö', 'ü', 'Ü', 'ß', 'è', 'é', '', '', '',
'&mdash;', ' ', ' ', ' ', '\\');
$value = rtrim(str_replace($search, $replace, $value), '}, ');
return trim($value);
}
}
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
{
"name": "audiolabs/bibtexparser",
"name": "flatplane/bibtexparser",
"type": "library",
"description": "Bibtex parser for PHP 5.3",
"keywords": ["bibtex","latex","parsing"],
"homepage": "http://github.com/alabs/bibtexparser",
"homepage": "https://github.com/Gefrierbrand/bibtexparser",
"license": "MIT",
"authors": [
{
"name": "Nils Werner",
"email": "[email protected]",
"homepage": "http://www.audiolabs-erlangen.de",
"role": "Developer"
},
{
"name": "Nikolai Neff",
"email": "[email protected]",
"homepage": "https://flatplane.de"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"AudioLabs":"src/"
}
"psr-4": {"de\\flatplane\\": "classes/"}
}
}
9 changes: 9 additions & 0 deletions demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace de\flatplane;

use de\flatplane\BibtexParser\BibtexParser;

include 'vendor/autoload.php';
$filename = 'tests/Fixures/publications.bib';
var_dump(BibtexParser::parseFile($filename));
Loading