Skip to content

Commit

Permalink
Fixed bug 9 (template does nothing)
Browse files Browse the repository at this point in the history
git-svn-id: https://plugins.svn.wordpress.org/papercite/trunk@369133 b8457f37-d9ea-0310-8a92-e5e31aec5664
  • Loading branch information
bpiwowar committed Apr 5, 2011
1 parent aa600c9 commit 818558d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 162 deletions.
158 changes: 31 additions & 127 deletions bib2tpl/bibtex_converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* By Raphael Reitzig, 2010
* [email protected]
* http://lmazy.verrech.net
*
* Modified by B. Piwowarski for inclusion in the papercite
* WordPress plug-in
*
*
* This work is subject to Creative Commons
* Attribution-NonCommercial-ShareAlike 3.0 Unported.
Expand Down Expand Up @@ -189,9 +193,10 @@ function convert($bibtex, $template)
*/
function display(&$data, $template)
{
$this->_pre_process($data);
$data = $this->_group($data);
$data = $this->_sort($data);
$text = $this->_post_process($data);
$this->_post_process($data);

$text = $this->_translate($data, $template);
return array("text" => &$text, "data" => &$data);
Expand Down Expand Up @@ -225,8 +230,6 @@ function _filter($data)
|| preg_match('/'.$this->_options['only']['entrytype'].'/i',
$entry['entrytype'])) )
{
$entry['firstauthor'] = $entry['author'][0];
$entry['entryid'] = $id++;
$entry['year'] = empty($entry['year']) ? '0000' : $entry['year'];
if ( empty($this->_options['lang']['entrytypes'][$entry['entrytype']]) )
{
Expand All @@ -239,6 +242,18 @@ function _filter($data)
return $result;
}

/**
* This function do some pre-processing on the entries
*/
function _pre_process(&$data) {
foreach ( $data as &$entry ) {
$entry['firstauthor'] = $entry['author'][0];
$entry['entryid'] = $id++;
}
}



/**
* This function do some post-processing on the grouped & ordered list of publications.
* In particular, it sets the key.
Expand Down Expand Up @@ -279,7 +294,7 @@ function _group($data)
{
foreach ( $data as $entry )
{
$target = $this->_options['group'] === 'firstauthor'
$target = $this->_options['group'] === 'firstauthor'
? $this->_helper->niceAuthor($entry['firstauthor'])
: $entry[$this->_options['group']];

Expand Down Expand Up @@ -313,7 +328,7 @@ function _group($data)
function _sort(&$data)
{
// Sort groups if there are any
if ( $this->_options['group-order'] !== 'none' )
if ( $this->_options['group_order'] !== 'none' )
{
uksort($data, array($this->_helper, 'group_cmp'));
}
Expand Down Expand Up @@ -449,7 +464,17 @@ function _callback($match) {
$groups = "";
foreach ( $this->_data as $groupkey => &$group )
{

if ( is_array($groupkey) )
// authors
$groupkey = $this->_helper->niceAuthor($key);
elseif ( $this->_options['group'] === 'entrytype' )
$groupkey = $this->_options['lang']['entrytypes'][$groupkey];

// Set the different global variables and parse
$this->_globals["groupkey"] = $groupkey;
$this->_globals["groupid"] = md5($groupkey);
$this->_globals["groupcount"] = count($group);
$this->_group = &$group;
$groups .= preg_replace_callback(BibtexConverter::$mainPattern, array($this, "_callback"), $this->group_tpl);
}
Expand Down Expand Up @@ -500,130 +525,9 @@ function _get_value($name) {
}


/**
* This function translates one entry group
*
* @access private
* @param string key The rendered group's key
* @param array data Array of entries in this group
* @param string template The group part of the template
* @return string String representing the passed group wrt template
*/
function _translate_group($key, $data, $template)
{
$result = $template;

// Replace group values
if ( is_array($key) )
{
$key = $this->_helper->niceAuthor($key);
}
elseif ( $this->_options['group'] === 'entrytype' )
{
$key = $this->_options['lang']['entrytypes'][$key];
}
$result = preg_replace('/@groupkey@/', $key, $result);
$result = preg_replace('/@groupid@/', md5($key), $result);
$result = preg_replace('/@groupcount@/', count($data), $result);

$pattern = '/@\{entry@(.*?)@\}entry@/s';

// Extract entry template
$entry_tpl = array();
preg_match($pattern, $template, $entry_tpl);
$entry_tpl = $entry_tpl[1];

// Translate all entries
$entries = '';
foreach ( $data as $entry )
{
$entries .= $this->_translate_entry($entry, $entry_tpl);
}


return preg_replace($pattern, $entries, $result);
}

/**
* This function translates one entry
*
* @access private
* @param array entry Array of fields
* @param string template The entry part of the template
* @return string String representing the passed entry wrt template
*/
function _translate_entry($entry, $template)
{
$result = $template;

// Resolve all conditions
$result = $this->_resolve_conditions($entry, $result);

// Global variables
$this->currentEntry = &$entry;
return preg_replace_callback('/@([^:@]+)(?::([^@]+))?@/', array($this, "_translate_variables"), $result);
}

function _translate_variables($input) {
// Special case: author
if ($input[1] == "author") {
return $this->_helper->niceAuthors($this->currentEntry["author"], $input[2]);
}

// Entry variable
if (array_key_exists($input[1], $this->currentEntry))
return $this->currentEntry[$input[1]];

// Global variable
if (array_key_exists($input[1], $this->_globals)) {
return $this->_globals[$input[1]];
}

return $input[0];
}

/**
* This function eliminates conditions in template parts.
*
* @access private
* @param array entry Entry with respect to which conditions are to be
* solved.
* @param string template The entry part of the template.
* @return string Template string without conditions.
*/
function _resolve_conditions($entry, $string) {
$pattern = '/@\?(\w+?)@(.*?)(@:\1@(.*?)){0,1}@;\1@/s';
/* Group 1: field
* Group 2: then
*[Group 4: else]
*/

$match = array();

/* Would like to do
* preg_match_all($pattern, $string, $matches);
* to get all matches at once but that results in Segmentation
* fault. Therefore iteratively:
*/
while ( preg_match($pattern, $string, $match) )
{
$resolved = '';
if ( !empty($entry[$match[1]]) )
{
$resolved = $match[2];
}
elseif ( !empty($match[4]) )
{
$resolved = $match[4];
}

// Recurse to cope with nested conditions
$resolved = $this->_resolve_conditions($entry, $resolved);

$string = str_replace($match[0], $resolved, $string);
}

return $string;
}
}

