DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:harvcite

harvcite plugin

Compatible with DokuWiki

No compatibility info given!

plugin [v.02] formats citations according to wiki template Harvard_citation

Last updated on
2008-02-27
Provides
Syntax
Requires
(only when anchors needed), bibtex

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Similar to bibtex, cite

Tagged with bibtex, quotes, references, syntax

Description

The plugin gives support for academics by putting hyperlinked references (requires small adjustment of bibtex plugin) in text.

It works similar to wikipedia's template

loc= is not implemented{{Harv |Smith|2006| loc=§8.5}} (Smith 2006, §8.5)
{{Harv |Smith|2006| p=25}} (Smith 2006, p. 25)
{{Harv |Smith|2006| pp=25–26}} (Smith 2006, pp. 25–26)
Ref is not implemented{{Harv |Smith|2006| pp=25–26 | Ref=none}} (Smith 2006, pp. 25–26)
{{Harv |Smith|Jones|2006| p=25}} (Smith & Jones 2006, p. 25)
{{Harv |Smith|Jones|Brown|2006| p=25}} (Smith, Jones & Brown 2006, p. 25)
{{Harv |Smith|Jones|Brown|Black|2006| p=25}} (Smith et al. 2006, p. 25)
{{Harvnb |Smith|2006| p=25}} Smith 2006, p. 25
{{Harvtxt |Smith|2006| p=25}} Smith (2006, p. 25)

Plugin

Put the following PHP code in /lib/plugins/harvcite/syntax.php

<?php
 
/**Plugin Harvcite: formats citations according to wiki template Harvard_citation.
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Martin Helmhout <martin_at_acis_dot_nl>
 */
 
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
 
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_harvcite extends DokuWiki_Syntax_Plugin {
 
 
    function getInfo(){
      return array(
        'author' => 'Martin Helmhout',
        'email'  => 'martin_at_acis_dot_nl',
        'date'   => '2008-02-27',
        'name'   => 'Harvcite plugin',
        'desc'   => 'formats citations according to wiki template Harvard_citation',
        'url'    => 'http://www.dokuwiki.org/plugin:harvcite',
      );
    }
 
    function getType() { return 'substition'; }
    function getSort() { return 32; }
    function connectTo($mode) { $this->Lexer->addSpecialPattern('\{\{Harv.*?\}\}',$mode,'plugin_harvcite'); }
    function handle($match, $state, $pos, &$handler){
	$match = explode('|',substr($match,2,-2));
 
 
	$harvType = trim($match[0]);
	$authors = array();
	$max1 = count($match);
 
	$isyear = False;
	for ($index = 1; $index < $max1; $index++){
		$var = $match[$index];
		//check year
		if(is_numeric($var)){
			$year = $var;
			$isYear = True;
		} else {
			//if isYear true then it is page nr
			if($isYear){
				$split = explode('=',trim($var),2);
				$page = TRUE;
			} else {
				//it is a last name
				array_push($authors, $var);
			}
		}	
	}
 
	//make cite string
	$cit = "";
	if (strcasecmp($harvType,"Harv") == 0){
		$type = 0;
		$cit .= "(";
	} elseif (strcasecmp($harvType, "Harvtxt") == 0){
		$type = 1;
		$cit = "";
	} elseif (strcasecmp($harvType, "Harvnb") == 0){
		$type = 2;
		$cit = "";
	}
 
	$link = "#";
	$max = count($authors);
	for($i=0; $i < $max; $i++){
		$cit .= "$authors[$i]" ;
		if ($max > 3){
			$cit .= " et al.";
			break;
		}
		if ($max > 1){
			if ($i == $max - 2){
				$cit .= " & ";
			} elseif ($i < $max -2) {
				$cit .= ", ";
			}
		}
		$link .= $authors[$i];
	}
	$cit .= " ";
	if ($type == 1){
		$cit .= "(";
	}
	$cit .= $year;
	$link .= $year;
	if ($page){
		$cit .= ", ".$split[0].". ".$split[1];
	}
	if ($type != 2){
		$cit .= ")";
	}
 
 
	return array($cit, $link);
	}
 
	function render($mode, &$renderer, $data) {
		list($citation, $link) = $data;
      			if($mode == 'xhtml'){
            		$renderer->doc .= '<a href="'.$link.'">'.$citation.'</a>';
          	return true;
      }
      return false;
    }
}
?>

Adjusted Bibtex Plugin

Thanks to the creators of the bibtex plugin.

This adjustment is only necessary when you want to make use of the auto-created internal anchor links

Download the bibtex plugin and follow their instructions

A quick hack (I did not study the plugin thoroughly) for getting anchors in the bibtex plugin. Anchors are based on a combinations of surname(s) and years. (e.g. (#)HelmhoutGazendamJorna2006)

Open syntax.php and go to line appr. 309:

    // In this case, BIBFORMAT::preProcess() adds all the resource elements automatically to the BIBFORMAT::item array...
    $bibformat->preProcess($resourceType, $entry);

Paste the following code after the previous statement

      	//
	// Added code: creates an anchor based on Surnames of authors and year (e.g. <a name="Williams2004"></a>)
	// J.M.Helmhout martin_at_acis_dot_nl
	// This added code fallse under GPLv 2.0 
	// quick and dirty code -> can be improved 
	//
	$parseAuthors = NEW PARSECREATORS();
 
	$authoretc = $bibformat->map();
	//strip away everything after authors and store year
	$eval = preg_match("/\(\d{4}\)/",$authoretc, $matches);
	//strip brackets
	$year = substr($matches[0],1,-1);
	$creatorstring = explode($matches[0], $authoretc);
	$creators_full = $creatorstring[0];
	//replace &amp with space)
	$creators_full = str_replace("&amp;"," ", $creators_full);
	//assuming authors have name >=2
	$eval2 = preg_match_all("/\w\w\w*/", $creators_full, $creators);
	$anchorvar = "";
	foreach($creators[0] as $creator){
		$anchorvar .= $creator;
	}
	$anchorvar .= $year;
	//
	// end added code
	//
 
	// Finally, get the formatted resource string ready for printing to the web browser or exporting to RTF, OpenOffice or plain text
      $citations.= '<dt><div id="bibtexdt"><a name="'.$anchorvar.'"></a>[' . $entry['year'] . ", " . $entry['bibtexEntryType'] . $this->toDownload($entry) . ']</div></dt><dd><div id="bibtexdd">'.   $bibformat->map()    . "</div></dd> \n" ;

That's all!

Revisions

  • [v0.2]Small bug concerning links (it placed an anchor (#) before every author)

Discussion

  • This is a quick and dirty PHP hack to relief myself from much work when creating citations. Although it works for fine for me at the moment, I'm sure it could be written more cleanly and enhanced with more features (like APA). Please feel free to comment or point out my errors. Martin Helmhout
plugin/harvcite.txt · Last modified: 2009-10-07 14:05 by laynee