?>
11 changes: 9 additions & 2 deletions bib2tpl/helper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function _e2mn($entry) {
*/
function group_cmp($k1, $k2)
{
return $this->_options['group-order'] !== 'desc'
return $this->_options['group_order'] !== 'desc'
? strcmp($k1, $k2)
: -strcmp($k1, $k2);
}
Expand All @@ -135,7 +135,14 @@ function entry_cmp($e1, $e2)
$order = -strcmp($e1['year'].$this->_e2mn($e1),
$e2['year'].$this->_e2mn($e2));
} else if ($name == "firstauthor") {
$order = -strcmp($e1["author"]["last"], $e2["author"]["last"]);
$order = -strcmp($e1["author"][0]["last"], $e2["author"][0]["last"]);
} else if ($name == "author") {
$n = min(sizeof($e1["author"]), sizeof($e2["author"]));
for($i = 0; $i < $n; $i++) {
$order = -strcmp($e1["author"][$i]["last"], $e2["author"][$i]["last"]);
if ($order != 0)
break;
}
} else
$order = -strcmp($e1[$name], $e2[$name]);

Expand Down
8 changes: 6 additions & 2 deletions bib2tpl/lib/BibTex.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ function parse()
if ($fieldname != "bibtex")
if ($fieldname == "author") {
foreach($field as &$text)
$text = preg_replace_callback("#\\\\['\"^¨`H~]\w|\\\\[LlcC]#", "Structures_BibTex::_accents_cb", $text);
Structures_BibTex::process_accents($text);
} else {
$entry[$fieldname] = preg_replace_callback("#\\\\['~\"^¨`H]\w|\\\\[LlcC]#", "Structures_BibTex::_accents_cb", $field);
Structures_BibTex::process_accents($entry[$fieldname]);
}


Expand All @@ -367,6 +367,10 @@ function parse()
}
}

static function process_accents(&$text) {
$text = preg_replace_callback("#\\\\['\"^¨`H~]\w|\\\\[LlcC]#", "Structures_BibTex::_accents_cb", $text);
}

static $accents = array(
"\'a" => "á", "\`a" => "à", "\^a" => "â", "\¨a" => "ä",
"\'A" => "Á", "\`A" => "À", "\^A" => "Â", "\¨A" => "Ä",
Expand Down
24 changes: 15 additions & 9 deletions papercite.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Papercite {

// bibshow options stack
var $bibshow_options = array();
var $bibshow_tpl_options = array();

// Global counter for unique references of each
// displayed citation (used by bibshow)
Expand Down Expand Up @@ -233,9 +234,12 @@ function process(&$matches) {
// (1) From the preferences
// (2) From the custom fields
// (3) From the general options
$options = array("format" => "ieee", "group" => "none", "order" => "year", "sort" => "desc", "key_format" => "numeric",
$options = array("format" => "ieee", "group" => "none", "order" => "desc", "sort" => "none", "key_format" => "numeric",
"bibtex_template" => "default-bibtex", "bibshow_template" => "default-bibshow");
if ($command == "bibtex")
$options["sort"] = "year";


// Get general preferences
if (!$this->pOptions)
$this->pOptions = &get_option('papercite_options');
Expand All @@ -252,10 +256,10 @@ function process(&$matches) {

// Gets the options from the command
foreach($options_pairs as $x) {
if ($x[1] == "template")
if ($x[1] == "template") {
// Special case of template: should overwrite the corresponding command template
$options["$command_$x[1]"] = $x[2];
else
$options["${command}_$x[1]"] = $x[2];
} else
$options[$x[1]] = $x[2];
}

Expand All @@ -265,7 +269,9 @@ function process(&$matches) {
$options["group_order"] = "desc";
}

$tplOptions = array("anonymous-whole" => true, "group" => $options["group"], "group-order" => $options["group_order"],
$tplOptions = array(
"anonymous-whole" => true, // for compatibility in the output
"group" => $options["group"], "group-order" => $options["group_order"],
"sort" => $options["sort"], "order" => $options["order"],
"key_format" => $options["key_format"]);
$data = null;
Expand Down Expand Up @@ -331,6 +337,8 @@ function process(&$matches) {
$refs[$key] = &$entry;
}

$this->bibshow_tpl_options[] = $tplOptions;
$this->bibshow_options[] = $options;
array_push($this->bibshows, &$refs);
$this->cites[] = array();
break;
Expand All @@ -343,8 +351,6 @@ function process(&$matches) {
$key = $options["key"];
$refs = &$this->bibshows[sizeof($this->bibshows)-1];
$cites = &$this->cites[sizeof($this->cites)-1];
$this->bibshow_options[] = $tplOptions;


// First, get the corresponding entry
if (array_key_exists($key, $refs)) {
Expand All @@ -369,7 +375,8 @@ function process(&$matches) {
// Remove the array from the stack
$data = &array_pop($this->bibshows);
$cites = &array_pop($this->cites);
$tplOptions = &array_pop($this->bibshow_options);
$tplOptions = &array_pop($this->bibshow_tpl_options);
$options = &array_pop($this->bibshow_options);
$refs = array();

// Order the citations according to citation order
Expand All @@ -381,7 +388,6 @@ function process(&$matches) {
$refs[$num[0]]["pKey"] = $num[1];
}
}

return $this->showEntries($refs, $tplOptions, true, $this->getTemplate($options["bibshow_template"], $options["format"]));

default:
Expand Down
Loading

0 comments on commit 818558d

Please sign in to comment.